Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 1 | /* |
Sébastien Blin | 31fa5f1 | 2021-01-21 16:52:49 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2021 Savoir-faire Linux Inc. |
Guillaume Roguez | 5236ab0 | 2015-09-21 12:44:55 -0400 | [diff] [blame] | 3 | * |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 4 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <cassert> |
| 23 | #include <climits> |
| 24 | #include <map> |
| 25 | #include <string> |
| 26 | #include <sstream> |
| 27 | #include <stdexcept> |
| 28 | #include <string> |
| 29 | #include <vector> |
| 30 | |
| 31 | #include "logger.h" |
| 32 | #include "../video_device.h" |
| 33 | |
| 34 | #import <AVFoundation/AVFoundation.h> |
| 35 | |
Adrien Béraud | 2130f06 | 2019-04-02 17:10:48 -0400 | [diff] [blame] | 36 | namespace jami { namespace video { |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 37 | |
| 38 | class VideoDeviceImpl { |
| 39 | public: |
| 40 | /** |
| 41 | * @throw std::runtime_error |
| 42 | */ |
| 43 | VideoDeviceImpl(const std::string& path); |
| 44 | |
Andreas Traczyk | d6e2b8a | 2019-10-28 11:10:44 -0400 | [diff] [blame] | 45 | std::string id; |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 46 | std::string name; |
| 47 | |
| 48 | std::vector<std::string> getChannelList() const; |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 49 | std::vector<VideoSize> getSizeList(const std::string& channel) const; |
| 50 | std::vector<VideoSize> getSizeList() const; |
| 51 | std::vector<FrameRate> getRateList(const std::string& channel, VideoSize size) const; |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 52 | |
| 53 | DeviceParams getDeviceParams() const; |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 54 | void setDeviceParams(const DeviceParams&); |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 55 | |
| 56 | private: |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 57 | VideoSize extractSize(VideoSize) const; |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 58 | |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 59 | AVCaptureDevice* avDevice_; |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 60 | std::vector<VideoSize> available_sizes_; |
| 61 | VideoSize current_size_; |
Kateryna Kostiuk | 08222f2 | 2019-02-12 14:43:56 -0500 | [diff] [blame] | 62 | FrameRate rate_ {}; |
| 63 | std::map<VideoSize, std::vector<FrameRate>> available_rates_; |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | VideoDeviceImpl::VideoDeviceImpl(const std::string& uniqueID) |
Andreas Traczyk | d6e2b8a | 2019-10-28 11:10:44 -0400 | [diff] [blame] | 67 | : id(uniqueID) |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 68 | , current_size_(-1, -1) |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 69 | , avDevice_([AVCaptureDevice deviceWithUniqueID: |
| 70 | [NSString stringWithCString:uniqueID.c_str() encoding:[NSString defaultCStringEncoding]]]) |
| 71 | { |
| 72 | name = [[avDevice_ localizedName] UTF8String]; |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 73 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 74 | available_sizes_.reserve(avDevice_.formats.count); |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 75 | for (AVCaptureDeviceFormat* format in avDevice_.formats) { |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 76 | auto dimensions = CMVideoFormatDescriptionGetDimensions(format.formatDescription); |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 77 | available_sizes_.emplace_back(dimensions.width, dimensions.height); |
Kateryna Kostiuk | 08222f2 | 2019-02-12 14:43:56 -0500 | [diff] [blame] | 78 | std::vector<FrameRate> v; |
| 79 | v.reserve(format.videoSupportedFrameRateRanges.count); |
| 80 | for (AVFrameRateRange* frameRateRange in format.videoSupportedFrameRateRanges) { |
| 81 | if(std::find(v.begin(), v.end(), frameRateRange.maxFrameRate) == v.end()) { |
| 82 | v.emplace_back(frameRateRange.maxFrameRate); |
| 83 | } |
| 84 | } |
| 85 | // if we have multiple formats with the same resolution use video supported framerates from last one |
| 86 | // because this format will be selected by ffmpeg |
| 87 | if (available_rates_.find( VideoSize(dimensions.width, dimensions.height) ) == available_rates_.end()) { |
| 88 | available_rates_.emplace(VideoSize(dimensions.width, dimensions.height), v); |
| 89 | } else { |
| 90 | available_rates_.at(VideoSize(dimensions.width, dimensions.height)) = v; |
| 91 | } |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 92 | } |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 95 | VideoSize |
| 96 | VideoDeviceImpl::extractSize(VideoSize size) const |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 97 | { |
| 98 | for (const auto item : available_sizes_) { |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 99 | if (item.first == size.first && item.second == size.second) |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 100 | return item; |
| 101 | } |
| 102 | |
| 103 | // fallback to last size |
| 104 | if (!available_sizes_.empty()) { |
| 105 | return available_sizes_.back(); |
| 106 | } |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 107 | return VideoSize(0, 0); |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 110 | DeviceParams |
| 111 | VideoDeviceImpl::getDeviceParams() const |
| 112 | { |
| 113 | DeviceParams params; |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 114 | params.name = [[avDevice_ localizedName] UTF8String]; |
Andreas Traczyk | ae7c912 | 2020-02-19 12:28:08 -0500 | [diff] [blame] | 115 | params.unique_id = id; |
Kateryna Kostiuk | 5e34506 | 2019-11-09 16:34:15 -0500 | [diff] [blame] | 116 | params.input = id; |
Kateryna Kostiuk | 08222f2 | 2019-02-12 14:43:56 -0500 | [diff] [blame] | 117 | params.framerate = rate_; |
philippegorley | 1408483 | 2017-09-25 14:35:17 -0400 | [diff] [blame] | 118 | params.format = "avfoundation"; |
Kateryna Kostiuk | df6abf2 | 2019-07-09 14:48:39 -0400 | [diff] [blame] | 119 | params.pixel_format = "nv12"; |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 120 | params.width = current_size_.first; |
| 121 | params.height = current_size_.second; |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 122 | return params; |
| 123 | } |
| 124 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 125 | void |
| 126 | VideoDeviceImpl::setDeviceParams(const DeviceParams& params) |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 127 | { |
Kateryna Kostiuk | 08222f2 | 2019-02-12 14:43:56 -0500 | [diff] [blame] | 128 | rate_ = params.framerate; |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 129 | current_size_ = extractSize({params.width, params.height}); |
| 130 | } |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 131 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 132 | std::vector<VideoSize> |
| 133 | VideoDeviceImpl::getSizeList() const |
| 134 | { |
| 135 | return getSizeList("default"); |
| 136 | } |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 137 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 138 | std::vector<FrameRate> |
| 139 | VideoDeviceImpl::getRateList(const std::string& channel, VideoSize size) const |
| 140 | { |
Kateryna Kostiuk | 08222f2 | 2019-02-12 14:43:56 -0500 | [diff] [blame] | 141 | return available_rates_.at(size); |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 142 | } |
Alexandre Lision | a6d408f | 2016-02-08 15:29:03 -0500 | [diff] [blame] | 143 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 144 | std::vector<VideoSize> |
| 145 | VideoDeviceImpl::getSizeList(const std::string& channel) const |
| 146 | { |
| 147 | return available_sizes_; |
| 148 | } |
| 149 | |
| 150 | std::vector<std::string> |
| 151 | VideoDeviceImpl::getChannelList() const |
| 152 | { |
| 153 | return {"default"}; |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 154 | } |
| 155 | |
atraczyk | a1b8b13 | 2016-12-28 11:51:56 -0500 | [diff] [blame] | 156 | VideoDevice::VideoDevice(const std::string& path, const std::vector<std::map<std::string, std::string>>&) : |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 157 | deviceImpl_(new VideoDeviceImpl(path)) |
| 158 | { |
Andreas Traczyk | d6e2b8a | 2019-10-28 11:10:44 -0400 | [diff] [blame] | 159 | id_ = path; |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 160 | name = deviceImpl_->name; |
| 161 | } |
| 162 | |
| 163 | DeviceParams |
| 164 | VideoDevice::getDeviceParams() const |
| 165 | { |
| 166 | return deviceImpl_->getDeviceParams(); |
| 167 | } |
| 168 | |
| 169 | void |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 170 | VideoDevice::setDeviceParams(const DeviceParams& params) |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 171 | { |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 172 | return deviceImpl_->setDeviceParams(params); |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | std::vector<std::string> |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 176 | VideoDevice::getChannelList() const |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 177 | { |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 178 | return deviceImpl_->getChannelList(); |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 181 | std::vector<VideoSize> |
| 182 | VideoDevice::getSizeList(const std::string& channel) const |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 183 | { |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 184 | return deviceImpl_->getSizeList(channel); |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 185 | } |
| 186 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 187 | std::vector<FrameRate> |
| 188 | VideoDevice::getRateList(const std::string& channel, VideoSize size) const |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 189 | { |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 190 | return deviceImpl_->getRateList(channel, size); |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | VideoDevice::~VideoDevice() |
| 194 | {} |
| 195 | |
Adrien Béraud | 2130f06 | 2019-04-02 17:10:48 -0400 | [diff] [blame] | 196 | }} // namespace jami::video |