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_; |
kkostiuk | ad7284d | 2021-11-26 17:36:02 -0500 | [diff] [blame^] | 64 | FrameRate desktopFrameRate_ = {30}; |
| 65 | std::vector<FrameRate> desktopFrameRates_ = {FrameRate(5), |
| 66 | FrameRate(10), |
| 67 | FrameRate(15), |
| 68 | FrameRate(20), |
| 69 | FrameRate(25), |
| 70 | FrameRate(30), |
| 71 | FrameRate(60), |
| 72 | FrameRate(120), |
| 73 | FrameRate(144)}; |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | VideoDeviceImpl::VideoDeviceImpl(const std::string& uniqueID) |
Andreas Traczyk | d6e2b8a | 2019-10-28 11:10:44 -0400 | [diff] [blame] | 77 | : id(uniqueID) |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 78 | , current_size_(-1, -1) |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 79 | , avDevice_([AVCaptureDevice deviceWithUniqueID: |
| 80 | [NSString stringWithCString:uniqueID.c_str() encoding:[NSString defaultCStringEncoding]]]) |
| 81 | { |
kkostiuk | ad7284d | 2021-11-26 17:36:02 -0500 | [diff] [blame^] | 82 | |
| 83 | if (id == DEVICE_DESKTOP) { |
| 84 | name = DEVICE_DESKTOP; |
| 85 | VideoSize size {0, 0}; |
| 86 | available_sizes_.emplace_back(size); |
| 87 | available_rates_[size] = desktopFrameRates_; |
| 88 | return; |
| 89 | } |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 90 | name = [[avDevice_ localizedName] UTF8String]; |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 91 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 92 | available_sizes_.reserve(avDevice_.formats.count); |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 93 | for (AVCaptureDeviceFormat* format in avDevice_.formats) { |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 94 | auto dimensions = CMVideoFormatDescriptionGetDimensions(format.formatDescription); |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 95 | available_sizes_.emplace_back(dimensions.width, dimensions.height); |
Kateryna Kostiuk | 08222f2 | 2019-02-12 14:43:56 -0500 | [diff] [blame] | 96 | std::vector<FrameRate> v; |
| 97 | v.reserve(format.videoSupportedFrameRateRanges.count); |
| 98 | for (AVFrameRateRange* frameRateRange in format.videoSupportedFrameRateRanges) { |
| 99 | if(std::find(v.begin(), v.end(), frameRateRange.maxFrameRate) == v.end()) { |
| 100 | v.emplace_back(frameRateRange.maxFrameRate); |
| 101 | } |
| 102 | } |
| 103 | // if we have multiple formats with the same resolution use video supported framerates from last one |
| 104 | // because this format will be selected by ffmpeg |
| 105 | if (available_rates_.find( VideoSize(dimensions.width, dimensions.height) ) == available_rates_.end()) { |
| 106 | available_rates_.emplace(VideoSize(dimensions.width, dimensions.height), v); |
| 107 | } else { |
| 108 | available_rates_.at(VideoSize(dimensions.width, dimensions.height)) = v; |
| 109 | } |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 110 | } |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 113 | VideoSize |
| 114 | VideoDeviceImpl::extractSize(VideoSize size) const |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 115 | { |
| 116 | for (const auto item : available_sizes_) { |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 117 | if (item.first == size.first && item.second == size.second) |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 118 | return item; |
| 119 | } |
| 120 | |
| 121 | // fallback to last size |
| 122 | if (!available_sizes_.empty()) { |
| 123 | return available_sizes_.back(); |
| 124 | } |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 125 | return VideoSize(0, 0); |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 126 | } |
| 127 | |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 128 | DeviceParams |
| 129 | VideoDeviceImpl::getDeviceParams() const |
| 130 | { |
| 131 | DeviceParams params; |
Andreas Traczyk | ae7c912 | 2020-02-19 12:28:08 -0500 | [diff] [blame] | 132 | params.unique_id = id; |
Kateryna Kostiuk | 5e34506 | 2019-11-09 16:34:15 -0500 | [diff] [blame] | 133 | params.input = id; |
kkostiuk | ad7284d | 2021-11-26 17:36:02 -0500 | [diff] [blame^] | 134 | if (id == DEVICE_DESKTOP) { |
| 135 | params.framerate = desktopFrameRate_; |
| 136 | return params; |
| 137 | } |
| 138 | params.name = [[avDevice_ localizedName] UTF8String]; |
Kateryna Kostiuk | 08222f2 | 2019-02-12 14:43:56 -0500 | [diff] [blame] | 139 | params.framerate = rate_; |
philippegorley | 1408483 | 2017-09-25 14:35:17 -0400 | [diff] [blame] | 140 | params.format = "avfoundation"; |
Kateryna Kostiuk | df6abf2 | 2019-07-09 14:48:39 -0400 | [diff] [blame] | 141 | params.pixel_format = "nv12"; |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 142 | params.width = current_size_.first; |
| 143 | params.height = current_size_.second; |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 144 | return params; |
| 145 | } |
| 146 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 147 | void |
| 148 | VideoDeviceImpl::setDeviceParams(const DeviceParams& params) |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 149 | { |
kkostiuk | ad7284d | 2021-11-26 17:36:02 -0500 | [diff] [blame^] | 150 | if (id == DEVICE_DESKTOP) { |
| 151 | name = DEVICE_DESKTOP; |
| 152 | desktopFrameRate_ = params.framerate; |
| 153 | return; |
| 154 | } |
Kateryna Kostiuk | 08222f2 | 2019-02-12 14:43:56 -0500 | [diff] [blame] | 155 | rate_ = params.framerate; |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 156 | current_size_ = extractSize({params.width, params.height}); |
| 157 | } |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 158 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 159 | std::vector<VideoSize> |
| 160 | VideoDeviceImpl::getSizeList() const |
| 161 | { |
| 162 | return getSizeList("default"); |
| 163 | } |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 164 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 165 | std::vector<FrameRate> |
| 166 | VideoDeviceImpl::getRateList(const std::string& channel, VideoSize size) const |
| 167 | { |
Kateryna Kostiuk | 08222f2 | 2019-02-12 14:43:56 -0500 | [diff] [blame] | 168 | return available_rates_.at(size); |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 169 | } |
Alexandre Lision | a6d408f | 2016-02-08 15:29:03 -0500 | [diff] [blame] | 170 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 171 | std::vector<VideoSize> |
| 172 | VideoDeviceImpl::getSizeList(const std::string& channel) const |
| 173 | { |
| 174 | return available_sizes_; |
| 175 | } |
| 176 | |
| 177 | std::vector<std::string> |
| 178 | VideoDeviceImpl::getChannelList() const |
| 179 | { |
| 180 | return {"default"}; |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 181 | } |
| 182 | |
atraczyk | a1b8b13 | 2016-12-28 11:51:56 -0500 | [diff] [blame] | 183 | 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] | 184 | deviceImpl_(new VideoDeviceImpl(path)) |
| 185 | { |
Andreas Traczyk | d6e2b8a | 2019-10-28 11:10:44 -0400 | [diff] [blame] | 186 | id_ = path; |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 187 | name = deviceImpl_->name; |
| 188 | } |
| 189 | |
| 190 | DeviceParams |
| 191 | VideoDevice::getDeviceParams() const |
| 192 | { |
| 193 | return deviceImpl_->getDeviceParams(); |
| 194 | } |
| 195 | |
| 196 | void |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 197 | VideoDevice::setDeviceParams(const DeviceParams& params) |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 198 | { |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 199 | return deviceImpl_->setDeviceParams(params); |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | std::vector<std::string> |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 203 | VideoDevice::getChannelList() const |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 204 | { |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 205 | return deviceImpl_->getChannelList(); |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 206 | } |
| 207 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 208 | std::vector<VideoSize> |
| 209 | VideoDevice::getSizeList(const std::string& channel) const |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 210 | { |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 211 | return deviceImpl_->getSizeList(channel); |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 212 | } |
| 213 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 214 | std::vector<FrameRate> |
| 215 | VideoDevice::getRateList(const std::string& channel, VideoSize size) const |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 216 | { |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 217 | return deviceImpl_->getRateList(channel, size); |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | VideoDevice::~VideoDevice() |
| 221 | {} |
| 222 | |
Adrien Béraud | 2130f06 | 2019-04-02 17:10:48 -0400 | [diff] [blame] | 223 | }} // namespace jami::video |