| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include <cstring> |
| 6 | #include <optional> |
| 7 | #include <string_view> |
| 8 | |
| 9 | #include "CallDevices.h" |
| 10 | #include "Logging.h" |
| 11 | #include "UserSettingsPage.h" |
| 12 | |
| 13 | #ifdef GSTREAMER_AVAILABLE |
| 14 | extern "C" |
| 15 | { |
| 16 | #include "gst/gst.h" |
| 17 | } |
| 18 | #endif |
| 19 | |
| 20 | CallDevices::CallDevices() |
| 21 | : QObject() |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | #ifdef GSTREAMER_AVAILABLE |
| 26 | namespace { |
| 27 | |
| 28 | struct AudioSource |
| 29 | { |
| 30 | std::string name; |
| 31 | GstDevice *device; |
| 32 | }; |
| 33 | |
| 34 | struct VideoSource |
| 35 | { |
| 36 | struct Caps |
| 37 | { |
| 38 | std::string resolution; |
| 39 | std::vector<std::string> frameRates; |
| 40 | }; |
| 41 | std::string name; |
| 42 | GstDevice *device; |
| 43 | std::vector<Caps> caps; |
| 44 | }; |
| 45 | |
| 46 | std::vector<AudioSource> audioSources_; |
| 47 | std::vector<VideoSource> videoSources_; |
| 48 | |
| 49 | using FrameRate = std::pair<int, int>; |
| 50 | std::optional<FrameRate> |
| 51 | getFrameRate(const GValue *value) |
| 52 | { |
| 53 | if (GST_VALUE_HOLDS_FRACTION(value)) { |
| 54 | gint num = gst_value_get_fraction_numerator(value); |
| 55 | gint den = gst_value_get_fraction_denominator(value); |
| 56 | return FrameRate{num, den}; |
| 57 | } |
| 58 | return std::nullopt; |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | addFrameRate(std::vector<std::string> &rates, const FrameRate &rate) |
| 63 | { |
| 64 | constexpr double minimumFrameRate = 15.0; |
| 65 | if (static_cast<double>(rate.first) / rate.second >= minimumFrameRate) |
| 66 | rates.push_back(std::to_string(rate.first) + "/" + std::to_string(rate.second)); |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | setDefaultDevice(bool isVideo) |
| 71 | { |
| 72 | auto settings = UserSettings::instance(); |
| 73 | if (isVideo && settings->camera().isEmpty()) { |
| 74 | const VideoSource &camera = videoSources_.front(); |
| 75 | settings->setCamera(QString::fromStdString(camera.name)); |
| 76 | settings->setCameraResolution(QString::fromStdString(camera.caps.front().resolution)); |
| 77 | settings->setCameraFrameRate( |
| 78 | QString::fromStdString(camera.caps.front().frameRates.front())); |
| 79 | } else if (!isVideo && settings->microphone().isEmpty()) { |
| 80 | settings->setMicrophone(QString::fromStdString(audioSources_.front().name)); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | addDevice(GstDevice *device) |
| 86 | { |
| 87 | if (!device) |
| 88 | return; |
| 89 | |
| 90 | gchar *name = gst_device_get_display_name(device); |
| 91 | gchar *type = gst_device_get_device_class(device); |
| 92 | bool isVideo = !std::strncmp(type, "Video", 5); |
| 93 | g_free(type); |
| 94 | nhlog::ui()->debug("WebRTC: {} device added: {}", isVideo ? "video" : "audio", name); |
| 95 | if (!isVideo) { |
| 96 | audioSources_.push_back({name, device}); |
| 97 | g_free(name); |
| 98 | setDefaultDevice(false); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | GstCaps *gstcaps = gst_device_get_caps(device); |
| 103 | if (!gstcaps) { |
| 104 | nhlog::ui()->debug("WebRTC: unable to get caps for {}", name); |
| 105 | g_free(name); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | VideoSource source{name, device, {}}; |
| 110 | g_free(name); |
| 111 | guint nCaps = gst_caps_get_size(gstcaps); |
| 112 | for (guint i = 0; i < nCaps; ++i) { |
| 113 | GstStructure *structure = gst_caps_get_structure(gstcaps, i); |
| 114 | const gchar *struct_name = gst_structure_get_name(structure); |
| 115 | if (!std::strcmp(struct_name, "video/x-raw")) { |
| 116 | gint widthpx, heightpx; |
| 117 | if (gst_structure_get(structure, |
| 118 | "width", |
| 119 | G_TYPE_INT, |
| 120 | &widthpx, |
| 121 | "height", |
| 122 | G_TYPE_INT, |
| 123 | &heightpx, |
| 124 | nullptr)) { |
| 125 | VideoSource::Caps caps; |
| 126 | caps.resolution = std::to_string(widthpx) + "x" + std::to_string(heightpx); |
| 127 | const GValue *value = gst_structure_get_value(structure, "framerate"); |
| 128 | if (auto fr = getFrameRate(value); fr) |
| 129 | addFrameRate(caps.frameRates, *fr); |
| 130 | else if (GST_VALUE_HOLDS_FRACTION_RANGE(value)) { |
| 131 | addFrameRate(caps.frameRates, |
| 132 | *getFrameRate(gst_value_get_fraction_range_min(value))); |
| 133 | addFrameRate(caps.frameRates, |
| 134 | *getFrameRate(gst_value_get_fraction_range_max(value))); |
| 135 | } else if (GST_VALUE_HOLDS_LIST(value)) { |
| 136 | guint nRates = gst_value_list_get_size(value); |
| 137 | for (guint j = 0; j < nRates; ++j) { |
| 138 | const GValue *rate = gst_value_list_get_value(value, j); |
| 139 | if (auto frate = getFrameRate(rate); frate) |
| 140 | addFrameRate(caps.frameRates, *frate); |
| 141 | } |
| 142 | } |
| 143 | if (!caps.frameRates.empty()) |
| 144 | source.caps.push_back(std::move(caps)); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | gst_caps_unref(gstcaps); |
| 149 | videoSources_.push_back(std::move(source)); |
| 150 | setDefaultDevice(true); |
| 151 | } |
| 152 | |
| 153 | template<typename T> |
| 154 | bool |
| 155 | removeDevice(T &sources, GstDevice *device, bool changed) |
| 156 | { |
| 157 | if (auto it = std::find_if( |
| 158 | sources.begin(), sources.end(), [device](const auto &s) { return s.device == device; }); |
| 159 | it != sources.end()) { |
| 160 | nhlog::ui()->debug("WebRTC: device {}: {}", (changed ? "changed" : "removed"), it->name); |
| 161 | gst_object_unref(device); |
| 162 | sources.erase(it); |
| 163 | return true; |
| 164 | } |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | removeDevice(GstDevice *device, bool changed) |
| 170 | { |
| 171 | if (device) { |
| 172 | if (removeDevice(audioSources_, device, changed) || |
| 173 | removeDevice(videoSources_, device, changed)) |
| 174 | return; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | gboolean |
| 179 | newBusMessage(GstBus *bus G_GNUC_UNUSED, GstMessage *msg, gpointer user_data G_GNUC_UNUSED) |
| 180 | { |
| 181 | switch (GST_MESSAGE_TYPE(msg)) { |
| 182 | case GST_MESSAGE_DEVICE_ADDED: { |
| 183 | GstDevice *device; |
| 184 | gst_message_parse_device_added(msg, &device); |
| 185 | addDevice(device); |
| 186 | emit CallDevices::instance().devicesChanged(); |
| 187 | break; |
| 188 | } |
| 189 | case GST_MESSAGE_DEVICE_REMOVED: { |
| 190 | GstDevice *device; |
| 191 | gst_message_parse_device_removed(msg, &device); |
| 192 | removeDevice(device, false); |
| 193 | emit CallDevices::instance().devicesChanged(); |
| 194 | break; |
| 195 | } |
| 196 | case GST_MESSAGE_DEVICE_CHANGED: { |
| 197 | GstDevice *device; |
| 198 | GstDevice *oldDevice; |
| 199 | gst_message_parse_device_changed(msg, &device, &oldDevice); |
| 200 | removeDevice(oldDevice, true); |
| 201 | addDevice(device); |
| 202 | break; |
| 203 | } |
| 204 | default: |
| 205 | break; |
| 206 | } |
| 207 | return TRUE; |
| 208 | } |
| 209 | |
| 210 | template<typename T> |
| 211 | std::vector<std::string> |
| 212 | deviceNames(T &sources, const std::string &defaultDevice) |
| 213 | { |
| 214 | std::vector<std::string> ret; |
| 215 | ret.reserve(sources.size()); |
| 216 | for (const auto &s : sources) |
| 217 | ret.push_back(s.name); |
| 218 | |
| 219 | // move default device to top of the list |
| 220 | if (auto it = std::find(ret.begin(), ret.end(), defaultDevice); it != ret.end()) |
| 221 | std::swap(ret.front(), *it); |
| 222 | |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | std::optional<VideoSource> |
| 227 | getVideoSource(const std::string &cameraName) |
| 228 | { |
| 229 | if (auto it = std::find_if(videoSources_.cbegin(), |
| 230 | videoSources_.cend(), |
| 231 | [&cameraName](const auto &s) { return s.name == cameraName; }); |
| 232 | it != videoSources_.cend()) { |
| 233 | return *it; |
| 234 | } |
| 235 | return std::nullopt; |
| 236 | } |
| 237 | |
| 238 | std::pair<int, int> |
| 239 | tokenise(std::string_view str, char delim) |
| 240 | { |
| 241 | std::pair<int, int> ret; |
| 242 | ret.first = std::atoi(str.data()); |
| 243 | auto pos = str.find_first_of(delim); |
| 244 | ret.second = std::atoi(str.data() + pos + 1); |
| 245 | return ret; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | static GstDeviceMonitor *monitor = nullptr; |
| 250 | |
| 251 | void |
| 252 | CallDevices::init() |
| 253 | { |
| 254 | if (!monitor) { |
| 255 | monitor = gst_device_monitor_new(); |
| 256 | GstCaps *caps = gst_caps_new_empty_simple("audio/x-raw"); |
| 257 | gst_device_monitor_add_filter(monitor, "Audio/Source", caps); |
| 258 | gst_device_monitor_add_filter(monitor, "Audio/Duplex", caps); |
| 259 | gst_caps_unref(caps); |
| 260 | caps = gst_caps_new_empty_simple("video/x-raw"); |
| 261 | gst_device_monitor_add_filter(monitor, "Video/Source", caps); |
| 262 | gst_device_monitor_add_filter(monitor, "Video/Duplex", caps); |
| 263 | gst_caps_unref(caps); |
| 264 | |
| 265 | GstBus *bus = gst_device_monitor_get_bus(monitor); |
| 266 | gst_bus_add_watch(bus, newBusMessage, nullptr); |
| 267 | gst_object_unref(bus); |
| 268 | if (!gst_device_monitor_start(monitor)) { |
| 269 | nhlog::ui()->error("WebRTC: failed to start device monitor"); |
| 270 | return; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void |
| 276 | CallDevices::deinit() |
| 277 | { |
| 278 | if (monitor) { |
| 279 | gst_device_monitor_stop(monitor); |
| 280 | gst_object_unref(monitor); |
| 281 | monitor = nullptr; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | bool |
| 286 | CallDevices::haveMic() const |
| 287 | { |
| 288 | return !audioSources_.empty(); |
| 289 | } |
| 290 | |
| 291 | bool |
| 292 | CallDevices::haveCamera() const |
| 293 | { |
| 294 | return !videoSources_.empty(); |
| 295 | } |
| 296 | |
| 297 | std::vector<std::string> |
| 298 | CallDevices::names(bool isVideo, const std::string &defaultDevice) const |
| 299 | { |
| 300 | return isVideo ? deviceNames(videoSources_, defaultDevice) |
| 301 | : deviceNames(audioSources_, defaultDevice); |
| 302 | } |
| 303 | |
| 304 | std::vector<std::string> |
| 305 | CallDevices::resolutions(const std::string &cameraName) const |
| 306 | { |
| 307 | std::vector<std::string> ret; |
| 308 | if (auto s = getVideoSource(cameraName); s) { |
| 309 | ret.reserve(s->caps.size()); |
| 310 | for (const auto &c : s->caps) |
| 311 | ret.push_back(c.resolution); |
| 312 | } |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | std::vector<std::string> |
| 317 | CallDevices::frameRates(const std::string &cameraName, const std::string &resolution) const |
| 318 | { |
| 319 | if (auto s = getVideoSource(cameraName); s) { |
| 320 | if (auto it = std::find_if(s->caps.cbegin(), |
| 321 | s->caps.cend(), |
| 322 | [&](const auto &c) { return c.resolution == resolution; }); |
| 323 | it != s->caps.cend()) |
| 324 | return it->frameRates; |
| 325 | } |
| 326 | return {}; |
| 327 | } |
| 328 | |
| 329 | GstDevice * |
| 330 | CallDevices::audioDevice() const |
| 331 | { |
| 332 | std::string name = UserSettings::instance()->microphone().toStdString(); |
| 333 | if (auto it = std::find_if(audioSources_.cbegin(), |
| 334 | audioSources_.cend(), |
| 335 | [&name](const auto &s) { return s.name == name; }); |
| 336 | it != audioSources_.cend()) { |
| 337 | nhlog::ui()->debug("WebRTC: microphone: {}", name); |
| 338 | return it->device; |
| 339 | } else { |
| 340 | nhlog::ui()->error("WebRTC: unknown microphone: {}", name); |
| 341 | return nullptr; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | GstDevice * |
| 346 | CallDevices::videoDevice(std::pair<int, int> &resolution, std::pair<int, int> &frameRate) const |
| 347 | { |
| 348 | auto settings = UserSettings::instance(); |
| 349 | std::string name = settings->camera().toStdString(); |
| 350 | if (auto s = getVideoSource(name); s) { |
| 351 | nhlog::ui()->debug("WebRTC: camera: {}", name); |
| 352 | resolution = tokenise(settings->cameraResolution().toStdString(), 'x'); |
| 353 | frameRate = tokenise(settings->cameraFrameRate().toStdString(), '/'); |
| 354 | nhlog::ui()->debug("WebRTC: camera resolution: {}x{}", resolution.first, resolution.second); |
| 355 | nhlog::ui()->debug("WebRTC: camera frame rate: {}/{}", frameRate.first, frameRate.second); |
| 356 | return s->device; |
| 357 | } else { |
| 358 | nhlog::ui()->error("WebRTC: unknown camera: {}", name); |
| 359 | return nullptr; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | #else |
| 364 | |
| 365 | bool |
| 366 | CallDevices::haveMic() const |
| 367 | { |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | bool |
| 372 | CallDevices::haveCamera() const |
| 373 | { |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | std::vector<std::string> |
| 378 | CallDevices::names(bool, const std::string &) const |
| 379 | { |
| 380 | return {}; |
| 381 | } |
| 382 | |
| 383 | std::vector<std::string> |
| 384 | CallDevices::resolutions(const std::string &) const |
| 385 | { |
| 386 | return {}; |
| 387 | } |
| 388 | |
| 389 | std::vector<std::string> |
| 390 | CallDevices::frameRates(const std::string &, const std::string &) const |
| 391 | { |
| 392 | return {}; |
| 393 | } |
| 394 | |
| 395 | #endif |
| 396 | |
| 397 | #include "moc_CallDevices.cpp" |