fake_camera_device.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_CAPTURE_MODE_FAKE_CAMERA_DEVICE_H_
  5. #define ASH_CAPTURE_MODE_FAKE_CAMERA_DEVICE_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "base/containers/flat_map.h"
  9. #include "base/time/time.h"
  10. #include "base/timer/timer.h"
  11. #include "media/capture/video/video_capture_device_info.h"
  12. #include "media/capture/video_capture_types.h"
  13. #include "mojo/public/cpp/bindings/pending_remote.h"
  14. #include "mojo/public/cpp/bindings/receiver_set.h"
  15. #include "services/video_capture/public/mojom/video_frame_handler.mojom.h"
  16. #include "services/video_capture/public/mojom/video_source.mojom.h"
  17. #include "third_party/abseil-cpp/absl/types/optional.h"
  18. #include "third_party/skia/include/core/SkBitmap.h"
  19. namespace ash {
  20. // A fake implementation of the `VideoSource` mojo API that represents a fake
  21. // camera in ash_unittests.
  22. class ASH_EXPORT FakeCameraDevice
  23. : public video_capture::mojom::VideoSource,
  24. public video_capture::mojom::VideoFrameAccessHandler {
  25. public:
  26. explicit FakeCameraDevice(const media::VideoCaptureDeviceInfo& device_info);
  27. FakeCameraDevice(const FakeCameraDevice&) = delete;
  28. FakeCameraDevice& operator=(const FakeCameraDevice&) = delete;
  29. ~FakeCameraDevice() override;
  30. // The max size of the `buffer_pool_` which contains the buffers backing the
  31. // video frames produced by this device.
  32. static constexpr size_t kMaxBufferCount = 4;
  33. // Returns the same bitmap used to paint the produced video frames from this
  34. // camera device for purposes of comparing them with the received frames in
  35. // tests.
  36. static SkBitmap GetProducedFrameAsBitmap(const gfx::Size& frame_size);
  37. const media::VideoCaptureDeviceInfo& device_info() const {
  38. return device_info_;
  39. }
  40. // Binds the given `pending_receiver` to this instance.
  41. void Bind(mojo::PendingReceiver<video_capture::mojom::VideoSource>
  42. pending_receiver);
  43. // Simulates a fatal error on this camera device, by sending an error of type
  44. // `kCrosHalV3DeviceDelegateMojoConnectionError` to the `VideoFrameHandler`.
  45. // See https://crbug.com/1316230 for more details.
  46. void TriggerFatalError();
  47. // video_capture::mojom::VideoSource:
  48. void CreatePushSubscription(
  49. mojo::PendingRemote<video_capture::mojom::VideoFrameHandler> subscriber,
  50. const media::VideoCaptureParams& requested_settings,
  51. bool force_reopen_with_new_settings,
  52. mojo::PendingReceiver<video_capture::mojom::PushVideoStreamSubscription>
  53. subscription,
  54. CreatePushSubscriptionCallback callback) override;
  55. // video_capture::mojom::VideoFrameAccessHandler:
  56. void OnFinishedConsumingBuffer(int32_t buffer_id) override;
  57. private:
  58. class Buffer;
  59. class Subscription;
  60. // Creates a new pending remote whose receiver is bound to this instance. The
  61. // pending remote will be sent to a remote `VideoFrameHandler` so it can
  62. // notify us when done consuming a buffer.
  63. mojo::PendingRemote<video_capture::mojom::VideoFrameAccessHandler>
  64. GetNewAccessHandlerRemote();
  65. // Handles the case when a subscriber `VideoFrameHandler` gets disconnected
  66. // from this instance.
  67. void OnSubscriberDisconnected(Subscription* subscription);
  68. // Called when the subscription activation changes or the settings used to
  69. // open this device changes. If this if for a specific `subscription`, it will
  70. // provided (i.e not null).
  71. void OnSubscriptionActivationChanged(Subscription* subscription);
  72. // Returns true if any of the current subscriptions to this device are active,
  73. // and therefore should be provided with video frames.
  74. bool IsAnySubscriptionActive() const;
  75. // Called repeatedly on every tick of `frame_timer_` which depends on the
  76. // frame desired to open this device.
  77. void OnNextFrame();
  78. // Allocates a new buffer (and therefore notifies active subscribers with
  79. // `OnNewBuffer()`) or reuses an existing one. Either way, the buffer is
  80. // returned. If all buffers in the `buffer_pool_` are used, and there's no
  81. // room for another buffer, active-non-suspended subscribers are notfied with
  82. // `OnFrameDropped()`, and nullptr is returned.
  83. Buffer* AllocateOrReuseBuffer();
  84. // Clears all the buffers in `buffer_pool_` and notifies active subscribers
  85. // with `OnBufferRetired()`.
  86. void RetireAllBuffers();
  87. // The device info this camera device has.
  88. const media::VideoCaptureDeviceInfo device_info_;
  89. mojo::ReceiverSet<video_capture::mojom::VideoSource>
  90. video_source_receiver_set_;
  91. mojo::ReceiverSet<video_capture::mojom::VideoFrameAccessHandler>
  92. access_handler_receiver_set_;
  93. // Maps each owned subscription instance by its instance ptr.
  94. base::flat_map<Subscription*, std::unique_ptr<Subscription>>
  95. subscriptions_map_;
  96. // The current settings used to open this device. It's a nullopt until a
  97. // subscription is created to this device.
  98. absl::optional<media::VideoCaptureParams> current_settings_;
  99. // Maps each buffer by its buffer ID.
  100. base::flat_map</*buffer_id=*/int, std::unique_ptr<Buffer>> buffer_pool_;
  101. // The time at which this device started producing video frames.
  102. base::Time start_time_;
  103. // The timer that invokes `OnNextFrame()` repeatedly depending on the frame
  104. // rate requested to open this device.
  105. base::RepeatingTimer frame_timer_;
  106. };
  107. } // namespace ash
  108. #endif // ASH_CAPTURE_MODE_FAKE_CAMERA_DEVICE_H_