video_capture_client.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2018 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 COMPONENTS_MIRRORING_SERVICE_VIDEO_CAPTURE_CLIENT_H_
  5. #define COMPONENTS_MIRRORING_SERVICE_VIDEO_CAPTURE_CLIENT_H_
  6. #include "base/callback.h"
  7. #include "base/component_export.h"
  8. #include "base/containers/flat_map.h"
  9. #include "base/memory/shared_memory_mapping.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/sequence_checker.h"
  12. #include "base/time/time.h"
  13. #include "media/capture/mojom/video_capture.mojom.h"
  14. #include "mojo/public/cpp/bindings/pending_remote.h"
  15. #include "mojo/public/cpp/bindings/receiver.h"
  16. #include "mojo/public/cpp/bindings/remote.h"
  17. #include "third_party/abseil-cpp/absl/types/variant.h"
  18. namespace media {
  19. class VideoFrame;
  20. class VideoFramePool;
  21. struct VideoCaptureFeedback;
  22. } // namespace media
  23. namespace mirroring {
  24. // On Start(), this class connects to |host| through the
  25. // media::mojom::VideoCaptureHost interface and requests to launch a video
  26. // capture device. After the device is started, the captured video frames are
  27. // received through the media::mojom::VideoCaptureObserver interface.
  28. class COMPONENT_EXPORT(MIRRORING_SERVICE) VideoCaptureClient
  29. : public media::mojom::VideoCaptureObserver {
  30. public:
  31. VideoCaptureClient(const media::VideoCaptureParams& params,
  32. mojo::PendingRemote<media::mojom::VideoCaptureHost> host);
  33. VideoCaptureClient(const VideoCaptureClient&) = delete;
  34. VideoCaptureClient& operator=(const VideoCaptureClient&) = delete;
  35. ~VideoCaptureClient() override;
  36. using FrameDeliverCallback = base::RepeatingCallback<void(
  37. scoped_refptr<media::VideoFrame> video_frame)>;
  38. void Start(FrameDeliverCallback deliver_callback,
  39. base::OnceClosure error_callback);
  40. void Stop();
  41. // Will stop delivering frames on this call.
  42. void Pause();
  43. void Resume(FrameDeliverCallback deliver_callback);
  44. // Feedback callback.
  45. void ProcessFeedback(const media::VideoCaptureFeedback& feedback);
  46. // Requests to receive a refreshed captured video frame. Do nothing if the
  47. // capturing device is not started or the capturing is paused.
  48. void RequestRefreshFrame();
  49. // media::mojom::VideoCaptureObserver implementations.
  50. void OnStateChanged(media::mojom::VideoCaptureResultPtr result) override;
  51. void OnNewBuffer(int32_t buffer_id,
  52. media::mojom::VideoBufferHandlePtr buffer_handle) override;
  53. void OnBufferReady(
  54. media::mojom::ReadyBufferPtr buffer,
  55. std::vector<media::mojom::ReadyBufferPtr> scaled_buffers) override;
  56. void OnBufferDestroyed(int32_t buffer_id) override;
  57. void OnNewCropVersion(uint32_t crop_version) override;
  58. private:
  59. using BufferFinishedCallback = base::OnceCallback<void()>;
  60. // Called by the VideoFrame destructor.
  61. static void DidFinishConsumingFrame(BufferFinishedCallback callback);
  62. // Reports the utilization to release the buffer for potential reuse.
  63. using MappingKeepAlive = absl::variant<absl::monostate,
  64. base::WritableSharedMemoryMapping,
  65. base::ReadOnlySharedMemoryMapping>;
  66. void OnClientBufferFinished(int buffer_id,
  67. MappingKeepAlive mapping_keep_alive);
  68. const media::VideoCaptureParams params_;
  69. const mojo::Remote<media::mojom::VideoCaptureHost> video_capture_host_;
  70. // Called when capturing failed to start.
  71. base::OnceClosure error_callback_;
  72. mojo::Receiver<media::mojom::VideoCaptureObserver> receiver_{this};
  73. // TODO(crbug.com/843117): Store the base::ReadOnlySharedMemoryRegion instead
  74. // after migrating the media::VideoCaptureDeviceClient to the new shared
  75. // memory API.
  76. using ClientBufferMap =
  77. base::flat_map<int32_t, media::mojom::VideoBufferHandlePtr>;
  78. // Stores the buffer handler on OnBufferCreated(). |buffer_id| is the key.
  79. ClientBufferMap client_buffers_;
  80. // The reference time for the first frame. Used to calculate the timestamp of
  81. // the captured frame if not provided in the frame info.
  82. base::TimeTicks first_frame_ref_time_;
  83. // The callback to deliver the received frame.
  84. FrameDeliverCallback frame_deliver_callback_;
  85. // Latest received feedback.
  86. media::VideoCaptureFeedback feedback_;
  87. // Cast Streaming does not support NV12 frames. When NV12 frames are received,
  88. // these structures are used to convert them to I420 on the CPU.
  89. // https://crbug.com/1206325
  90. std::unique_ptr<media::VideoFramePool> nv12_to_i420_pool_;
  91. std::vector<uint8_t> nv12_to_i420_tmp_buf_;
  92. SEQUENCE_CHECKER(sequence_checker_);
  93. base::WeakPtrFactory<VideoCaptureClient> weak_factory_{this};
  94. };
  95. } // namespace mirroring
  96. #endif // COMPONENTS_MIRRORING_SERVICE_VIDEO_CAPTURE_CLIENT_H_