blob: a34fb537774bf08727a13f1cb6b185a99d9623cb [file] [log] [blame]
Shridhar Sundarraj2af6bca2018-04-12 09:26:201// 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 Wiitalafc5fe702018-06-20 06:14:008#include <memory>
9
Shridhar Sundarraj2af6bca2018-04-12 09:26:2010#include "base/time/time.h"
Saman Samia4c3ee72018-05-25 19:38:0711#include "components/viz/host/client_frame_sink_video_capturer.h"
Shridhar Sundarraj2af6bca2018-04-12 09:26:2012#include "content/common/content_export.h"
Shridhar Sundarraj2af6bca2018-04-12 09:26:2013#include "ui/gfx/geometry/size.h"
14
15class SkBitmap;
16
17namespace 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 Samia4c3ee72018-05-25 19:38:0722// TODO(crbug.com/846811): This class can probably be merged into
23// viz::ClientFrameSinkVideoCapturer.
Shridhar Sundarraj2af6bca2018-04-12 09:26:2024class 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 Samia4c3ee72018-05-25 19:38:0739 // Stops capturing and resets |capturer_|.
Shridhar Sundarraj2af6bca2018-04-12 09:26:2040 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 Sundarraj2af6bca2018-04-12 09:26:2053
Saman Samia4c3ee72018-05-25 19:38:0754 // Sets |capturer_|, sends capture parameters, and starts capture. Normally,
55 // CreateCapturer produces the |capturer|, but unittests can provide a mock.
Shridhar Sundarraj2af6bca2018-04-12 09:26:2056 void InnerStartCapture(
Saman Samia4c3ee72018-05-25 19:38:0757 std::unique_ptr<viz::ClientFrameSinkVideoCapturer> capturer);
Shridhar Sundarraj2af6bca2018-04-12 09:26:2058
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 Wiitala4a74fb02018-08-29 06:09:3566 base::ReadOnlySharedMemoryRegion data,
Shridhar Sundarraj2af6bca2018-04-12 09:26:2067 ::media::mojom::VideoFrameInfoPtr info,
Shridhar Sundarraj2af6bca2018-04-12 09:26:2068 const gfx::Rect& content_rect,
69 viz::mojom::FrameSinkVideoConsumerFrameCallbacksPtr callbacks) override;
Shridhar Sundarraj2af6bca2018-04-12 09:26:2070 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 Samia4c3ee72018-05-25 19:38:0787 // If |capturer_| is alive, then we are currently capturing.
88 std::unique_ptr<viz::ClientFrameSinkVideoCapturer> capturer_;
Shridhar Sundarraj2af6bca2018-04-12 09:26:2089
90 DISALLOW_COPY_AND_ASSIGN(DevToolsVideoConsumer);
91};
92
93} // namespace content
94
95#endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_VIDEO_CONSUMER_H_