Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(442)

Side by Side Diff: content/renderer/media_recorder/video_track_recorder.h

Issue 2793303003: Refactor VideoTrackRecorder into smaller classes (Closed)
Patch Set: Rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 using OnErrorCB = base::Closure; 66 using OnErrorCB = base::Closure;
58 67
68 // Base class to describe a generic Encoder, encapsulating all actual encoder
69 // (re)configurations, encoding and delivery of received frames. This class is
70 // ref-counted to allow the MediaStreamVideoTrack to hold a reference to it
71 // (via the callback that MediaStreamVideoSink passes along) and to jump back
72 // and forth to an internal encoder thread. Moreover, this class:
73 // - is created on its parent's thread (usually the main Render thread), that
74 // is, |main_task_runner_|.
75 // - receives VideoFrames on |origin_task_runner_| and runs OnEncodedVideoCB
76 // on that thread as well. This task runner is cached on first frame arrival,
77 // and is supposed to be the render IO thread (but this is not enforced);
78 // - uses an internal |encoding_task_runner_| for actual encoder interactions,
79 // namely configuration, encoding (which might take some time) and
80 // destruction. This task runner can be passed on the creation. If nothing is
81 // passed, a new encoding thread is created and used.
82 class Encoder : public base::RefCountedThreadSafe<Encoder> {
83 public:
84 Encoder(const OnEncodedVideoCB& on_encoded_video_callback,
85 int32_t bits_per_second,
86 scoped_refptr<base::SingleThreadTaskRunner> encoding_task_runner =
87 nullptr);
88
89 // Start encoding |frame|, returning via |on_encoded_video_callback_|. This
90 // call will also trigger an encode configuration upon first frame arrival
91 // or parameter change, and an EncodeOnEncodingTaskRunner() to actually
92 // encode the frame. If the |frame|'s data is not directly available (e.g.
93 // it's a texture) then RetrieveFrameOnMainThread() is called, and if even
94 // that fails, black frames are sent instead.
95 void StartFrameEncode(const scoped_refptr<media::VideoFrame>& frame,
96 base::TimeTicks capture_timestamp);
97 void RetrieveFrameOnMainThread(
98 const scoped_refptr<media::VideoFrame>& video_frame,
99 base::TimeTicks capture_timestamp);
100
101 static void OnFrameEncodeCompleted(
102 const VideoTrackRecorder::OnEncodedVideoCB& on_encoded_video_cb,
103 const media::WebmMuxer::VideoParameters& params,
104 std::unique_ptr<std::string> data,
105 std::unique_ptr<std::string> alpha_data,
106 base::TimeTicks capture_timestamp,
107 bool keyframe);
108
109 void SetPaused(bool paused);
110 virtual bool CanEncodeAlphaChannel();
111
112 protected:
113 friend class base::RefCountedThreadSafe<Encoder>;
114 virtual ~Encoder();
115
116 virtual void EncodeOnEncodingTaskRunner(
117 scoped_refptr<media::VideoFrame> frame,
118 base::TimeTicks capture_timestamp) = 0;
119
120 // Used to shutdown properly on the same thread we were created.
121 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
122
123 // Task runner where frames to encode and reply callbacks must happen.
124 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_;
125
126 // Task runner where encoding interactions happen.
127 scoped_refptr<base::SingleThreadTaskRunner> encoding_task_runner_;
128
129 // Optional thread for encoding. Active for the lifetime of VpxEncoder.
130 std::unique_ptr<base::Thread> encoding_thread_;
131
132 // While |paused_|, frames are not encoded. Used only from
133 // |encoding_thread_|.
134 bool paused_;
135
136 // This callback should be exercised on IO thread.
137 const OnEncodedVideoCB on_encoded_video_callback_;
138
139 // Target bitrate for video encoding. If 0, a standard bitrate is used.
140 const int32_t bits_per_second_;
141
142 // Used to retrieve incoming opaque VideoFrames (i.e. VideoFrames backed by
143 // textures). Created on-demand on |main_task_runner_|.
144 std::unique_ptr<media::SkCanvasVideoRenderer> video_renderer_;
145 SkBitmap bitmap_;
146 std::unique_ptr<cc::PaintCanvas> canvas_;
147
148 DISALLOW_COPY_AND_ASSIGN(Encoder);
149 };
150
59 static CodecId GetPreferredCodecId(); 151 static CodecId GetPreferredCodecId();
60 152
61 VideoTrackRecorder(CodecId codec, 153 VideoTrackRecorder(CodecId codec,
62 const blink::WebMediaStreamTrack& track, 154 const blink::WebMediaStreamTrack& track,
63 const OnEncodedVideoCB& on_encoded_video_cb, 155 const OnEncodedVideoCB& on_encoded_video_cb,
64 int32_t bits_per_second); 156 int32_t bits_per_second);
65 ~VideoTrackRecorder() override; 157 ~VideoTrackRecorder() override;
66 158
67 void Pause(); 159 void Pause();
68 void Resume(); 160 void Resume();
69 161
70 void OnVideoFrameForTesting(const scoped_refptr<media::VideoFrame>& frame, 162 void OnVideoFrameForTesting(const scoped_refptr<media::VideoFrame>& frame,
71 base::TimeTicks capture_time); 163 base::TimeTicks capture_time);
72 private: 164 private:
73 friend class VideoTrackRecorderTest; 165 friend class VideoTrackRecorderTest;
74 166
75 void InitializeEncoder(CodecId codec, 167 void InitializeEncoder(CodecId codec,
76 const OnEncodedVideoCB& on_encoded_video_callback, 168 const OnEncodedVideoCB& on_encoded_video_callback,
77 int32_t bits_per_second, 169 int32_t bits_per_second,
78 bool allow_vea_encoder, 170 bool allow_vea_encoder,
79 const scoped_refptr<media::VideoFrame>& frame, 171 const scoped_refptr<media::VideoFrame>& frame,
80 base::TimeTicks capture_time); 172 base::TimeTicks capture_time);
81 void OnError(); 173 void OnError();
82 174
83 // TODO(emircan): Remove after refactor, see http://crbug.com/700433.
84 bool CanEncodeAlphaChannelForTesting();
85
86 // Used to check that we are destroyed on the same thread we were created. 175 // Used to check that we are destroyed on the same thread we were created.
87 base::ThreadChecker main_render_thread_checker_; 176 base::ThreadChecker main_render_thread_checker_;
88 177
89 // We need to hold on to the Blink track to remove ourselves on dtor. 178 // We need to hold on to the Blink track to remove ourselves on dtor.
90 blink::WebMediaStreamTrack track_; 179 blink::WebMediaStreamTrack track_;
91 180
92 // Inner class to encode using whichever codec is configured. 181 // Inner class to encode using whichever codec is configured.
93 scoped_refptr<Encoder> encoder_; 182 scoped_refptr<Encoder> encoder_;
94 183
95 base::Callback<void(bool allow_vea_encoder, 184 base::Callback<void(bool allow_vea_encoder,
96 const scoped_refptr<media::VideoFrame>& frame, 185 const scoped_refptr<media::VideoFrame>& frame,
97 base::TimeTicks capture_time)> 186 base::TimeTicks capture_time)>
98 initialize_encoder_callback_; 187 initialize_encoder_callback_;
99 188
100 // Used to track the paused state during the initialization process. 189 // Used to track the paused state during the initialization process.
101 bool paused_before_init_; 190 bool paused_before_init_;
102 191
103 base::WeakPtrFactory<VideoTrackRecorder> weak_ptr_factory_; 192 base::WeakPtrFactory<VideoTrackRecorder> weak_ptr_factory_;
104 193
105 DISALLOW_COPY_AND_ASSIGN(VideoTrackRecorder); 194 DISALLOW_COPY_AND_ASSIGN(VideoTrackRecorder);
106 }; 195 };
107 196
108 } // namespace content 197 } // namespace content
109 #endif // CONTENT_RENDERER_MEDIA_RECORDER_VIDEO_TRACK_RECORDER_H_ 198 #endif // CONTENT_RENDERER_MEDIA_RECORDER_VIDEO_TRACK_RECORDER_H_
OLDNEW
« no previous file with comments | « content/renderer/media_recorder/vea_encoder.cc ('k') | content/renderer/media_recorder/video_track_recorder.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698