Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 1 | /* |
Guillaume Roguez | a5ded29 | 2017-01-03 14:55:42 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2017 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 | |
| 36 | namespace ring { namespace video { |
| 37 | |
| 38 | class VideoDeviceImpl { |
| 39 | public: |
| 40 | /** |
| 41 | * @throw std::runtime_error |
| 42 | */ |
| 43 | VideoDeviceImpl(const std::string& path); |
| 44 | |
| 45 | std::string device; |
| 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_; |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | VideoDeviceImpl::VideoDeviceImpl(const std::string& uniqueID) |
| 65 | : device(uniqueID) |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 66 | , current_size_(-1, -1) |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 67 | , avDevice_([AVCaptureDevice deviceWithUniqueID: |
| 68 | [NSString stringWithCString:uniqueID.c_str() encoding:[NSString defaultCStringEncoding]]]) |
| 69 | { |
| 70 | name = [[avDevice_ localizedName] UTF8String]; |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 71 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 72 | available_sizes_.reserve(avDevice_.formats.count); |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 73 | for (AVCaptureDeviceFormat* format in avDevice_.formats) { |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 74 | auto dimensions = CMVideoFormatDescriptionGetDimensions(format.formatDescription); |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 75 | available_sizes_.emplace_back(dimensions.width, dimensions.height); |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 76 | } |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 79 | VideoSize |
| 80 | VideoDeviceImpl::extractSize(VideoSize size) const |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 81 | { |
| 82 | for (const auto item : available_sizes_) { |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 83 | if (item.first == size.first && item.second == size.second) |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 84 | return item; |
| 85 | } |
| 86 | |
| 87 | // fallback to last size |
| 88 | if (!available_sizes_.empty()) { |
| 89 | return available_sizes_.back(); |
| 90 | } |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 91 | return VideoSize(0, 0); |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 92 | } |
| 93 | |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 94 | DeviceParams |
| 95 | VideoDeviceImpl::getDeviceParams() const |
| 96 | { |
| 97 | DeviceParams params; |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 98 | params.name = [[avDevice_ localizedName] UTF8String]; |
Alexandre Lision | d0cb337 | 2016-05-11 14:33:19 -0400 | [diff] [blame] | 99 | params.input = device; |
philippegorley | 1408483 | 2017-09-25 14:35:17 -0400 | [diff] [blame^] | 100 | params.format = "avfoundation"; |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 101 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 102 | params.width = current_size_.first; |
| 103 | params.height = current_size_.second; |
Alexandre Lision | e0676bf | 2015-12-08 10:35:27 -0500 | [diff] [blame] | 104 | |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 105 | auto format = [avDevice_ activeFormat]; |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 106 | auto frameRate = (AVFrameRateRange*) |
| 107 | [format.videoSupportedFrameRateRanges objectAtIndex:0]; |
| 108 | params.framerate = frameRate.maxFrameRate; |
| 109 | return params; |
| 110 | } |
| 111 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 112 | void |
| 113 | VideoDeviceImpl::setDeviceParams(const DeviceParams& params) |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 114 | { |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 115 | //TODO: add framerate |
| 116 | // rate_ = size_.getRate(settings["rate"]); |
| 117 | current_size_ = extractSize({params.width, params.height}); |
| 118 | } |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 119 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 120 | std::vector<VideoSize> |
| 121 | VideoDeviceImpl::getSizeList() const |
| 122 | { |
| 123 | return getSizeList("default"); |
| 124 | } |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 125 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 126 | std::vector<FrameRate> |
| 127 | VideoDeviceImpl::getRateList(const std::string& channel, VideoSize size) const |
| 128 | { |
Alexandre Lision | a6d408f | 2016-02-08 15:29:03 -0500 | [diff] [blame] | 129 | auto format = [avDevice_ activeFormat]; |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 130 | std::vector<FrameRate> v; |
| 131 | v.reserve(format.videoSupportedFrameRateRanges.count); |
| 132 | for (AVFrameRateRange* frameRateRange in format.videoSupportedFrameRateRanges) |
| 133 | v.emplace_back(frameRateRange.maxFrameRate); |
| 134 | return v; |
| 135 | } |
Alexandre Lision | a6d408f | 2016-02-08 15:29:03 -0500 | [diff] [blame] | 136 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 137 | std::vector<VideoSize> |
| 138 | VideoDeviceImpl::getSizeList(const std::string& channel) const |
| 139 | { |
| 140 | return available_sizes_; |
| 141 | } |
| 142 | |
| 143 | std::vector<std::string> |
| 144 | VideoDeviceImpl::getChannelList() const |
| 145 | { |
| 146 | return {"default"}; |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 147 | } |
| 148 | |
atraczyk | a1b8b13 | 2016-12-28 11:51:56 -0500 | [diff] [blame] | 149 | 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] | 150 | deviceImpl_(new VideoDeviceImpl(path)) |
| 151 | { |
| 152 | node_ = path; |
| 153 | name = deviceImpl_->name; |
| 154 | } |
| 155 | |
| 156 | DeviceParams |
| 157 | VideoDevice::getDeviceParams() const |
| 158 | { |
| 159 | return deviceImpl_->getDeviceParams(); |
| 160 | } |
| 161 | |
| 162 | void |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 163 | VideoDevice::setDeviceParams(const DeviceParams& params) |
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 | return deviceImpl_->setDeviceParams(params); |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | std::vector<std::string> |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 169 | VideoDevice::getChannelList() const |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 170 | { |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 171 | return deviceImpl_->getChannelList(); |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 172 | } |
| 173 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 174 | std::vector<VideoSize> |
| 175 | VideoDevice::getSizeList(const std::string& channel) const |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 176 | { |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 177 | return deviceImpl_->getSizeList(channel); |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 180 | std::vector<FrameRate> |
| 181 | VideoDevice::getRateList(const std::string& channel, VideoSize size) const |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 182 | { |
Adrien Béraud | bdd139c | 2016-02-17 13:52:44 -0500 | [diff] [blame] | 183 | return deviceImpl_->getRateList(channel, size); |
Alexandre Lision | be732d4 | 2015-02-18 11:48:42 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | VideoDevice::~VideoDevice() |
| 187 | {} |
| 188 | |
| 189 | }} // namespace ring::video |