summaryrefslogtreecommitdiff
path: root/chromium/media/capture/video/fuchsia/video_capture_device_factory_fuchsia.cc
blob: b41314befe4ac38c5f977c1822f6e663fec917d9 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/capture/video/fuchsia/video_capture_device_factory_fuchsia.h"

#include <lib/sys/cpp/component_context.h>

#include "base/check_op.h"
#include "base/fuchsia/fuchsia_logging.h"
#include "base/fuchsia/process_context.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/system/system_monitor.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "media/capture/video/fuchsia/video_capture_device_fuchsia.h"

namespace media {

// Helper class that calls GetIdentifier() and GetConfigurations() for a
// fuchsia::camera3::Device instance and stores the results afterwards.
class VideoCaptureDeviceFactoryFuchsia::DeviceConfigFetcher {
 public:
  DeviceConfigFetcher(uint64_t device_id, fuchsia::camera3::DevicePtr device)
      : device_id_(device_id), device_(std::move(device)) {
    device_.set_error_handler(
        fit::bind_member(this, &DeviceConfigFetcher::OnError));
  }

  void Fetch(base::OnceClosure on_fetched_callback) {
    DCHECK(device_);
    DCHECK(!have_results());
    DCHECK(!on_fetched_callback_);

    on_fetched_callback_ = std::move(on_fetched_callback);
    device_->GetIdentifier(
        fit::bind_member(this, &DeviceConfigFetcher::OnDescription));
    device_->GetConfigurations(
        fit::bind_member(this, &DeviceConfigFetcher::OnConfigurations));
  }

  ~DeviceConfigFetcher() = default;

  DeviceConfigFetcher(const DeviceConfigFetcher&) = delete;
  const DeviceConfigFetcher& operator=(const DeviceConfigFetcher&) = delete;

  bool is_pending() const { return !!device_; }
  bool have_results() const { return description_ && formats_; }
  bool is_usable() const { return device_ || have_results(); }

  uint64_t device_id() const { return device_id_; }
  const std::string& description() const { return description_.value(); }
  const VideoCaptureFormats& formats() const { return formats_.value(); }

 private:
  void OnError(zx_status_t status) {
    ZX_LOG(ERROR, status) << "fuchsia.camera3.Device disconnected";

    if (on_fetched_callback_)
      std::move(on_fetched_callback_).Run();
  }

  void OnDescription(fidl::StringPtr identifier) {
    description_ = identifier.value_or("");
    MaybeSignalDone();
  }

  void OnConfigurations(std::vector<fuchsia::camera3::Configuration> configs) {
    VideoCaptureFormats formats;
    for (auto& config : configs) {
      for (auto& props : config.streams) {
        VideoCaptureFormat format;
        format.frame_size = gfx::Size(props.image_format.display_width,
                                      props.image_format.display_height);
        format.frame_rate = static_cast<float>(props.frame_rate.numerator) /
                            props.frame_rate.denominator;
        format.pixel_format =
            VideoCaptureDeviceFuchsia::GetConvertedPixelFormat(
                props.image_format.pixel_format.type);
        if (format.pixel_format == PIXEL_FORMAT_UNKNOWN)
          continue;
        formats.push_back(format);
      }
    }

    formats_ = std::move(formats);
    MaybeSignalDone();
  }

  void MaybeSignalDone() {
    if (!have_results())
      return;

    device_.Unbind();

    std::move(on_fetched_callback_).Run();
  }

  uint64_t device_id_;
  fuchsia::camera3::DevicePtr device_;
  absl::optional<std::string> description_;
  absl::optional<VideoCaptureFormats> formats_;
  base::OnceClosure on_fetched_callback_;
};

VideoCaptureDeviceFactoryFuchsia::VideoCaptureDeviceFactoryFuchsia() {
  DETACH_FROM_THREAD(thread_checker_);
}

VideoCaptureDeviceFactoryFuchsia::~VideoCaptureDeviceFactoryFuchsia() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
}

VideoCaptureErrorOrDevice VideoCaptureDeviceFactoryFuchsia::CreateDevice(
    const VideoCaptureDeviceDescriptor& device_descriptor) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  uint64_t device_id;
  bool converted =
      base::StringToUint64(device_descriptor.device_id, &device_id);

  // Test may call CreateDevice() with an invalid |device_id|.
  if (!converted)
    return VideoCaptureErrorOrDevice(
        VideoCaptureError::
            kVideoCaptureControllerInvalidOrUnsupportedVideoCaptureParametersRequested);

  // CreateDevice() may be called before GetDeviceDescriptors(). Make sure
  // |device_watcher_| is initialized.
  if (!device_watcher_)
    Initialize();

  fidl::InterfaceHandle<fuchsia::camera3::Device> device;
  device_watcher_->ConnectToDevice(device_id, device.NewRequest());
  return VideoCaptureErrorOrDevice(
      std::make_unique<VideoCaptureDeviceFuchsia>(std::move(device)));
}

void VideoCaptureDeviceFactoryFuchsia::GetDevicesInfo(
    GetDevicesInfoCallback callback) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

  // If the list hasn't been received, then wait until it's received.
  if (!devices_ || num_pending_device_info_requests_) {
    pending_devices_info_requests_.push_back(std::move(callback));

    if (!device_watcher_)
      Initialize();

    return;
  }

  std::move(callback).Run(MakeDevicesInfo());
}

void VideoCaptureDeviceFactoryFuchsia::Initialize() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  DCHECK(!device_watcher_);
  DCHECK(!devices_);

  base::ComponentContextForProcess()->svc()->Connect(
      device_watcher_.NewRequest());

  device_watcher_.set_error_handler(fit::bind_member(
      this, &VideoCaptureDeviceFactoryFuchsia::OnDeviceWatcherDisconnected));

  WatchDevices();
}

void VideoCaptureDeviceFactoryFuchsia::OnDeviceWatcherDisconnected(
    zx_status_t status) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

  // CastRunner may close the channel with ZX_ERR_UNAVAILABLE error code when
  // none of the running applications have access to camera. No need to log the
  // error in that case.
  if (status != ZX_ERR_UNAVAILABLE)
    ZX_LOG(ERROR, status) << "fuchsia.camera3.DeviceWatcher disconnected.";

  // Clear the list of devices, so we don't report any camera devices while
  // DeviceWatcher is disconnected. We will try connecting DeviceWatcher again
  // when GetDevicesInfo() is called.
  devices_ = absl::nullopt;
  num_pending_device_info_requests_ = 0;

  MaybeResolvePendingDeviceInfoCallbacks();
}

void VideoCaptureDeviceFactoryFuchsia::WatchDevices() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

  device_watcher_->WatchDevices(fit::bind_member(
      this, &VideoCaptureDeviceFactoryFuchsia::OnWatchDevicesResult));
}

void VideoCaptureDeviceFactoryFuchsia::OnWatchDevicesResult(
    std::vector<fuchsia::camera3::WatchDevicesEvent> events) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

  if (!devices_)
    devices_.emplace();

  for (auto& e : events) {
    if (e.is_removed()) {
      auto it = devices_->find(e.removed());
      if (it == devices_->end()) {
        LOG(WARNING) << "Received device removed event for a device that "
                        "wasn't previously registered.";
        continue;
      }
      if (it->second->is_pending()) {
        // If the device info request was still pending then consider it
        // complete now. If this was the only device in pending state then all
        // callbacks will be resolved in
        // MaybeResolvePendingDeviceInfoCallbacks() called below.
        DCHECK_GT(num_pending_device_info_requests_, 0U);
        num_pending_device_info_requests_--;
      }
      devices_->erase(it);
      continue;
    }

    uint64_t id;
    if (e.is_added()) {
      id = e.added();
      if (devices_->find(id) != devices_->end()) {
        LOG(WARNING) << "Received device added event for a device that was "
                        "previously registered.";
        continue;
      }
    } else {
      id = e.existing();
      if (devices_->find(id) != devices_->end()) {
        continue;
      }
      LOG(WARNING) << "Received device exists event for a device that wasn't "
                      "previously registered.";
    }

    fuchsia::camera3::DevicePtr device;
    device_watcher_->ConnectToDevice(id, device.NewRequest());

    auto fetcher = std::make_unique<DeviceConfigFetcher>(id, std::move(device));

    // base::Unretained() is safe because the |fetcher| is owned by |this|.
    fetcher->Fetch(
        base::BindOnce(&VideoCaptureDeviceFactoryFuchsia::OnDeviceInfoFetched,
                       base::Unretained(this)));
    num_pending_device_info_requests_++;

    devices_->emplace(id, std::move(fetcher));
  }

  // Watch for further updates.
  WatchDevices();

  // Notify system monitor that the list of the devices has changed, except if
  // this is the first WatchDevices() response we've received. The first
  // time WatchDevices() is called it responds immediately with the current list
  // of the devices, i.e. there is no need to emit notification in that case. If
  // the `device_watcher_` was disconnected and reconnected later then we still
  // want to emit the notification as the list could change while
  // `device_watcher_` was disconnected. There is no need to compare the content
  // of the list: `MediaDeviceManager` will notify applications only if the list
  // has actually changed.
  if (received_initial_list_) {
    auto* system_monitor = base::SystemMonitor::Get();
    if (system_monitor) {
      system_monitor->ProcessDevicesChanged(
          base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE);
    }
  } else {
    received_initial_list_ = true;
  }

  // Calls callbacks, which may delete |this|.
  MaybeResolvePendingDeviceInfoCallbacks();
}

void VideoCaptureDeviceFactoryFuchsia::OnDeviceInfoFetched() {
  DCHECK_GT(num_pending_device_info_requests_, 0U);
  num_pending_device_info_requests_--;
  MaybeResolvePendingDeviceInfoCallbacks();
}

std::vector<VideoCaptureDeviceInfo>
VideoCaptureDeviceFactoryFuchsia::MakeDevicesInfo() {
  std::vector<VideoCaptureDeviceInfo> devices_info;
  // Leave the list empty if |devices_| isn't set (e.g. after DeviceWatcher
  // has disconnected).
  if (devices_) {
    for (auto& device : devices_.value()) {
      DCHECK(!device.second->is_pending());
      if (device.second->is_usable()) {
        devices_info.emplace_back(VideoCaptureDeviceDescriptor(
            device.second->description(), base::NumberToString(device.first),
            VideoCaptureApi::FUCHSIA_CAMERA3));
        devices_info.back().supported_formats = device.second->formats();
      }
    }
  }
  return devices_info;
}

void VideoCaptureDeviceFactoryFuchsia::
    MaybeResolvePendingDeviceInfoCallbacks() {
  if (num_pending_device_info_requests_ > 0)
    return;

  std::vector<GetDevicesInfoCallback> callbacks;
  callbacks.swap(pending_devices_info_requests_);

  auto weak_this = weak_factory_.GetWeakPtr();
  for (auto& c : callbacks) {
    auto devices_info = MakeDevicesInfo();
    std::move(c).Run(devices_info);
    if (!weak_this)
      return;
  }
}

}  // namespace media