OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_RENDERER_MEDIA_RECORDER_VIDEO_TRACK_RECORDER_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_RECORDER_VIDEO_TRACK_RECORDER_H_ |
6 #define CONTENT_RENDERER_MEDIA_RECORDER_VIDEO_TRACK_RECORDER_H_ | 6 #define CONTENT_RENDERER_MEDIA_RECORDER_VIDEO_TRACK_RECORDER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 #include "content/public/common/features.h" | 15 #include "content/public/common/features.h" |
16 #include "content/public/renderer/media_stream_video_sink.h" | 16 #include "content/public/renderer/media_stream_video_sink.h" |
17 #include "media/muxers/webm_muxer.h" | 17 #include "media/muxers/webm_muxer.h" |
18 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" | 18 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
19 #include "third_party/skia/include/core/SkBitmap.h" | |
20 | |
21 namespace base { | |
22 class Thread; | |
23 } // namespace base | |
24 | |
25 namespace cc { | |
26 class PaintCanvas; | |
27 } // namespace cc | |
19 | 28 |
20 namespace media { | 29 namespace media { |
30 class SkCanvasVideoRenderer; | |
21 class VideoFrame; | 31 class VideoFrame; |
22 } // namespace media | 32 } // namespace media |
23 | 33 |
24 namespace video_track_recorder { | 34 namespace video_track_recorder { |
25 const int kVEAEncoderMinResolutionWidth = 640; | 35 const int kVEAEncoderMinResolutionWidth = 640; |
26 const int kVEAEncoderMinResolutionHeight = 480; | 36 const int kVEAEncoderMinResolutionHeight = 480; |
27 } // namespace video_track_recorder | 37 } // namespace video_track_recorder |
28 | 38 |
29 namespace content { | 39 namespace content { |
30 | 40 |
31 // VideoTrackRecorder is a MediaStreamVideoSink that encodes the video frames | 41 // VideoTrackRecorder is a MediaStreamVideoSink that encodes the video frames |
32 // received from a Stream Video Track. This class is constructed and used on a | 42 // received from a Stream Video Track. This class is constructed and used on a |
33 // single thread, namely the main Render thread. This mirrors the other | 43 // single thread, namely the main Render thread. This mirrors the other |
34 // MediaStreamVideo* classes that are constructed/configured on Main Render | 44 // MediaStreamVideo* classes that are constructed/configured on Main Render |
35 // thread but that pass frames on Render IO thread. It has an internal Encoder | 45 // thread but that pass frames on Render IO thread. It has an internal Encoder |
36 // with its own threading subtleties, see the implementation file. | 46 // with its own threading subtleties, see the implementation file. |
37 class CONTENT_EXPORT VideoTrackRecorder | 47 class CONTENT_EXPORT VideoTrackRecorder |
38 : NON_EXPORTED_BASE(public MediaStreamVideoSink) { | 48 : NON_EXPORTED_BASE(public MediaStreamVideoSink) { |
39 public: | 49 public: |
40 // Do not change the order of codecs; add new ones right before LAST. | 50 // Do not change the order of codecs; add new ones right before LAST. |
41 enum class CodecId { | 51 enum class CodecId { |
42 VP8, | 52 VP8, |
43 VP9, | 53 VP9, |
44 #if BUILDFLAG(RTC_USE_H264) | 54 #if BUILDFLAG(RTC_USE_H264) |
45 H264, | 55 H264, |
46 #endif | 56 #endif |
47 LAST | 57 LAST |
48 }; | 58 }; |
49 class Encoder; | |
50 | 59 |
51 using OnEncodedVideoCB = | 60 using OnEncodedVideoCB = |
52 base::Callback<void(const media::WebmMuxer::VideoParameters& params, | 61 base::Callback<void(const media::WebmMuxer::VideoParameters& params, |
53 std::unique_ptr<std::string> encoded_data, | 62 std::unique_ptr<std::string> encoded_data, |
54 std::unique_ptr<std::string> encoded_alpha, | 63 std::unique_ptr<std::string> encoded_alpha, |
55 base::TimeTicks capture_timestamp, | 64 base::TimeTicks capture_timestamp, |
56 bool is_key_frame)>; | 65 bool is_key_frame)>; |
57 | 66 |
67 // Base class to describe a generic Encoder, encapsulating all actual encoder | |
68 // (re)configurations, encoding and delivery of received frames. This class is | |
69 // ref-counted to allow the MediaStreamVideoTrack to hold a reference to it | |
70 // (via the callback that MediaStreamVideoSink passes along) and to jump back | |
71 // and forth to an internal encoder thread. Moreover, this class: - is created | |
72 // on its parent's thread (usually the main Render thread), that is, | |
73 // |main_task_runner_|. - receives VideoFrames on |origin_task_runner_| and | |
74 // runs OnEncodedVideoCB on that thread as well. This task runner is cached on | |
75 // first frame arrival, and is supposed to be the render IO thread (but this | |
76 // is not enforced); - uses an internal |encoding_task_runner_| for actual | |
mcasas
2017/04/05 01:23:59
The dashes are supposed to go in each line:
https
emircan
2017/04/05 21:13:44
Done.
| |
77 // encoder interactions, namely configuration, encoding (which might take some | |
78 // time) and destruction. This task runner can be passed on the creation. If | |
79 // nothing is passed, a new encoding thread is created and used. | |
80 class Encoder : public base::RefCountedThreadSafe<Encoder> { | |
81 public: | |
82 Encoder(const OnEncodedVideoCB& on_encoded_video_callback, | |
83 int32_t bits_per_second, | |
84 scoped_refptr<base::SingleThreadTaskRunner> encoding_task_runner = | |
85 nullptr); | |
86 | |
87 // Start encoding |frame|, returning via |on_encoded_video_callback_|. This | |
88 // call will also trigger an encode configuration upon first frame arrival | |
89 // or parameter change, and an EncodeOnEncodingTaskRunner() to actually | |
90 // encode the frame. If the |frame|'s data is not directly available (e.g. | |
91 // it's a texture) then RetrieveFrameOnMainThread() is called, and if even | |
92 // that fails, black frames are sent instead. | |
93 void StartFrameEncode(const scoped_refptr<media::VideoFrame>& frame, | |
94 base::TimeTicks capture_timestamp); | |
95 void RetrieveFrameOnMainThread( | |
96 const scoped_refptr<media::VideoFrame>& video_frame, | |
97 base::TimeTicks capture_timestamp); | |
98 | |
99 static void OnFrameEncodeCompleted( | |
100 const VideoTrackRecorder::OnEncodedVideoCB& on_encoded_video_cb, | |
101 const media::WebmMuxer::VideoParameters& params, | |
102 std::unique_ptr<std::string> data, | |
103 std::unique_ptr<std::string> alpha_data, | |
104 base::TimeTicks capture_timestamp, | |
105 bool keyframe); | |
106 | |
107 void SetPaused(bool paused); | |
108 virtual bool CanEncodeAlphaChannel(); | |
109 | |
110 protected: | |
111 friend class base::RefCountedThreadSafe<Encoder>; | |
112 virtual ~Encoder(); | |
113 | |
114 virtual void EncodeOnEncodingTaskRunner( | |
115 scoped_refptr<media::VideoFrame> frame, | |
116 base::TimeTicks capture_timestamp) = 0; | |
117 | |
118 // Used to shutdown properly on the same thread we were created. | |
119 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | |
120 | |
121 // Task runner where frames to encode and reply callbacks must happen. | |
122 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; | |
123 | |
124 // Task runner where encoding interactions happen. | |
125 scoped_refptr<base::SingleThreadTaskRunner> encoding_task_runner_; | |
126 | |
127 // Optional thread for encoding. Active for the lifetime of VpxEncoder. | |
128 std::unique_ptr<base::Thread> encoding_thread_; | |
129 | |
130 // While |paused_|, frames are not encoded. Used only from | |
131 // |encoding_thread_|. | |
132 bool paused_; | |
133 | |
134 // This callback should be exercised on IO thread. | |
135 const OnEncodedVideoCB on_encoded_video_callback_; | |
136 | |
137 // Target bitrate for video encoding. If 0, a standard bitrate is used. | |
138 const int32_t bits_per_second_; | |
139 | |
140 // Used to retrieve incoming opaque VideoFrames (i.e. VideoFrames backed by | |
141 // textures). Created on-demand on |main_task_runner_|. | |
142 std::unique_ptr<media::SkCanvasVideoRenderer> video_renderer_; | |
143 SkBitmap bitmap_; | |
144 std::unique_ptr<cc::PaintCanvas> canvas_; | |
145 | |
146 DISALLOW_COPY_AND_ASSIGN(Encoder); | |
147 }; | |
148 | |
58 static CodecId GetPreferredCodecId(); | 149 static CodecId GetPreferredCodecId(); |
59 | 150 |
60 VideoTrackRecorder(CodecId codec, | 151 VideoTrackRecorder(CodecId codec, |
61 const blink::WebMediaStreamTrack& track, | 152 const blink::WebMediaStreamTrack& track, |
62 const OnEncodedVideoCB& on_encoded_video_cb, | 153 const OnEncodedVideoCB& on_encoded_video_cb, |
63 int32_t bits_per_second); | 154 int32_t bits_per_second); |
64 ~VideoTrackRecorder() override; | 155 ~VideoTrackRecorder() override; |
65 | 156 |
66 void Pause(); | 157 void Pause(); |
67 void Resume(); | 158 void Resume(); |
68 | 159 |
69 void OnVideoFrameForTesting(const scoped_refptr<media::VideoFrame>& frame, | 160 void OnVideoFrameForTesting(const scoped_refptr<media::VideoFrame>& frame, |
70 base::TimeTicks capture_time); | 161 base::TimeTicks capture_time); |
71 private: | 162 private: |
72 friend class VideoTrackRecorderTest; | 163 friend class VideoTrackRecorderTest; |
73 | 164 |
74 void InitializeEncoder(CodecId codec, | 165 void InitializeEncoder(CodecId codec, |
75 const OnEncodedVideoCB& on_encoded_video_callback, | 166 const OnEncodedVideoCB& on_encoded_video_callback, |
76 int32_t bits_per_second, | 167 int32_t bits_per_second, |
77 const scoped_refptr<media::VideoFrame>& frame, | 168 const scoped_refptr<media::VideoFrame>& frame, |
78 base::TimeTicks capture_time); | 169 base::TimeTicks capture_time); |
79 | 170 |
80 // TODO(emircan): Remove after refactor, see http://crbug.com/700433. | |
81 bool CanEncodeAlphaChannelForTesting(); | |
82 | |
83 // Used to check that we are destroyed on the same thread we were created. | 171 // Used to check that we are destroyed on the same thread we were created. |
84 base::ThreadChecker main_render_thread_checker_; | 172 base::ThreadChecker main_render_thread_checker_; |
85 | 173 |
86 // We need to hold on to the Blink track to remove ourselves on dtor. | 174 // We need to hold on to the Blink track to remove ourselves on dtor. |
87 blink::WebMediaStreamTrack track_; | 175 blink::WebMediaStreamTrack track_; |
88 | 176 |
89 // Inner class to encode using whichever codec is configured. | 177 // Inner class to encode using whichever codec is configured. |
90 scoped_refptr<Encoder> encoder_; | 178 scoped_refptr<Encoder> encoder_; |
91 | 179 |
92 base::Callback<void(const scoped_refptr<media::VideoFrame>& frame, | 180 base::Callback<void(const scoped_refptr<media::VideoFrame>& frame, |
93 base::TimeTicks capture_time)> | 181 base::TimeTicks capture_time)> |
94 initialize_encoder_callback_; | 182 initialize_encoder_callback_; |
95 | 183 |
96 // Used to track the paused state during the initialization process. | 184 // Used to track the paused state during the initialization process. |
97 bool paused_before_init_; | 185 bool paused_before_init_; |
98 | 186 |
99 base::WeakPtrFactory<VideoTrackRecorder> weak_ptr_factory_; | 187 base::WeakPtrFactory<VideoTrackRecorder> weak_ptr_factory_; |
100 | 188 |
101 DISALLOW_COPY_AND_ASSIGN(VideoTrackRecorder); | 189 DISALLOW_COPY_AND_ASSIGN(VideoTrackRecorder); |
102 }; | 190 }; |
103 | 191 |
104 } // namespace content | 192 } // namespace content |
105 #endif // CONTENT_RENDERER_MEDIA_RECORDER_VIDEO_TRACK_RECORDER_H_ | 193 #endif // CONTENT_RENDERER_MEDIA_RECORDER_VIDEO_TRACK_RECORDER_H_ |
OLD | NEW |