Line data Source code
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 :
23 : #include "videomanager_interface.h"
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 :
36 : namespace jami {
37 : namespace video {
38 :
39 : using VideoSize = std::pair<unsigned, unsigned>;
40 : using FrameRate = rational<double>;
41 : static constexpr const char DEVICE_DESKTOP[] = "desktop";
42 :
43 : class VideoDeviceImpl;
44 :
45 : class VideoDevice
46 : {
47 : public:
48 : VideoDevice(const std::string& path, const std::vector<std::map<std::string, std::string>>& devInfo);
49 : ~VideoDevice();
50 :
51 : /*
52 : * The device name, e.g. "Integrated Camera",
53 : * actually used as the identifier.
54 : */
55 : std::string name {};
56 :
57 1639 : 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 : */
74 0 : libjami::VideoCapabilities getCapabilities() const
75 : {
76 0 : libjami::VideoCapabilities cap;
77 :
78 0 : for (const auto& chan : getChannelList())
79 0 : for (const auto& size : getSizeList(chan)) {
80 0 : std::string sz = fmt::format("{}x{}", size.first, size.second);
81 0 : auto rates = getRateList(chan, size);
82 0 : std::vector<std::string> rates_str {rates.size()};
83 0 : std::transform(rates.begin(), rates.end(), rates_str.begin(), [](const FrameRate& r) {
84 0 : return jami::to_string(r.real());
85 : });
86 0 : cap[chan][sz] = std::move(rates_str);
87 0 : }
88 :
89 0 : return cap;
90 0 : }
91 :
92 : /* Default setting is found by using following rules:
93 : * - frame height <= 640 pixels
94 : * - frame rate >= 10 fps
95 : */
96 31 : VideoSettings getDefaultSettings() const
97 : {
98 31 : auto settings = getSettings();
99 31 : auto channels = getChannelList();
100 31 : if (channels.empty())
101 0 : return {};
102 31 : settings.channel = getChannelList().front();
103 :
104 31 : VideoSize max_size {0, 0};
105 31 : FrameRate max_size_rate {0};
106 :
107 31 : auto sizes = getSizeList(settings.channel);
108 62 : for (auto& s : sizes) {
109 31 : if (s.second > 640)
110 0 : continue;
111 31 : auto rates = getRateList(settings.channel, s);
112 31 : if (rates.empty())
113 0 : continue;
114 31 : auto max_rate = *std::max_element(rates.begin(), rates.end());
115 31 : if (max_rate < 10)
116 0 : continue;
117 31 : if (s.second > max_size.second || (s.second == max_size.second && s.first > max_size.first)) {
118 0 : max_size = s;
119 0 : max_size_rate = max_rate;
120 : }
121 31 : }
122 31 : if (max_size.second > 0) {
123 0 : settings.video_size = fmt::format("{}x{}", max_size.first, max_size.second);
124 0 : settings.framerate = jami::to_string(max_size_rate.real());
125 0 : JAMI_WARNING("[{}] Default video settings: {}, {} FPS", name, settings.video_size, settings.framerate);
126 : }
127 :
128 31 : return settings;
129 31 : }
130 :
131 : /*
132 : * Get the settings for the device.
133 : */
134 62 : VideoSettings getSettings() const
135 : {
136 62 : auto params = getDeviceParams();
137 62 : VideoSettings settings;
138 62 : settings.name = name.empty() ? params.name : name;
139 62 : settings.unique_id = params.unique_id;
140 62 : settings.input = params.input;
141 62 : settings.channel = params.channel_name;
142 62 : settings.video_size = sizeToString(params.width, params.height);
143 62 : settings.framerate = jami::to_string(params.framerate.real());
144 124 : return settings;
145 62 : }
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 : */
154 59 : void applySettings(const VideoSettings& settings)
155 : {
156 59 : DeviceParams params {};
157 59 : params.name = settings.name;
158 59 : params.input = settings.input;
159 59 : params.unique_id = settings.unique_id;
160 59 : params.channel_name = settings.channel;
161 59 : auto size = sizeFromString(settings.channel, settings.video_size);
162 59 : params.width = size.first;
163 59 : params.height = size.second;
164 59 : params.framerate = rateFromString(settings.channel, size, settings.framerate);
165 59 : setDeviceParams(params);
166 59 : }
167 :
168 0 : void setOrientation(int orientation) { orientation_ = orientation; }
169 :
170 : /**
171 : * Returns the parameters needed for actual use of the device
172 : */
173 : DeviceParams getDeviceParams() const;
174 : std::vector<std::string> getChannelList() const;
175 :
176 : private:
177 : std::vector<VideoSize> getSizeList(const std::string& channel) const;
178 : std::vector<FrameRate> getRateList(const std::string& channel, VideoSize size) const;
179 :
180 59 : VideoSize sizeFromString(const std::string& channel, const std::string& size) const
181 : {
182 59 : auto size_list = getSizeList(channel);
183 59 : for (const auto& s : size_list) {
184 59 : if (sizeToString(s.first, s.second) == size)
185 59 : return s;
186 : }
187 0 : return {0, 0};
188 59 : }
189 :
190 242 : std::string sizeToString(unsigned w, unsigned h) const { return fmt::format("{}x{}", w, h); }
191 :
192 59 : FrameRate rateFromString(const std::string& channel, VideoSize size, const std::string& rate) const
193 : {
194 59 : FrameRate closest {0};
195 59 : double rate_val = 0;
196 : try {
197 59 : rate_val = rate.empty() ? 0 : std::stod(rate);
198 0 : } catch (...) {
199 0 : JAMI_WARN("Unable to read framerate \"%s\"", rate.c_str());
200 0 : }
201 : // fallback to framerate closest to 30 FPS
202 59 : if (rate_val == 0)
203 0 : rate_val = 30;
204 59 : double closest_dist = std::numeric_limits<double>::max();
205 59 : auto rate_list = getRateList(channel, size);
206 649 : for (const auto& r : rate_list) {
207 590 : double dist = std::fabs(r.real() - rate_val);
208 590 : if (dist < closest_dist) {
209 413 : closest = r;
210 413 : closest_dist = dist;
211 : }
212 : }
213 59 : return closest;
214 59 : }
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
|