Ring Daemon
Loading...
Searching...
No Matches
iosvideo/video_device_impl.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2026 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/jami_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, const std::vector<std::map<std::string, std::string>>& devInfo);
46
47 std::string name;
48
50
53
54 std::vector<VideoSize> getSizeList() const;
55 std::vector<FrameRate> getRateList() const;
56
57private:
58 VideoSize getSize(VideoSize size) const;
59 FrameRate getRate(FrameRate rate) const;
60
61 std::vector<std::string> formats_ {};
62 std::vector<VideoSize> sizes_ {};
63 std::vector<FrameRate> rates_ {};
64
65 const ios_fmt* fmt_ {nullptr};
66 VideoSize size_ {};
67 FrameRate rate_ {};
68};
69
70void
71VideoDeviceImpl::selectFormat()
72{
73 unsigned best = UINT_MAX;
74 for (auto fmt : formats_) {
75 auto f = ios_formats.begin();
76 for (; f != ios_formats.end(); ++f) {
77 if (f->name == fmt) {
78 auto pos = std::distance(ios_formats.begin(), f);
79 if (pos < best)
80 best = pos;
81 break;
82 }
83 }
84 if (f == ios_formats.end())
85 JAMI_WARN("Video: No format matching %s", fmt.c_str());
86 }
87
88 if (best != UINT_MAX) {
89 fmt_ = &ios_formats[best];
90 JAMI_DBG("Video: picked format %s", fmt_->name.c_str());
91 } else {
92 fmt_ = &ios_formats[0];
93 JAMI_ERR("Video: Unable to find a known format to use");
94 }
95}
96
97VideoDeviceImpl::VideoDeviceImpl(const std::string& path, const std::vector<std::map<std::string, std::string>>& devInfo)
98 : name(path)
99{
100 for (auto& setting : devInfo) {
101 formats_.emplace_back(setting.at("format"));
102 sizes_.emplace_back(std::stoi(setting.at("width")), std::stoi(setting.at("height")));
103 rates_.emplace_back(std::stoi(setting.at("rate")), 1);
104 }
105 selectFormat();
106}
107
109VideoDeviceImpl::getSize(VideoSize size) const
110{
111 for (const auto& iter : sizes_) {
112 if (iter == size)
113 return iter;
114 }
115
116 return sizes_.empty() ? VideoSize {0, 0} : sizes_.back();
117}
118
120VideoDeviceImpl::getRate(FrameRate rate) const
121{
122 for (const auto& iter : rates_) {
123 if (iter == rate)
124 return iter;
125 }
126
127 return rates_.empty() ? FrameRate {0, 0} : rates_.back();
128}
129
130std::vector<VideoSize>
132{
133 return sizes_;
134}
135
136std::vector<FrameRate>
138{
139 return rates_;
140}
141
142DeviceParams
144{
145 DeviceParams params;
146 params.format = std::to_string(fmt_->pixfmt);
147 params.name = name;
148 params.input = name;
149 params.unique_id = name;
150 params.channel = 0;
151 params.pixel_format = "nv12";
152 params.width = size_.first;
153 params.height = size_.second;
154 params.framerate = rate_;
155
156 return params;
157}
158
159void
160VideoDeviceImpl::setDeviceParams(const DeviceParams& params)
161{
162 size_ = getSize({params.width, params.height});
163 rate_ = getRate(params.framerate);
165}
166
167VideoDevice::VideoDevice(const std::string& path, const std::vector<std::map<std::string, std::string>>& devInfo)
168 : deviceImpl_(new VideoDeviceImpl(path, devInfo))
169{
170 id_ = path;
171 name = deviceImpl_->name;
172}
173
174DeviceParams
176{
177 return deviceImpl_->getDeviceParams();
178}
179
180void
181VideoDevice::setDeviceParams(const DeviceParams& params)
182{
183 return deviceImpl_->setDeviceParams(params);
184}
185
186std::vector<std::string>
188{
189 return {"default"};
190}
191
192std::vector<VideoSize>
193VideoDevice::getSizeList(const std::string& channel) const
194{
195 return deviceImpl_->getSizeList();
196}
197
198std::vector<FrameRate>
199VideoDevice::getRateList(const std::string& channel, VideoSize size) const
200{
201 return deviceImpl_->getRateList();
202}
203
205
206} // namespace video
207} // 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:230
#define JAMI_DBG(...)
Definition logger.h:228
#define JAMI_WARN(...)
Definition logger.h:229
static const std::array< ios_fmt, 4 > ios_formats
std::pair< unsigned, unsigned > VideoSize
rational< double > FrameRate
void emitSignal(Args... args)
Definition jami_signal.h:64
DeviceParams Parameters used by MediaDecoder and MediaEncoder to open a LibAV device/stream.
std::string format