summaryrefslogtreecommitdiff
path: root/chromium/media/capture/video/fuchsia/video_capture_device_factory_fuchsia.h
blob: 72a380967216a7cdaf87f9900c7e79a8b3c6099c (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
// 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.

#ifndef MEDIA_CAPTURE_VIDEO_FUCHSIA_VIDEO_CAPTURE_DEVICE_FACTORY_FUCHSIA_H_
#define MEDIA_CAPTURE_VIDEO_FUCHSIA_VIDEO_CAPTURE_DEVICE_FACTORY_FUCHSIA_H_

#include <fuchsia/camera3/cpp/fidl.h>

#include <map>

#include "base/containers/small_map.h"
#include "media/capture/video/video_capture_device_factory.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace media {

class CAPTURE_EXPORT VideoCaptureDeviceFactoryFuchsia
    : public VideoCaptureDeviceFactory {
 public:
  VideoCaptureDeviceFactoryFuchsia();
  ~VideoCaptureDeviceFactoryFuchsia() override;

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

  // VideoCaptureDeviceFactory implementation.
  VideoCaptureErrorOrDevice CreateDevice(
      const VideoCaptureDeviceDescriptor& device_descriptor) override;
  void GetDevicesInfo(GetDevicesInfoCallback callback) override;

 private:
  // Helper class used to fetch per-device information.
  class DeviceConfigFetcher;

  void Initialize();

  void OnDeviceWatcherDisconnected(zx_status_t status);

  void WatchDevices();
  void OnWatchDevicesResult(
      std::vector<fuchsia::camera3::WatchDevicesEvent> events);

  void OnDeviceInfoFetched();

  std::vector<VideoCaptureDeviceInfo> MakeDevicesInfo();
  void MaybeResolvePendingDeviceInfoCallbacks();

  bool received_initial_list_ = false;

  fuchsia::camera3::DeviceWatcherPtr device_watcher_;

  // Current list of devices. Set to nullopt if the list hasn't been received
  // yet.
  absl::optional<
      base::small_map<std::map<uint64_t, std::unique_ptr<DeviceConfigFetcher>>>>
      devices_;

  size_t num_pending_device_info_requests_ = 0;

  std::vector<GetDevicesInfoCallback> pending_devices_info_requests_;

  base::WeakPtrFactory<VideoCaptureDeviceFactoryFuchsia> weak_factory_{this};
};

}  // namespace media

#endif  // MEDIA_CAPTURE_VIDEO_FUCHSIA_VIDEO_CAPTURE_DEVICE_FACTORY_FUCHSIA_H_