Ring Daemon 16.0.0
Loading...
Searching...
No Matches
iosvideo/video_device_impl.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 *
4 * This program 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 * This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#include <array>
19
20extern "C" {
21#include <libavutil/pixfmt.h>
22}
23
24#include "logger.h"
25#include "../video_device.h"
26
27#include "client/ring_signal.h"
28
29namespace jami {
30namespace video {
31
32typedef struct
33{
34 std::string name;
36} ios_fmt;
37
38static const std::array<ios_fmt, 4> ios_formats {ios_fmt {"RGBA", AV_PIX_FMT_RGBA},
39 ios_fmt {"BGRA", AV_PIX_FMT_BGRA},
40 ios_fmt {"YUV420P", AV_PIX_FMT_YUV420P}};
41
42class VideoDeviceImpl
43{
44public:
45 VideoDeviceImpl(const std::string& path,
46 const std::vector<std::map<std::string, std::string>>& devInfo);
47
48 std::string name;
49
51
54
55 std::vector<VideoSize> getSizeList() const;
56 std::vector<FrameRate> getRateList() const;
57
58private:
59 VideoSize getSize(VideoSize size) const;
60 FrameRate getRate(FrameRate rate) const;
61
62 std::vector<std::string> formats_ {};
63 std::vector<VideoSize> sizes_ {};
64 std::vector<FrameRate> rates_ {};
65
66 const ios_fmt* fmt_ {nullptr};
67 VideoSize size_ {};
68 FrameRate rate_ {};
69};
70
71void
72VideoDeviceImpl::selectFormat()
73{
74 unsigned best = UINT_MAX;
75 for (auto fmt : formats_) {
76 auto f = ios_formats.begin();
77 for (; f != ios_formats.end(); ++f) {
78 if (f->name == fmt) {
79 auto pos = std::distance(ios_formats.begin(), f);
80 if (pos < best)
81 best = pos;
82 break;
83 }
84 }
85 if (f == ios_formats.end())
86 JAMI_WARN("Video: No format matching %s", fmt.c_str());
87 }
88
89 if (best != UINT_MAX) {
90 fmt_ = &ios_formats[best];
91 JAMI_DBG("Video: picked format %s", fmt_->name.c_str());
92 } else {
93 fmt_ = &ios_formats[0];
94 JAMI_ERR("Video: Unable to find a known format to use");
95 }
96}
97
98VideoDeviceImpl::VideoDeviceImpl(const std::string& path,
99 const std::vector<std::map<std::string, std::string>>& devInfo)
100 : name(path)
101{
102 for (auto& setting : devInfo) {
103 formats_.emplace_back(setting.at("format"));
104 sizes_.emplace_back(std::stoi(setting.at("width")), std::stoi(setting.at("height")));
105 rates_.emplace_back(std::stoi(setting.at("rate")), 1);
106 }
107 selectFormat();
108}
109
111VideoDeviceImpl::getSize(VideoSize size) const
112{
113 for (const auto& iter : sizes_) {
114 if (iter == size)
115 return iter;
116 }
117
118 return sizes_.empty() ? VideoSize {0, 0} : sizes_.back();
119}
120
122VideoDeviceImpl::getRate(FrameRate rate) const
123{
124 for (const auto& iter : rates_) {
125 if (iter == rate)
126 return iter;
127 }
128
129 return rates_.empty() ? FrameRate {0, 0} : rates_.back();
130}
131
132std::vector<VideoSize>
134{
135 return sizes_;
136}
137
138std::vector<FrameRate>
140{
141 return rates_;
142}
143
144DeviceParams
146{
147 DeviceParams params;
148 params.format = std::to_string(fmt_->pixfmt);
149 params.name = name;
150 params.input = name;
151 params.unique_id = name;
152 params.channel = 0;
153 params.pixel_format = "nv12";
154 params.width = size_.first;
155 params.height = size_.second;
156 params.framerate = rate_;
157
158 return params;
159}
160
161void
162VideoDeviceImpl::setDeviceParams(const DeviceParams& params)
163{
164 size_ = getSize({params.width, params.height});
165 rate_ = getRate(params.framerate);
167}
168
169VideoDevice::VideoDevice(const std::string& path,
170 const std::vector<std::map<std::string, std::string>>& devInfo)
171 : deviceImpl_(new VideoDeviceImpl(path, devInfo))
172{
173 id_ = path;
174 name = deviceImpl_->name;
175}
176
177DeviceParams
179{
180 return deviceImpl_->getDeviceParams();
181}
182
183void
184VideoDevice::setDeviceParams(const DeviceParams& params)
185{
186 return deviceImpl_->setDeviceParams(params);
187}
188
189std::vector<std::string>
191{
192 return {"default"};
193}
194
195std::vector<VideoSize>
196VideoDevice::getSizeList(const std::string& channel) const
197{
198 return deviceImpl_->getSizeList();
199}
200
201std::vector<FrameRate>
202VideoDevice::getRateList(const std::string& channel, VideoSize size) const
203{
204 return deviceImpl_->getRateList();
205}
206
208
209} // namespace video
210} // namespace jami
std::vector< VideoSize > getSizeList() const
void setDeviceParams(const DeviceParams &)
DeviceParams getDeviceParams() const
std::vector< FrameRate > getRateList() const
std::vector< std::string > getChannelList() const
VideoDevice(const std::string &path, const std::vector< std::map< std::string, std::string > > &devInfo)
DeviceParams getDeviceParams() const
Returns the parameters needed for actual use of the device.
#define JAMI_ERR(...)
Definition logger.h:218
#define JAMI_DBG(...)
Definition logger.h:216
#define JAMI_WARN(...)
Definition logger.h:217
static const std::array< ios_fmt, 4 > ios_formats
std::pair< unsigned, unsigned > VideoSize
rational< double > FrameRate
void emitSignal(Args... args)
Definition ring_signal.h:64
DeviceParams Parameters used by MediaDecoder and MediaEncoder to open a LibAV device/stream.
std::string format