Ring Daemon 16.0.0
Loading...
Searching...
No Matches
androidvideo/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 "logger.h"
19#include "../video_device.h"
20
21#include "client/ring_signal.h"
22
23#include <algorithm>
24#include <map>
25#include <sstream>
26#include <stdexcept>
27#include <string>
28#include <vector>
29#include <array>
30
31namespace jami {
32namespace video {
33
34/*
35 * Array to match Android formats. List formats in ascending
36 * preferrence: the format with the lower index will be picked.
37 */
39{
40 int code;
41 std::string name;
43};
44
45static const std::array<android_fmt, 2> and_formats {
46 android_fmt {17, "NV21", AV_PIX_FMT_NV21},
47 android_fmt {842094169, "YUV420", AV_PIX_FMT_YUV420P},
48};
49
51{
52public:
56 VideoDeviceImpl(const std::string& path);
57
58 std::string name;
59
61 void setDeviceParams(const DeviceParams&);
62
63 std::vector<VideoSize> getSizeList() const;
64 std::vector<FrameRate> getRateList() const;
65
66private:
67 void selectFormat();
68 VideoSize getSize(const VideoSize& size) const;
69 FrameRate getRate(const FrameRate& rate) const;
70
71 std::vector<int> formats_ {};
72 std::vector<VideoSize> sizes_ {};
73 std::vector<FrameRate> rates_ {};
74
75 const android_fmt* fmt_ {nullptr};
76 VideoSize size_ {};
77 FrameRate rate_ {};
78};
79
80void
81VideoDeviceImpl::selectFormat()
82{
83 /*
84 * formats_ contains camera parameters as returned by the GetCameraInfo
85 * signal, find the matching Android formats
86 */
87 unsigned best = UINT_MAX;
88 for (auto fmt : formats_) {
89 auto f = and_formats.begin();
90 for (; f != and_formats.end(); ++f) {
91 if (f->code == fmt) {
92 auto pos = std::distance(and_formats.begin(), f);
93 if (pos < best)
94 best = pos;
95 break;
96 }
97 }
98 if (f == and_formats.end())
99 JAMI_WARN("AndroidVideo: No format matching %d", fmt);
100 }
101
102 if (best != UINT_MAX) {
103 fmt_ = &and_formats[best];
104 JAMI_DBG("AndroidVideo: picked format %s", fmt_->name.c_str());
105 } else {
106 fmt_ = &and_formats[0];
107 JAMI_ERR("AndroidVideo: Unable to find a known format to use");
108 }
109}
110
111VideoDeviceImpl::VideoDeviceImpl(const std::string& path)
112 : name(path)
113{
114 std::vector<unsigned> sizes;
115 std::vector<unsigned> rates;
116 formats_.reserve(16);
117 sizes.reserve(16);
118 rates.reserve(16);
120 for (size_t i = 0, n = sizes.size(); i < n; i += 2)
121 sizes_.emplace_back(sizes[i], sizes[i + 1]);
122 for (const auto& r : rates)
123 rates_.emplace_back(r, 1000);
124
125 selectFormat();
126}
127
129VideoDeviceImpl::getSize(const VideoSize& size) const
130{
131 for (const auto& iter : sizes_) {
132 if (iter == size)
133 return iter;
134 }
135
136 return sizes_.empty() ? VideoSize {0, 0} : sizes_.back();
137}
138
140VideoDeviceImpl::getRate(const FrameRate& rate) const
141{
142 for (const auto& iter : rates_) {
143 if (iter == rate)
144 return iter;
145 }
146
147 return rates_.empty() ? FrameRate {} : rates_.back();
148}
149
150std::vector<VideoSize>
152{
153 return sizes_;
154}
155
156std::vector<FrameRate>
158{
159 return rates_;
160}
161
164{
166 params.format = std::to_string(fmt_->ring_format);
167 params.unique_id = name;
168 params.name = name;
169 params.input = name;
170 params.channel = 0;
171 params.width = size_.first;
172 params.height = size_.second;
173 params.framerate = rate_;
174 return params;
175}
176
177void
179{
180 size_ = getSize({params.width, params.height});
181 rate_ = getRate(params.framerate);
183 fmt_->code,
184 size_.first,
185 size_.second,
186 rate_.real());
187}
188
189VideoDevice::VideoDevice(const std::string& id,
190 const std::vector<std::map<std::string, std::string>>&)
191 : deviceImpl_(new VideoDeviceImpl(id))
192{
193 id_ = id;
194 name = deviceImpl_->name;
195}
196
199{
200 return deviceImpl_->getDeviceParams();
201}
202
203void
204VideoDevice::setDeviceParams(const DeviceParams& params)
205{
206 return deviceImpl_->setDeviceParams(params);
207}
208
209std::vector<std::string>
211{
212 return {"default"};
213}
214
215std::vector<VideoSize>
216VideoDevice::getSizeList(const std::string& /* channel */) const
217{
218 return deviceImpl_->getSizeList();
219}
220
221std::vector<FrameRate>
222VideoDevice::getRateList(const std::string& /* channel */, VideoSize /* size */) const
223{
224 return deviceImpl_->getRateList();
225}
226
228
229} // namespace video
230} // namespace jami
std::vector< VideoSize > getSizeList() 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< android_fmt, 2 > and_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