summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/video_capture/linux/device_info_pipewire.cc
blob: ad6cea57b847ebe9016bf4aeb63536b091de6b4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 *  Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "modules/video_capture/linux/device_info_pipewire.h"

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include <vector>

#include "modules/video_capture/video_capture.h"
#include "modules/video_capture/video_capture_defines.h"
#include "modules/video_capture/video_capture_impl.h"
#include "modules/video_capture/video_capture_options.h"
#include "rtc_base/logging.h"

namespace webrtc {
namespace videocapturemodule {
DeviceInfoPipeWire::DeviceInfoPipeWire(VideoCaptureOptions* options)
    : DeviceInfoImpl(), pipewire_session_(options->pipewire_session()) {}

int32_t DeviceInfoPipeWire::Init() {
  return 0;
}

DeviceInfoPipeWire::~DeviceInfoPipeWire() = default;

uint32_t DeviceInfoPipeWire::NumberOfDevices() {
  return pipewire_session_->nodes().size();
}

int32_t DeviceInfoPipeWire::GetDeviceName(uint32_t deviceNumber,
                                          char* deviceNameUTF8,
                                          uint32_t deviceNameLength,
                                          char* deviceUniqueIdUTF8,
                                          uint32_t deviceUniqueIdUTF8Length,
                                          char* productUniqueIdUTF8,
                                          uint32_t productUniqueIdUTF8Length,
                                          pid_t* pid,
                                          bool* deviceIsPlaceholder) {
  if (deviceNumber >= NumberOfDevices())
    return -1;

  const PipeWireNode& node = pipewire_session_->nodes().at(deviceNumber);

  if (deviceNameLength <= node.display_name().length()) {
    RTC_LOG(LS_INFO) << "deviceNameUTF8 buffer passed is too small";
    return -1;
  }
  if (deviceUniqueIdUTF8Length <= node.unique_id().length()) {
    RTC_LOG(LS_INFO) << "deviceUniqueIdUTF8 buffer passed is too small";
    return -1;
  }
  if (productUniqueIdUTF8 &&
      productUniqueIdUTF8Length <= node.model_id().length()) {
    RTC_LOG(LS_INFO) << "productUniqueIdUTF8 buffer passed is too small";
    return -1;
  }

  memset(deviceNameUTF8, 0, deviceNameLength);
  node.display_name().copy(deviceNameUTF8, deviceNameLength);

  memset(deviceUniqueIdUTF8, 0, deviceUniqueIdUTF8Length);
  node.unique_id().copy(deviceUniqueIdUTF8, deviceUniqueIdUTF8Length);

  if (productUniqueIdUTF8) {
    memset(productUniqueIdUTF8, 0, productUniqueIdUTF8Length);
    node.model_id().copy(productUniqueIdUTF8, productUniqueIdUTF8Length);
  }

  return 0;
}

int32_t DeviceInfoPipeWire::CreateCapabilityMap(
    const char* deviceUniqueIdUTF8) {
  for (auto& node : pipewire_session_->nodes()) {
    if (node.unique_id().compare(deviceUniqueIdUTF8) != 0)
      continue;

    _captureCapabilities = node.capabilities();
    _lastUsedDeviceNameLength = node.unique_id().length();
    _lastUsedDeviceName = static_cast<char*>(
        realloc(_lastUsedDeviceName, _lastUsedDeviceNameLength + 1));
    memcpy(_lastUsedDeviceName, deviceUniqueIdUTF8,
           _lastUsedDeviceNameLength + 1);
    return _captureCapabilities.size();
  }
  return -1;
}

int32_t DeviceInfoPipeWire::DisplayCaptureSettingsDialogBox(
    const char* /*deviceUniqueIdUTF8*/,
    const char* /*dialogTitleUTF8*/,
    void* /*parentWindow*/,
    uint32_t /*positionX*/,
    uint32_t /*positionY*/) {
  return -1;
}

}  // namespace videocapturemodule
}  // namespace webrtc