camera_video_frame_handler.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_CAMERA_VIDEO_FRAME_HANDLER_H_
  5. #define ASH_CAPTURE_MODE_CAMERA_VIDEO_FRAME_HANDLER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/containers/flat_map.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "media/capture/mojom/video_capture_buffer.mojom-forward.h"
  12. #include "mojo/public/cpp/bindings/receiver.h"
  13. #include "mojo/public/cpp/bindings/remote.h"
  14. #include "services/video_capture/public/mojom/video_frame_handler.mojom.h"
  15. #include "services/video_capture/public/mojom/video_source.mojom.h"
  16. namespace media {
  17. class VideoFrame;
  18. } // namespace media
  19. namespace ash {
  20. // Defines an interface for an object that can hold and own a video buffer
  21. // handle, and able to extract a `VideoFrame` from the buffer when the frame
  22. // becomes ready in it. Concrete subclasses implements this frame extraction
  23. // differently based on the type of the buffer.
  24. class BufferHandleHolder {
  25. public:
  26. BufferHandleHolder(const BufferHandleHolder&) = delete;
  27. BufferHandleHolder& operator=(const BufferHandleHolder&) = delete;
  28. virtual ~BufferHandleHolder();
  29. // Creates and returns a concrete implementation of this interface that
  30. // matches the buffer type of the given `buffer_handle`.
  31. // We only support the `kGpuMemoryBuffer` and `kSharedMemory` buffer types.
  32. static std::unique_ptr<BufferHandleHolder> Create(
  33. media::mojom::VideoBufferHandlePtr buffer_handle);
  34. // Extracts and returns the ready video frame in the given `buffer`.
  35. virtual scoped_refptr<media::VideoFrame> OnFrameReadyInBuffer(
  36. video_capture::mojom::ReadyFrameInBufferPtr buffer) = 0;
  37. protected:
  38. BufferHandleHolder() = default;
  39. };
  40. // -----------------------------------------------------------------------------
  41. // Defines an object that will subscribe to a camera device, whose remote video
  42. // source is the given `camera_video_source`. It will handle the reception of
  43. // the video frames from that device and provide them to its `Delegate`.
  44. class CameraVideoFrameHandler : public video_capture::mojom::VideoFrameHandler {
  45. public:
  46. // Defines an interface for a delegate of this class, which will be provided
  47. // by the video frames received from the camera device.
  48. class Delegate {
  49. public:
  50. // Will be called on the UI thread whenever a video `frame` is received from
  51. // the camera device.
  52. virtual void OnCameraVideoFrame(scoped_refptr<media::VideoFrame> frame) = 0;
  53. protected:
  54. virtual ~Delegate() = default;
  55. };
  56. // Creates an instance of this class which will subscribe to the given
  57. // `camera_video_source` requesting to receive video frames of its feed with
  58. // the given `capture_format`. The video frames will then be provided to the
  59. // `delegate`.
  60. CameraVideoFrameHandler(
  61. Delegate* delegate,
  62. mojo::Remote<video_capture::mojom::VideoSource> camera_video_source,
  63. const media::VideoCaptureFormat& capture_format);
  64. CameraVideoFrameHandler(const CameraVideoFrameHandler&) = delete;
  65. CameraVideoFrameHandler& operator=(const CameraVideoFrameHandler&) = delete;
  66. ~CameraVideoFrameHandler() override;
  67. // Activates the subscription so we start receiving video frames.
  68. void StartHandlingFrames();
  69. // video_capture::mojom::VideoFrameHandler:
  70. void OnNewBuffer(int buffer_id,
  71. media::mojom::VideoBufferHandlePtr buffer_handle) override;
  72. void OnFrameAccessHandlerReady(
  73. mojo::PendingRemote<video_capture::mojom::VideoFrameAccessHandler>
  74. pending_frame_access_handler) override;
  75. void OnFrameReadyInBuffer(
  76. video_capture::mojom::ReadyFrameInBufferPtr buffer,
  77. std::vector<video_capture::mojom::ReadyFrameInBufferPtr> scaled_buffers)
  78. override;
  79. void OnBufferRetired(int buffer_id) override;
  80. void OnError(media::VideoCaptureError error) override;
  81. void OnFrameDropped(media::VideoCaptureFrameDropReason reason) override;
  82. void OnNewCropVersion(uint32_t crop_version) override;
  83. void OnFrameWithEmptyRegionCapture() override;
  84. void OnLog(const std::string& message) override;
  85. void OnStarted() override;
  86. void OnStartedUsingGpuDecode() override;
  87. void OnStopped() override;
  88. // The `kGpuMemoryBuffer` type is requested only when running on an actual
  89. // device. This allows force-requesting them even when running on
  90. // linux-chromeos for unit testing purposes.
  91. static void SetForceUseGpuMemoryBufferForTest(bool value);
  92. private:
  93. // Called when a video frame is destroyed, which was backed by a buffer whose
  94. // ID is the given `buffer_id`. This lets us inform the video capture
  95. // service's `VideoFrameAccessHandler` that we're done consuming this buffer
  96. // so it can be reused again by the video frames producer.
  97. void OnVideoFrameGone(int buffer_id);
  98. // Called when a fatal error is reported in `OnError()` or the mojo remote to
  99. // `VideoSource` gets disconnected.
  100. void OnFatalErrorOrDisconnection();
  101. Delegate* const delegate_;
  102. mojo::Receiver<video_capture::mojom::VideoFrameHandler>
  103. video_frame_handler_receiver_{this};
  104. // A remote bound to the camera's `VideoSource` implementation in the video
  105. // capture service.
  106. mojo::Remote<video_capture::mojom::VideoSource> camera_video_source_remote_;
  107. // A remote bound to the `PushVideoStreamSubscription` implementation in the
  108. // video capture service. Once this subscription is activated, we start
  109. // receiving video frames.
  110. mojo::Remote<video_capture::mojom::PushVideoStreamSubscription>
  111. camera_video_stream_subsciption_remote_;
  112. // A remote bound to the `VideoFrameAccessHandler` implementation in the video
  113. // capture service, which is used to signal that we're done consuming a buffer
  114. // so that the buffer can be reused by the frames producer.
  115. mojo::Remote<video_capture::mojom::VideoFrameAccessHandler>
  116. video_frame_access_handler_remote_;
  117. // Maps the `BufferHandleHolder`s by the `buffer_id`. An entry is inserted
  118. // when `OnNewBuffer()` is called, and removed when `OnBufferRetired()` is
  119. // called.
  120. base::flat_map</*buffer_id=*/int, std::unique_ptr<BufferHandleHolder>>
  121. buffer_map_;
  122. base::WeakPtrFactory<CameraVideoFrameHandler> weak_ptr_factory_{this};
  123. };
  124. } // namespace ash
  125. #endif // ASH_CAPTURE_MODE_CAMERA_VIDEO_FRAME_HANDLER_H_