Ring Daemon
Loading...
Searching...
No Matches
video_device.h
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#pragma once
18
19#include "media/media_device.h"
20#include "video_base.h"
21#include "rational.h"
22
24#include "string_utils.h"
25#include "logger.h"
26
27#include <fmt/core.h>
28
29#include <cmath>
30#include <map>
31#include <memory>
32#include <string>
33#include <vector>
34#include <algorithm>
35
36namespace jami {
37namespace video {
38
39using VideoSize = std::pair<unsigned, unsigned>;
41static constexpr const char DEVICE_DESKTOP[] = "desktop";
42
43class VideoDeviceImpl;
44
46{
47public:
48 VideoDevice(const std::string& path, const std::vector<std::map<std::string, std::string>>& devInfo);
50
51 /*
52 * The device name, e.g. "Integrated Camera",
53 * actually used as the identifier.
54 */
55 std::string name {};
56
57 const std::string& getDeviceId() const { return id_; }
58
59 /*
60 * Get the 3 level deep tree of possible settings for the device.
61 * The levels are channels, sizes, and rates.
62 *
63 * The result map for the "Integrated Camera" looks like this:
64 *
65 * {'Camera 1': {'1280x720': ['10'],
66 * '320x240': ['30', '15'],
67 * '352x288': ['30', '15'],
68 * '424x240': ['30', '15'],
69 * '640x360': ['30', '15'],
70 * '640x480': ['30', '15'],
71 * '800x448': ['15'],
72 * '960x540': ['10']}}
73 */
75 {
77
78 for (const auto& chan : getChannelList())
79 for (const auto& size : getSizeList(chan)) {
80 std::string sz = fmt::format("{}x{}", size.first, size.second);
81 auto rates = getRateList(chan, size);
82 std::vector<std::string> rates_str {rates.size()};
83 std::transform(rates.begin(), rates.end(), rates_str.begin(), [](const FrameRate& r) {
84 return jami::to_string(r.real());
85 });
86 cap[chan][sz] = std::move(rates_str);
87 }
88
89 return cap;
90 }
91
92 /* Default setting is found by using following rules:
93 * - frame height <= 640 pixels
94 * - frame rate >= 10 fps
95 */
97 {
98 auto settings = getSettings();
99 auto channels = getChannelList();
100 if (channels.empty())
101 return {};
102 settings.channel = getChannelList().front();
103
104 VideoSize max_size {0, 0};
106
107 auto sizes = getSizeList(settings.channel);
108 for (auto& s : sizes) {
109 if (s.second > 640)
110 continue;
111 auto rates = getRateList(settings.channel, s);
112 if (rates.empty())
113 continue;
114 auto max_rate = *std::max_element(rates.begin(), rates.end());
115 if (max_rate < 10)
116 continue;
117 if (s.second > max_size.second || (s.second == max_size.second && s.first > max_size.first)) {
118 max_size = s;
120 }
121 }
122 if (max_size.second > 0) {
123 settings.video_size = fmt::format("{}x{}", max_size.first, max_size.second);
124 settings.framerate = jami::to_string(max_size_rate.real());
125 JAMI_WARNING("[{}] Default video settings: {}, {} FPS", name, settings.video_size, settings.framerate);
126 }
127
128 return settings;
129 }
130
131 /*
132 * Get the settings for the device.
133 */
135 {
136 auto params = getDeviceParams();
138 settings.name = name.empty() ? params.name : name;
139 settings.unique_id = params.unique_id;
140 settings.input = params.input;
141 settings.channel = params.channel_name;
142 settings.video_size = sizeToString(params.width, params.height);
143 settings.framerate = jami::to_string(params.framerate.real());
144 return settings;
145 }
146
147 /*
148 * Setup the device with the preferences listed in the "settings" map.
149 * The expected map should be similar to the result of getSettings().
150 *
151 * If a key is missing, a valid default value is choosen. Thus, calling
152 * this function with an empty map will reset the device to default.
153 */
155 {
157 params.name = settings.name;
158 params.input = settings.input;
159 params.unique_id = settings.unique_id;
160 params.channel_name = settings.channel;
161 auto size = sizeFromString(settings.channel, settings.video_size);
162 params.width = size.first;
163 params.height = size.second;
164 params.framerate = rateFromString(settings.channel, size, settings.framerate);
165 setDeviceParams(params);
166 }
167
168 void setOrientation(int orientation) { orientation_ = orientation; }
169
174 std::vector<std::string> getChannelList() const;
175
176private:
177 std::vector<VideoSize> getSizeList(const std::string& channel) const;
178 std::vector<FrameRate> getRateList(const std::string& channel, VideoSize size) const;
179
180 VideoSize sizeFromString(const std::string& channel, const std::string& size) const
181 {
182 auto size_list = getSizeList(channel);
183 for (const auto& s : size_list) {
184 if (sizeToString(s.first, s.second) == size)
185 return s;
186 }
187 return {0, 0};
188 }
189
190 std::string sizeToString(unsigned w, unsigned h) const { return fmt::format("{}x{}", w, h); }
191
192 FrameRate rateFromString(const std::string& channel, VideoSize size, const std::string& rate) const
193 {
194 FrameRate closest {0};
195 double rate_val = 0;
196 try {
197 rate_val = rate.empty() ? 0 : std::stod(rate);
198 } catch (...) {
199 JAMI_WARN("Unable to read framerate \"%s\"", rate.c_str());
200 }
201 // fallback to framerate closest to 30 FPS
202 if (rate_val == 0)
203 rate_val = 30;
204 double closest_dist = std::numeric_limits<double>::max();
205 auto rate_list = getRateList(channel, size);
206 for (const auto& r : rate_list) {
207 double dist = std::fabs(r.real() - rate_val);
208 if (dist < closest_dist) {
209 closest = r;
211 }
212 }
213 return closest;
214 }
215
216 void setDeviceParams(const DeviceParams&);
217
218 /*
219 * The device node, e.g. "046d082dF41A2B3F".
220 */
221 std::string id_ {};
222
223 int orientation_ {0};
224
225 /*
226 * Device specific implementation.
227 * On Linux, V4L2 stuffs go there.
228 *
229 * Note: since a VideoDevice is copyable,
230 * deviceImpl_ cannot be an unique_ptr.
231 */
232 std::shared_ptr<VideoDeviceImpl> deviceImpl_;
233};
234
235} // namespace video
236} // namespace jami
VideoSettings getSettings() const
const std::string & getDeviceId() const
std::vector< std::string > getChannelList() const
void applySettings(const VideoSettings &settings)
VideoSettings getDefaultSettings() const
libjami::VideoCapabilities getCapabilities() const
DeviceParams getDeviceParams() const
Returns the parameters needed for actual use of the device.
void setOrientation(int orientation)
#define JAMI_WARN(...)
Definition logger.h:229
#define JAMI_WARNING(formatstr,...)
Definition logger.h:242
static constexpr const char DEVICE_DESKTOP[]
std::pair< unsigned, unsigned > VideoSize
rational< double > FrameRate
void emitSignal(Args... args)
Definition jami_signal.h:64
std::string to_string(double value)
std::map< std::string, std::map< std::string, std::vector< std::string > > > VideoCapabilities
DeviceParams Parameters used by MediaDecoder and MediaEncoder to open a LibAV device/stream.