Shridhar Sundarraj | 2af6bca | 2018-04-12 09:26:20 | [diff] [blame] | 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 | |
| 5 | #ifndef CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_VIDEO_CONSUMER_H_ |
| 6 | #define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_VIDEO_CONSUMER_H_ |
| 7 | |
Yuri Wiitala | fc5fe70 | 2018-06-20 06:14:00 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
Shridhar Sundarraj | 2af6bca | 2018-04-12 09:26:20 | [diff] [blame] | 10 | #include "base/time/time.h" |
Saman Sami | a4c3ee7 | 2018-05-25 19:38:07 | [diff] [blame] | 11 | #include "components/viz/host/client_frame_sink_video_capturer.h" |
Shridhar Sundarraj | 2af6bca | 2018-04-12 09:26:20 | [diff] [blame] | 12 | #include "content/common/content_export.h" |
Shridhar Sundarraj | 2af6bca | 2018-04-12 09:26:20 | [diff] [blame] | 13 | #include "ui/gfx/geometry/size.h" |
| 14 | |
| 15 | class SkBitmap; |
| 16 | |
| 17 | namespace content { |
| 18 | |
| 19 | // This class is the video consumer to FrameSinkVideoCapturerImpl. This class, |
| 20 | // in turn sends video frames to its host via the OnFrameCapturedCallback. Used |
| 21 | // when the VizDisplayCompositor feature is enabled. |
Saman Sami | a4c3ee7 | 2018-05-25 19:38:07 | [diff] [blame] | 22 | // TODO(crbug.com/846811): This class can probably be merged into |
| 23 | // viz::ClientFrameSinkVideoCapturer. |
Shridhar Sundarraj | 2af6bca | 2018-04-12 09:26:20 | [diff] [blame] | 24 | class CONTENT_EXPORT DevToolsVideoConsumer |
| 25 | : public viz::mojom::FrameSinkVideoConsumer { |
| 26 | public: |
| 27 | using OnFrameCapturedCallback = |
| 28 | base::RepeatingCallback<void(scoped_refptr<media::VideoFrame> frame)>; |
| 29 | |
| 30 | explicit DevToolsVideoConsumer(OnFrameCapturedCallback callback); |
| 31 | ~DevToolsVideoConsumer() override; |
| 32 | |
| 33 | // Copies |frame| onto a SkBitmap and returns it. |
| 34 | static SkBitmap GetSkBitmapFromFrame(scoped_refptr<media::VideoFrame> frame); |
| 35 | |
| 36 | // If not currently capturing, this creates the capturer and starts capturing. |
| 37 | void StartCapture(); |
| 38 | |
Saman Sami | a4c3ee7 | 2018-05-25 19:38:07 | [diff] [blame] | 39 | // Stops capturing and resets |capturer_|. |
Shridhar Sundarraj | 2af6bca | 2018-04-12 09:26:20 | [diff] [blame] | 40 | void StopCapture(); |
| 41 | |
| 42 | // These functions cache the values passed to them and if we're currently |
| 43 | // capturing, they call the corresponding |capturer_| functions. |
| 44 | // TODO(samans): Add a SetFormat function here so that ARGB pixel format can |
| 45 | // be used. |
| 46 | void SetFrameSinkId(const viz::FrameSinkId& frame_sink_id); |
| 47 | void SetMinCapturePeriod(base::TimeDelta min_capture_period); |
| 48 | void SetMinAndMaxFrameSize(gfx::Size min_frame_size, |
| 49 | gfx::Size max_frame_size); |
| 50 | |
| 51 | private: |
| 52 | friend class DevToolsVideoConsumerTest; |
Shridhar Sundarraj | 2af6bca | 2018-04-12 09:26:20 | [diff] [blame] | 53 | |
Saman Sami | a4c3ee7 | 2018-05-25 19:38:07 | [diff] [blame] | 54 | // Sets |capturer_|, sends capture parameters, and starts capture. Normally, |
| 55 | // CreateCapturer produces the |capturer|, but unittests can provide a mock. |
Shridhar Sundarraj | 2af6bca | 2018-04-12 09:26:20 | [diff] [blame] | 56 | void InnerStartCapture( |
Saman Sami | a4c3ee7 | 2018-05-25 19:38:07 | [diff] [blame] | 57 | std::unique_ptr<viz::ClientFrameSinkVideoCapturer> capturer); |
Shridhar Sundarraj | 2af6bca | 2018-04-12 09:26:20 | [diff] [blame] | 58 | |
| 59 | // Checks that |min_frame_size| and |max_frame_size| are in the expected |
| 60 | // range. Limits are specified in media::limits. |
| 61 | bool IsValidMinAndMaxFrameSize(gfx::Size min_frame_size, |
| 62 | gfx::Size max_frame_size); |
| 63 | |
| 64 | // viz::mojom::FrameSinkVideoConsumer: |
| 65 | void OnFrameCaptured( |
Yuri Wiitala | 4a74fb0 | 2018-08-29 06:09:35 | [diff] [blame] | 66 | base::ReadOnlySharedMemoryRegion data, |
Shridhar Sundarraj | 2af6bca | 2018-04-12 09:26:20 | [diff] [blame] | 67 | ::media::mojom::VideoFrameInfoPtr info, |
Shridhar Sundarraj | 2af6bca | 2018-04-12 09:26:20 | [diff] [blame] | 68 | const gfx::Rect& content_rect, |
| 69 | viz::mojom::FrameSinkVideoConsumerFrameCallbacksPtr callbacks) override; |
Shridhar Sundarraj | 2af6bca | 2018-04-12 09:26:20 | [diff] [blame] | 70 | void OnStopped() override; |
| 71 | |
| 72 | // Default min frame size is 1x1, as otherwise, nothing would be captured. |
| 73 | static constexpr gfx::Size kDefaultMinFrameSize = gfx::Size(1, 1); |
| 74 | |
| 75 | // Using an arbitrary default max frame size of 500x500. |
| 76 | static constexpr gfx::Size kDefaultMaxFrameSize = gfx::Size(500, 500); |
| 77 | |
| 78 | // Callback that is run when a frame is received. |
| 79 | const OnFrameCapturedCallback callback_; |
| 80 | |
| 81 | // Capture parameters. |
| 82 | base::TimeDelta min_capture_period_; |
| 83 | gfx::Size min_frame_size_; |
| 84 | gfx::Size max_frame_size_; |
| 85 | viz::FrameSinkId frame_sink_id_; |
| 86 | |
Saman Sami | a4c3ee7 | 2018-05-25 19:38:07 | [diff] [blame] | 87 | // If |capturer_| is alive, then we are currently capturing. |
| 88 | std::unique_ptr<viz::ClientFrameSinkVideoCapturer> capturer_; |
Shridhar Sundarraj | 2af6bca | 2018-04-12 09:26:20 | [diff] [blame] | 89 | |
| 90 | DISALLOW_COPY_AND_ASSIGN(DevToolsVideoConsumer); |
| 91 | }; |
| 92 | |
| 93 | } // namespace content |
| 94 | |
| 95 | #endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_VIDEO_CONSUMER_H_ |