ningwebbeginner

原项目

要显示太多修改。

为保证性能只显示 27 of 27+ 个文件。

  1 +mac_task:
  2 + matrix:
  3 + - name: MacOS Sequoia (15.0)
  4 + macos_instance:
  5 + image: ghcr.io/cirruslabs/macos-sequoia-base:latest
  6 + env:
  7 + UPLOAD: 0
  8 + - name: MacOS Sonoma (14.0)
  9 + macos_instance:
  10 + image: ghcr.io/cirruslabs/macos-sonoma-base:latest
  11 + env:
  12 + UPLOAD: 0
  13 + - name: MacOS Ventura (13.0)
  14 + macos_instance:
  15 + image: ghcr.io/cirruslabs/macos-ventura-base:latest
  16 + env:
  17 + UPLOAD: 0
  18 + - name: MacOS Monterrey (12.0)
  19 + macos_instance:
  20 + image: ghcr.io/cirruslabs/macos-monterey-base:latest
  21 + env:
  22 + UPLOAD: 0
  23 + env:
  24 + GITHUB_TOKEN: ENCRYPTED[fa47530508b5b9a63cbf1a9b4b9b2741c01c2f430b5ef0b39319fe7c1cd4cb285cf5e0c2ed5aa1bf990adb6200bcc742]
  25 + HOMEBREW_PATH: /opt/homebrew
  26 + NJOBS: 4
  27 + env:
  28 + only_if: ${CIRRUS_TAG} == ""
  29 + DAILY_BUILD: 1
  30 + env:
  31 + only_if: ${CIRRUS_TAG} != ""
  32 + DAILY_BUILD: 0
  33 + macos_instance:
  34 + cpu: 2
  35 + memory: 4G
  36 + timeout_in: 60m
  37 + install_script:
  38 + - chmod +x ports/ci/mac/install_deps.sh
  39 + - ./ports/ci/mac/install_deps.sh
  40 + script:
  41 + - chmod +x ports/ci/mac/build.sh
  42 + - ./ports/ci/mac/build.sh
  43 + deploy_script:
  44 + - chmod +x ports/ci/mac/deploy.sh
  45 + - ./ports/ci/mac/deploy.sh
  46 + upload_script:
  47 + - chmod +x ports/ci/mac/upload.sh
  48 + - ./ports/ci/mac/upload.sh
  1 +github: hipersayanX
  2 +patreon: hipersayanx
  3 +ko_fi: hipersayanx
  4 +liberapay: hipersayanx
  5 +custom: ["https://www.paypal.me/WebcamoidDonations", "https://buymeacoffee.com/hipersayanx", "https://webcamoid.github.io/donations#crypto"]
  1 +name: Linux MinGW
  2 +
  3 +on: [push, pull_request]
  4 +
  5 +env:
  6 + NSIS_VERSION: 3.10
  7 + DOCKERIMG: archlinux:latest
  8 + ARCH_ROOT_URL: https://mirror.rackspace.com/archlinux
  9 + NJOBS: 4
  10 +
  11 +jobs:
  12 + build:
  13 + runs-on: ubuntu-latest
  14 + strategy:
  15 + max-parallel: 10
  16 + fail-fast: false
  17 + matrix:
  18 + include:
  19 + - compiler: gcc
  20 + - compiler: clang
  21 + steps:
  22 + - uses: actions/checkout@v2
  23 + - name: Build
  24 + uses: addnab/docker-run-action@v3
  25 + with:
  26 + username: ${{ secrets.DOCKER_USERNAME }}
  27 + password: ${{ secrets.DOCKER_PASSWORD }}
  28 + registry: gcr.io
  29 + image: ${{ env.DOCKERIMG }}
  30 + options: >-
  31 + -v ${{ github.workspace }}:/sources
  32 + -e GITHUB_REF=${{ github.ref }}
  33 + -e GITHUB_SERVER_URL=${{ github.server_url }}
  34 + -e GITHUB_REPOSITORY=${{ github.repository }}
  35 + -e GITHUB_RUN_ID=${{ github.run_id }}
  36 + -e GIT_COMMIT_HASH=${{ github.sha }}
  37 + -e GIT_BRANCH_NAME=${{ github.ref_name }}
  38 + -e NSIS_VERSION=${{ env.NSIS_VERSION }}
  39 + -e TARGET_ARCH=${{ matrix.target_arch }}
  40 + -e COMPILER=${{ matrix.compiler }}
  41 + -e DOCKERIMG=${{ env.DOCKERIMG }}
  42 + -e ARCH_ROOT_URL=${{ env.ARCH_ROOT_URL }}
  43 + -e NJOBS=${{ env.NJOBS }}
  44 + run: |
  45 + cd /sources
  46 + echo
  47 + echo Install dependencies
  48 + echo
  49 + chmod +x ports/ci/linux-mingw/install_deps.sh
  50 + ./ports/ci/linux-mingw/install_deps.sh
  51 + echo
  52 + echo Release Build
  53 + echo
  54 + chmod +x ports/ci/linux-mingw/build.sh
  55 + ./ports/ci/linux-mingw/build.sh
  56 + echo
  57 + echo Release Deploy
  58 + echo
  59 + chmod +x ports/ci/linux-mingw/deploy.sh
  60 + ./ports/ci/linux-mingw/deploy.sh
  1 +name: Mac
  2 +
  3 +on: [push, pull_request]
  4 +
  5 +env:
  6 + HOMEBREW_PATH: /usr/local
  7 + NJOBS: 4
  8 +
  9 +jobs:
  10 + build:
  11 + strategy:
  12 + max-parallel: 10
  13 + fail-fast: false
  14 + matrix:
  15 + include:
  16 + - os: macos-15
  17 + upload: 0
  18 + - os: macos-14
  19 + upload: 1
  20 + - os: macos-13
  21 + upload: 1
  22 + runs-on: ${{ matrix.os }}
  23 + env:
  24 + UPLOAD: ${{ matrix.upload }}
  25 + steps:
  26 + - uses: actions/checkout@v2
  27 + - name: Install dependencies
  28 + run: |
  29 + chmod +x ports/ci/mac/install_deps.sh
  30 + ./ports/ci/mac/install_deps.sh
  31 + - name: Release Build
  32 + if: ${{ startsWith(github.ref, 'refs/tags/') }}
  33 + run: |
  34 + chmod +x ports/ci/mac/build.sh
  35 + ./ports/ci/mac/build.sh
  36 + - name: Release Deploy
  37 + if: ${{ startsWith(github.ref, 'refs/tags/') }}
  38 + run: |
  39 + chmod +x ports/ci/mac/deploy.sh
  40 + ./ports/ci/mac/deploy.sh
  41 + - name: Daily Build
  42 + if: ${{ !startsWith(github.ref, 'refs/tags/') }}
  43 + env:
  44 + DAILY_BUILD: 1
  45 + run: |
  46 + chmod +x ports/ci/mac/build.sh
  47 + ./ports/ci/mac/build.sh
  48 + - name: Daily Deploy
  49 + if: ${{ !startsWith(github.ref, 'refs/tags/') }}
  50 + env:
  51 + DAILY_BUILD: 1
  52 + run: |
  53 + chmod +x ports/ci/mac/deploy.sh
  54 + ./ports/ci/mac/deploy.sh
  55 + - name: Release Upload
  56 + uses: softprops/action-gh-release@v1
  57 + if: ${{ startsWith(github.ref, 'refs/tags/') && matrix.upload }}
  58 + with:
  59 + files: packages/mac/*
  60 + env:
  61 + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  62 + - name: Daily Build Upload
  63 + uses: softprops/action-gh-release@v1
  64 + if: ${{ !startsWith(github.ref, 'refs/tags/') && matrix.upload }}
  65 + with:
  66 + body: "${{ github.event.head_commit.message }} (commit: [${{ github.sha }}](https://github.com/webcamoid/akvirtualcamera/commit/${{ github.sha }}))<hr>**Note**: Ignore the commit information of the tag, the files in the release keep updating with every new build, these packages were built from [${{ github.sha }}](https://github.com/webcamoid/akvirtualcamera/commit/${{ github.sha }}) commit."
  67 + prerelease: true
  68 + files: packages/mac/*
  69 + name: Daily Build
  70 + tag_name: daily-build
  71 + env:
  72 + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  1 +name: Windows MSYS
  2 +
  3 +on: [push, pull_request]
  4 +
  5 +env:
  6 + DISABLE_CCACHE: 1
  7 + NJOBS: 4
  8 +
  9 +jobs:
  10 + build:
  11 + runs-on: windows-latest
  12 + defaults:
  13 + run:
  14 + shell: msys2 {0}
  15 + strategy:
  16 + max-parallel: 10
  17 + fail-fast: false
  18 + matrix:
  19 + include:
  20 + - compiler: gcc
  21 + - compiler: clang
  22 + env:
  23 + COMPILER: ${{ matrix.compiler }}
  24 + steps:
  25 + - uses: actions/checkout@v2
  26 + - uses: msys2/setup-msys2@v2
  27 + with:
  28 + update: true
  29 + msystem: mingw64
  30 + install: >-
  31 + ccache
  32 + clang
  33 + cmake
  34 + git
  35 + make
  36 + pkgconf
  37 + python3
  38 + mingw-w64-i686-binutils
  39 + mingw-w64-i686-clang
  40 + mingw-w64-i686-cmake
  41 + mingw-w64-i686-gcc
  42 + mingw-w64-i686-pkgconf
  43 + mingw-w64-x86_64-binutils
  44 + mingw-w64-x86_64-clang
  45 + mingw-w64-x86_64-cmake
  46 + mingw-w64-x86_64-gcc
  47 + mingw-w64-x86_64-pkgconf
  48 + - name: Release Build
  49 + if: ${{ startsWith(github.ref, 'refs/tags/') }}
  50 + run: ./ports/ci/windows-msys/build.sh
  51 + - name: Release Deploy
  52 + if: ${{ startsWith(github.ref, 'refs/tags/') }}
  53 + run: ./ports/ci/windows-msys/deploy.sh
  54 + - name: Daily Build
  55 + if: ${{ !startsWith(github.ref, 'refs/tags/') }}
  56 + env:
  57 + DAILY_BUILD: 1
  58 + run: ./ports/ci/windows-msys/build.sh
  59 + - name: Daily Deploy
  60 + if: ${{ !startsWith(github.ref, 'refs/tags/') }}
  61 + env:
  62 + DAILY_BUILD: 1
  63 + run: ./ports/ci/windows-msys/deploy.sh
  64 + - name: Release Upload
  65 + uses: softprops/action-gh-release@v1
  66 + if: ${{ startsWith(github.ref, 'refs/tags/') }}
  67 + with:
  68 + files: packages/windows-gcc/*
  69 + env:
  70 + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  71 + - name: Daily Build Upload
  72 + uses: softprops/action-gh-release@v1
  73 + if: ${{ !startsWith(github.ref, 'refs/tags/') }}
  74 + with:
  75 + body: "${{ github.event.head_commit.message }} (commit: [${{ github.sha }}](https://github.com/webcamoid/akvirtualcamera/commit/${{ github.sha }}))<hr>**Note**: Ignore the commit information of the tag, the files in the release keep updating with every new build, these packages were built from [${{ github.sha }}](https://github.com/webcamoid/akvirtualcamera/commit/${{ github.sha }}) commit."
  76 + prerelease: true
  77 + files: packages/windows-gcc/*
  78 + name: Daily Build
  79 + tag_name: daily-build
  80 + env:
  81 + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  1 +name: Windows Visual Studio
  2 +
  3 +on: [push, pull_request]
  4 +
  5 +env:
  6 + CMAKE_GENERATOR: Visual Studio 17 2022
  7 + PYTHON_VERSION: Python312
  8 + NJOBS: 4
  9 +
  10 +jobs:
  11 + build:
  12 + runs-on: windows-latest
  13 + defaults:
  14 + run:
  15 + shell: cmd
  16 + steps:
  17 + - uses: actions/checkout@v2
  18 + - name: Build
  19 + run: ports\ci\windows-vs\build.bat
  20 + - name: Deploy
  21 + run: ports\ci\windows-vs\deploy.bat
  1 +# Compiled source #
  2 +###################
  3 +*.com
  4 +*.class
  5 +*.dll
  6 +*.exe
  7 +*.o
  8 +*.so
  9 +*.so.*
  10 +*.so.debug
  11 +*.run
  12 +*.AppImage
  13 +*.framework
  14 +
  15 +# Packages #
  16 +############
  17 +*.7z
  18 +*.dmg
  19 +*.gz
  20 +*.iso
  21 +*.jar
  22 +*.rar
  23 +*.tar
  24 +*.zip
  25 +*.xz
  26 +
  27 +# Logs and databases #
  28 +######################
  29 +*.log
  30 +*.sql
  31 +*.sqlite
  32 +vgcore.*
  33 +
  34 +# OS generated files #
  35 +######################
  36 +.DS_Store
  37 +.DS_Store?
  38 +._*
  39 +.Spotlight-V100
  40 +.Trashes
  41 +Icon?
  42 +ehthumbs.db
  43 +Thumbs.db
  44 +*~
  45 +*_resource.rc
  46 +
  47 +# Ignore files generated by Qt #
  48 +################################
  49 +*.a
  50 +*.moc
  51 +moc_*.cpp
  52 +ui_*.h
  53 +*.obj
  54 +*.dylib
  55 +Makefile*
  56 +*.prl
  57 +*.app
  58 +*.autosave
  59 +*.qmlc
  60 +.qmake.stash
  61 +.qmake.cache
  62 +callgrind.out.*
  63 +*.debug
  64 +*.plugin
  65 +*_qmlcache.qrc
  66 +test.o-*
  67 +object_script.*
  68 +*.user.*
  69 +*.user
  70 +
  71 +# Ignore files generated by Python #
  72 +####################################
  73 +__pycache__
  74 +*.pyc
  75 +
  76 +# Android files #
  77 +#################
  78 +android-build
  79 +android-*-deployment-settings.json
  80 +
  81 +# VIM temporary files #
  82 +#######################
  83 +.*.swp
  84 +
  85 +# Kate temporary files #
  86 +#######################
  87 +.*.kate-swp
  88 +
  89 +# KDevelop generated files #
  90 +############################
  91 +.kdev4
  92 +*.kdev4
  93 +
  94 +# MinGW files #
  95 +###############
  96 +*.Debug
  97 +*.Release
  98 +
  99 +# MSVC files #
  100 +##############
  101 +*.exp
  102 +*.ilk
  103 +*.lib
  104 +*.pdb
  105 +
  106 +# Failed patch #
  107 +################
  108 +*.orig
  109 +*.rej
  110 +
  111 +# Project Files #
  112 +#################
  113 +AkVCamAssistant
  114 +
  115 +# Ignore Directories #
  116 +######################
  117 +build
  118 +debug
  119 +release
  120 +
  121 +# Ignore Auto Generated Files #
  122 +###############################
  123 +*_auto*
  124 +
  125 +# Ignore Private Files and Folders #
  126 +####################################
  127 +*_priv
  128 +*_priv.*
  1 +# akvirtualcamera, virtual camera for Mac and Windows.
  2 +# Copyright (C) 2021 Gonzalo Exequiel Pedone
  3 +#
  4 +# akvirtualcamera is free software: you can redistribute it and/or modify
  5 +# it under the terms of the GNU General Public License as published by
  6 +# the Free Software Foundation, either version 3 of the License, or
  7 +# (at your option) any later version.
  8 +#
  9 +# akvirtualcamera is distributed in the hope that it will be useful,
  10 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 +# GNU General Public License for more details.
  13 +#
  14 +# You should have received a copy of the GNU General Public License
  15 +# along with akvirtualcamera. If not, see <http://www.gnu.org/licenses/>.
  16 +#
  17 +# Web-Site: http://webcamoid.github.io/
  18 +
  19 +cmake_minimum_required(VERSION 3.5)
  20 +
  21 +project(AkVirtualCamera)
  22 +
  23 +include(commons.cmake)
  24 +
  25 +add_subdirectory(VCamUtils)
  26 +add_subdirectory(Manager)
  27 +add_subdirectory(Service)
  28 +add_subdirectory(cmio)
  29 +add_subdirectory(dshow)
  30 +
  31 +configure_file(package_info.conf.in package_info.conf)
  1 +# akvcam Individual Contributor License Agreement #
  2 +
  3 +Thank you for your interest in contributing to akvcam ("We" or "Us").
  4 +
  5 +This contributor agreement ("Agreement") documents the rights granted by contributors to Us. To make this document effective, please sign it and send it to Us by email, following the instructions at CONTRIBUTING.md. This is a legally binding document, so please read it carefully before agreeing to it. The Agreement may cover more than one software project managed by Us.
  6 +
  7 +You are accepting this agreement by making a pull request to the repository.
  8 +
  9 +## 1. Definitions ##
  10 +
  11 +"You" means the individual who Submits a Contribution to Us.
  12 +
  13 +"Contribution" means any work of authorship that is Submitted by You to Us in which You own or assert ownership of the Copyright. If You do not own the Copyright in the entire work of authorship, please follow the instructions in CONTRIBUTING.md.
  14 +
  15 +"Copyright" means all rights protecting works of authorship owned or controlled by You, including copyright, moral and neighboring rights, as appropriate, for the full term of their existence including any extensions by You.
  16 +
  17 +"Material" means the work of authorship which is made available by Us to third parties. When this Agreement covers more than one software project, the Material means the work of authorship to which the Contribution was Submitted. After You Submit the Contribution, it may be included in the Material.
  18 +
  19 +"Submit" means any form of electronic, verbal, or written communication sent to Us or our representatives, including but not limited to electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, Us for the purpose of discussing and improving the Material, but excluding communication that is conspicuously marked or otherwise designated in writing by You as "Not a Contribution."
  20 +
  21 +"Submission Date" means the date on which You Submit a Contribution to Us.
  22 +
  23 +"Effective Date" means the date You execute this Agreement or the date You first Submit a Contribution to Us, whichever is earlier.
  24 +
  25 +"Media" means any portion of a Contribution which is not software.
  26 +
  27 +## 2. Grant of Rights ##
  28 +
  29 +### 2.1 Copyright License ###
  30 +
  31 +(a) You retain ownership of the Copyright in Your Contribution and have the same rights to use or license the Contribution which You would have had without entering into the Agreement.
  32 +
  33 +(b) To the maximum extent permitted by the relevant law, You grant to Us a perpetual, worldwide, non-exclusive, transferable, royalty-free, irrevocable license under the Copyright covering the Contribution, with the right to sublicense such rights through multiple tiers of sublicensees, to reproduce, modify, display, perform and distribute the Contribution as part of the Material; provided that this license is conditioned upon compliance with Section 2.3.
  34 +
  35 +### 2.2 Patent License ###
  36 +
  37 +For patent claims including, without limitation, method, process, and apparatus claims which You own, control or have the right to grant, now or in the future, You grant to Us a perpetual, worldwide, non-exclusive, transferable, royalty-free, irrevocable patent license, with the right to sublicense these rights to multiple tiers of sublicensees, to make, have made, use, sell, offer for sale, import and otherwise transfer the Contribution and the Contribution in combination with the Material (and portions of such combination). This license is granted only to the extent that the exercise of the licensed rights infringes such patent claims; and provided that this license is conditioned upon compliance with Section 2.3.
  38 +
  39 +### 2.3 Outbound License ###
  40 +
  41 +As a condition on the grant of rights in Sections 2.1 and 2.2, We agree to license the Contribution only under the terms of the license or licenses which We are using on the Submission Date for the Material or any licenses on the Free Software Foundation's list of "Recommended copyleft licenses" on or after the Effective Date, whether or not such licenses are subsequently disapproved (including any right to adopt any future version of a license if permitted).
  42 +
  43 +In addition, We may use the following licenses for Media in the Contribution: Creative Commons Attribution Share Alike 3.0 (including any right to adopt any future version of a license if permitted).
  44 +
  45 +**2.4 Moral Rights.** If moral rights apply to the Contribution, to the maximum extent permitted by law, You waive and agree not to assert such moral rights against Us or our successors in interest, or any of our licensees, either direct or indirect.
  46 +
  47 +**2.5 Our Rights.** You acknowledge that We are not obligated to use Your Contribution as part of the Material and may decide to include any Contribution We consider appropriate.
  48 +
  49 +**2.6 Reservation of Rights.** Any rights not expressly licensed under this section are expressly reserved by You.
  50 +
  51 +## 3. Agreement ##
  52 +
  53 +You confirm that:
  54 +
  55 +(a) You have the legal authority to enter into this Agreement.
  56 +
  57 +(b) You own the Copyright and patent claims covering the Contribution which are required to grant the rights under Section 2.
  58 +
  59 +(c) The grant of rights under Section 2 does not violate any grant of rights which You have made to third parties, including Your employer. If You are an employee, You have had Your employer approve this Agreement or sign the Entity version of this document. If You are less than eighteen years old, please have Your parents or guardian sign the Agreement.
  60 +
  61 +(d) You have followed the instructions in CONTRIBUTING.md, if You do not own the Copyright in the entire work of authorship Submitted.
  62 +
  63 +## 4. Disclaimer ##
  64 +
  65 +EXCEPT FOR THE EXPRESS WARRANTIES IN SECTION 3, THE CONTRIBUTION IS PROVIDED "AS IS". MORE PARTICULARLY, ALL EXPRESS OR IMPLIED WARRANTIES INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY DISCLAIMED BY YOU TO US. TO THE EXTENT THAT ANY SUCH WARRANTIES CANNOT BE DISCLAIMED, SUCH WARRANTY IS LIMITED IN DURATION TO THE MINIMUM PERIOD PERMITTED BY LAW.
  66 +
  67 +## 5. Consequential Damage Waiver ##
  68 +
  69 +TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT WILL YOU BE LIABLE FOR ANY LOSS OF PROFITS, LOSS OF ANTICIPATED SAVINGS, LOSS OF DATA, INDIRECT, SPECIAL, INCIDENTAL, CONSEQUENTIAL AND EXEMPLARY DAMAGES ARISING OUT OF THIS AGREEMENT REGARDLESS OF THE LEGAL OR EQUITABLE THEORY (CONTRACT, TORT OR OTHERWISE) UPON WHICH THE CLAIM IS BASED.
  70 +
  71 +## 6. Miscellaneous ##
  72 +
  73 +6.1 This Agreement will be governed by and construed in accordance with the laws of Argentina excluding its conflicts of law provisions. Under certain circumstances, the governing law in this section might be superseded by the United Nations Convention on Contracts for the International Sale of Goods ("UN Convention") and the parties intend to avoid the application of the UN Convention to this Agreement and, thus, exclude the application of the UN Convention in its entirety to this Agreement.
  74 +
  75 +6.2 This Agreement sets out the entire agreement between You and Us for Your Contributions to Us and overrides all other agreements or understandings.
  76 +
  77 +6.3 If You or We assign the rights or obligations received through this Agreement to a third party, as a condition of the assignment, that third party must agree in writing to abide by all the rights and obligations in the Agreement.
  78 +
  79 +6.4 The failure of either party to require performance by the other party of any provision of this Agreement in one situation shall not affect the right of a party to require such performance at any time in the future. A waiver of performance under a provision in one situation shall not be considered a waiver of the performance of the provision in the future or a waiver of the provision in its entirety.
  80 +
  81 +6.5 If any provision of this Agreement is found void and unenforceable, such provision will be replaced to the extent possible with a provision that comes closest to the meaning of the original provision and which is enforceable. The terms and conditions set forth in this Agreement shall apply notwithstanding any failure of essential purpose of this Agreement or any limited remedy to the maximum extent possible under law.
  1 + GNU GENERAL PUBLIC LICENSE
  2 + Version 3, 29 June 2007
  3 +
  4 + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
  5 + Everyone is permitted to copy and distribute verbatim copies
  6 + of this license document, but changing it is not allowed.
  7 +
  8 + Preamble
  9 +
  10 + The GNU General Public License is a free, copyleft license for
  11 +software and other kinds of works.
  12 +
  13 + The licenses for most software and other practical works are designed
  14 +to take away your freedom to share and change the works. By contrast,
  15 +the GNU General Public License is intended to guarantee your freedom to
  16 +share and change all versions of a program--to make sure it remains free
  17 +software for all its users. We, the Free Software Foundation, use the
  18 +GNU General Public License for most of our software; it applies also to
  19 +any other work released this way by its authors. You can apply it to
  20 +your programs, too.
  21 +
  22 + When we speak of free software, we are referring to freedom, not
  23 +price. Our General Public Licenses are designed to make sure that you
  24 +have the freedom to distribute copies of free software (and charge for
  25 +them if you wish), that you receive source code or can get it if you
  26 +want it, that you can change the software or use pieces of it in new
  27 +free programs, and that you know you can do these things.
  28 +
  29 + To protect your rights, we need to prevent others from denying you
  30 +these rights or asking you to surrender the rights. Therefore, you have
  31 +certain responsibilities if you distribute copies of the software, or if
  32 +you modify it: responsibilities to respect the freedom of others.
  33 +
  34 + For example, if you distribute copies of such a program, whether
  35 +gratis or for a fee, you must pass on to the recipients the same
  36 +freedoms that you received. You must make sure that they, too, receive
  37 +or can get the source code. And you must show them these terms so they
  38 +know their rights.
  39 +
  40 + Developers that use the GNU GPL protect your rights with two steps:
  41 +(1) assert copyright on the software, and (2) offer you this License
  42 +giving you legal permission to copy, distribute and/or modify it.
  43 +
  44 + For the developers' and authors' protection, the GPL clearly explains
  45 +that there is no warranty for this free software. For both users' and
  46 +authors' sake, the GPL requires that modified versions be marked as
  47 +changed, so that their problems will not be attributed erroneously to
  48 +authors of previous versions.
  49 +
  50 + Some devices are designed to deny users access to install or run
  51 +modified versions of the software inside them, although the manufacturer
  52 +can do so. This is fundamentally incompatible with the aim of
  53 +protecting users' freedom to change the software. The systematic
  54 +pattern of such abuse occurs in the area of products for individuals to
  55 +use, which is precisely where it is most unacceptable. Therefore, we
  56 +have designed this version of the GPL to prohibit the practice for those
  57 +products. If such problems arise substantially in other domains, we
  58 +stand ready to extend this provision to those domains in future versions
  59 +of the GPL, as needed to protect the freedom of users.
  60 +
  61 + Finally, every program is threatened constantly by software patents.
  62 +States should not allow patents to restrict development and use of
  63 +software on general-purpose computers, but in those that do, we wish to
  64 +avoid the special danger that patents applied to a free program could
  65 +make it effectively proprietary. To prevent this, the GPL assures that
  66 +patents cannot be used to render the program non-free.
  67 +
  68 + The precise terms and conditions for copying, distribution and
  69 +modification follow.
  70 +
  71 + TERMS AND CONDITIONS
  72 +
  73 + 0. Definitions.
  74 +
  75 + "This License" refers to version 3 of the GNU General Public License.
  76 +
  77 + "Copyright" also means copyright-like laws that apply to other kinds of
  78 +works, such as semiconductor masks.
  79 +
  80 + "The Program" refers to any copyrightable work licensed under this
  81 +License. Each licensee is addressed as "you". "Licensees" and
  82 +"recipients" may be individuals or organizations.
  83 +
  84 + To "modify" a work means to copy from or adapt all or part of the work
  85 +in a fashion requiring copyright permission, other than the making of an
  86 +exact copy. The resulting work is called a "modified version" of the
  87 +earlier work or a work "based on" the earlier work.
  88 +
  89 + A "covered work" means either the unmodified Program or a work based
  90 +on the Program.
  91 +
  92 + To "propagate" a work means to do anything with it that, without
  93 +permission, would make you directly or secondarily liable for
  94 +infringement under applicable copyright law, except executing it on a
  95 +computer or modifying a private copy. Propagation includes copying,
  96 +distribution (with or without modification), making available to the
  97 +public, and in some countries other activities as well.
  98 +
  99 + To "convey" a work means any kind of propagation that enables other
  100 +parties to make or receive copies. Mere interaction with a user through
  101 +a computer network, with no transfer of a copy, is not conveying.
  102 +
  103 + An interactive user interface displays "Appropriate Legal Notices"
  104 +to the extent that it includes a convenient and prominently visible
  105 +feature that (1) displays an appropriate copyright notice, and (2)
  106 +tells the user that there is no warranty for the work (except to the
  107 +extent that warranties are provided), that licensees may convey the
  108 +work under this License, and how to view a copy of this License. If
  109 +the interface presents a list of user commands or options, such as a
  110 +menu, a prominent item in the list meets this criterion.
  111 +
  112 + 1. Source Code.
  113 +
  114 + The "source code" for a work means the preferred form of the work
  115 +for making modifications to it. "Object code" means any non-source
  116 +form of a work.
  117 +
  118 + A "Standard Interface" means an interface that either is an official
  119 +standard defined by a recognized standards body, or, in the case of
  120 +interfaces specified for a particular programming language, one that
  121 +is widely used among developers working in that language.
  122 +
  123 + The "System Libraries" of an executable work include anything, other
  124 +than the work as a whole, that (a) is included in the normal form of
  125 +packaging a Major Component, but which is not part of that Major
  126 +Component, and (b) serves only to enable use of the work with that
  127 +Major Component, or to implement a Standard Interface for which an
  128 +implementation is available to the public in source code form. A
  129 +"Major Component", in this context, means a major essential component
  130 +(kernel, window system, and so on) of the specific operating system
  131 +(if any) on which the executable work runs, or a compiler used to
  132 +produce the work, or an object code interpreter used to run it.
  133 +
  134 + The "Corresponding Source" for a work in object code form means all
  135 +the source code needed to generate, install, and (for an executable
  136 +work) run the object code and to modify the work, including scripts to
  137 +control those activities. However, it does not include the work's
  138 +System Libraries, or general-purpose tools or generally available free
  139 +programs which are used unmodified in performing those activities but
  140 +which are not part of the work. For example, Corresponding Source
  141 +includes interface definition files associated with source files for
  142 +the work, and the source code for shared libraries and dynamically
  143 +linked subprograms that the work is specifically designed to require,
  144 +such as by intimate data communication or control flow between those
  145 +subprograms and other parts of the work.
  146 +
  147 + The Corresponding Source need not include anything that users
  148 +can regenerate automatically from other parts of the Corresponding
  149 +Source.
  150 +
  151 + The Corresponding Source for a work in source code form is that
  152 +same work.
  153 +
  154 + 2. Basic Permissions.
  155 +
  156 + All rights granted under this License are granted for the term of
  157 +copyright on the Program, and are irrevocable provided the stated
  158 +conditions are met. This License explicitly affirms your unlimited
  159 +permission to run the unmodified Program. The output from running a
  160 +covered work is covered by this License only if the output, given its
  161 +content, constitutes a covered work. This License acknowledges your
  162 +rights of fair use or other equivalent, as provided by copyright law.
  163 +
  164 + You may make, run and propagate covered works that you do not
  165 +convey, without conditions so long as your license otherwise remains
  166 +in force. You may convey covered works to others for the sole purpose
  167 +of having them make modifications exclusively for you, or provide you
  168 +with facilities for running those works, provided that you comply with
  169 +the terms of this License in conveying all material for which you do
  170 +not control copyright. Those thus making or running the covered works
  171 +for you must do so exclusively on your behalf, under your direction
  172 +and control, on terms that prohibit them from making any copies of
  173 +your copyrighted material outside their relationship with you.
  174 +
  175 + Conveying under any other circumstances is permitted solely under
  176 +the conditions stated below. Sublicensing is not allowed; section 10
  177 +makes it unnecessary.
  178 +
  179 + 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
  180 +
  181 + No covered work shall be deemed part of an effective technological
  182 +measure under any applicable law fulfilling obligations under article
  183 +11 of the WIPO copyright treaty adopted on 20 December 1996, or
  184 +similar laws prohibiting or restricting circumvention of such
  185 +measures.
  186 +
  187 + When you convey a covered work, you waive any legal power to forbid
  188 +circumvention of technological measures to the extent such circumvention
  189 +is effected by exercising rights under this License with respect to
  190 +the covered work, and you disclaim any intention to limit operation or
  191 +modification of the work as a means of enforcing, against the work's
  192 +users, your or third parties' legal rights to forbid circumvention of
  193 +technological measures.
  194 +
  195 + 4. Conveying Verbatim Copies.
  196 +
  197 + You may convey verbatim copies of the Program's source code as you
  198 +receive it, in any medium, provided that you conspicuously and
  199 +appropriately publish on each copy an appropriate copyright notice;
  200 +keep intact all notices stating that this License and any
  201 +non-permissive terms added in accord with section 7 apply to the code;
  202 +keep intact all notices of the absence of any warranty; and give all
  203 +recipients a copy of this License along with the Program.
  204 +
  205 + You may charge any price or no price for each copy that you convey,
  206 +and you may offer support or warranty protection for a fee.
  207 +
  208 + 5. Conveying Modified Source Versions.
  209 +
  210 + You may convey a work based on the Program, or the modifications to
  211 +produce it from the Program, in the form of source code under the
  212 +terms of section 4, provided that you also meet all of these conditions:
  213 +
  214 + a) The work must carry prominent notices stating that you modified
  215 + it, and giving a relevant date.
  216 +
  217 + b) The work must carry prominent notices stating that it is
  218 + released under this License and any conditions added under section
  219 + 7. This requirement modifies the requirement in section 4 to
  220 + "keep intact all notices".
  221 +
  222 + c) You must license the entire work, as a whole, under this
  223 + License to anyone who comes into possession of a copy. This
  224 + License will therefore apply, along with any applicable section 7
  225 + additional terms, to the whole of the work, and all its parts,
  226 + regardless of how they are packaged. This License gives no
  227 + permission to license the work in any other way, but it does not
  228 + invalidate such permission if you have separately received it.
  229 +
  230 + d) If the work has interactive user interfaces, each must display
  231 + Appropriate Legal Notices; however, if the Program has interactive
  232 + interfaces that do not display Appropriate Legal Notices, your
  233 + work need not make them do so.
  234 +
  235 + A compilation of a covered work with other separate and independent
  236 +works, which are not by their nature extensions of the covered work,
  237 +and which are not combined with it such as to form a larger program,
  238 +in or on a volume of a storage or distribution medium, is called an
  239 +"aggregate" if the compilation and its resulting copyright are not
  240 +used to limit the access or legal rights of the compilation's users
  241 +beyond what the individual works permit. Inclusion of a covered work
  242 +in an aggregate does not cause this License to apply to the other
  243 +parts of the aggregate.
  244 +
  245 + 6. Conveying Non-Source Forms.
  246 +
  247 + You may convey a covered work in object code form under the terms
  248 +of sections 4 and 5, provided that you also convey the
  249 +machine-readable Corresponding Source under the terms of this License,
  250 +in one of these ways:
  251 +
  252 + a) Convey the object code in, or embodied in, a physical product
  253 + (including a physical distribution medium), accompanied by the
  254 + Corresponding Source fixed on a durable physical medium
  255 + customarily used for software interchange.
  256 +
  257 + b) Convey the object code in, or embodied in, a physical product
  258 + (including a physical distribution medium), accompanied by a
  259 + written offer, valid for at least three years and valid for as
  260 + long as you offer spare parts or customer support for that product
  261 + model, to give anyone who possesses the object code either (1) a
  262 + copy of the Corresponding Source for all the software in the
  263 + product that is covered by this License, on a durable physical
  264 + medium customarily used for software interchange, for a price no
  265 + more than your reasonable cost of physically performing this
  266 + conveying of source, or (2) access to copy the
  267 + Corresponding Source from a network server at no charge.
  268 +
  269 + c) Convey individual copies of the object code with a copy of the
  270 + written offer to provide the Corresponding Source. This
  271 + alternative is allowed only occasionally and noncommercially, and
  272 + only if you received the object code with such an offer, in accord
  273 + with subsection 6b.
  274 +
  275 + d) Convey the object code by offering access from a designated
  276 + place (gratis or for a charge), and offer equivalent access to the
  277 + Corresponding Source in the same way through the same place at no
  278 + further charge. You need not require recipients to copy the
  279 + Corresponding Source along with the object code. If the place to
  280 + copy the object code is a network server, the Corresponding Source
  281 + may be on a different server (operated by you or a third party)
  282 + that supports equivalent copying facilities, provided you maintain
  283 + clear directions next to the object code saying where to find the
  284 + Corresponding Source. Regardless of what server hosts the
  285 + Corresponding Source, you remain obligated to ensure that it is
  286 + available for as long as needed to satisfy these requirements.
  287 +
  288 + e) Convey the object code using peer-to-peer transmission, provided
  289 + you inform other peers where the object code and Corresponding
  290 + Source of the work are being offered to the general public at no
  291 + charge under subsection 6d.
  292 +
  293 + A separable portion of the object code, whose source code is excluded
  294 +from the Corresponding Source as a System Library, need not be
  295 +included in conveying the object code work.
  296 +
  297 + A "User Product" is either (1) a "consumer product", which means any
  298 +tangible personal property which is normally used for personal, family,
  299 +or household purposes, or (2) anything designed or sold for incorporation
  300 +into a dwelling. In determining whether a product is a consumer product,
  301 +doubtful cases shall be resolved in favor of coverage. For a particular
  302 +product received by a particular user, "normally used" refers to a
  303 +typical or common use of that class of product, regardless of the status
  304 +of the particular user or of the way in which the particular user
  305 +actually uses, or expects or is expected to use, the product. A product
  306 +is a consumer product regardless of whether the product has substantial
  307 +commercial, industrial or non-consumer uses, unless such uses represent
  308 +the only significant mode of use of the product.
  309 +
  310 + "Installation Information" for a User Product means any methods,
  311 +procedures, authorization keys, or other information required to install
  312 +and execute modified versions of a covered work in that User Product from
  313 +a modified version of its Corresponding Source. The information must
  314 +suffice to ensure that the continued functioning of the modified object
  315 +code is in no case prevented or interfered with solely because
  316 +modification has been made.
  317 +
  318 + If you convey an object code work under this section in, or with, or
  319 +specifically for use in, a User Product, and the conveying occurs as
  320 +part of a transaction in which the right of possession and use of the
  321 +User Product is transferred to the recipient in perpetuity or for a
  322 +fixed term (regardless of how the transaction is characterized), the
  323 +Corresponding Source conveyed under this section must be accompanied
  324 +by the Installation Information. But this requirement does not apply
  325 +if neither you nor any third party retains the ability to install
  326 +modified object code on the User Product (for example, the work has
  327 +been installed in ROM).
  328 +
  329 + The requirement to provide Installation Information does not include a
  330 +requirement to continue to provide support service, warranty, or updates
  331 +for a work that has been modified or installed by the recipient, or for
  332 +the User Product in which it has been modified or installed. Access to a
  333 +network may be denied when the modification itself materially and
  334 +adversely affects the operation of the network or violates the rules and
  335 +protocols for communication across the network.
  336 +
  337 + Corresponding Source conveyed, and Installation Information provided,
  338 +in accord with this section must be in a format that is publicly
  339 +documented (and with an implementation available to the public in
  340 +source code form), and must require no special password or key for
  341 +unpacking, reading or copying.
  342 +
  343 + 7. Additional Terms.
  344 +
  345 + "Additional permissions" are terms that supplement the terms of this
  346 +License by making exceptions from one or more of its conditions.
  347 +Additional permissions that are applicable to the entire Program shall
  348 +be treated as though they were included in this License, to the extent
  349 +that they are valid under applicable law. If additional permissions
  350 +apply only to part of the Program, that part may be used separately
  351 +under those permissions, but the entire Program remains governed by
  352 +this License without regard to the additional permissions.
  353 +
  354 + When you convey a copy of a covered work, you may at your option
  355 +remove any additional permissions from that copy, or from any part of
  356 +it. (Additional permissions may be written to require their own
  357 +removal in certain cases when you modify the work.) You may place
  358 +additional permissions on material, added by you to a covered work,
  359 +for which you have or can give appropriate copyright permission.
  360 +
  361 + Notwithstanding any other provision of this License, for material you
  362 +add to a covered work, you may (if authorized by the copyright holders of
  363 +that material) supplement the terms of this License with terms:
  364 +
  365 + a) Disclaiming warranty or limiting liability differently from the
  366 + terms of sections 15 and 16 of this License; or
  367 +
  368 + b) Requiring preservation of specified reasonable legal notices or
  369 + author attributions in that material or in the Appropriate Legal
  370 + Notices displayed by works containing it; or
  371 +
  372 + c) Prohibiting misrepresentation of the origin of that material, or
  373 + requiring that modified versions of such material be marked in
  374 + reasonable ways as different from the original version; or
  375 +
  376 + d) Limiting the use for publicity purposes of names of licensors or
  377 + authors of the material; or
  378 +
  379 + e) Declining to grant rights under trademark law for use of some
  380 + trade names, trademarks, or service marks; or
  381 +
  382 + f) Requiring indemnification of licensors and authors of that
  383 + material by anyone who conveys the material (or modified versions of
  384 + it) with contractual assumptions of liability to the recipient, for
  385 + any liability that these contractual assumptions directly impose on
  386 + those licensors and authors.
  387 +
  388 + All other non-permissive additional terms are considered "further
  389 +restrictions" within the meaning of section 10. If the Program as you
  390 +received it, or any part of it, contains a notice stating that it is
  391 +governed by this License along with a term that is a further
  392 +restriction, you may remove that term. If a license document contains
  393 +a further restriction but permits relicensing or conveying under this
  394 +License, you may add to a covered work material governed by the terms
  395 +of that license document, provided that the further restriction does
  396 +not survive such relicensing or conveying.
  397 +
  398 + If you add terms to a covered work in accord with this section, you
  399 +must place, in the relevant source files, a statement of the
  400 +additional terms that apply to those files, or a notice indicating
  401 +where to find the applicable terms.
  402 +
  403 + Additional terms, permissive or non-permissive, may be stated in the
  404 +form of a separately written license, or stated as exceptions;
  405 +the above requirements apply either way.
  406 +
  407 + 8. Termination.
  408 +
  409 + You may not propagate or modify a covered work except as expressly
  410 +provided under this License. Any attempt otherwise to propagate or
  411 +modify it is void, and will automatically terminate your rights under
  412 +this License (including any patent licenses granted under the third
  413 +paragraph of section 11).
  414 +
  415 + However, if you cease all violation of this License, then your
  416 +license from a particular copyright holder is reinstated (a)
  417 +provisionally, unless and until the copyright holder explicitly and
  418 +finally terminates your license, and (b) permanently, if the copyright
  419 +holder fails to notify you of the violation by some reasonable means
  420 +prior to 60 days after the cessation.
  421 +
  422 + Moreover, your license from a particular copyright holder is
  423 +reinstated permanently if the copyright holder notifies you of the
  424 +violation by some reasonable means, this is the first time you have
  425 +received notice of violation of this License (for any work) from that
  426 +copyright holder, and you cure the violation prior to 30 days after
  427 +your receipt of the notice.
  428 +
  429 + Termination of your rights under this section does not terminate the
  430 +licenses of parties who have received copies or rights from you under
  431 +this License. If your rights have been terminated and not permanently
  432 +reinstated, you do not qualify to receive new licenses for the same
  433 +material under section 10.
  434 +
  435 + 9. Acceptance Not Required for Having Copies.
  436 +
  437 + You are not required to accept this License in order to receive or
  438 +run a copy of the Program. Ancillary propagation of a covered work
  439 +occurring solely as a consequence of using peer-to-peer transmission
  440 +to receive a copy likewise does not require acceptance. However,
  441 +nothing other than this License grants you permission to propagate or
  442 +modify any covered work. These actions infringe copyright if you do
  443 +not accept this License. Therefore, by modifying or propagating a
  444 +covered work, you indicate your acceptance of this License to do so.
  445 +
  446 + 10. Automatic Licensing of Downstream Recipients.
  447 +
  448 + Each time you convey a covered work, the recipient automatically
  449 +receives a license from the original licensors, to run, modify and
  450 +propagate that work, subject to this License. You are not responsible
  451 +for enforcing compliance by third parties with this License.
  452 +
  453 + An "entity transaction" is a transaction transferring control of an
  454 +organization, or substantially all assets of one, or subdividing an
  455 +organization, or merging organizations. If propagation of a covered
  456 +work results from an entity transaction, each party to that
  457 +transaction who receives a copy of the work also receives whatever
  458 +licenses to the work the party's predecessor in interest had or could
  459 +give under the previous paragraph, plus a right to possession of the
  460 +Corresponding Source of the work from the predecessor in interest, if
  461 +the predecessor has it or can get it with reasonable efforts.
  462 +
  463 + You may not impose any further restrictions on the exercise of the
  464 +rights granted or affirmed under this License. For example, you may
  465 +not impose a license fee, royalty, or other charge for exercise of
  466 +rights granted under this License, and you may not initiate litigation
  467 +(including a cross-claim or counterclaim in a lawsuit) alleging that
  468 +any patent claim is infringed by making, using, selling, offering for
  469 +sale, or importing the Program or any portion of it.
  470 +
  471 + 11. Patents.
  472 +
  473 + A "contributor" is a copyright holder who authorizes use under this
  474 +License of the Program or a work on which the Program is based. The
  475 +work thus licensed is called the contributor's "contributor version".
  476 +
  477 + A contributor's "essential patent claims" are all patent claims
  478 +owned or controlled by the contributor, whether already acquired or
  479 +hereafter acquired, that would be infringed by some manner, permitted
  480 +by this License, of making, using, or selling its contributor version,
  481 +but do not include claims that would be infringed only as a
  482 +consequence of further modification of the contributor version. For
  483 +purposes of this definition, "control" includes the right to grant
  484 +patent sublicenses in a manner consistent with the requirements of
  485 +this License.
  486 +
  487 + Each contributor grants you a non-exclusive, worldwide, royalty-free
  488 +patent license under the contributor's essential patent claims, to
  489 +make, use, sell, offer for sale, import and otherwise run, modify and
  490 +propagate the contents of its contributor version.
  491 +
  492 + In the following three paragraphs, a "patent license" is any express
  493 +agreement or commitment, however denominated, not to enforce a patent
  494 +(such as an express permission to practice a patent or covenant not to
  495 +sue for patent infringement). To "grant" such a patent license to a
  496 +party means to make such an agreement or commitment not to enforce a
  497 +patent against the party.
  498 +
  499 + If you convey a covered work, knowingly relying on a patent license,
  500 +and the Corresponding Source of the work is not available for anyone
  501 +to copy, free of charge and under the terms of this License, through a
  502 +publicly available network server or other readily accessible means,
  503 +then you must either (1) cause the Corresponding Source to be so
  504 +available, or (2) arrange to deprive yourself of the benefit of the
  505 +patent license for this particular work, or (3) arrange, in a manner
  506 +consistent with the requirements of this License, to extend the patent
  507 +license to downstream recipients. "Knowingly relying" means you have
  508 +actual knowledge that, but for the patent license, your conveying the
  509 +covered work in a country, or your recipient's use of the covered work
  510 +in a country, would infringe one or more identifiable patents in that
  511 +country that you have reason to believe are valid.
  512 +
  513 + If, pursuant to or in connection with a single transaction or
  514 +arrangement, you convey, or propagate by procuring conveyance of, a
  515 +covered work, and grant a patent license to some of the parties
  516 +receiving the covered work authorizing them to use, propagate, modify
  517 +or convey a specific copy of the covered work, then the patent license
  518 +you grant is automatically extended to all recipients of the covered
  519 +work and works based on it.
  520 +
  521 + A patent license is "discriminatory" if it does not include within
  522 +the scope of its coverage, prohibits the exercise of, or is
  523 +conditioned on the non-exercise of one or more of the rights that are
  524 +specifically granted under this License. You may not convey a covered
  525 +work if you are a party to an arrangement with a third party that is
  526 +in the business of distributing software, under which you make payment
  527 +to the third party based on the extent of your activity of conveying
  528 +the work, and under which the third party grants, to any of the
  529 +parties who would receive the covered work from you, a discriminatory
  530 +patent license (a) in connection with copies of the covered work
  531 +conveyed by you (or copies made from those copies), or (b) primarily
  532 +for and in connection with specific products or compilations that
  533 +contain the covered work, unless you entered into that arrangement,
  534 +or that patent license was granted, prior to 28 March 2007.
  535 +
  536 + Nothing in this License shall be construed as excluding or limiting
  537 +any implied license or other defenses to infringement that may
  538 +otherwise be available to you under applicable patent law.
  539 +
  540 + 12. No Surrender of Others' Freedom.
  541 +
  542 + If conditions are imposed on you (whether by court order, agreement or
  543 +otherwise) that contradict the conditions of this License, they do not
  544 +excuse you from the conditions of this License. If you cannot convey a
  545 +covered work so as to satisfy simultaneously your obligations under this
  546 +License and any other pertinent obligations, then as a consequence you may
  547 +not convey it at all. For example, if you agree to terms that obligate you
  548 +to collect a royalty for further conveying from those to whom you convey
  549 +the Program, the only way you could satisfy both those terms and this
  550 +License would be to refrain entirely from conveying the Program.
  551 +
  552 + 13. Use with the GNU Affero General Public License.
  553 +
  554 + Notwithstanding any other provision of this License, you have
  555 +permission to link or combine any covered work with a work licensed
  556 +under version 3 of the GNU Affero General Public License into a single
  557 +combined work, and to convey the resulting work. The terms of this
  558 +License will continue to apply to the part which is the covered work,
  559 +but the special requirements of the GNU Affero General Public License,
  560 +section 13, concerning interaction through a network will apply to the
  561 +combination as such.
  562 +
  563 + 14. Revised Versions of this License.
  564 +
  565 + The Free Software Foundation may publish revised and/or new versions of
  566 +the GNU General Public License from time to time. Such new versions will
  567 +be similar in spirit to the present version, but may differ in detail to
  568 +address new problems or concerns.
  569 +
  570 + Each version is given a distinguishing version number. If the
  571 +Program specifies that a certain numbered version of the GNU General
  572 +Public License "or any later version" applies to it, you have the
  573 +option of following the terms and conditions either of that numbered
  574 +version or of any later version published by the Free Software
  575 +Foundation. If the Program does not specify a version number of the
  576 +GNU General Public License, you may choose any version ever published
  577 +by the Free Software Foundation.
  578 +
  579 + If the Program specifies that a proxy can decide which future
  580 +versions of the GNU General Public License can be used, that proxy's
  581 +public statement of acceptance of a version permanently authorizes you
  582 +to choose that version for the Program.
  583 +
  584 + Later license versions may give you additional or different
  585 +permissions. However, no additional obligations are imposed on any
  586 +author or copyright holder as a result of your choosing to follow a
  587 +later version.
  588 +
  589 + 15. Disclaimer of Warranty.
  590 +
  591 + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
  592 +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
  593 +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
  594 +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
  595 +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  596 +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
  597 +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
  598 +ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  599 +
  600 + 16. Limitation of Liability.
  601 +
  602 + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
  603 +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
  604 +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
  605 +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  606 +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
  607 +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
  608 +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
  609 +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
  610 +SUCH DAMAGES.
  611 +
  612 + 17. Interpretation of Sections 15 and 16.
  613 +
  614 + If the disclaimer of warranty and limitation of liability provided
  615 +above cannot be given local legal effect according to their terms,
  616 +reviewing courts shall apply local law that most closely approximates
  617 +an absolute waiver of all civil liability in connection with the
  618 +Program, unless a warranty or assumption of liability accompanies a
  619 +copy of the Program in return for a fee.
  620 +
  621 + END OF TERMS AND CONDITIONS
  622 +
  623 + How to Apply These Terms to Your New Programs
  624 +
  625 + If you develop a new program, and you want it to be of the greatest
  626 +possible use to the public, the best way to achieve this is to make it
  627 +free software which everyone can redistribute and change under these terms.
  628 +
  629 + To do so, attach the following notices to the program. It is safest
  630 +to attach them to the start of each source file to most effectively
  631 +state the exclusion of warranty; and each file should have at least
  632 +the "copyright" line and a pointer to where the full notice is found.
  633 +
  634 + <one line to give the program's name and a brief idea of what it does.>
  635 + Copyright (C) <year> <name of author>
  636 +
  637 + This program is free software: you can redistribute it and/or modify
  638 + it under the terms of the GNU General Public License as published by
  639 + the Free Software Foundation, either version 3 of the License, or
  640 + (at your option) any later version.
  641 +
  642 + This program is distributed in the hope that it will be useful,
  643 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  644 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  645 + GNU General Public License for more details.
  646 +
  647 + You should have received a copy of the GNU General Public License
  648 + along with this program. If not, see <http://www.gnu.org/licenses/>.
  649 +
  650 +Also add information on how to contact you by electronic and paper mail.
  651 +
  652 + If the program does terminal interaction, make it output a short
  653 +notice like this when it starts in an interactive mode:
  654 +
  655 + <program> Copyright (C) <year> <name of author>
  656 + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
  657 + This is free software, and you are welcome to redistribute it
  658 + under certain conditions; type `show c' for details.
  659 +
  660 +The hypothetical commands `show w' and `show c' should show the appropriate
  661 +parts of the General Public License. Of course, your program's commands
  662 +might be different; for a GUI interface, you would use an "about box".
  663 +
  664 + You should also get your employer (if you work as a programmer) or school,
  665 +if any, to sign a "copyright disclaimer" for the program, if necessary.
  666 +For more information on this, and how to apply and follow the GNU GPL, see
  667 +<http://www.gnu.org/licenses/>.
  668 +
  669 + The GNU General Public License does not permit incorporating your program
  670 +into proprietary programs. If your program is a subroutine library, you
  671 +may consider it more useful to permit linking proprietary applications with
  672 +the library. If this is what you want to do, use the GNU Lesser General
  673 +Public License instead of this License. But first, please read
  674 +<http://www.gnu.org/philosophy/why-not-lgpl.html>.
  1 +akvirtualcamera 9.1.3:
  2 +
  3 +- Quote file path if it contains spaces (issue #75).
  4 +- Added --build-info flag.
  5 +
  6 +akvirtualcamera 9.1.2:
  7 +
  8 +- Added ARM 64 packages for Mac.
  9 +
  10 +akvirtualcamera 9.1.1:
  11 +
  12 +- Mark Mac packages architecture.
  13 +
  14 +akvirtualcamera 9.1.0:
  15 +
  16 +- Added -f, --force flags to the manager, this bypass the virtual camera usage
  17 + check.
  18 +- Fixed output example in Windows.
  19 +- Now distributing the Mac installer as a pkg.
  20 +
  21 +akvirtualcamera 9.0.0:
  22 +
  23 +- Code forked from Webcamoid. This virtual camera isn't compatible with
  24 + Webcamoid 8.x and older releases.
  25 +- Build system switched to Cmake.
  26 +- Fixed plugins installers and switched to NSIS in Windows.
  27 +- Added a command line tool to communicate with the virtual camera
  28 + (AkVCamManager).
  29 +- Added support for PNG and JPG for the placeholder picture.
  30 +- Added documentation and example usages.
  1 +# README
  2 +
  3 +Some considerations before doing anything:
  4 +
  5 +* Search the [issues list](https://github.com/webcamoid/akvirtualcamera/issues) for similar topics before opening a new one.
  6 +* If you know how to fix the problem, consider doing a [pull request](https://github.com/webcamoid/akvirtualcamera/pulls) instead of opening a new issue.
  7 +* Only report problems in [latest](https://github.com/webcamoid/akvirtualcamera/releases) and [development](https://github.com/webcamoid/akvirtualcamera/) version. Reporting issues related to older versions will be rejected.
  8 +* Use [gist](https://gist.github.com/) to post logs longer than 1024 characters.
  9 +* akvirtualcamera as project, is not affiliated or endorsed to any distribution, report packaging problems in their respective issue tracker.
  10 +* Respect the templates, we need as much information as possible.
  11 +* Don't open an issue and disappear, we need you at least the first week to clear up missing information.
  12 +* Missing information makes useless and unsolvable an issue report.
  13 +* You are our debbuger, eyes and hands, if an issue is not reproducible then you will be the only person able to solve it.
  14 +* Take a seat and wait for your turn, as many others that has their issues open, or much better try fixing it your self and collaborate with the solution :smile:
  15 +
  16 +Choose one of the templates bellow that fit better your issue.
  17 +
  18 +# Reporting a problem?
  19 +
  20 +## Summary
  21 +
  22 +Write here a brief description of the problem.
  23 +
  24 +## Current Behavior
  25 +
  26 +Describe the problem the best as you can, don't omit information.
  27 +
  28 +## Expected Behavior
  29 +
  30 +How it should have work?
  31 +
  32 +## Steps to Reproduce
  33 +
  34 +If akvirtualcamera crashed, try to reproduce the crash several times to be sure where is the problem. Write the steps to reproduce the issue bellow:
  35 +
  36 +1. Open this
  37 +2. Click that
  38 +3. Drag those
  39 +4. ...
  40 +
  41 +## Suggestions and tips
  42 +
  43 +How would you solve the problem?
  44 +
  45 +## Your Environment
  46 +
  47 +* akvirtualcamera inormation: (version) (architecture)
  48 +* Operating System information: (name) (numeric version and codename if applied) (architecture)
  49 +* Any other useful information: (logs, gdb backtrace, valgrind logs, screenshots, hardware, etc.)
  50 +
  51 +# Want a new feature?
  52 +
  53 +Describe your idea the best as you can, include sketches, mockups and diagrams if required.
  54 +Be patient, take in mind that there may be other priorities. Your idea will be accepted if fit in project goals.
  55 +
  56 +# Questinons and other matters?
  57 +
  58 +Go ahead!
  1 +# akvirtualcamera, virtual camera for Mac and Windows.
  2 +# Copyright (C) 2021 Gonzalo Exequiel Pedone
  3 +#
  4 +# akvirtualcamera is free software: you can redistribute it and/or modify
  5 +# it under the terms of the GNU General Public License as published by
  6 +# the Free Software Foundation, either version 3 of the License, or
  7 +# (at your option) any later version.
  8 +#
  9 +# akvirtualcamera is distributed in the hope that it will be useful,
  10 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 +# GNU General Public License for more details.
  13 +#
  14 +# You should have received a copy of the GNU General Public License
  15 +# along with akvirtualcamera. If not, see <http://www.gnu.org/licenses/>.
  16 +#
  17 +# Web-Site: http://webcamoid.github.io/
  18 +
  19 +cmake_minimum_required(VERSION 3.5)
  20 +
  21 +project(Manager LANGUAGES CXX)
  22 +
  23 +include(../commons.cmake)
  24 +
  25 +set(CMAKE_CXX_STANDARD 11)
  26 +set(CMAKE_CXX_STANDARD_REQUIRED ON)
  27 +
  28 +add_executable(Manager
  29 + src/cmdparser.h
  30 + src/main.cpp
  31 + src/cmdparser.cpp)
  32 +
  33 +if (APPLE OR FAKE_APPLE)
  34 + set_target_properties(Manager PROPERTIES
  35 + OUTPUT_NAME ${AKVCAM_MANAGER_NAME}
  36 + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${BUILDDIR}/${DATAROOTDIR})
  37 +else ()
  38 + set_target_properties(Manager PROPERTIES
  39 + OUTPUT_NAME ${AKVCAM_MANAGER_NAME}
  40 + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${BUILDDIR}/${BINDIR}
  41 + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${BUILDDIR}/${BINDIR})
  42 +endif ()
  43 +
  44 +add_dependencies(Manager VCamUtils)
  45 +
  46 +if (APPLE OR FAKE_APPLE)
  47 + add_dependencies(Manager
  48 + VCamIPC_cmio
  49 + PlatformUtils_cmio)
  50 +elseif (WIN32)
  51 + add_dependencies(Manager
  52 + VCamIPC_dshow
  53 + PlatformUtils_dshow)
  54 +endif ()
  55 +
  56 +target_include_directories(Manager
  57 + PRIVATE ..)
  58 +target_link_libraries(Manager
  59 + VCamUtils)
  60 +
  61 +if (APPLE OR FAKE_APPLE)
  62 + target_link_libraries(Manager
  63 + VCamIPC_cmio
  64 + PlatformUtils_cmio)
  65 +elseif (WIN32)
  66 + target_link_libraries(Manager
  67 + VCamIPC_dshow
  68 + PlatformUtils_dshow)
  69 +endif ()
  70 +
  71 +if (APPLE OR FAKE_APPLE)
  72 + if (APPLE)
  73 + find_library(COREFOUNDATION_FRAMEWORK NAMES CoreFoundation)
  74 + find_library(COREMEDIA_FRAMEWORK NAMES CoreMedia)
  75 + find_library(COREMEDIAIO_FRAMEWORK NAMES CoreMediaIO)
  76 + find_library(COREVIDEO_FRAMEWORK NAMES CoreVideo)
  77 + find_library(FOUNDATION_FRAMEWORK NAMES Foundation)
  78 + find_library(IOKIT_FRAMEWORK NAMES IOKit)
  79 +
  80 + set(EXTRA_LIBS ${COREFOUNDATION_FRAMEWORK}
  81 + ${COREMEDIA_FRAMEWORK}
  82 + ${COREMEDIAIO_FRAMEWORK}
  83 + ${COREVIDEO_FRAMEWORK}
  84 + ${FOUNDATION_FRAMEWORK}
  85 + ${IOKIT_FRAMEWORK})
  86 + target_link_libraries(Manager ${EXTRA_LIBS})
  87 + endif ()
  88 +elseif (WIN32)
  89 + target_link_libraries(Manager
  90 + advapi32
  91 + gdi32
  92 + ole32
  93 + oleaut32
  94 + shell32
  95 + strmiids
  96 + uuid)
  97 +endif ()
  98 +
  99 +if (APPLE OR FAKE_APPLE)
  100 + install(TARGETS Manager DESTINATION ${DATAROOTDIR})
  101 +elseif (WIN32)
  102 + install(TARGETS Manager DESTINATION ${BINDIR})
  103 +endif ()
  1 +/* akvirtualcamera, virtual camera for Mac and Windows.
  2 + * Copyright (C) 2020 Gonzalo Exequiel Pedone
  3 + *
  4 + * akvirtualcamera is free software: you can redistribute it and/or modify
  5 + * it under the terms of the GNU General Public License as published by
  6 + * the Free Software Foundation, either version 3 of the License, or
  7 + * (at your option) any later version.
  8 + *
  9 + * akvirtualcamera is distributed in the hope that it will be useful,
  10 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 + * GNU General Public License for more details.
  13 + *
  14 + * You should have received a copy of the GNU General Public License
  15 + * along with akvirtualcamera. If not, see <http://www.gnu.org/licenses/>.
  16 + *
  17 + * Web-Site: http://webcamoid.github.io/
  18 + */
  19 +
  20 +#include <algorithm>
  21 +#include <cerrno>
  22 +#include <chrono>
  23 +#include <cmath>
  24 +#include <codecvt>
  25 +#include <csignal>
  26 +#include <cstring>
  27 +#include <iostream>
  28 +#include <functional>
  29 +#include <locale>
  30 +#include <sstream>
  31 +#include <thread>
  32 +
  33 +#ifdef _WIN32
  34 +#include <fcntl.h>
  35 +#include <io.h>
  36 +#endif
  37 +
  38 +#include "cmdparser.h"
  39 +#include "VCamUtils/src/ipcbridge.h"
  40 +#include "VCamUtils/src/settings.h"
  41 +#include "VCamUtils/src/videoformat.h"
  42 +#include "VCamUtils/src/videoframe.h"
  43 +#include "VCamUtils/src/logger.h"
  44 +
  45 +#define COMMONS_PROJECT_COMMIT_URL "https://github.com/webcamoid/akvirtualcamera/commit"
  46 +
  47 +#define AKVCAM_BIND_FUNC(member) \
  48 + std::bind(&member, this->d, std::placeholders::_1, std::placeholders::_2)
  49 +
  50 +namespace AkVCam {
  51 + using StringMatrix = std::vector<StringVector>;
  52 + using VideoFormatMatrix = std::vector<std::vector<VideoFormat>>;
  53 +
  54 + struct CmdParserFlags
  55 + {
  56 + StringVector flags;
  57 + std::string value;
  58 + std::string helpString;
  59 + };
  60 +
  61 + struct CmdParserCommand
  62 + {
  63 + std::string command;
  64 + std::string arguments;
  65 + std::string helpString;
  66 + ProgramOptionsFunc func;
  67 + std::vector<CmdParserFlags> flags;
  68 + bool advanced {false};
  69 +
  70 + CmdParserCommand();
  71 + CmdParserCommand(const std::string &command,
  72 + const std::string &arguments,
  73 + const std::string &helpString,
  74 + const ProgramOptionsFunc &func,
  75 + const std::vector<CmdParserFlags> &flags,
  76 + bool advanced);
  77 + };
  78 +
  79 + class CmdParserPrivate
  80 + {
  81 + public:
  82 + std::vector<CmdParserCommand> m_commands;
  83 + IpcBridge m_ipcBridge;
  84 + bool m_parseable {false};
  85 + bool m_force {false};
  86 +
  87 + static const std::map<ControlType, std::string> &typeStrMap();
  88 + void printFlags(const std::vector<CmdParserFlags> &cmdFlags,
  89 + size_t indent);
  90 + size_t maxCommandLength(bool showAdvancedHelp);
  91 + size_t maxArgumentsLength(bool showAdvancedHelp);
  92 + size_t maxFlagsLength(const std::vector<CmdParserFlags> &flags);
  93 + size_t maxFlagsValueLength(const std::vector<CmdParserFlags> &flags);
  94 + size_t maxColumnLength(const StringVector &table,
  95 + size_t width,
  96 + size_t column);
  97 + std::vector<size_t> maxColumnsLength(const StringVector &table,
  98 + size_t width);
  99 + void drawTableHLine(const std::vector<size_t> &columnsLength,
  100 + bool toStdErr=false);
  101 + void drawTable(const StringVector &table,
  102 + size_t width,
  103 + bool toStdErr=false);
  104 + CmdParserCommand *parserCommand(const std::string &command);
  105 + const CmdParserFlags *parserFlag(const std::vector<CmdParserFlags> &cmdFlags,
  106 + const std::string &flag);
  107 + bool containsFlag(const StringMap &flags,
  108 + const std::string &command,
  109 + const std::string &flagAlias);
  110 + std::string flagValue(const StringMap &flags,
  111 + const std::string &command,
  112 + const std::string &flagAlias);
  113 + int defaultHandler(const StringMap &flags,
  114 + const StringVector &args);
  115 + int showHelp(const StringMap &flags, const StringVector &args);
  116 + int showDevices(const StringMap &flags, const StringVector &args);
  117 + int addDevice(const StringMap &flags, const StringVector &args);
  118 + int removeDevice(const StringMap &flags, const StringVector &args);
  119 + int removeDevices(const StringMap &flags, const StringVector &args);
  120 + int showDeviceDescription(const StringMap &flags,
  121 + const StringVector &args);
  122 + int setDeviceDescription(const StringMap &flags,
  123 + const StringVector &args);
  124 + int showSupportedFormats(const StringMap &flags,
  125 + const StringVector &args);
  126 + int showDefaultFormat(const StringMap &flags,
  127 + const StringVector &args);
  128 + int showFormats(const StringMap &flags, const StringVector &args);
  129 + int addFormat(const StringMap &flags, const StringVector &args);
  130 + int removeFormat(const StringMap &flags, const StringVector &args);
  131 + int removeFormats(const StringMap &flags, const StringVector &args);
  132 + int update(const StringMap &flags, const StringVector &args);
  133 + int loadSettings(const StringMap &flags, const StringVector &args);
  134 + int stream(const StringMap &flags, const StringVector &args);
  135 + int listenEvents(const StringMap &flags, const StringVector &args);
  136 + int showControls(const StringMap &flags, const StringVector &args);
  137 + int readControl(const StringMap &flags, const StringVector &args);
  138 + int writeControls(const StringMap &flags, const StringVector &args);
  139 + int picture(const StringMap &flags, const StringVector &args);
  140 + int setPicture(const StringMap &flags, const StringVector &args);
  141 + int logLevel(const StringMap &flags, const StringVector &args);
  142 + int setLogLevel(const StringMap &flags, const StringVector &args);
  143 + int showClients(const StringMap &flags, const StringVector &args);
  144 + int dumpInfo(const StringMap &flags, const StringVector &args);
  145 + int hacks(const StringMap &flags, const StringVector &args);
  146 + int hackInfo(const StringMap &flags, const StringVector &args);
  147 + int hack(const StringMap &flags, const StringVector &args);
  148 + void loadGenerals(Settings &settings);
  149 + VideoFormatMatrix readFormats(Settings &settings);
  150 + std::vector<VideoFormat> readFormat(Settings &settings);
  151 + StringMatrix matrixCombine(const StringMatrix &matrix);
  152 + void matrixCombineP(const StringMatrix &matrix,
  153 + size_t index,
  154 + StringVector combined,
  155 + StringMatrix &combinations);
  156 + void createDevices(Settings &settings,
  157 + const VideoFormatMatrix &availableFormats);
  158 + void createDevice(Settings &settings,
  159 + const VideoFormatMatrix &availableFormats);
  160 + std::vector<VideoFormat> readDeviceFormats(Settings &settings,
  161 + const VideoFormatMatrix &availableFormats);
  162 + };
  163 +
  164 + std::string operator *(const std::string &str, size_t n);
  165 + std::string operator *(size_t n, const std::string &str);
  166 +}
  167 +
  168 +AkVCam::CmdParser::CmdParser()
  169 +{
  170 + this->d = new CmdParserPrivate();
  171 + auto logFile = this->d->m_ipcBridge.logPath("AkVCamManager");
  172 + AkLogInfo() << "Sending debug output to " << logFile << std::endl;
  173 + AkVCam::Logger::setLogFile(logFile);
  174 +
  175 + this->d->m_commands.push_back({});
  176 + this->setDefaultFuntion(AKVCAM_BIND_FUNC(CmdParserPrivate::defaultHandler));
  177 + this->addFlags("",
  178 + {"-h", "--help"},
  179 + "Show help.");
  180 + this->addFlags("",
  181 + {"--help-all"},
  182 + "Show advanced help.");
  183 + this->addFlags("",
  184 + {"-v", "--version"},
  185 + "Show program version.");
  186 + this->addFlags("",
  187 + {"-p", "--parseable"},
  188 + "Show parseable output.");
  189 + this->addFlags("",
  190 + {"-f", "--force"},
  191 + "Force command.");
  192 + this->addFlags("",
  193 + {"--build-info"},
  194 + "Show build information.");
  195 + this->addCommand("devices",
  196 + "",
  197 + "List devices.",
  198 + AKVCAM_BIND_FUNC(CmdParserPrivate::showDevices));
  199 + this->addCommand("add-device",
  200 + "DESCRIPTION",
  201 + "Add a new device.",
  202 + AKVCAM_BIND_FUNC(CmdParserPrivate::addDevice));
  203 + this->addFlags("add-device",
  204 + {"-i", "--id"},
  205 + "DEVICEID",
  206 + "Create device as DEVICEID.");
  207 + this->addCommand("remove-device",
  208 + "DEVICE",
  209 + "Remove a device.",
  210 + AKVCAM_BIND_FUNC(CmdParserPrivate::removeDevice));
  211 + this->addCommand("remove-devices",
  212 + "",
  213 + "Remove all devices.",
  214 + AKVCAM_BIND_FUNC(CmdParserPrivate::removeDevices));
  215 + this->addCommand("description",
  216 + "DEVICE",
  217 + "Show device description.",
  218 + AKVCAM_BIND_FUNC(CmdParserPrivate::showDeviceDescription));
  219 + this->addCommand("set-description",
  220 + "DEVICE DESCRIPTION",
  221 + "Set device description.",
  222 + AKVCAM_BIND_FUNC(CmdParserPrivate::setDeviceDescription));
  223 + this->addCommand("supported-formats",
  224 + "",
  225 + "Show supported formats.",
  226 + AKVCAM_BIND_FUNC(CmdParserPrivate::showSupportedFormats));
  227 + this->addFlags("supported-formats",
  228 + {"-i", "--input"},
  229 + "Show supported input formats.");
  230 + this->addFlags("supported-formats",
  231 + {"-o", "--output"},
  232 + "Show supported output formats.");
  233 + this->addCommand("default-format",
  234 + "",
  235 + "Default device format.",
  236 + AKVCAM_BIND_FUNC(CmdParserPrivate::showDefaultFormat));
  237 + this->addFlags("default-format",
  238 + {"-i", "--input"},
  239 + "Default input format.");
  240 + this->addFlags("default-format",
  241 + {"-o", "--output"},
  242 + "Default output format.");
  243 + this->addCommand("formats",
  244 + "DEVICE",
  245 + "Show device formats.",
  246 + AKVCAM_BIND_FUNC(CmdParserPrivate::showFormats));
  247 + this->addCommand("add-format",
  248 + "DEVICE FORMAT WIDTH HEIGHT FPS",
  249 + "Add a new device format.",
  250 + AKVCAM_BIND_FUNC(CmdParserPrivate::addFormat));
  251 + this->addFlags("add-format",
  252 + {"-i", "--index"},
  253 + "INDEX",
  254 + "Add format at INDEX.");
  255 + this->addCommand("remove-format",
  256 + "DEVICE INDEX",
  257 + "Remove device format.",
  258 + AKVCAM_BIND_FUNC(CmdParserPrivate::removeFormat));
  259 + this->addCommand("remove-formats",
  260 + "DEVICE",
  261 + "Remove all device formats.",
  262 + AKVCAM_BIND_FUNC(CmdParserPrivate::removeFormats));
  263 + this->addCommand("update",
  264 + "",
  265 + "Update devices.",
  266 + AKVCAM_BIND_FUNC(CmdParserPrivate::update));
  267 + this->addCommand("load",
  268 + "SETTINGS.INI",
  269 + "Create devices from a setting file.",
  270 + AKVCAM_BIND_FUNC(CmdParserPrivate::loadSettings));
  271 + this->addCommand("stream",
  272 + "DEVICE FORMAT WIDTH HEIGHT",
  273 + "Read frames from stdin and send them to the device.",
  274 + AKVCAM_BIND_FUNC(CmdParserPrivate::stream));
  275 + this->addFlags("stream",
  276 + {"-f", "--fps"},
  277 + "FPS",
  278 + "Read stream input at a constant frame rate.");
  279 + this->addCommand("listen-events",
  280 + "",
  281 + "Keep the manager running and listening to global events.",
  282 + AKVCAM_BIND_FUNC(CmdParserPrivate::listenEvents));
  283 + this->addCommand("controls",
  284 + "DEVICE",
  285 + "Show device controls.",
  286 + AKVCAM_BIND_FUNC(CmdParserPrivate::showControls));
  287 + this->addCommand("get-control",
  288 + "DEVICE CONTROL",
  289 + "Read device control.",
  290 + AKVCAM_BIND_FUNC(CmdParserPrivate::readControl));
  291 + this->addFlags("get-control",
  292 + {"-c", "--description"},
  293 + "Show control description.");
  294 + this->addFlags("get-control",
  295 + {"-t", "--type"},
  296 + "Show control type.");
  297 + this->addFlags("get-control",
  298 + {"-m", "--min"},
  299 + "Show minimum value for the control.");
  300 + this->addFlags("get-control",
  301 + {"-M", "--max"},
  302 + "Show maximum value for the control.");
  303 + this->addFlags("get-control",
  304 + {"-s", "--step"},
  305 + "Show increment/decrement step for the control.");
  306 + this->addFlags("get-control",
  307 + {"-d", "--default"},
  308 + "Show default value for the control.");
  309 + this->addFlags("get-control",
  310 + {"-l", "--menu"},
  311 + "Show options of a memu type control.");
  312 + this->addCommand("set-controls",
  313 + "DEVICE CONTROL_1=VALUE CONTROL_2=VALUE...",
  314 + "Write device controls values.",
  315 + AKVCAM_BIND_FUNC(CmdParserPrivate::writeControls));
  316 + this->addCommand("picture",
  317 + "",
  318 + "Placeholder picture to show when no streaming.",
  319 + AKVCAM_BIND_FUNC(CmdParserPrivate::picture));
  320 + this->addCommand("set-picture",
  321 + "FILE",
  322 + "Set placeholder picture.",
  323 + AKVCAM_BIND_FUNC(CmdParserPrivate::setPicture));
  324 + this->addCommand("loglevel",
  325 + "",
  326 + "Show current debugging level.",
  327 + AKVCAM_BIND_FUNC(CmdParserPrivate::logLevel));
  328 + this->addCommand("set-loglevel",
  329 + "LEVEL",
  330 + "Set debugging level.",
  331 + AKVCAM_BIND_FUNC(CmdParserPrivate::setLogLevel));
  332 + this->addCommand("clients",
  333 + "",
  334 + "Show clients using the camera.",
  335 + AKVCAM_BIND_FUNC(CmdParserPrivate::showClients));
  336 + this->addCommand("dump",
  337 + "",
  338 + "Show all information in a parseable XML format.",
  339 + AKVCAM_BIND_FUNC(CmdParserPrivate::dumpInfo));
  340 + this->addCommand("hacks",
  341 + "",
  342 + "List system hacks to make the virtual camera work.",
  343 + AKVCAM_BIND_FUNC(CmdParserPrivate::hacks),
  344 + true);
  345 + this->addCommand("hack-info",
  346 + "HACK",
  347 + "Show hack information.",
  348 + AKVCAM_BIND_FUNC(CmdParserPrivate::hackInfo),
  349 + true);
  350 + this->addFlags("hack-info",
  351 + {"-s", "--issafe"},
  352 + "Is hack safe?");
  353 + this->addFlags("hack-info",
  354 + {"-c", "--description"},
  355 + "Show hack description.");
  356 + this->addCommand("hack",
  357 + "HACK PARAMS...",
  358 + "Apply system hack.",
  359 + AKVCAM_BIND_FUNC(CmdParserPrivate::hack),
  360 + true);
  361 + this->addFlags("hack",
  362 + {"-y", "--yes"},
  363 + "Accept all risks and continue anyway.");
  364 +}
  365 +
  366 +AkVCam::CmdParser::~CmdParser()
  367 +{
  368 + delete this->d;
  369 +}
  370 +
  371 +int AkVCam::CmdParser::parse(int argc, char **argv)
  372 +{
  373 + auto program = basename(argv[0]);
  374 + auto command = &this->d->m_commands[0];
  375 + StringMap flags;
  376 + StringVector arguments {program};
  377 +
  378 + for (int i = 1; i < argc; i++) {
  379 + std::string arg = argv[i];
  380 + char *p = nullptr;
  381 + strtod(arg.c_str(), &p);
  382 +
  383 + if (arg[0] == '-' && p && strnlen(p, 1024) != 0) {
  384 + auto flag = this->d->parserFlag(command->flags, arg);
  385 +
  386 + if (!flag) {
  387 + if (command->command.empty())
  388 + std::cout << "Invalid option '"
  389 + << arg
  390 + << "'"
  391 + << std::endl;
  392 + else
  393 + std::cout << "Invalid option '"
  394 + << arg << "' for '"
  395 + << command->command
  396 + << "'"
  397 + << std::endl;
  398 +
  399 + return -EINVAL;
  400 + }
  401 +
  402 + std::string value;
  403 +
  404 + if (!flag->value.empty()) {
  405 + auto next = i + 1;
  406 +
  407 + if (next < argc) {
  408 + value = argv[next];
  409 + i++;
  410 + }
  411 + }
  412 +
  413 + flags[arg] = value;
  414 + } else {
  415 + if (command->command.empty()) {
  416 + if (!flags.empty()) {
  417 + auto result = command->func(flags, {program});
  418 +
  419 + if (result < 0)
  420 + return result;
  421 +
  422 + flags.clear();
  423 + }
  424 +
  425 + auto cmd = this->d->parserCommand(arg);
  426 +
  427 + if (cmd) {
  428 + command = cmd;
  429 + flags.clear();
  430 + } else {
  431 + std::cout << "Unknown command '" << arg << "'" << std::endl;
  432 +
  433 + return -EINVAL;
  434 + }
  435 + } else {
  436 + arguments.push_back(arg);
  437 + }
  438 + }
  439 + }
  440 +
  441 + if (!this->d->m_force && this->d->m_ipcBridge.isBusyFor(command->command)) {
  442 + std::cerr << "This operation is not permitted." << std::endl;
  443 + std::cerr << "The virtual camera is in use. Stop or close the virtual "
  444 + << "camera clients and try again." << std::endl;
  445 + std::cerr << std::endl;
  446 + auto clients = this->d->m_ipcBridge.clientsPids();
  447 +
  448 + if (!clients.empty()) {
  449 + std::vector<std::string> table {
  450 + "Pid",
  451 + "Executable"
  452 + };
  453 + auto columns = table.size();
  454 +
  455 + for (auto &pid: clients) {
  456 + table.push_back(std::to_string(pid));
  457 + table.push_back(this->d->m_ipcBridge.clientExe(pid));
  458 + }
  459 +
  460 + this->d->drawTable(table, columns, true);
  461 + }
  462 +
  463 + return -EBUSY;
  464 + }
  465 +
  466 + if (this->d->m_ipcBridge.needsRoot(command->command)
  467 + || (command->command == "hack"
  468 + && arguments.size() >= 2
  469 + && this->d->m_ipcBridge.hackNeedsRoot(arguments[1]))) {
  470 + std::cerr << "You must run this command with administrator privileges." << std::endl;
  471 +
  472 + return -EPERM;
  473 + }
  474 +
  475 + return command->func(flags, arguments);
  476 +}
  477 +
  478 +void AkVCam::CmdParser::setDefaultFuntion(const ProgramOptionsFunc &func)
  479 +{
  480 + this->d->m_commands[0].func = func;
  481 +}
  482 +
  483 +void AkVCam::CmdParser::addCommand(const std::string &command,
  484 + const std::string &arguments,
  485 + const std::string &helpString,
  486 + const ProgramOptionsFunc &func,
  487 + bool advanced)
  488 +{
  489 + auto it =
  490 + std::find_if(this->d->m_commands.begin(),
  491 + this->d->m_commands.end(),
  492 + [&command] (const CmdParserCommand &cmd) -> bool {
  493 + return cmd.command == command;
  494 + });
  495 +
  496 + if (it == this->d->m_commands.end()) {
  497 + this->d->m_commands.push_back({command,
  498 + arguments,
  499 + helpString,
  500 + func,
  501 + {},
  502 + advanced});
  503 + } else {
  504 + it->command = command;
  505 + it->arguments = arguments;
  506 + it->helpString = helpString;
  507 + it->func = func;
  508 + it->advanced = advanced;
  509 + }
  510 +}
  511 +
  512 +void AkVCam::CmdParser::addFlags(const std::string &command,
  513 + const StringVector &flags,
  514 + const std::string &value,
  515 + const std::string &helpString)
  516 +{
  517 + auto it =
  518 + std::find_if(this->d->m_commands.begin(),
  519 + this->d->m_commands.end(),
  520 + [&command] (const CmdParserCommand &cmd) -> bool {
  521 + return cmd.command == command;
  522 + });
  523 +
  524 + if (it == this->d->m_commands.end())
  525 + return;
  526 +
  527 + it->flags.push_back({flags, value, helpString});
  528 +}
  529 +
  530 +void AkVCam::CmdParser::addFlags(const std::string &command,
  531 + const StringVector &flags,
  532 + const std::string &helpString)
  533 +{
  534 + this->addFlags(command, flags, "", helpString);
  535 +}
  536 +
  537 +const std::map<AkVCam::ControlType, std::string> &AkVCam::CmdParserPrivate::typeStrMap()
  538 +{
  539 + static const std::map<ControlType, std::string> typeStr {
  540 + {ControlTypeInteger, "Integer"},
  541 + {ControlTypeBoolean, "Boolean"},
  542 + {ControlTypeMenu , "Menu" },
  543 + };
  544 +
  545 + return typeStr;
  546 +}
  547 +
  548 +void AkVCam::CmdParserPrivate::printFlags(const std::vector<CmdParserFlags> &cmdFlags,
  549 + size_t indent)
  550 +{
  551 + std::vector<char> spaces(indent, ' ');
  552 + auto maxFlagsLen = this->maxFlagsLength(cmdFlags);
  553 + auto maxFlagsValueLen = this->maxFlagsValueLength(cmdFlags);
  554 +
  555 + for (auto &flag: cmdFlags) {
  556 + auto allFlags = join(flag.flags, ", ");
  557 + std::cout << std::string(spaces.data(), indent)
  558 + << fill(allFlags, maxFlagsLen);
  559 +
  560 + if (maxFlagsValueLen > 0)
  561 + std::cout << " " << fill(flag.value, maxFlagsValueLen);
  562 +
  563 + std::cout << " "
  564 + << flag.helpString
  565 + << std::endl;
  566 + }
  567 +}
  568 +
  569 +size_t AkVCam::CmdParserPrivate::maxCommandLength(bool showAdvancedHelp)
  570 +{
  571 + size_t length = 0;
  572 +
  573 + for (auto &cmd: this->m_commands)
  574 + if (!cmd.advanced || showAdvancedHelp)
  575 + length = std::max(cmd.command.size(), length);
  576 +
  577 + return length;
  578 +}
  579 +
  580 +size_t AkVCam::CmdParserPrivate::maxArgumentsLength(bool showAdvancedHelp)
  581 +{
  582 + size_t length = 0;
  583 +
  584 + for (auto &cmd: this->m_commands)
  585 + if (!cmd.advanced || showAdvancedHelp)
  586 + length = std::max(cmd.arguments.size(), length);
  587 +
  588 + return length;
  589 +}
  590 +
  591 +size_t AkVCam::CmdParserPrivate::maxFlagsLength(const std::vector<CmdParserFlags> &flags)
  592 +{
  593 + size_t length = 0;
  594 +
  595 + for (auto &flag: flags)
  596 + length = std::max(join(flag.flags, ", ").size(), length);
  597 +
  598 + return length;
  599 +}
  600 +
  601 +size_t AkVCam::CmdParserPrivate::maxFlagsValueLength(const std::vector<CmdParserFlags> &flags)
  602 +{
  603 + size_t length = 0;
  604 +
  605 + for (auto &flag: flags)
  606 + length = std::max(flag.value.size(), length);
  607 +
  608 + return length;
  609 +}
  610 +
  611 +size_t AkVCam::CmdParserPrivate::maxColumnLength(const AkVCam::StringVector &table,
  612 + size_t width,
  613 + size_t column)
  614 +{
  615 + size_t length = 0;
  616 + size_t height = table.size() / width;
  617 +
  618 + for (size_t y = 0; y < height; y++) {
  619 + auto &str = table[y * width + column];
  620 + length = std::max(str.size(), length);
  621 + }
  622 +
  623 + return length;
  624 +}
  625 +
  626 +std::vector<size_t> AkVCam::CmdParserPrivate::maxColumnsLength(const AkVCam::StringVector &table,
  627 + size_t width)
  628 +{
  629 + std::vector<size_t> lengths;
  630 +
  631 + for (size_t x = 0; x < width; x++)
  632 + lengths.push_back(this->maxColumnLength(table, width, x));
  633 +
  634 + return lengths;
  635 +}
  636 +
  637 +void AkVCam::CmdParserPrivate::drawTableHLine(const std::vector<size_t> &columnsLength,
  638 + bool toStdErr)
  639 +{
  640 + std::ostream *out = &std::cout;
  641 +
  642 + if (toStdErr)
  643 + out = &std::cerr;
  644 +
  645 + *out << '+';
  646 +
  647 + for (auto &len: columnsLength)
  648 + *out << std::string("-") * (len + 2) << '+';
  649 +
  650 + *out << std::endl;
  651 +}
  652 +
  653 +void AkVCam::CmdParserPrivate::drawTable(const AkVCam::StringVector &table,
  654 + size_t width,
  655 + bool toStdErr)
  656 +{
  657 + size_t height = table.size() / width;
  658 + auto columnsLength = this->maxColumnsLength(table, width);
  659 + this->drawTableHLine(columnsLength, toStdErr);
  660 + std::ostream *out = &std::cout;
  661 +
  662 + if (toStdErr)
  663 + out = &std::cerr;
  664 +
  665 + for (size_t y = 0; y < height; y++) {
  666 + *out << "|";
  667 +
  668 + for (size_t x = 0; x < width; x++) {
  669 + auto &element = table[x + y * width];
  670 + *out << " " << fill(element, columnsLength[x]) << " |";
  671 + }
  672 +
  673 + *out << std::endl;
  674 +
  675 + if (y == 0 && height > 1)
  676 + this->drawTableHLine(columnsLength, toStdErr);
  677 + }
  678 +
  679 + this->drawTableHLine(columnsLength, toStdErr);
  680 +}
  681 +
  682 +AkVCam::CmdParserCommand *AkVCam::CmdParserPrivate::parserCommand(const std::string &command)
  683 +{
  684 + for (auto &cmd: this->m_commands)
  685 + if (cmd.command == command)
  686 + return &cmd;
  687 +
  688 + return nullptr;
  689 +}
  690 +
  691 +const AkVCam::CmdParserFlags *AkVCam::CmdParserPrivate::parserFlag(const std::vector<CmdParserFlags> &cmdFlags,
  692 + const std::string &flag)
  693 +{
  694 + for (auto &flags: cmdFlags)
  695 + for (auto &f: flags.flags)
  696 + if (f == flag)
  697 + return &flags;
  698 +
  699 + return nullptr;
  700 +}
  701 +
  702 +bool AkVCam::CmdParserPrivate::containsFlag(const StringMap &flags,
  703 + const std::string &command,
  704 + const std::string &flagAlias)
  705 +{
  706 + for (auto &cmd: this->m_commands)
  707 + if (cmd.command == command) {
  708 + for (auto &flag: cmd.flags) {
  709 + auto it = std::find(flag.flags.begin(),
  710 + flag.flags.end(),
  711 + flagAlias);
  712 +
  713 + if (it != flag.flags.end()) {
  714 + for (auto &f1: flags)
  715 + for (auto &f2: flag.flags)
  716 + if (f1.first == f2)
  717 + return true;
  718 +
  719 + return false;
  720 + }
  721 + }
  722 +
  723 + return false;
  724 + }
  725 +
  726 + return false;
  727 +}
  728 +
  729 +std::string AkVCam::CmdParserPrivate::flagValue(const AkVCam::StringMap &flags,
  730 + const std::string &command,
  731 + const std::string &flagAlias)
  732 +{
  733 + for (auto &cmd: this->m_commands)
  734 + if (cmd.command == command) {
  735 + for (auto &flag: cmd.flags) {
  736 + auto it = std::find(flag.flags.begin(),
  737 + flag.flags.end(),
  738 + flagAlias);
  739 +
  740 + if (it != flag.flags.end()) {
  741 + for (auto &f1: flags)
  742 + for (auto &f2: flag.flags)
  743 + if (f1.first == f2)
  744 + return f1.second;
  745 +
  746 + return {};
  747 + }
  748 + }
  749 +
  750 + return {};
  751 + }
  752 +
  753 + return {};
  754 +}
  755 +
  756 +int AkVCam::CmdParserPrivate::defaultHandler(const StringMap &flags,
  757 + const StringVector &args)
  758 +{
  759 + if (flags.empty()
  760 + || this->containsFlag(flags, "", "-h")
  761 + || this->containsFlag(flags, "", "--help-all")) {
  762 + return this->showHelp(flags, args);
  763 + }
  764 +
  765 + if (this->containsFlag(flags, "", "-v")) {
  766 + std::cout << COMMONS_VERSION << std::endl;
  767 +
  768 + return 0;
  769 + }
  770 +
  771 + if (this->containsFlag(flags, "", "--build-info")) {
  772 +#ifdef GIT_COMMIT_HASH
  773 + std::string commitHash = GIT_COMMIT_HASH;
  774 + std::string commitUrl = COMMONS_PROJECT_COMMIT_URL "/" GIT_COMMIT_HASH;
  775 +
  776 + if (commitHash.empty())
  777 + commitHash = "Unknown";
  778 +
  779 + if (commitUrl.empty())
  780 + commitUrl = "Unknown";
  781 +#else
  782 + std::string commitHash;
  783 + std::string commitUrl;
  784 +#endif
  785 +
  786 + std::cout << "Commit hash: " << commitHash << std::endl;
  787 + std::cout << "Commit URL: " << commitUrl << std::endl;
  788 +
  789 + return 0;
  790 + }
  791 +
  792 + if (this->containsFlag(flags, "", "-p"))
  793 + this->m_parseable = true;
  794 +
  795 + if (this->containsFlag(flags, "", "-f"))
  796 + this->m_force = true;
  797 +
  798 + return 0;
  799 +}
  800 +
  801 +int AkVCam::CmdParserPrivate::showHelp(const StringMap &flags,
  802 + const StringVector &args)
  803 +{
  804 + UNUSED(flags);
  805 +
  806 + std::cout << args[0]
  807 + << " [OPTIONS...] COMMAND [COMMAND_OPTIONS...] ..."
  808 + << std::endl;
  809 + std::cout << std::endl;
  810 + std::cout << "AkVirtualCamera virtual device manager." << std::endl;
  811 + std::cout << std::endl;
  812 + std::cout << "General Options:" << std::endl;
  813 + std::cout << std::endl;
  814 + this->printFlags(this->m_commands[0].flags, 4);
  815 + std::cout << std::endl;
  816 + std::cout << "Commands:" << std::endl;
  817 + std::cout << std::endl;
  818 +
  819 + bool showAdvancedHelp = this->containsFlag(flags, "", "--help-all");
  820 + auto maxCmdLen = this->maxCommandLength(showAdvancedHelp);
  821 + auto maxArgsLen = this->maxArgumentsLength(showAdvancedHelp);
  822 +
  823 + for (auto &cmd: this->m_commands) {
  824 + if (cmd.command.empty()
  825 + || (cmd.advanced && !showAdvancedHelp))
  826 + continue;
  827 +
  828 + std::cout << " "
  829 + << fill(cmd.command, maxCmdLen)
  830 + << " "
  831 + << fill(cmd.arguments, maxArgsLen)
  832 + << " "
  833 + << cmd.helpString << std::endl;
  834 +
  835 + if (!cmd.flags.empty())
  836 + std::cout << std::endl;
  837 +
  838 + this->printFlags(cmd.flags, 8);
  839 +
  840 + if (!cmd.flags.empty())
  841 + std::cout << std::endl;
  842 + }
  843 +
  844 + return 0;
  845 +}
  846 +
  847 +int AkVCam::CmdParserPrivate::showDevices(const StringMap &flags,
  848 + const StringVector &args)
  849 +{
  850 + UNUSED(flags);
  851 + UNUSED(args);
  852 +
  853 + auto devices = this->m_ipcBridge.devices();
  854 +
  855 + if (devices.empty())
  856 + return 0;
  857 +
  858 + std::sort(devices.begin(), devices.end());
  859 +
  860 + if (this->m_parseable) {
  861 + for (auto &device: devices)
  862 + std::cout << device << std::endl;
  863 + } else {
  864 + std::vector<std::string> table {
  865 + "Device",
  866 + "Description"
  867 + };
  868 + auto columns = table.size();
  869 +
  870 + for (auto &device: devices) {
  871 + table.push_back(device);
  872 + table.push_back(this->m_ipcBridge.description(device));
  873 + }
  874 +
  875 + this->drawTable(table, columns);
  876 + }
  877 +
  878 + return 0;
  879 +}
  880 +
  881 +int AkVCam::CmdParserPrivate::addDevice(const StringMap &flags,
  882 + const StringVector &args)
  883 +{
  884 + UNUSED(flags);
  885 +
  886 + if (args.size() < 2) {
  887 + std::cerr << "Device description not provided." << std::endl;
  888 +
  889 + return -EINVAL;
  890 + }
  891 +
  892 + auto deviceId = this->flagValue(flags, "add-device", "-i");
  893 + deviceId = this->m_ipcBridge.addDevice(args[1], deviceId);
  894 +
  895 + if (deviceId.empty()) {
  896 + std::cerr << "Failed to create device." << std::endl;
  897 +
  898 + return -EIO;
  899 + }
  900 +
  901 + if (this->m_parseable)
  902 + std::cout << deviceId << std::endl;
  903 + else
  904 + std::cout << "Device created as " << deviceId << std::endl;
  905 +
  906 + return 0;
  907 +}
  908 +
  909 +int AkVCam::CmdParserPrivate::removeDevice(const StringMap &flags,
  910 + const StringVector &args)
  911 +{
  912 + UNUSED(flags);
  913 +
  914 + if (args.size() < 2) {
  915 + std::cerr << "Device not provided." << std::endl;
  916 +
  917 + return -EINVAL;
  918 + }
  919 +
  920 + auto deviceId = args[1];
  921 + auto devices = this->m_ipcBridge.devices();
  922 + auto it = std::find(devices.begin(), devices.end(), deviceId);
  923 +
  924 + if (it == devices.end()) {
  925 + std::cerr << "'" << deviceId << "' doesn't exists." << std::endl;
  926 +
  927 + return -ENODEV;
  928 + }
  929 +
  930 + this->m_ipcBridge.removeDevice(args[1]);
  931 +
  932 + return 0;
  933 +}
  934 +
  935 +int AkVCam::CmdParserPrivate::removeDevices(const AkVCam::StringMap &flags,
  936 + const AkVCam::StringVector &args)
  937 +{
  938 + UNUSED(flags);
  939 + UNUSED(args);
  940 + auto devices = this->m_ipcBridge.devices();
  941 +
  942 + for (auto &device: devices)
  943 + this->m_ipcBridge.removeDevice(device);
  944 +
  945 + return 0;
  946 +}
  947 +
  948 +int AkVCam::CmdParserPrivate::showDeviceDescription(const StringMap &flags,
  949 + const StringVector &args)
  950 +{
  951 + UNUSED(flags);
  952 +
  953 + if (args.size() < 2) {
  954 + std::cerr << "Device not provided." << std::endl;
  955 +
  956 + return -EINVAL;
  957 + }
  958 +
  959 + auto deviceId = args[1];
  960 + auto devices = this->m_ipcBridge.devices();
  961 + auto it = std::find(devices.begin(), devices.end(), deviceId);
  962 +
  963 + if (it == devices.end()) {
  964 + std::cerr << "'" << deviceId << "' doesn't exists." << std::endl;
  965 +
  966 + return -ENODEV;
  967 + }
  968 +
  969 + std::cout << this->m_ipcBridge.description(args[1]) << std::endl;
  970 +
  971 + return 0;
  972 +}
  973 +
  974 +int AkVCam::CmdParserPrivate::setDeviceDescription(const AkVCam::StringMap &flags,
  975 + const AkVCam::StringVector &args)
  976 +{
  977 + UNUSED(flags);
  978 +
  979 + if (args.size() < 3) {
  980 + std::cerr << "Not enough arguments." << std::endl;
  981 +
  982 + return -EINVAL;
  983 + }
  984 +
  985 + auto deviceId = args[1];
  986 + auto devices = this->m_ipcBridge.devices();
  987 + auto dit = std::find(devices.begin(), devices.end(), deviceId);
  988 +
  989 + if (dit == devices.end()) {
  990 + std::cerr << "'" << deviceId << "' doesn't exists." << std::endl;
  991 +
  992 + return -ENODEV;
  993 + }
  994 +
  995 + this->m_ipcBridge.setDescription(deviceId, args[2]);
  996 +
  997 + return 0;
  998 +}
  999 +
  1000 +int AkVCam::CmdParserPrivate::showSupportedFormats(const StringMap &flags,
  1001 + const StringVector &args)
  1002 +{
  1003 + UNUSED(args);
  1004 +
  1005 + auto type =
  1006 + this->containsFlag(flags, "supported-formats", "-i")?
  1007 + IpcBridge::StreamType_Input:
  1008 + IpcBridge::StreamType_Output;
  1009 + auto formats = this->m_ipcBridge.supportedPixelFormats(type);
  1010 +
  1011 + if (!this->m_parseable) {
  1012 + if (type == IpcBridge::StreamType_Input)
  1013 + std::cout << "Input formats:" << std::endl;
  1014 + else
  1015 + std::cout << "Output formats:" << std::endl;
  1016 +
  1017 + std::cout << std::endl;
  1018 + }
  1019 +
  1020 + for (auto &format: formats)
  1021 + std::cout << VideoFormat::stringFromFourcc(format) << std::endl;
  1022 +
  1023 + return 0;
  1024 +}
  1025 +
  1026 +int AkVCam::CmdParserPrivate::showDefaultFormat(const AkVCam::StringMap &flags,
  1027 + const AkVCam::StringVector &args)
  1028 +{
  1029 + UNUSED(args);
  1030 +
  1031 + auto type =
  1032 + this->containsFlag(flags, "default-format", "-i")?
  1033 + IpcBridge::StreamType_Input:
  1034 + IpcBridge::StreamType_Output;
  1035 + auto format = this->m_ipcBridge.defaultPixelFormat(type);
  1036 + std::cout << VideoFormat::stringFromFourcc(format) << std::endl;
  1037 +
  1038 + return 0;
  1039 +}
  1040 +
  1041 +int AkVCam::CmdParserPrivate::showFormats(const StringMap &flags,
  1042 + const StringVector &args)
  1043 +{
  1044 + UNUSED(flags);
  1045 +
  1046 + if (args.size() < 2) {
  1047 + std::cerr << "Device not provided." << std::endl;
  1048 +
  1049 + return -EINVAL;
  1050 + }
  1051 +
  1052 + auto deviceId = args[1];
  1053 + auto devices = this->m_ipcBridge.devices();
  1054 + auto it = std::find(devices.begin(), devices.end(), deviceId);
  1055 +
  1056 + if (it == devices.end()) {
  1057 + std::cerr << "'" << deviceId << "' doesn't exists." << std::endl;
  1058 +
  1059 + return -ENODEV;
  1060 + }
  1061 +
  1062 + if (this->m_parseable) {
  1063 + for (auto &format: this->m_ipcBridge.formats(args[1]))
  1064 + std::cout << VideoFormat::stringFromFourcc(format.fourcc())
  1065 + << ' '
  1066 + << format.width()
  1067 + << ' '
  1068 + << format.height()
  1069 + << ' '
  1070 + << format.minimumFrameRate().num()
  1071 + << ' '
  1072 + << format.minimumFrameRate().den()
  1073 + << std::endl;
  1074 + } else {
  1075 + int i = 0;
  1076 +
  1077 + for (auto &format: this->m_ipcBridge.formats(args[1])) {
  1078 + std::cout << i
  1079 + << ": "
  1080 + << VideoFormat::stringFromFourcc(format.fourcc())
  1081 + << ' '
  1082 + << format.width()
  1083 + << 'x'
  1084 + << format.height()
  1085 + << ' '
  1086 + << format.minimumFrameRate().num()
  1087 + << '/'
  1088 + << format.minimumFrameRate().den()
  1089 + << " FPS"
  1090 + << std::endl;
  1091 + i++;
  1092 + }
  1093 + }
  1094 +
  1095 + return 0;
  1096 +}
  1097 +
  1098 +int AkVCam::CmdParserPrivate::addFormat(const StringMap &flags,
  1099 + const StringVector &args)
  1100 +{
  1101 + if (args.size() < 6) {
  1102 + std::cerr << "Not enough arguments." << std::endl;
  1103 +
  1104 + return -EINVAL;
  1105 + }
  1106 +
  1107 + auto deviceId = args[1];
  1108 + auto devices = this->m_ipcBridge.devices();
  1109 + auto dit = std::find(devices.begin(), devices.end(), deviceId);
  1110 +
  1111 + if (dit == devices.end()) {
  1112 + std::cerr << "'" << deviceId << "' doesn't exists." << std::endl;
  1113 +
  1114 + return -ENODEV;
  1115 + }
  1116 +
  1117 + auto format = VideoFormat::fourccFromString(args[2]);
  1118 +
  1119 + if (!format) {
  1120 + std::cerr << "Invalid pixel format." << std::endl;
  1121 +
  1122 + return -EINVAL;
  1123 + }
  1124 +
  1125 + auto formats =
  1126 + this->m_ipcBridge.supportedPixelFormats(IpcBridge::StreamType_Output);
  1127 + auto fit = std::find(formats.begin(), formats.end(), format);
  1128 +
  1129 + if (fit == formats.end()) {
  1130 + std::cerr << "Format not supported." << std::endl;
  1131 +
  1132 + return -EINVAL;
  1133 + }
  1134 +
  1135 + char *p = nullptr;
  1136 + auto width = strtoul(args[3].c_str(), &p, 10);
  1137 +
  1138 + if (*p) {
  1139 + std::cerr << "Width must be an unsigned integer." << std::endl;
  1140 +
  1141 + return -EINVAL;
  1142 + }
  1143 +
  1144 + p = nullptr;
  1145 + auto height = strtoul(args[4].c_str(), &p, 10);
  1146 +
  1147 + if (*p) {
  1148 + std::cerr << "Height must be an unsigned integer." << std::endl;
  1149 +
  1150 + return -EINVAL;
  1151 + }
  1152 +
  1153 + Fraction fps(args[5]);
  1154 +
  1155 + if (fps.num() < 1 || fps.den() < 1) {
  1156 + std::cerr << "Invalid frame rate." << std::endl;
  1157 +
  1158 + return -EINVAL;
  1159 + }
  1160 +
  1161 + auto indexStr = this->flagValue(flags, "add-format", "-i");
  1162 + int index = -1;
  1163 +
  1164 + if (!indexStr.empty()) {
  1165 + p = nullptr;
  1166 + index = int(strtoul(indexStr.c_str(), &p, 10));
  1167 +
  1168 + if (*p) {
  1169 + std::cerr << "Index must be an unsigned integer." << std::endl;
  1170 +
  1171 + return -EINVAL;
  1172 + }
  1173 + }
  1174 +
  1175 + VideoFormat fmt(format, int(width), int(height), {fps});
  1176 + this->m_ipcBridge.addFormat(deviceId, fmt, index);
  1177 +
  1178 + return 0;
  1179 +}
  1180 +
  1181 +int AkVCam::CmdParserPrivate::removeFormat(const StringMap &flags,
  1182 + const StringVector &args)
  1183 +{
  1184 + UNUSED(flags);
  1185 +
  1186 + if (args.size() < 3) {
  1187 + std::cerr << "Not enough arguments." << std::endl;
  1188 +
  1189 + return -EINVAL;
  1190 + }
  1191 +
  1192 + auto deviceId = args[1];
  1193 + auto devices = this->m_ipcBridge.devices();
  1194 + auto dit = std::find(devices.begin(), devices.end(), deviceId);
  1195 +
  1196 + if (dit == devices.end()) {
  1197 + std::cerr << "'" << deviceId << "' doesn't exists." << std::endl;
  1198 +
  1199 + return -ENODEV;
  1200 + }
  1201 +
  1202 + char *p = nullptr;
  1203 + auto index = strtoul(args[2].c_str(), &p, 10);
  1204 +
  1205 + if (*p) {
  1206 + std::cerr << "Index must be an unsigned integer." << std::endl;
  1207 +
  1208 + return -EINVAL;
  1209 + }
  1210 +
  1211 + auto formats = this->m_ipcBridge.formats(deviceId);
  1212 +
  1213 + if (index >= formats.size()) {
  1214 + std::cerr << "Index is out of range." << std::endl;
  1215 +
  1216 + return -ERANGE;
  1217 + }
  1218 +
  1219 + this->m_ipcBridge.removeFormat(deviceId, int(index));
  1220 +
  1221 + return 0;
  1222 +}
  1223 +
  1224 +int AkVCam::CmdParserPrivate::removeFormats(const AkVCam::StringMap &flags,
  1225 + const AkVCam::StringVector &args)
  1226 +{
  1227 + UNUSED(flags);
  1228 +
  1229 + if (args.size() < 2) {
  1230 + std::cerr << "Not enough arguments." << std::endl;
  1231 +
  1232 + return -EINVAL;
  1233 + }
  1234 +
  1235 + auto deviceId = args[1];
  1236 + auto devices = this->m_ipcBridge.devices();
  1237 + auto dit = std::find(devices.begin(), devices.end(), deviceId);
  1238 +
  1239 + if (dit == devices.end()) {
  1240 + std::cerr << "'" << deviceId << "' doesn't exists." << std::endl;
  1241 +
  1242 + return -ENODEV;
  1243 + }
  1244 +
  1245 + this->m_ipcBridge.setFormats(deviceId, {});
  1246 +
  1247 + return 0;
  1248 +}
  1249 +
  1250 +int AkVCam::CmdParserPrivate::update(const StringMap &flags,
  1251 + const StringVector &args)
  1252 +{
  1253 + UNUSED(flags);
  1254 + UNUSED(args);
  1255 + this->m_ipcBridge.updateDevices();
  1256 +
  1257 + return 0;
  1258 +}
  1259 +
  1260 +int AkVCam::CmdParserPrivate::loadSettings(const AkVCam::StringMap &flags,
  1261 + const AkVCam::StringVector &args)
  1262 +{
  1263 + UNUSED(flags);
  1264 +
  1265 + if (args.size() < 2) {
  1266 + std::cerr << "Settings file not provided." << std::endl;
  1267 +
  1268 + return -EINVAL;
  1269 + }
  1270 +
  1271 + Settings settings;
  1272 +
  1273 + if (!settings.load(args[1])) {
  1274 + std::cerr << "Settings file not valid." << std::endl;
  1275 +
  1276 + return -EIO;
  1277 + }
  1278 +
  1279 + this->loadGenerals(settings);
  1280 + auto devices = this->m_ipcBridge.devices();
  1281 +
  1282 + for (auto &device: devices)
  1283 + this->m_ipcBridge.removeDevice(device);
  1284 +
  1285 + this->createDevices(settings, this->readFormats(settings));
  1286 +
  1287 + return 0;
  1288 +}
  1289 +
  1290 +int AkVCam::CmdParserPrivate::stream(const AkVCam::StringMap &flags,
  1291 + const AkVCam::StringVector &args)
  1292 +{
  1293 + UNUSED(flags);
  1294 +
  1295 + if (args.size() < 5) {
  1296 + std::cerr << "Not enough arguments." << std::endl;
  1297 +
  1298 + return -EINVAL;
  1299 + }
  1300 +
  1301 + auto deviceId = args[1];
  1302 + auto devices = this->m_ipcBridge.devices();
  1303 + auto dit = std::find(devices.begin(), devices.end(), deviceId);
  1304 +
  1305 + if (dit == devices.end()) {
  1306 + std::cerr << "'" << deviceId << "' doesn't exists." << std::endl;
  1307 +
  1308 + return -ENODEV;
  1309 + }
  1310 +
  1311 + auto format = VideoFormat::fourccFromString(args[2]);
  1312 +
  1313 + if (!format) {
  1314 + std::cerr << "Invalid pixel format." << std::endl;
  1315 +
  1316 + return -EINVAL;
  1317 + }
  1318 +
  1319 + auto formats =
  1320 + this->m_ipcBridge.supportedPixelFormats(IpcBridge::StreamType_Output);
  1321 + auto fit = std::find(formats.begin(), formats.end(), format);
  1322 +
  1323 + if (fit == formats.end()) {
  1324 + std::cerr << "Format not supported." << std::endl;
  1325 +
  1326 + return -EINVAL;
  1327 + }
  1328 +
  1329 + char *p = nullptr;
  1330 + auto width = strtoul(args[3].c_str(), &p, 10);
  1331 +
  1332 + if (*p) {
  1333 + std::cerr << "Width must be an unsigned integer." << std::endl;
  1334 +
  1335 + return -EINVAL;
  1336 + }
  1337 +
  1338 + p = nullptr;
  1339 + auto height = strtoul(args[4].c_str(), &p, 10);
  1340 +
  1341 + if (*p) {
  1342 + std::cerr << "Height must be an unsigned integer." << std::endl;
  1343 +
  1344 + return -EINVAL;
  1345 + }
  1346 +
  1347 + auto fpsStr = this->flagValue(flags, "stream", "-f");
  1348 + double fps = std::numeric_limits<double>::quiet_NaN();
  1349 +
  1350 + if (!fpsStr.empty()) {
  1351 + p = nullptr;
  1352 + fps = int(strtod(fpsStr.c_str(), &p));
  1353 +
  1354 + if (*p) {
  1355 + if (!Fraction::isFraction(fpsStr)) {
  1356 + std::cerr << "The framerate must be a number or a fraction." << std::endl;
  1357 +
  1358 + return -EINVAL;
  1359 + }
  1360 +
  1361 + fps = Fraction(fpsStr).value();
  1362 + }
  1363 +
  1364 + if (fps <= 0 || std::isinf(fps)) {
  1365 + std::cerr << "The framerate is out of range." << std::endl;
  1366 +
  1367 + return -ERANGE;
  1368 + }
  1369 + }
  1370 +
  1371 + VideoFormat fmt(format, int(width), int(height), {{30, 1}});
  1372 +
  1373 + if (!this->m_ipcBridge.deviceStart(IpcBridge::StreamType_Output, deviceId)) {
  1374 + std::cerr << "Can't start stream." << std::endl;
  1375 +
  1376 + return -EIO;
  1377 + }
  1378 +
  1379 + static bool exit = false;
  1380 + auto signalHandler = [] (int) {
  1381 + exit = true;
  1382 + };
  1383 + signal(SIGINT, signalHandler);
  1384 + signal(SIGTERM, signalHandler);
  1385 +
  1386 +#ifndef _WIN32
  1387 + signal(SIGPIPE, [] (int) {
  1388 + });
  1389 +#endif
  1390 +
  1391 + AkVCam::VideoFrame frame(fmt);
  1392 + size_t bufferSize = 0;
  1393 +
  1394 +#ifdef _WIN32
  1395 + // Set std::cin in binary mode.
  1396 + _setmode(_fileno(stdin), _O_BINARY);
  1397 +#endif
  1398 +
  1399 + auto clock = [] (const std::chrono::time_point<std::chrono::high_resolution_clock> &since) -> double {
  1400 + return std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - since).count();
  1401 + };
  1402 +
  1403 + const double minThreshold = 0.04;
  1404 + const double maxThreshold = 0.1;
  1405 + const double framedupThreshold = 0.1;
  1406 + const double nosyncThreshold = 10.0;
  1407 +
  1408 + double lastPts = 0.0;
  1409 + auto t0 = std::chrono::high_resolution_clock::now();
  1410 + double drift = 0;
  1411 + uint64_t i = 0;
  1412 +
  1413 + do {
  1414 + std::cin.read(reinterpret_cast<char *>(frame.data().data()
  1415 + + bufferSize),
  1416 + std::streamsize(frame.data().size() - bufferSize));
  1417 + bufferSize += size_t(std::cin.gcount());
  1418 +
  1419 + if (bufferSize == frame.data().size()) {
  1420 + if (fpsStr.empty()) {
  1421 + this->m_ipcBridge.write(deviceId, frame);
  1422 + } else {
  1423 + double pts = double(i) / fps;
  1424 +
  1425 + for (;;) {
  1426 + double clock_pts = clock(t0) + drift;
  1427 + double diff = pts - clock_pts;
  1428 + double delay = pts - lastPts;
  1429 + double syncThreshold =
  1430 + std::max(minThreshold,
  1431 + std::min(delay, maxThreshold));
  1432 +
  1433 + if (!std::isnan(diff)
  1434 + && std::abs(diff) < nosyncThreshold
  1435 + && delay < framedupThreshold) {
  1436 + if (diff <= -syncThreshold) {
  1437 + lastPts = pts;
  1438 +
  1439 + break;
  1440 + }
  1441 +
  1442 + if (diff > syncThreshold) {
  1443 + std::this_thread::sleep_for(std::chrono::duration<double>(diff - syncThreshold));
  1444 +
  1445 + continue;
  1446 + }
  1447 + } else {
  1448 + drift = clock(t0) - pts;
  1449 + }
  1450 +
  1451 + this->m_ipcBridge.write(deviceId, frame);
  1452 + lastPts = pts;
  1453 +
  1454 + break;
  1455 + }
  1456 +
  1457 + i++;
  1458 + }
  1459 +
  1460 + bufferSize = 0;
  1461 + }
  1462 + } while (!std::cin.eof() && !exit);
  1463 +
  1464 + this->m_ipcBridge.deviceStop(deviceId);
  1465 +
  1466 + return 0;
  1467 +}
  1468 +
  1469 +int AkVCam::CmdParserPrivate::listenEvents(const AkVCam::StringMap &flags,
  1470 + const AkVCam::StringVector &args)
  1471 +{
  1472 + UNUSED(flags);
  1473 + UNUSED(args);
  1474 +
  1475 + auto devicesChanged = [] (void *, const std::vector<std::string> &) {
  1476 + std::cout << "DevicesUpdated" << std::endl;
  1477 + };
  1478 + auto pictureChanged = [] (void *, const std::string &) {
  1479 + std::cout << "PictureUpdated" << std::endl;
  1480 + };
  1481 +
  1482 + this->m_ipcBridge.connectDevicesChanged(this, devicesChanged);
  1483 + this->m_ipcBridge.connectPictureChanged(this, pictureChanged);
  1484 +
  1485 + static bool exit = false;
  1486 + auto signalHandler = [] (int) {
  1487 + exit = true;
  1488 + };
  1489 + signal(SIGINT, signalHandler);
  1490 + signal(SIGTERM, signalHandler);
  1491 +
  1492 + while (!exit)
  1493 + std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  1494 +
  1495 + return 0;
  1496 +}
  1497 +
  1498 +int AkVCam::CmdParserPrivate::showControls(const StringMap &flags,
  1499 + const StringVector &args)
  1500 +{
  1501 + UNUSED(flags);
  1502 +
  1503 + if (args.size() < 2) {
  1504 + std::cerr << "Device not provided." << std::endl;
  1505 +
  1506 + return -EINVAL;
  1507 + }
  1508 +
  1509 + auto deviceId = args[1];
  1510 + auto devices = this->m_ipcBridge.devices();
  1511 + auto dit = std::find(devices.begin(), devices.end(), deviceId);
  1512 +
  1513 + if (dit == devices.end()) {
  1514 + std::cerr << "'" << deviceId << "' doesn't exists." << std::endl;
  1515 +
  1516 + return -ENODEV;
  1517 + }
  1518 +
  1519 + if (this->m_parseable) {
  1520 + for (auto &control: this->m_ipcBridge.controls(deviceId))
  1521 + std::cout << control.id << std::endl;
  1522 + } else {
  1523 + auto typeStr = typeStrMap();
  1524 +
  1525 + std::vector<std::string> table {
  1526 + "Control",
  1527 + "Description",
  1528 + "Type",
  1529 + "Minimum",
  1530 + "Maximum",
  1531 + "Step",
  1532 + "Default",
  1533 + "Value"
  1534 + };
  1535 + auto columns = table.size();
  1536 +
  1537 + for (auto &control: this->m_ipcBridge.controls(deviceId)) {
  1538 + table.push_back(control.id);
  1539 + table.push_back(control.description);
  1540 + table.push_back(typeStr[control.type]);
  1541 + table.push_back(std::to_string(control.minimum));
  1542 + table.push_back(std::to_string(control.maximum));
  1543 + table.push_back(std::to_string(control.step));
  1544 + table.push_back(std::to_string(control.defaultValue));
  1545 + table.push_back(std::to_string(control.value));
  1546 + }
  1547 +
  1548 + this->drawTable(table, columns);
  1549 + }
  1550 +
  1551 + return 0;
  1552 +}
  1553 +
  1554 +int AkVCam::CmdParserPrivate::readControl(const StringMap &flags,
  1555 + const StringVector &args)
  1556 +{
  1557 + if (args.size() < 3) {
  1558 + std::cerr << "Not enough arguments." << std::endl;
  1559 +
  1560 + return -EINVAL;
  1561 + }
  1562 +
  1563 + auto deviceId = args[1];
  1564 + auto devices = this->m_ipcBridge.devices();
  1565 + auto dit = std::find(devices.begin(), devices.end(), deviceId);
  1566 +
  1567 + if (dit == devices.end()) {
  1568 + std::cerr << "'" << deviceId << "' doesn't exists." << std::endl;
  1569 +
  1570 + return -ENODEV;
  1571 + }
  1572 +
  1573 + for (auto &control: this->m_ipcBridge.controls(deviceId))
  1574 + if (control.id == args[2]) {
  1575 + if (flags.empty()) {
  1576 + std::cout << control.value << std::endl;
  1577 + } else {
  1578 + if (this->containsFlag(flags, "get-control", "-c")) {
  1579 + std::cout << control.description << std::endl;
  1580 + }
  1581 +
  1582 + if (this->containsFlag(flags, "get-control", "-t")) {
  1583 + auto typeStr = typeStrMap();
  1584 + std::cout << typeStr[control.type] << std::endl;
  1585 + }
  1586 +
  1587 + if (this->containsFlag(flags, "get-control", "-m")) {
  1588 + std::cout << control.minimum << std::endl;
  1589 + }
  1590 +
  1591 + if (this->containsFlag(flags, "get-control", "-M")) {
  1592 + std::cout << control.maximum << std::endl;
  1593 + }
  1594 +
  1595 + if (this->containsFlag(flags, "get-control", "-s")) {
  1596 + std::cout << control.step << std::endl;
  1597 + }
  1598 +
  1599 + if (this->containsFlag(flags, "get-control", "-d")) {
  1600 + std::cout << control.defaultValue << std::endl;
  1601 + }
  1602 +
  1603 + if (this->containsFlag(flags, "get-control", "-l")) {
  1604 + for (size_t i = 0; i < control.menu.size(); i++)
  1605 + if (this->m_parseable)
  1606 + std::cout << control.menu[i] << std::endl;
  1607 + else
  1608 + std::cout << i
  1609 + << ": "
  1610 + << control.menu[i]
  1611 + << std::endl;
  1612 + }
  1613 + }
  1614 +
  1615 + return 0;
  1616 + }
  1617 +
  1618 + std::cerr << "'" << args[2] << "' control not available." << std::endl;
  1619 +
  1620 + return -ENOSYS;
  1621 +}
  1622 +
  1623 +int AkVCam::CmdParserPrivate::writeControls(const StringMap &flags,
  1624 + const StringVector &args)
  1625 +{
  1626 + UNUSED(flags);
  1627 +
  1628 + if (args.size() < 3) {
  1629 + std::cerr << "Not enough arguments." << std::endl;
  1630 +
  1631 + return -EINVAL;
  1632 + }
  1633 +
  1634 + auto deviceId = args[1];
  1635 + auto devices = this->m_ipcBridge.devices();
  1636 + auto dit = std::find(devices.begin(), devices.end(), deviceId);
  1637 +
  1638 + if (dit == devices.end()) {
  1639 + std::cerr << "'" << deviceId << "' doesn't exists." << std::endl;
  1640 +
  1641 + return -ENODEV;
  1642 + }
  1643 +
  1644 + std::map<std::string, int> controls;
  1645 +
  1646 + for (size_t i = 2; i < args.size(); i++) {
  1647 + if (args[i].find('=') == std::string::npos) {
  1648 + std::cerr << "Argumment "
  1649 + << i
  1650 + << " is not in the form KEY=VALUE."
  1651 + << std::endl;
  1652 +
  1653 + return -EINVAL;
  1654 + }
  1655 +
  1656 + auto pair = splitOnce(args[i], "=");
  1657 +
  1658 + if (pair.first.empty()) {
  1659 + std::cerr << "Key for argumment "
  1660 + << i
  1661 + << " is emty."
  1662 + << std::endl;
  1663 +
  1664 + return -EINVAL;
  1665 + }
  1666 +
  1667 + auto key = trimmed(pair.first);
  1668 + auto value = trimmed(pair.second);
  1669 + bool found = false;
  1670 +
  1671 + for (auto &control: this->m_ipcBridge.controls(deviceId))
  1672 + if (control.id == key) {
  1673 + switch (control.type) {
  1674 + case ControlTypeInteger: {
  1675 + char *p = nullptr;
  1676 + auto val = strtol(value.c_str(), &p, 10);
  1677 +
  1678 + if (*p) {
  1679 + std::cerr << "Value at argument "
  1680 + << i
  1681 + << " must be an integer."
  1682 + << std::endl;
  1683 +
  1684 + return -EINVAL;
  1685 + }
  1686 +
  1687 + controls[key] = val;
  1688 +
  1689 + break;
  1690 + }
  1691 +
  1692 + case ControlTypeBoolean: {
  1693 + std::locale loc;
  1694 + std::transform(value.begin(),
  1695 + value.end(),
  1696 + value.begin(),
  1697 + [&loc](char c) {
  1698 + return std::tolower(c, loc);
  1699 + });
  1700 +
  1701 + if (value == "0" || value == "false") {
  1702 + controls[key] = 0;
  1703 + } else if (value == "1" || value == "true") {
  1704 + controls[key] = 1;
  1705 + } else {
  1706 + std::cerr << "Value at argument "
  1707 + << i
  1708 + << " must be a boolean."
  1709 + << std::endl;
  1710 +
  1711 + return -EINVAL;
  1712 + }
  1713 +
  1714 + break;
  1715 + }
  1716 +
  1717 + case ControlTypeMenu: {
  1718 + char *p = nullptr;
  1719 + auto val = strtoul(value.c_str(), &p, 10);
  1720 +
  1721 + if (*p) {
  1722 + auto it = std::find(control.menu.begin(),
  1723 + control.menu.end(),
  1724 + value);
  1725 +
  1726 + if (it == control.menu.end()) {
  1727 + std::cerr << "Value at argument "
  1728 + << i
  1729 + << " is not valid."
  1730 + << std::endl;
  1731 +
  1732 + return -EINVAL;
  1733 + }
  1734 +
  1735 + controls[key] = int(it - control.menu.begin());
  1736 + } else {
  1737 + if (val >= control.menu.size()) {
  1738 + std::cerr << "Value at argument "
  1739 + << i
  1740 + << " is out of range."
  1741 + << std::endl;
  1742 +
  1743 + return -ERANGE;
  1744 + }
  1745 +
  1746 + controls[key] = int(val);
  1747 + }
  1748 +
  1749 + break;
  1750 + }
  1751 +
  1752 + default:
  1753 + break;
  1754 + }
  1755 +
  1756 + found = true;
  1757 +
  1758 + break;
  1759 + }
  1760 +
  1761 + if (!found) {
  1762 + std::cerr << "No such '"
  1763 + << key
  1764 + << "' control in argument "
  1765 + << i
  1766 + << "."
  1767 + << std::endl;
  1768 +
  1769 + return -ENOSYS;
  1770 + }
  1771 + }
  1772 +
  1773 + this->m_ipcBridge.setControls(deviceId, controls);
  1774 +
  1775 + return 0;
  1776 +}
  1777 +
  1778 +int AkVCam::CmdParserPrivate::picture(const AkVCam::StringMap &flags,
  1779 + const AkVCam::StringVector &args)
  1780 +{
  1781 + UNUSED(flags);
  1782 + UNUSED(args);
  1783 +
  1784 + std::cout << this->m_ipcBridge.picture() << std::endl;
  1785 +
  1786 + return 0;
  1787 +}
  1788 +
  1789 +int AkVCam::CmdParserPrivate::setPicture(const AkVCam::StringMap &flags,
  1790 + const AkVCam::StringVector &args)
  1791 +{
  1792 + UNUSED(flags);
  1793 +
  1794 + if (args.size() < 2) {
  1795 + std::cerr << "Not enough arguments." << std::endl;
  1796 +
  1797 + return -EINVAL;
  1798 + }
  1799 +
  1800 + this->m_ipcBridge.setPicture(args[1]);
  1801 +
  1802 + return 0;
  1803 +}
  1804 +
  1805 +int AkVCam::CmdParserPrivate::logLevel(const AkVCam::StringMap &flags,
  1806 + const AkVCam::StringVector &args)
  1807 +{
  1808 + UNUSED(flags);
  1809 + UNUSED(args);
  1810 +
  1811 + auto level = this->m_ipcBridge.logLevel();
  1812 +
  1813 + if (this->m_parseable)
  1814 + std::cout << level << std::endl;
  1815 + else
  1816 + std::cout << AkVCam::Logger::levelToString(level) << std::endl;
  1817 +
  1818 + return 0;
  1819 +}
  1820 +
  1821 +int AkVCam::CmdParserPrivate::setLogLevel(const AkVCam::StringMap &flags,
  1822 + const AkVCam::StringVector &args)
  1823 +{
  1824 + UNUSED(flags);
  1825 +
  1826 + if (args.size() < 2) {
  1827 + std::cerr << "Not enough arguments." << std::endl;
  1828 +
  1829 + return -EINVAL;
  1830 + }
  1831 +
  1832 + auto levelStr = args[1];
  1833 + char *p = nullptr;
  1834 + auto level = strtol(levelStr.c_str(), &p, 10);
  1835 +
  1836 + if (*p)
  1837 + level = AkVCam::Logger::levelFromString(levelStr);
  1838 +
  1839 + this->m_ipcBridge.setLogLevel(level);
  1840 +
  1841 + return 0;
  1842 +}
  1843 +
  1844 +int AkVCam::CmdParserPrivate::showClients(const StringMap &flags,
  1845 + const StringVector &args)
  1846 +{
  1847 + UNUSED(flags);
  1848 + UNUSED(args);
  1849 + auto clients = this->m_ipcBridge.clientsPids();
  1850 +
  1851 + if (clients.empty())
  1852 + return 0;
  1853 +
  1854 + if (this->m_parseable) {
  1855 + for (auto &pid: clients)
  1856 + std::cout << pid
  1857 + << " "
  1858 + << this->m_ipcBridge.clientExe(pid)
  1859 + << std::endl;
  1860 + } else {
  1861 + std::vector<std::string> table {
  1862 + "Pid",
  1863 + "Executable"
  1864 + };
  1865 + auto columns = table.size();
  1866 +
  1867 + for (auto &pid: clients) {
  1868 + table.push_back(std::to_string(pid));
  1869 + table.push_back(this->m_ipcBridge.clientExe(pid));
  1870 + }
  1871 +
  1872 + this->drawTable(table, columns);
  1873 + }
  1874 +
  1875 + return 0;
  1876 +}
  1877 +
  1878 +int AkVCam::CmdParserPrivate::dumpInfo(const AkVCam::StringMap &flags,
  1879 + const AkVCam::StringVector &args)
  1880 +{
  1881 + UNUSED(flags);
  1882 + UNUSED(args);
  1883 + static const auto indent = 4 * std::string(" ");
  1884 +
  1885 + std::cout << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" << std::endl;
  1886 + std::cout << "<info>" << std::endl;
  1887 + std::cout << indent << "<devices>" << std::endl;
  1888 +
  1889 + auto devices = this->m_ipcBridge.devices();
  1890 +
  1891 + for (auto &device: devices) {
  1892 + std::cout << 2 * indent << "<device>" << std::endl;
  1893 + std::cout << 3 * indent << "<id>" << device << "</id>" << std::endl;
  1894 + std::cout << 3 * indent
  1895 + << "<description>"
  1896 + << this->m_ipcBridge.description(device)
  1897 + << "</description>"
  1898 + << std::endl;
  1899 + std::cout << 3 * indent << "<formats>" << std::endl;
  1900 +
  1901 + for (auto &format: this->m_ipcBridge.formats(device)) {
  1902 + std::cout << 4 * indent << "<format>" << std::endl;
  1903 + std::cout << 5 * indent
  1904 + << "<pixel-format>"
  1905 + << VideoFormat::stringFromFourcc(format.fourcc())
  1906 + << "</pixel-format>"
  1907 + << std::endl;
  1908 + std::cout << 5 * indent
  1909 + << "<width>"
  1910 + << format.width()
  1911 + << "</width>"
  1912 + << std::endl;
  1913 + std::cout << 5 * indent
  1914 + << "<height>"
  1915 + << format.height()
  1916 + << "</height>"
  1917 + << std::endl;
  1918 + std::cout << 5 * indent
  1919 + << "<fps>"
  1920 + << format.minimumFrameRate().toString()
  1921 + << "</fps>"
  1922 + << std::endl;
  1923 + std::cout << 4 * indent << "</format>" << std::endl;
  1924 + }
  1925 +
  1926 + std::cout << 3 * indent << "</formats>" << std::endl;
  1927 + std::cout << 3 * indent << "<controls>" << std::endl;
  1928 +
  1929 + for (auto &control: this->m_ipcBridge.controls(device)) {
  1930 + std::cout << 4 * indent << "<control>" << std::endl;
  1931 + std::cout << 5 * indent
  1932 + << "<id>"
  1933 + << control.id
  1934 + << "</id>"
  1935 + << std::endl;
  1936 + std::cout << 5 * indent
  1937 + << "<description>"
  1938 + << control.description
  1939 + << "</description>"
  1940 + << std::endl;
  1941 + auto typeStr = typeStrMap();
  1942 + std::cout << 5 * indent
  1943 + << "<type>"
  1944 + << typeStr[control.type]
  1945 + << "</type>"
  1946 + << std::endl;
  1947 + std::cout << 5 * indent
  1948 + << "<minimum>"
  1949 + << control.minimum
  1950 + << "</minimum>"
  1951 + << std::endl;
  1952 + std::cout << 5 * indent
  1953 + << "<maximum>"
  1954 + << control.maximum
  1955 + << "</maximum>"
  1956 + << std::endl;
  1957 + std::cout << 5 * indent
  1958 + << "<step>"
  1959 + << control.step
  1960 + << "</step>"
  1961 + << std::endl;
  1962 + std::cout << 5 * indent
  1963 + << "<default-value>"
  1964 + << control.defaultValue
  1965 + << "</default-value>"
  1966 + << std::endl;
  1967 + std::cout << 5 * indent
  1968 + << "<value>"
  1969 + << control.value
  1970 + << "</value>"
  1971 + << std::endl;
  1972 +
  1973 + if (!control.menu.empty() && control.type == ControlTypeMenu) {
  1974 + std::cout << 5 * indent << "<menu>" << std::endl;
  1975 +
  1976 + for (auto &item: control.menu)
  1977 + std::cout << 6 * indent
  1978 + << "<item>"
  1979 + << item
  1980 + << "</item>"
  1981 + << std::endl;
  1982 +
  1983 + std::cout << 5 * indent << "</menu>" << std::endl;
  1984 + }
  1985 +
  1986 + std::cout << 4 * indent << "</control>" << std::endl;
  1987 + }
  1988 +
  1989 + std::cout << 3 * indent << "</controls>" << std::endl;
  1990 + std::cout << 2 * indent << "</device>" << std::endl;
  1991 + }
  1992 +
  1993 + std::cout << indent << "</devices>" << std::endl;
  1994 + std::cout << indent << "<input-formats>" << std::endl;
  1995 +
  1996 + for (auto &format: this->m_ipcBridge.supportedPixelFormats(IpcBridge::StreamType_Input))
  1997 + std::cout << 2 * indent
  1998 + << "<pixel-format>"
  1999 + << VideoFormat::stringFromFourcc(format)
  2000 + << "</pixel-format>"
  2001 + << std::endl;
  2002 +
  2003 + std::cout << indent << "</input-formats>" << std::endl;
  2004 +
  2005 + auto defInputFormat =
  2006 + this->m_ipcBridge.defaultPixelFormat(IpcBridge::StreamType_Input);
  2007 + std::cout << indent
  2008 + << "<default-input-format>"
  2009 + << VideoFormat::stringFromFourcc(defInputFormat)
  2010 + << "</default-input-format>"
  2011 + << std::endl;
  2012 +
  2013 + std::cout << indent << "<output-formats>" << std::endl;
  2014 +
  2015 + for (auto &format: this->m_ipcBridge.supportedPixelFormats(IpcBridge::StreamType_Output))
  2016 + std::cout << 2 * indent
  2017 + << "<pixel-format>"
  2018 + << VideoFormat::stringFromFourcc(format)
  2019 + << "</pixel-format>"
  2020 + << std::endl;
  2021 +
  2022 + std::cout << indent << "</output-formats>" << std::endl;
  2023 +
  2024 + auto defOutputFormat =
  2025 + this->m_ipcBridge.defaultPixelFormat(IpcBridge::StreamType_Output);
  2026 + std::cout << indent
  2027 + << "<default-output-format>"
  2028 + << VideoFormat::stringFromFourcc(defOutputFormat)
  2029 + << "</default-output-format>"
  2030 + << std::endl;
  2031 +
  2032 + std::cout << indent << "<clients>" << std::endl;
  2033 +
  2034 + for (auto &pid: this->m_ipcBridge.clientsPids()) {
  2035 + std::cout << 2 * indent << "<client>" << std::endl;
  2036 + std::cout << 3 * indent << "<pid>" << pid << "</pid>" << std::endl;
  2037 + std::cout << 3 * indent
  2038 + << "<exe>"
  2039 + << this->m_ipcBridge.clientExe(pid)
  2040 + << "</exe>"
  2041 + << std::endl;
  2042 + std::cout << 2 * indent << "</client>" << std::endl;
  2043 + }
  2044 +
  2045 + std::cout << indent << "</clients>" << std::endl;
  2046 + std::cout << indent
  2047 + << "<picture>"
  2048 + << this->m_ipcBridge.picture()
  2049 + << "</picture>"
  2050 + << std::endl;
  2051 + std::cout << indent
  2052 + << "<loglevel>"
  2053 + << this->m_ipcBridge.logLevel()
  2054 + << "</loglevel>"
  2055 + << std::endl;
  2056 + std::cout << "</info>" << std::endl;
  2057 +
  2058 + return 0;
  2059 +}
  2060 +
  2061 +int AkVCam::CmdParserPrivate::hacks(const AkVCam::StringMap &flags,
  2062 + const AkVCam::StringVector &args)
  2063 +{
  2064 + UNUSED(flags);
  2065 + UNUSED(args);
  2066 +
  2067 + auto hacks = this->m_ipcBridge.hacks();
  2068 +
  2069 + if (hacks.empty())
  2070 + return 0;
  2071 +
  2072 + if (this->m_parseable) {
  2073 + for (auto &hack: hacks)
  2074 + std::cout << hack << std::endl;
  2075 + } else {
  2076 + std::cout << "Hacks are intended to fix common problems with the "
  2077 + "virtual camera, and are intended to be used by developers "
  2078 + "and advanced users only." << std::endl;
  2079 + std::cout << std::endl;
  2080 + std::cout << "WARNING: Unsafe hacks can brick your system, make it "
  2081 + "unstable, or expose it to a serious security risk, "
  2082 + "remember to make a backup of your files and system. You "
  2083 + "are solely responsible of whatever happens for using "
  2084 + "them. You been warned, don't come and cry later."
  2085 + << std::endl;
  2086 + std::cout << std::endl;
  2087 + std::vector<std::string> table {
  2088 + "Hack",
  2089 + "Is safe?",
  2090 + "Description"
  2091 + };
  2092 + auto columns = table.size();
  2093 +
  2094 + for (auto &hack: hacks) {
  2095 + table.push_back(hack);
  2096 + table.push_back(this->m_ipcBridge.hackIsSafe(hack)? "Yes": "No");
  2097 + table.push_back(this->m_ipcBridge.hackDescription(hack));
  2098 + }
  2099 +
  2100 + this->drawTable(table, columns);
  2101 + }
  2102 +
  2103 + return 0;
  2104 +}
  2105 +
  2106 +int AkVCam::CmdParserPrivate::hackInfo(const AkVCam::StringMap &flags,
  2107 + const AkVCam::StringVector &args)
  2108 +{
  2109 + if (args.size() < 2) {
  2110 + std::cerr << "Not enough arguments." << std::endl;
  2111 +
  2112 + return -EINVAL;
  2113 + }
  2114 +
  2115 + auto hack = args[1];
  2116 + auto hacks = this->m_ipcBridge.hacks();
  2117 + auto dit = std::find(hacks.begin(), hacks.end(), hack);
  2118 +
  2119 + if (dit == hacks.end()) {
  2120 + std::cerr << "Unknown hack: " << hack << "." << std::endl;
  2121 +
  2122 + return -ENOSYS;
  2123 + }
  2124 +
  2125 + if (this->containsFlag(flags, "hack-info", "-c"))
  2126 + std::cout << this->m_ipcBridge.hackDescription(hack) << std::endl;
  2127 +
  2128 + if (this->containsFlag(flags, "hack-info", "-s")) {
  2129 + if (this->m_ipcBridge.hackIsSafe(hack))
  2130 + std::cout << "Yes" << std::endl;
  2131 + else
  2132 + std::cout << "No" << std::endl;
  2133 + }
  2134 +
  2135 + return 0;
  2136 +}
  2137 +
  2138 +int AkVCam::CmdParserPrivate::hack(const AkVCam::StringMap &flags,
  2139 + const AkVCam::StringVector &args)
  2140 +{
  2141 + if (args.size() < 2) {
  2142 + std::cerr << "Not enough arguments." << std::endl;
  2143 +
  2144 + return -EINVAL;
  2145 + }
  2146 +
  2147 + auto hack = args[1];
  2148 + auto hacks = this->m_ipcBridge.hacks();
  2149 + auto dit = std::find(hacks.begin(), hacks.end(), hack);
  2150 +
  2151 + if (dit == hacks.end()) {
  2152 + std::cerr << "Unknown hack: " << hack << "." << std::endl;
  2153 +
  2154 + return -ENOSYS;
  2155 + }
  2156 +
  2157 + bool accepted = this->m_parseable | this->m_ipcBridge.hackIsSafe(hack);
  2158 +
  2159 + if (!accepted && !this->m_parseable) {
  2160 + std::cout << "WARNING: Applying this hack can brick your system, make "
  2161 + "it unstable, or expose it to a serious security risk, "
  2162 + "remember to make a backup of your files and system. "
  2163 + "Agreeing to continue, you accept the full responsability "
  2164 + "of whatever happens from now on."
  2165 + << std::endl;
  2166 + std::cout << std::endl;
  2167 +
  2168 + if (this->containsFlag(flags, "hack", "-y")) {
  2169 + std::cout << "You agreed to continue from command line."
  2170 + << std::endl;
  2171 + std::cout << std::endl;
  2172 + accepted = true;
  2173 + } else {
  2174 + std::cout << "If you agree to continue write YES: ";
  2175 + std::string answer;
  2176 + std::cin >> answer;
  2177 + std::cout << std::endl;
  2178 + accepted = answer == "YES";
  2179 + }
  2180 + }
  2181 +
  2182 + if (!accepted) {
  2183 + std::cerr << "Hack not applied." << std::endl;
  2184 +
  2185 + return -EIO;
  2186 + }
  2187 +
  2188 + StringVector hargs;
  2189 +
  2190 + for (size_t i = 2; i < args.size(); i++)
  2191 + hargs.push_back(args[i]);
  2192 +
  2193 + auto result = this->m_ipcBridge.execHack(hack, hargs);
  2194 +
  2195 + if (result == 0)
  2196 + std::cout << "Success" << std::endl;
  2197 + else
  2198 + std::cout << "Failed" << std::endl;
  2199 +
  2200 + return result;
  2201 +}
  2202 +
  2203 +void AkVCam::CmdParserPrivate::loadGenerals(Settings &settings)
  2204 +{
  2205 + settings.beginGroup("General");
  2206 +
  2207 + if (settings.contains("default_frame"))
  2208 + this->m_ipcBridge.setPicture(settings.value("default_frame"));
  2209 +
  2210 + if (settings.contains("loglevel")) {
  2211 + auto logLevel= settings.value("loglevel");
  2212 + char *p = nullptr;
  2213 + auto level = strtol(logLevel.c_str(), &p, 10);
  2214 +
  2215 + if (*p)
  2216 + level = AkVCam::Logger::levelFromString(logLevel);
  2217 +
  2218 + this->m_ipcBridge.setLogLevel(level);
  2219 + }
  2220 +
  2221 + settings.endGroup();
  2222 +}
  2223 +
  2224 +AkVCam::VideoFormatMatrix AkVCam::CmdParserPrivate::readFormats(Settings &settings)
  2225 +{
  2226 + VideoFormatMatrix formatsMatrix;
  2227 + settings.beginGroup("Formats");
  2228 + auto nFormats = settings.beginArray("formats");
  2229 +
  2230 + for (size_t i = 0; i < nFormats; i++) {
  2231 + settings.setArrayIndex(i);
  2232 + formatsMatrix.push_back(this->readFormat(settings));
  2233 + }
  2234 +
  2235 + settings.endArray();
  2236 + settings.endGroup();
  2237 +
  2238 + return formatsMatrix;
  2239 +}
  2240 +
  2241 +std::vector<AkVCam::VideoFormat> AkVCam::CmdParserPrivate::readFormat(Settings &settings)
  2242 +{
  2243 + std::vector<AkVCam::VideoFormat> formats;
  2244 +
  2245 + auto pixFormats = settings.valueList("format", ",");
  2246 + auto widths = settings.valueList("width", ",");
  2247 + auto heights = settings.valueList("height", ",");
  2248 + auto frameRates = settings.valueList("fps", ",");
  2249 +
  2250 + if (pixFormats.empty()
  2251 + || widths.empty()
  2252 + || heights.empty()
  2253 + || frameRates.empty()) {
  2254 + std::cerr << "Error reading formats." << std::endl;
  2255 +
  2256 + return {};
  2257 + }
  2258 +
  2259 + StringMatrix formatMatrix;
  2260 + formatMatrix.push_back(pixFormats);
  2261 + formatMatrix.push_back(widths);
  2262 + formatMatrix.push_back(heights);
  2263 + formatMatrix.push_back(frameRates);
  2264 +
  2265 + for (auto &format_list: this->matrixCombine(formatMatrix)) {
  2266 + auto pixFormat = VideoFormat::fourccFromString(format_list[0]);
  2267 + char *p = nullptr;
  2268 + auto width = strtol(format_list[1].c_str(), &p, 10);
  2269 + p = nullptr;
  2270 + auto height = strtol(format_list[2].c_str(), &p, 10);
  2271 + Fraction frame_rate(format_list[3]);
  2272 + VideoFormat format(pixFormat,
  2273 + width,
  2274 + height,
  2275 + {frame_rate});
  2276 +
  2277 + if (format.isValid())
  2278 + formats.push_back(format);
  2279 + }
  2280 +
  2281 + return formats;
  2282 +}
  2283 +
  2284 +AkVCam::StringMatrix AkVCam::CmdParserPrivate::matrixCombine(const StringMatrix &matrix)
  2285 +{
  2286 + StringVector combined;
  2287 + StringMatrix combinations;
  2288 + this->matrixCombineP(matrix, 0, combined, combinations);
  2289 +
  2290 + return combinations;
  2291 +}
  2292 +
  2293 +/* A matrix is a list of lists where each element in the main list is a row,
  2294 + * and each element in a row is a column. We combine each element in a row with
  2295 + * each element in the next rows.
  2296 + */
  2297 +void AkVCam::CmdParserPrivate::matrixCombineP(const StringMatrix &matrix,
  2298 + size_t index,
  2299 + StringVector combined,
  2300 + StringMatrix &combinations)
  2301 +{
  2302 + if (index >= matrix.size()) {
  2303 + combinations.push_back(combined);
  2304 +
  2305 + return;
  2306 + }
  2307 +
  2308 + for (auto &data: matrix[index]) {
  2309 + auto combinedP1 = combined;
  2310 + combinedP1.push_back(data);
  2311 + this->matrixCombineP(matrix, index + 1, combinedP1, combinations);
  2312 + }
  2313 +}
  2314 +
  2315 +void AkVCam::CmdParserPrivate::createDevices(Settings &settings,
  2316 + const VideoFormatMatrix &availableFormats)
  2317 +{
  2318 + auto devices = this->m_ipcBridge.devices();
  2319 +
  2320 + for (auto &device: devices)
  2321 + this->m_ipcBridge.removeDevice(device);
  2322 +
  2323 + settings.beginGroup("Cameras");
  2324 + size_t nCameras = settings.beginArray("cameras");
  2325 +
  2326 + for (size_t i = 0; i < nCameras; i++) {
  2327 + settings.setArrayIndex(i);
  2328 + this->createDevice(settings, availableFormats);
  2329 + }
  2330 +
  2331 + settings.endArray();
  2332 + settings.endGroup();
  2333 + this->m_ipcBridge.updateDevices();
  2334 +}
  2335 +
  2336 +void AkVCam::CmdParserPrivate::createDevice(Settings &settings,
  2337 + const VideoFormatMatrix &availableFormats)
  2338 +{
  2339 + auto description = settings.value("description");
  2340 +
  2341 + if (description.empty()) {
  2342 + std::cerr << "Device description is empty" << std::endl;
  2343 +
  2344 + return;
  2345 + }
  2346 +
  2347 + auto formats = this->readDeviceFormats(settings, availableFormats);
  2348 +
  2349 + if (formats.empty()) {
  2350 + std::cerr << "Can't read device formats" << std::endl;
  2351 +
  2352 + return;
  2353 + }
  2354 +
  2355 + auto deviceId = settings.value("id");
  2356 + deviceId = this->m_ipcBridge.addDevice(description, deviceId);
  2357 + auto supportedFormats =
  2358 + this->m_ipcBridge.supportedPixelFormats(IpcBridge::StreamType_Output);
  2359 +
  2360 + for (auto &format: formats) {
  2361 + auto it = std::find(supportedFormats.begin(),
  2362 + supportedFormats.end(),
  2363 + format.fourcc());
  2364 +
  2365 + if (it != supportedFormats.end())
  2366 + this->m_ipcBridge.addFormat(deviceId, format, -1);
  2367 + }
  2368 +}
  2369 +
  2370 +std::vector<AkVCam::VideoFormat> AkVCam::CmdParserPrivate::readDeviceFormats(Settings &settings,
  2371 + const VideoFormatMatrix &availableFormats)
  2372 +{
  2373 + std::vector<AkVCam::VideoFormat> formats;
  2374 + auto formatsIndex = settings.valueList("formats", ",");
  2375 +
  2376 + for (auto &indexStr: formatsIndex) {
  2377 + char *p = nullptr;
  2378 + auto index = strtoul(indexStr.c_str(), &p, 10);
  2379 +
  2380 + if (*p)
  2381 + continue;
  2382 +
  2383 + index--;
  2384 +
  2385 + if (index >= availableFormats.size())
  2386 + continue;
  2387 +
  2388 + for (auto &format: availableFormats[index])
  2389 + formats.push_back(format);
  2390 + }
  2391 +
  2392 + return formats;
  2393 +}
  2394 +
  2395 +std::string AkVCam::operator *(const std::string &str, size_t n)
  2396 +{
  2397 + std::stringstream ss;
  2398 +
  2399 + for (size_t i = 0; i < n; i++)
  2400 + ss << str;
  2401 +
  2402 + return ss.str();
  2403 +}
  2404 +
  2405 +std::string AkVCam::operator *(size_t n, const std::string &str)
  2406 +{
  2407 + std::stringstream ss;
  2408 +
  2409 + for (size_t i = 0; i < n; i++)
  2410 + ss << str;
  2411 +
  2412 + return ss.str();
  2413 +}
  2414 +
  2415 +AkVCam::CmdParserCommand::CmdParserCommand()
  2416 +{
  2417 +}
  2418 +
  2419 +AkVCam::CmdParserCommand::CmdParserCommand(const std::string &command,
  2420 + const std::string &arguments,
  2421 + const std::string &helpString,
  2422 + const AkVCam::ProgramOptionsFunc &func,
  2423 + const std::vector<AkVCam::CmdParserFlags> &flags,
  2424 + bool advanced):
  2425 + command(command),
  2426 + arguments(arguments),
  2427 + helpString(helpString),
  2428 + func(func),
  2429 + flags(flags),
  2430 + advanced(advanced)
  2431 +{
  2432 +}
  1 +/* akvirtualcamera, virtual camera for Mac and Windows.
  2 + * Copyright (C) 2020 Gonzalo Exequiel Pedone
  3 + *
  4 + * akvirtualcamera is free software: you can redistribute it and/or modify
  5 + * it under the terms of the GNU General Public License as published by
  6 + * the Free Software Foundation, either version 3 of the License, or
  7 + * (at your option) any later version.
  8 + *
  9 + * akvirtualcamera is distributed in the hope that it will be useful,
  10 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 + * GNU General Public License for more details.
  13 + *
  14 + * You should have received a copy of the GNU General Public License
  15 + * along with akvirtualcamera. If not, see <http://www.gnu.org/licenses/>.
  16 + *
  17 + * Web-Site: http://webcamoid.github.io/
  18 + */
  19 +
  20 +#ifndef CMDPARSER_H
  21 +#define CMDPARSER_H
  22 +
  23 +#include <functional>
  24 +#include <map>
  25 +#include <string>
  26 +#include <vector>
  27 +
  28 +namespace AkVCam {
  29 + class CmdParserPrivate;
  30 + using StringVector = std::vector<std::string>;
  31 + using StringMap = std::map<std::string, std::string>;
  32 + using ProgramOptionsFunc = std::function<int (const StringMap &flags,
  33 + const StringVector &args)>;
  34 +
  35 + class CmdParser
  36 + {
  37 + public:
  38 + CmdParser();
  39 + ~CmdParser();
  40 + int parse(int argc, char **argv);
  41 + void setDefaultFuntion(const ProgramOptionsFunc &func);
  42 + void addCommand(const std::string &command,
  43 + const std::string &arguments,
  44 + const std::string &helpString,
  45 + const ProgramOptionsFunc &func,
  46 + bool advanced=false);
  47 + void addFlags(const std::string &command,
  48 + const StringVector &flags,
  49 + const std::string &value,
  50 + const std::string &helpString);
  51 + void addFlags(const std::string &command,
  52 + const StringVector &flags,
  53 + const std::string &helpString);
  54 +
  55 + private:
  56 + CmdParserPrivate *d;
  57 + };
  58 +}
  59 +
  60 +#endif // CMDPARSER_H
  1 +/* akvirtualcamera, virtual camera for Mac and Windows.
  2 + * Copyright (C) 2020 Gonzalo Exequiel Pedone
  3 + *
  4 + * akvirtualcamera is free software: you can redistribute it and/or modify
  5 + * it under the terms of the GNU General Public License as published by
  6 + * the Free Software Foundation, either version 3 of the License, or
  7 + * (at your option) any later version.
  8 + *
  9 + * akvirtualcamera is distributed in the hope that it will be useful,
  10 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 + * GNU General Public License for more details.
  13 + *
  14 + * You should have received a copy of the GNU General Public License
  15 + * along with akvirtualcamera. If not, see <http://www.gnu.org/licenses/>.
  16 + *
  17 + * Web-Site: http://webcamoid.github.io/
  18 + */
  19 +
  20 +#include "cmdparser.h"
  21 +
  22 +int main(int argc, char **argv)
  23 +{
  24 + return AkVCam::CmdParser().parse(argc, argv);
  25 +}
  1 +# README
  2 +
  3 +Before contributing, please read [the contributing document](https://github.com/webcamoid/akvirtualcamera/blob/master/CONTRIBUTING.md), and the [coding style and conventions](https://github.com/webcamoid/webcamoid/wiki/Coding-style-and-conventions) guide.
  4 +Search the [pull request list](https://github.com/webcamoid/akvirtualcamera/pulls) for similar pulls before opening a new one.
  5 +Check your code doesn't throw any warning or error message while compiling, and doesn't give any warning in Clang static analyzer. Make sure your code is GCC, Clang, MinGW and MSVC compliant (use Github actions for that).
  6 +Check your code using GDB, Valgrind and similar tools to remove all possible memory leaks and segfaults.
  7 +
  8 +# Pull request
  9 +
  10 +## Type of change
  11 +
  12 +Is your pull request a bug fix, new feature, code refactor, breaking change, etc.?
  13 +If your change is too big consider [discussing it](https://github.com/webcamoid/akvirtualcamera/issues) before pulling.
  14 +
  15 +## Summary
  16 +
  17 +Describe your pull request the best as you can.
  18 +
  19 +## Related Issue
  20 +
  21 +Is this pull request related to some [issue](https://github.com/webcamoid/akvirtualcamera/issues)? Cite the issue as #NNN, where NNN is the number of issue.
  22 +
  23 +## More info
  24 +
  25 +Provide screenshots, logs, etc. if required.
  26 +
  27 +## Added dependencies
  28 +
  29 +Does your pull request add more dependencies to the project? This may require some discussion. Minimal dependencies is a requirement.
  30 +
  31 +## Target Environment
  32 +
  33 +Is this pull request specific to a target operating system?
  34 +
  35 +* Operating System information: (name) (numeric version and codename if applied) (architecture)
  1 +# akvirtualcamera, virtual camera for Mac and Windows
  2 +
  3 +akvirtualcamera is virtual camera implemented as a DirectShow filter in Windows, and as a CoreMediaIO plugin in Mac.
  4 +
  5 +## Features
  6 +
  7 +* Supports emulated camera controls in capture devices (brightness, contrast, saturation, etc.).
  8 +* Configurable default picture in case no input signal available.
  9 +
  10 +## Build and Install
  11 +
  12 +Visit the [wiki](https://github.com/webcamoid/akvirtualcamera/wiki) for a comprehensive compile and install instructions.
  13 +
  14 +## Downloads ##
  15 +
  16 +[![Download](https://img.shields.io/badge/Download-Releases-3f2a7e.svg)](https://github.com/webcamoid/akvirtualcamera/releases)
  17 +[![Daily Build](https://img.shields.io/badge/Download-Daily%20Build-3f2a7e.svg)](https://github.com/webcamoid/akvirtualcamera/releases/tag/daily-build)
  18 +[![Total Downloads](https://img.shields.io/github/downloads/webcamoid/akvirtualcamera/total.svg?label=Total%20Downloads&color=3f2a7e)](https://tooomm.github.io/github-release-stats/?username=webcamoid&repository=akvirtualcamera)
  19 +
  20 +## Donations ##
  21 +
  22 +If you are interested in donating to the project you can look at all available methods in the [donations page](https://webcamoid.github.io/donations).
  23 +
  24 +## Status
  25 +
  26 +[![Linux MinGW](https://github.com/webcamoid/akvirtualcamera/actions/workflows/linux-mingw.yml/badge.svg)](https://github.com/webcamoid/akvirtualcamera/actions/workflows/linux-mingw.yml)
  27 +[![Mac](https://github.com/webcamoid/akvirtualcamera/actions/workflows/mac.yml/badge.svg)](https://github.com/webcamoid/akvirtualcamera/actions/workflows/mac.yml)
  28 +[![Windows MSYS](https://github.com/webcamoid/akvirtualcamera/actions/workflows/windows-msys.yml/badge.svg)](https://github.com/webcamoid/akvirtualcamera/actions/workflows/windows-msys.yml)
  29 +[![Windows MSVC](https://github.com/webcamoid/akvirtualcamera/actions/workflows/windows-vs.yml/badge.svg)](https://github.com/webcamoid/akvirtualcamera/actions/workflows/windows-vs.yml)
  30 +[![Build status](https://api.cirrus-ci.com/github/webcamoid/akvirtualcamera.svg)](https://cirrus-ci.com/github/webcamoid/akvirtualcamera)
  31 +[![Codacy Badge](https://app.codacy.com/project/badge/Grade/1cee2645a3604633a506a203fb8c3161)](https://www.codacy.com/gh/webcamoid/akvirtualcamera/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=webcamoid/akvirtualcamera&amp;utm_campaign=Badge_Grade)
  32 +[![Project Stats](https://www.openhub.net/p/akvirtualcamera/widgets/project_thin_badge.gif)](https://www.openhub.net/p/akvirtualcamera)
  33 +
  34 +## Reporting Bugs
  35 +
  36 +Report all issues in the [issues tracker](https://github.com/webcamoid/akvirtualcamera/issues).
  1 +# Security Policy
  2 +
  3 +## Supported Versions
  4 +
  5 +Only report vulnerabilities in the [latest stable](https://github.com/webcamoid/akvirtualcamera/releases) version and the [master repository](https://github.com/webcamoid/akvirtualcamera) (that includes [daily build](https://bintray.com/webcamoid/webcamoid/akvirtualcamera/daily/link)).
  6 +
  7 +## Reporting a Vulnerability
  8 +
  9 +Report all vulnerabilities at the [issues section](https://github.com/webcamoid/akvirtualcamera/issues).
  10 +
  1 +# akvirtualcamera, virtual camera for Mac and Windows.
  2 +# Copyright (C) 2021 Gonzalo Exequiel Pedone
  3 +#
  4 +# akvirtualcamera is free software: you can redistribute it and/or modify
  5 +# it under the terms of the GNU General Public License as published by
  6 +# the Free Software Foundation, either version 3 of the License, or
  7 +# (at your option) any later version.
  8 +#
  9 +# akvirtualcamera is distributed in the hope that it will be useful,
  10 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 +# GNU General Public License for more details.
  13 +#
  14 +# You should have received a copy of the GNU General Public License
  15 +# along with akvirtualcamera. If not, see <http://www.gnu.org/licenses/>.
  16 +#
  17 +# Web-Site: http://webcamoid.github.io/
  18 +
  19 +cmake_minimum_required(VERSION 3.5)
  20 +
  21 +project(Service LANGUAGES CXX)
  22 +
  23 +include(../commons.cmake)
  24 +
  25 +set(CMAKE_INCLUDE_CURRENT_DIR ON)
  26 +set(CMAKE_CXX_STANDARD 11)
  27 +set(CMAKE_CXX_STANDARD_REQUIRED ON)
  28 +
  29 +add_executable(Service
  30 + src/main.cpp
  31 + src/service.cpp
  32 + src/service.h)
  33 +
  34 +if (APPLE OR FAKE_APPLE)
  35 + set_target_properties(Service PROPERTIES
  36 + OUTPUT_NAME ${AKVCAM_SERVICE_NAME}
  37 + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${BUILDDIR}/${DATAROOTDIR})
  38 +else ()
  39 + set_target_properties(Service PROPERTIES
  40 + OUTPUT_NAME ${AKVCAM_SERVICE_NAME}
  41 + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${BUILDDIR}/${BINDIR}
  42 + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${BUILDDIR}/${BINDIR})
  43 +endif ()
  44 +
  45 +add_dependencies(Service
  46 + VCamUtils)
  47 +
  48 +if (APPLE OR FAKE_APPLE)
  49 + add_dependencies(Service
  50 + VCamIPC_cmio PlatformUtils_cmio)
  51 +elseif (WIN32)
  52 + add_dependencies(Service
  53 + VCamIPC_dshow PlatformUtils_dshow)
  54 +endif ()
  55 +
  56 +target_include_directories(Service
  57 + PRIVATE
  58 + ..)
  59 +target_link_libraries(Service
  60 + VCamUtils)
  61 +
  62 +if (APPLE OR FAKE_APPLE)
  63 + target_include_directories(Service
  64 + PRIVATE
  65 + ../cmio)
  66 +
  67 + if (FAKE_APPLE)
  68 + target_include_directories(Service
  69 + PRIVATE
  70 + ../cmio/FakeAPI)
  71 + endif ()
  72 +
  73 + target_link_libraries(Service
  74 + VCamIPC_cmio
  75 + PlatformUtils_cmio)
  76 +elseif (WIN32)
  77 + target_include_directories(Service
  78 + PRIVATE
  79 + ../dshow)
  80 + target_link_libraries(Service
  81 + VCamIPC_dshow
  82 + PlatformUtils_dshow)
  83 +endif ()
  84 +
  85 +if (WIN32)
  86 + target_link_libraries(Service
  87 + ws2_32)
  88 +endif ()
  89 +
  90 +if (APPLE OR FAKE_APPLE)
  91 + install(TARGETS Service DESTINATION ${DATAROOTDIR})
  92 +elseif (WIN32)
  93 + install(TARGETS Service DESTINATION ${BINDIR})
  94 +endif ()
  1 +/* akvirtualcamera, virtual camera for Mac and Windows.
  2 + * Copyright (C) 2020 Gonzalo Exequiel Pedone
  3 + *
  4 + * akvirtualcamera is free software: you can redistribute it and/or modify
  5 + * it under the terms of the GNU General Public License as published by
  6 + * the Free Software Foundation, either version 3 of the License, or
  7 + * (at your option) any later version.
  8 + *
  9 + * akvirtualcamera is distributed in the hope that it will be useful,
  10 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 + * GNU General Public License for more details.
  13 + *
  14 + * You should have received a copy of the GNU General Public License
  15 + * along with akvirtualcamera. If not, see <http://www.gnu.org/licenses/>.
  16 + *
  17 + * Web-Site: http://webcamoid.github.io/
  18 + */
  19 +
  20 +#include <string>
  21 +#include <csignal>
  22 +
  23 +#include "service.h"
  24 +#include "PlatformUtils/src/preferences.h"
  25 +#include "PlatformUtils/src/utils.h"
  26 +#include "VCamUtils/src/logger.h"
  27 +
  28 +AkVCam::Service *servicePtr = nullptr;
  29 +
  30 +int main(int argc, char **argv)
  31 +{
  32 + auto loglevel = AkVCam::Preferences::logLevel();
  33 + AkVCam::Logger::setLogLevel(loglevel);
  34 + auto defaultLogFile = AkVCam::tempPath()
  35 + + "/"
  36 + + AkVCam::basename(argv[0])
  37 + + ".log";
  38 + auto logFile = AkVCam::Preferences::readString("logfile", defaultLogFile);
  39 + AkVCam::Logger::setLogFile(logFile);
  40 + AkVCam::Service service;
  41 + servicePtr = &service;
  42 +
  43 + signal(SIGTERM, [] (int) {
  44 + servicePtr->stop();
  45 + });
  46 +
  47 +#ifndef _WIN32
  48 + signal(SIGPIPE, [] (int) {
  49 + });
  50 +#endif
  51 +
  52 + return service.run();
  53 +}
  1 +/* akvirtualcamera, virtual camera for Mac and Windows.
  2 + * Copyright (C) 2020 Gonzalo Exequiel Pedone
  3 + *
  4 + * akvirtualcamera is free software: you can redistribute it and/or modify
  5 + * it under the terms of the GNU General Public License as published by
  6 + * the Free Software Foundation, either version 3 of the License, or
  7 + * (at your option) any later version.
  8 + *
  9 + * akvirtualcamera is distributed in the hope that it will be useful,
  10 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 + * GNU General Public License for more details.
  13 + *
  14 + * You should have received a copy of the GNU General Public License
  15 + * along with akvirtualcamera. If not, see <http://www.gnu.org/licenses/>.
  16 + *
  17 + * Web-Site: http://webcamoid.github.io/
  18 + */
  19 +
  20 +#include <algorithm>
  21 +#include <condition_variable>
  22 +#include <iostream>
  23 +#include <map>
  24 +#include <memory>
  25 +#include <mutex>
  26 +#include <string>
  27 +#include <vector>
  28 +#include <thread>
  29 +
  30 +#include "service.h"
  31 +#include "PlatformUtils/src/preferences.h"
  32 +#include "VCamUtils/src/logger.h"
  33 +#include "VCamUtils/src/message.h"
  34 +#include "VCamUtils/src/messageserver.h"
  35 +#include "VCamUtils/src/servicemsg.h"
  36 +#include "VCamUtils/src/videoformat.h"
  37 +#include "VCamUtils/src/videoframe.h"
  38 +
  39 +namespace AkVCam
  40 +{
  41 + struct Peer
  42 + {
  43 + uint64_t clientId {0};
  44 + uint64_t pid {0};
  45 +
  46 + Peer(uint64_t clientId=0, uint64_t pid=0):
  47 + clientId(clientId),
  48 + pid(pid)
  49 + {
  50 +
  51 + }
  52 + };
  53 +
  54 + struct BroadcastSlot
  55 + {
  56 + Peer broadcaster;
  57 + std::vector<Peer> listeners;
  58 + VideoFrame frame;
  59 + };
  60 +
  61 + typedef std::map<std::string, BroadcastSlot> Broadcasts;
  62 +
  63 + class ServicePrivate
  64 + {
  65 + public:
  66 + MessageServer m_messageServer;
  67 +
  68 + // Broadcasting and listen
  69 + Broadcasts m_broadcasts;
  70 + std::condition_variable_any m_frameAvailable;
  71 + std::mutex m_peerMutex;
  72 +
  73 + ServicePrivate();
  74 + static void removeClientById(void *userData, uint64_t clientId);
  75 + bool clients(uint64_t clientId,
  76 + const Message &inMessage,
  77 + Message &outMessage);
  78 + bool broadcast(uint64_t clientId,
  79 + const Message &inMessage,
  80 + Message &outMessage);
  81 + bool listen(uint64_t clientId,
  82 + const Message &inMessage,
  83 + Message &outMessage);
  84 + };
  85 +}
  86 +
  87 +AkVCam::Service::Service()
  88 +{
  89 + this->d = new ServicePrivate;
  90 +}
  91 +
  92 +AkVCam::Service::~Service()
  93 +{
  94 + delete this->d;
  95 +}
  96 +
  97 +int AkVCam::Service::run()
  98 +{
  99 + AkLogFunction();
  100 +
  101 + return this->d->m_messageServer.run();
  102 +}
  103 +
  104 +void AkVCam::Service::stop()
  105 +{
  106 + AkLogFunction();
  107 + this->d->m_messageServer.stop();
  108 +}
  109 +
  110 +#define BIND(member) \
  111 + std::bind(&member, \
  112 + this, \
  113 + std::placeholders::_1, \
  114 + std::placeholders::_2, \
  115 + std::placeholders::_3)
  116 +
  117 +AkVCam::ServicePrivate::ServicePrivate()
  118 +{
  119 + AkLogFunction();
  120 +
  121 + this->m_messageServer.setPort(Preferences::servicePort());
  122 +
  123 + this->m_messageServer.subscribe(AKVCAM_SERVICE_MSG_CLIENTS , BIND(ServicePrivate::clients) );
  124 + this->m_messageServer.subscribe(AKVCAM_SERVICE_MSG_BROADCAST, BIND(ServicePrivate::broadcast));
  125 + this->m_messageServer.subscribe(AKVCAM_SERVICE_MSG_LISTEN , BIND(ServicePrivate::listen) );
  126 +
  127 + this->m_messageServer.connectConnectionClosed(this, &ServicePrivate::removeClientById);
  128 +}
  129 +
  130 +void AkVCam::ServicePrivate::removeClientById(void *userData,
  131 + uint64_t clientId)
  132 +{
  133 + AkLogFunction();
  134 + AkLogDebug() << "Removing client: " << clientId << std::endl;
  135 + auto self = reinterpret_cast<ServicePrivate *>(userData);
  136 +
  137 + self->m_peerMutex.lock();
  138 + std::string removeDevice;
  139 +
  140 + for (auto &slot: self->m_broadcasts) {
  141 + if (slot.second.broadcaster.clientId == clientId) {
  142 + slot.second.broadcaster = {0, 0};
  143 +
  144 + if (slot.second.listeners.empty())
  145 + removeDevice = slot.first;
  146 +
  147 + break;
  148 + } else {
  149 + auto it = std::find_if(slot.second.listeners.begin(),
  150 + slot.second.listeners.end(),
  151 + [&clientId] (const Peer &peer) -> bool {
  152 + return peer.clientId == clientId;
  153 + });
  154 +
  155 + if (it != slot.second.listeners.end()) {
  156 + slot.second.listeners.erase(it);
  157 +
  158 + if (slot.second.broadcaster.pid == 0
  159 + && slot.second.listeners.empty()) {
  160 + removeDevice = slot.first;
  161 + }
  162 +
  163 + break;
  164 + }
  165 + }
  166 + }
  167 +
  168 + if (!removeDevice.empty())
  169 + self->m_broadcasts.erase(removeDevice);
  170 +
  171 + self->m_peerMutex.unlock();
  172 +}
  173 +
  174 +bool AkVCam::ServicePrivate::clients(uint64_t clientId,
  175 + const Message &inMessage,
  176 + Message &outMessage)
  177 +{
  178 + AkLogFunction();
  179 + UNUSED(clientId);
  180 + MsgClients msgClients(inMessage);
  181 + std::vector<uint64_t> clients;
  182 +
  183 + this->m_peerMutex.lock();
  184 +
  185 + for (auto &slot: this->m_broadcasts) {
  186 + if (msgClients.clientType() == MsgClients::ClientType_Any
  187 + && slot.second.broadcaster.pid
  188 + && std::find(clients.begin(),
  189 + clients.end(),
  190 + slot.second.broadcaster.pid) == clients.end()) {
  191 + clients.push_back(slot.second.broadcaster.pid);
  192 + }
  193 +
  194 + for (auto &client: slot.second.listeners)
  195 + if (std::find(clients.begin(),
  196 + clients.end(),
  197 + client.pid) == clients.end())
  198 + clients.push_back(client.pid);
  199 + }
  200 +
  201 + this->m_peerMutex.unlock();
  202 + outMessage = MsgClients(msgClients.clientType(),
  203 + clients,
  204 + inMessage.queryId()).toMessage();
  205 +
  206 + return true;
  207 +}
  208 +
  209 +bool AkVCam::ServicePrivate::broadcast(uint64_t clientId,
  210 + const Message &inMessage,
  211 + Message &outMessage)
  212 +{
  213 + AkLogFunction();
  214 + MsgBroadcast msgBroadcast(inMessage);
  215 + MsgStatus status(-1, inMessage.queryId());
  216 + this->m_peerMutex.lock();
  217 +
  218 + bool isBroadcasting = this->m_broadcasts.count(msgBroadcast.device()) < 1;
  219 + AkLogDebug() << "Device" << msgBroadcast.device() << "is broadcasting?:" << (isBroadcasting? "YES": "NO") << std::endl;
  220 +
  221 + if (isBroadcasting) {
  222 + AkLogDebug() << "Adding device slot:" << std::endl;
  223 + AkLogDebug() << " Device ID:" << msgBroadcast.device() << std::endl;
  224 + AkLogDebug() << " Client ID:" << clientId << std::endl;
  225 + AkLogDebug() << " Client PID:" << msgBroadcast.pid() << std::endl;
  226 +
  227 + this->m_broadcasts[msgBroadcast.device()] =
  228 + {{clientId, msgBroadcast.pid()}, {}, {}};
  229 + }
  230 +
  231 + AkLogDebug() << "Get slot" << std::endl;
  232 + auto &slot = this->m_broadcasts[msgBroadcast.device()];
  233 +
  234 + if (slot.broadcaster.pid == 0) {
  235 + AkLogDebug() << "Set client as broadcaster" << std::endl;
  236 + slot.broadcaster = {clientId, msgBroadcast.pid()};
  237 + }
  238 +
  239 + if (slot.broadcaster.pid == msgBroadcast.pid()
  240 + && slot.broadcaster.clientId == clientId) {
  241 + AkLogDebug() << "Save frame" << std::endl;
  242 + slot.frame = msgBroadcast.frame();
  243 + status = MsgStatus(0, inMessage.queryId());
  244 + this->m_frameAvailable.notify_all();
  245 + }
  246 +
  247 + this->m_peerMutex.unlock();
  248 +
  249 + AkLogDebug() << "Sending the response" << std::endl;
  250 + outMessage = status.toMessage();
  251 +
  252 + return status.status() == 0;
  253 +}
  254 +
  255 +bool AkVCam::ServicePrivate::listen(uint64_t clientId,
  256 + const Message &inMessage,
  257 + Message &outMessage)
  258 +{
  259 + AkLogFunction();
  260 + MsgListen msgListen(inMessage);
  261 + bool ok = false;
  262 +
  263 + this->m_peerMutex.lock();
  264 +
  265 + if (this->m_broadcasts.count(msgListen.device()) < 1)
  266 + this->m_broadcasts[msgListen.device()] = {};
  267 +
  268 + auto &slot = this->m_broadcasts[msgListen.device()];
  269 + slot.listeners.push_back({clientId, msgListen.pid()});
  270 +
  271 + if (!slot.frame)
  272 + this->m_frameAvailable.wait_for(this->m_peerMutex,
  273 + std::chrono::seconds(1));
  274 +
  275 + outMessage = MsgFrameReady(msgListen.device(),
  276 + slot.frame,
  277 + slot.broadcaster.pid != 0,
  278 + inMessage.queryId()).toMessage();
  279 + slot.frame = {};
  280 + ok = true;
  281 + this->m_peerMutex.unlock();
  282 +
  283 + return ok;
  284 +}
  1 +/* akvirtualcamera, virtual camera for Mac and Windows.
  2 + * Copyright (C) 2020 Gonzalo Exequiel Pedone
  3 + *
  4 + * akvirtualcamera is free software: you can redistribute it and/or modify
  5 + * it under the terms of the GNU General Public License as published by
  6 + * the Free Software Foundation, either version 3 of the License, or
  7 + * (at your option) any later version.
  8 + *
  9 + * akvirtualcamera is distributed in the hope that it will be useful,
  10 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 + * GNU General Public License for more details.
  13 + *
  14 + * You should have received a copy of the GNU General Public License
  15 + * along with akvirtualcamera. If not, see <http://www.gnu.org/licenses/>.
  16 + *
  17 + * Web-Site: http://webcamoid.github.io/
  18 + */
  19 +
  20 +#ifndef SERVICE_H
  21 +#define SERVICE_H
  22 +
  23 +namespace AkVCam
  24 +{
  25 + class ServicePrivate;
  26 +
  27 + class Service
  28 + {
  29 + public:
  30 + Service();
  31 + ~Service();
  32 +
  33 + int run();
  34 + void stop();
  35 +
  36 + private:
  37 + ServicePrivate *d;
  38 + };
  39 +}
  40 +
  41 +#endif // SERVICE_H
  1 +# akvirtualcamera, virtual camera for Mac and Windows.
  2 +# Copyright (C) 2021 Gonzalo Exequiel Pedone
  3 +#
  4 +# akvirtualcamera is free software: you can redistribute it and/or modify
  5 +# it under the terms of the GNU General Public License as published by
  6 +# the Free Software Foundation, either version 3 of the License, or
  7 +# (at your option) any later version.
  8 +#
  9 +# akvirtualcamera is distributed in the hope that it will be useful,
  10 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 +# GNU General Public License for more details.
  13 +#
  14 +# You should have received a copy of the GNU General Public License
  15 +# along with akvirtualcamera. If not, see <http://www.gnu.org/licenses/>.
  16 +#
  17 +# Web-Site: http://webcamoid.github.io/
  18 +
  19 +cmake_minimum_required(VERSION 3.14)
  20 +
  21 +project(VCamUtils LANGUAGES CXX)
  22 +
  23 +include(../commons.cmake)
  24 +
  25 +set(CMAKE_INCLUDE_CURRENT_DIR ON)
  26 +set(CMAKE_CXX_STANDARD 11)
  27 +set(CMAKE_CXX_STANDARD_REQUIRED ON)
  28 +
  29 +add_library(VCamUtils STATIC
  30 + src/color.h
  31 + src/fraction.cpp
  32 + src/fraction.h
  33 + src/ipcbridge.h
  34 + src/logger.cpp
  35 + src/logger.h
  36 + src/message.cpp
  37 + src/message.h
  38 + src/messageclient.cpp
  39 + src/messageclient.h
  40 + src/messageserver.cpp
  41 + src/messageserver.h
  42 + src/servicemsg.h
  43 + src/settings.cpp
  44 + src/settings.h
  45 + src/sockets.cpp
  46 + src/sockets.h
  47 + src/timer.cpp
  48 + src/timer.h
  49 + src/utils.cpp
  50 + src/utils.h
  51 + src/videoformat.cpp
  52 + src/videoformat.h
  53 + src/videoformattypes.h
  54 + src/videoframe.cpp
  55 + src/videoframe.h
  56 + src/videoframetypes.h)
  57 +
  58 +if (WIN32)
  59 + target_link_libraries(VCamUtils
  60 + ws2_32)
  61 +endif ()
  62 +
  63 +target_compile_definitions(VCamUtils PRIVATE VCAMUTILS_LIBRARY)
  1 +/* akvirtualcamera, virtual camera for Mac and Windows.
  2 + * Copyright (C) 2020 Gonzalo Exequiel Pedone
  3 + *
  4 + * akvirtualcamera is free software: you can redistribute it and/or modify
  5 + * it under the terms of the GNU General Public License as published by
  6 + * the Free Software Foundation, either version 3 of the License, or
  7 + * (at your option) any later version.
  8 + *
  9 + * akvirtualcamera is distributed in the hope that it will be useful,
  10 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 + * GNU General Public License for more details.
  13 + *
  14 + * You should have received a copy of the GNU General Public License
  15 + * along with akvirtualcamera. If not, see <http://www.gnu.org/licenses/>.
  16 + *
  17 + * Web-Site: http://webcamoid.github.io/
  18 + */
  19 +
  20 +#ifndef AKVCAMUTILS_COLOR_H
  21 +#define AKVCAMUTILS_COLOR_H
  22 +
  23 +#include <cstdint>
  24 +
  25 +namespace AkVCam
  26 +{
  27 + namespace Color
  28 + {
  29 + inline uint32_t rgb(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
  30 + {
  31 + return (a << 24) | (r << 16) | (g << 8) | b;
  32 + }
  33 +
  34 + inline uint32_t red(uint32_t rgba)
  35 + {
  36 + return (rgba >> 16) & 0xff;
  37 + }
  38 +
  39 + inline uint32_t green(uint32_t rgba)
  40 + {
  41 + return (rgba >> 8) & 0xff;
  42 + }
  43 +
  44 + inline uint32_t blue(uint32_t rgba)
  45 + {
  46 + return rgba & 0xff;
  47 + }
  48 +
  49 + inline uint32_t alpha(uint32_t rgba)
  50 + {
  51 + return rgba >> 24;
  52 + }
  53 + }
  54 +}
  55 +
  56 +#endif // AKVCAMUTILS_COLOR_H
  1 +/* akvirtualcamera, virtual camera for Mac and Windows.
  2 + * Copyright (C) 2020 Gonzalo Exequiel Pedone
  3 + *
  4 + * akvirtualcamera is free software: you can redistribute it and/or modify
  5 + * it under the terms of the GNU General Public License as published by
  6 + * the Free Software Foundation, either version 3 of the License, or
  7 + * (at your option) any later version.
  8 + *
  9 + * akvirtualcamera is distributed in the hope that it will be useful,
  10 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 + * GNU General Public License for more details.
  13 + *
  14 + * You should have received a copy of the GNU General Public License
  15 + * along with akvirtualcamera. If not, see <http://www.gnu.org/licenses/>.
  16 + *
  17 + * Web-Site: http://webcamoid.github.io/
  18 + */
  19 +
  20 +#include <cmath>
  21 +#include <cstdlib>
  22 +#include <sstream>
  23 +#include <string>
  24 +
  25 +#include "fraction.h"
  26 +#include "utils.h"
  27 +
  28 +namespace AkVCam
  29 +{
  30 + class FractionPrivate
  31 + {
  32 + public:
  33 + int64_t m_num;
  34 + int64_t m_den;
  35 +
  36 + template<typename T> inline static int sign(T value)
  37 + {
  38 + return value < 0? -1: 1;
  39 + }
  40 + };
  41 +}
  42 +
  43 +AkVCam::Fraction::Fraction()
  44 +{
  45 + this->d = new FractionPrivate;
  46 + this->d->m_num = 0;
  47 + this->d->m_den = 0;
  48 +}
  49 +
  50 +AkVCam::Fraction::Fraction(int64_t num, int64_t den)
  51 +{
  52 + this->d = new FractionPrivate;
  53 + this->d->m_num = num;
  54 + this->d->m_den = den;
  55 +}
  56 +
  57 +AkVCam::Fraction::Fraction(const std::string &str)
  58 +{
  59 + this->d = new FractionPrivate;
  60 + this->d->m_num = 0;
  61 + this->d->m_den = 1;
  62 + auto pos = str.find('/');
  63 +
  64 + if (pos == std::string::npos) {
  65 + auto strCpy = trimmed(str);
  66 + this->d->m_num = uint32_t(strtol(strCpy.c_str(), nullptr, 10));
  67 + } else {
  68 + auto numStr = trimmed(str.substr(0, pos));
  69 + auto denStr = trimmed(str.substr(pos + 1));
  70 +
  71 + this->d->m_num = uint32_t(strtol(numStr.c_str(), nullptr, 10));
  72 + this->d->m_den = uint32_t(strtol(denStr.c_str(), nullptr, 10));
  73 +
  74 + if (this->d->m_den < 1) {
  75 + this->d->m_num = 0;
  76 + this->d->m_den = 1;
  77 + }
  78 + }
  79 +}
  80 +
  81 +AkVCam::Fraction::Fraction(const Fraction &other)
  82 +{
  83 + this->d = new FractionPrivate;
  84 + this->d->m_num = other.d->m_num;
  85 + this->d->m_den = other.d->m_den;
  86 +}
  87 +
  88 +AkVCam::Fraction::~Fraction()
  89 +{
  90 + delete this->d;
  91 +}
  92 +
  93 +AkVCam::Fraction &AkVCam::Fraction::operator =(const Fraction &other)
  94 +{
  95 + if (this != &other) {
  96 + this->d->m_num = other.d->m_num;
  97 + this->d->m_den = other.d->m_den;
  98 + }
  99 +
  100 + return *this;
  101 +}
  102 +
  103 +bool AkVCam::Fraction::operator ==(const Fraction &other) const
  104 +{
  105 + if (this->d->m_den == 0 && other.d->m_den != 0)
  106 + return false;
  107 +
  108 + if (this->d->m_den != 0 && other.d->m_den == 0)
  109 + return false;
  110 +
  111 + return this->d->m_num * other.d->m_den == this->d->m_den * other.d->m_num;
  112 +}
  113 +
  114 +bool AkVCam::Fraction::operator <(const Fraction &other) const
  115 +{
  116 + return this->d->m_num * other.d->m_den < this->d->m_den * other.d->m_num;
  117 +}
  118 +
  119 +int64_t AkVCam::Fraction::num() const
  120 +{
  121 + return this->d->m_num;
  122 +}
  123 +
  124 +int64_t &AkVCam::Fraction::num()
  125 +{
  126 + return this->d->m_num;
  127 +}
  128 +
  129 +int64_t AkVCam::Fraction::den() const
  130 +{
  131 + return this->d->m_den;
  132 +}
  133 +
  134 +int64_t &AkVCam::Fraction::den()
  135 +{
  136 + return this->d->m_den;
  137 +}
  138 +
  139 +double AkVCam::Fraction::value() const
  140 +{
  141 + return double(this->d->m_num) / double(this->d->m_den);
  142 +}
  143 +
  144 +std::string AkVCam::Fraction::toString() const
  145 +{
  146 + std::stringstream ss;
  147 + ss << this->d->m_num << '/' << this->d->m_den;
  148 +
  149 + return ss.str();
  150 +}
  151 +
  152 +bool AkVCam::Fraction::isInfinity() const
  153 +{
  154 + return this->d->m_num != 0 && this->d->m_den == 0;
  155 +}
  156 +
  157 +int AkVCam::Fraction::sign() const
  158 +{
  159 + return FractionPrivate::sign(this->d->m_num) == FractionPrivate::sign(this->d->m_den)? 1: -1;
  160 +}
  161 +
  162 +bool AkVCam::Fraction::isFraction(const std::string &str)
  163 +{
  164 + auto pos = str.find('/');
  165 +
  166 + if (pos == std::string::npos) {
  167 + auto strCpy = trimmed(str);
  168 + char *p = nullptr;
  169 + strtol(strCpy.c_str(), &p, 10);
  170 +
  171 + if (*p)
  172 + return false;
  173 + } else {
  174 + auto numStr = trimmed(str.substr(0, pos));
  175 + auto denStr = trimmed(str.substr(pos + 1));
  176 + char *p = nullptr;
  177 + char *q = nullptr;
  178 + strtol(numStr.c_str(), &p, 10);
  179 + strtol(denStr.c_str(), &q, 10);
  180 +
  181 + if (*p || *q)
  182 + return false;
  183 + }
  184 +
  185 + return true;
  186 +}
  187 +
  188 +std::ostream &operator <<(std::ostream &os, const AkVCam::Fraction &fraction)
  189 +{
  190 + os << fraction.toString();
  191 +
  192 + return os;
  193 +}
  1 +/* akvirtualcamera, virtual camera for Mac and Windows.
  2 + * Copyright (C) 2020 Gonzalo Exequiel Pedone
  3 + *
  4 + * akvirtualcamera is free software: you can redistribute it and/or modify
  5 + * it under the terms of the GNU General Public License as published by
  6 + * the Free Software Foundation, either version 3 of the License, or
  7 + * (at your option) any later version.
  8 + *
  9 + * akvirtualcamera is distributed in the hope that it will be useful,
  10 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 + * GNU General Public License for more details.
  13 + *
  14 + * You should have received a copy of the GNU General Public License
  15 + * along with akvirtualcamera. If not, see <http://www.gnu.org/licenses/>.
  16 + *
  17 + * Web-Site: http://webcamoid.github.io/
  18 + */
  19 +
  20 +#ifndef FRACTION_H
  21 +#define FRACTION_H
  22 +
  23 +#include <cstdint>
  24 +#include <string>
  25 +
  26 +namespace AkVCam
  27 +{
  28 + class Fraction;
  29 + class FractionPrivate;
  30 + using FractionRange = std::pair<Fraction, Fraction>;
  31 +
  32 + class Fraction
  33 + {
  34 + public:
  35 + Fraction();
  36 + Fraction(int64_t num, int64_t den);
  37 + Fraction(const std::string &str);
  38 + Fraction(const Fraction &other);
  39 + virtual ~Fraction();
  40 + Fraction &operator =(const Fraction &other);
  41 + bool operator ==(const Fraction &other) const;
  42 + bool operator <(const Fraction &other) const;
  43 +
  44 + int64_t num() const;
  45 + int64_t &num();
  46 + int64_t den() const;
  47 + int64_t &den();
  48 + double value() const;
  49 + std::string toString() const;
  50 + bool isInfinity() const;
  51 + int sign() const;
  52 + static bool isFraction(const std::string &str);
  53 +
  54 + private:
  55 + FractionPrivate *d;
  56 + };
  57 +}
  58 +
  59 +std::ostream &operator <<(std::ostream &os, const AkVCam::Fraction &fraction);
  60 +
  61 +#endif // FRACTION_H