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 #include "content/renderer/media/media_recorder_handler.h" | 5 #include "content/renderer/media/media_recorder_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/string_tokenizer.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "content/renderer/media/audio_track_recorder.h" |
10 #include "content/renderer/media/video_track_recorder.h" | 13 #include "content/renderer/media/video_track_recorder.h" |
11 #include "content/renderer/media/webrtc_uma_histograms.h" | 14 #include "content/renderer/media/webrtc_uma_histograms.h" |
| 15 #include "media/audio/audio_parameters.h" |
| 16 #include "media/base/audio_bus.h" |
12 #include "media/base/bind_to_current_loop.h" | 17 #include "media/base/bind_to_current_loop.h" |
13 #include "media/base/video_frame.h" | 18 #include "media/base/video_frame.h" |
14 #include "third_party/WebKit/public/platform/WebMediaRecorderHandlerClient.h" | 19 #include "third_party/WebKit/public/platform/WebMediaRecorderHandlerClient.h" |
15 #include "third_party/WebKit/public/platform/WebString.h" | 20 #include "third_party/WebKit/public/platform/WebString.h" |
16 | 21 |
17 #if !defined(MEDIA_DISABLE_LIBWEBM) | 22 #if !defined(MEDIA_DISABLE_LIBWEBM) |
18 #include "media/capture/webm_muxer.h" | 23 #include "media/capture/webm_muxer.h" |
19 #endif | 24 #endif |
20 | 25 |
21 using base::TimeDelta; | 26 using base::TimeDelta; |
22 using base::TimeTicks; | 27 using base::TimeTicks; |
23 | 28 |
24 namespace content { | 29 namespace content { |
25 | 30 |
26 MediaRecorderHandler::MediaRecorderHandler() | 31 MediaRecorderHandler::MediaRecorderHandler() |
27 : use_vp9_(false), | 32 : use_vp9_(false), |
28 recording_(false), | 33 recording_(false), |
29 client_(nullptr), | 34 client_(nullptr), |
30 weak_factory_(this) { | 35 weak_factory_(this) {} |
31 DVLOG(3) << __FUNCTION__; | |
32 } | |
33 | 36 |
34 MediaRecorderHandler::~MediaRecorderHandler() { | 37 MediaRecorderHandler::~MediaRecorderHandler() { |
35 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 38 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
36 // Send a |last_in_slice| to our |client_|. | 39 // Send a |last_in_slice| to our |client_|. |
37 if (client_) | 40 if (client_) |
38 client_->writeData(nullptr, 0u, true); | 41 client_->writeData(nullptr, 0u, true); |
39 } | 42 } |
40 | 43 |
41 bool MediaRecorderHandler::canSupportMimeType( | 44 bool MediaRecorderHandler::canSupportMimeType( |
42 const blink::WebString& mimeType) { | 45 const blink::WebString& mimeType) { |
43 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 46 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
44 // TODO(mcasas): So far only empty or "video/vp{8,9}" are supported. | 47 // Ensure we can support each passed MIME type. |
45 return mimeType.isEmpty() || mimeType.utf8().compare("video/vp8") == 0 || | 48 const std::string input = mimeType.utf8(); // Must outlive tokenizer! |
46 mimeType.utf8().compare("video/vp9") == 0; | 49 base::StringTokenizer tokenizer(input, ","); |
| 50 while (tokenizer.GetNext()) { |
| 51 // Strip whitespace. |
| 52 const std::string token(base::CollapseWhitespaceASCII( |
| 53 tokenizer.token(), true /* trim sequences with line breaks*/)); |
| 54 if (!token.empty() && |
| 55 !base::EqualsCaseInsensitiveASCII(token, "video/vp8") && |
| 56 !base::EqualsCaseInsensitiveASCII(token, "video/vp9") && |
| 57 !base::EqualsCaseInsensitiveASCII(token, "audio/opus") && |
| 58 !base::EqualsCaseInsensitiveASCII(token, "video/webm") && |
| 59 !base::EqualsCaseInsensitiveASCII(token, "audio/webm")) { |
| 60 return false; |
| 61 } |
| 62 } |
| 63 |
| 64 return true; |
47 } | 65 } |
48 | 66 |
49 bool MediaRecorderHandler::initialize( | 67 bool MediaRecorderHandler::initialize( |
50 blink::WebMediaRecorderHandlerClient* client, | 68 blink::WebMediaRecorderHandlerClient* client, |
51 const blink::WebMediaStream& media_stream, | 69 const blink::WebMediaStream& media_stream, |
52 const blink::WebString& mimeType) { | 70 const blink::WebString& mimeType) { |
53 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 71 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
54 // Save histogram data so we can see how much MediaStream Recorder is used. | 72 // Save histogram data so we can see how much MediaStream Recorder is used. |
55 // The histogram counts the number of calls to the JS API. | 73 // The histogram counts the number of calls to the JS API. |
56 UpdateWebRTCMethodCount(WEBKIT_MEDIA_STREAM_RECORDER); | 74 UpdateWebRTCMethodCount(WEBKIT_MEDIA_STREAM_RECORDER); |
(...skipping 18 matching lines...) Expand all Loading... |
75 | 93 |
76 bool MediaRecorderHandler::start(int timeslice) { | 94 bool MediaRecorderHandler::start(int timeslice) { |
77 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 95 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
78 DCHECK(!recording_); | 96 DCHECK(!recording_); |
79 DCHECK(!media_stream_.isNull()); | 97 DCHECK(!media_stream_.isNull()); |
80 DCHECK(timeslice_.is_zero()); | 98 DCHECK(timeslice_.is_zero()); |
81 | 99 |
82 timeslice_ = TimeDelta::FromMilliseconds(timeslice); | 100 timeslice_ = TimeDelta::FromMilliseconds(timeslice); |
83 slice_origin_timestamp_ = TimeTicks::Now(); | 101 slice_origin_timestamp_ = TimeTicks::Now(); |
84 | 102 |
85 blink::WebVector<blink::WebMediaStreamTrack> video_tracks; | 103 blink::WebVector<blink::WebMediaStreamTrack> video_tracks, audio_tracks; |
86 media_stream_.videoTracks(video_tracks); | 104 media_stream_.videoTracks(video_tracks); |
| 105 media_stream_.audioTracks(audio_tracks); |
87 | 106 |
88 #if !defined(MEDIA_DISABLE_LIBWEBM) | 107 #if defined(MEDIA_DISABLE_LIBWEBM) |
89 DCHECK(!webm_muxer_); | |
90 | |
91 webm_muxer_.reset(new media::WebmMuxer( | |
92 use_vp9_ ? media::kCodecVP9 : media::kCodecVP8, video_tracks.size() > 0, | |
93 false /* no audio for now - http://crbug.com/528519 */, | |
94 base::Bind(&MediaRecorderHandler::WriteData, | |
95 weak_factory_.GetWeakPtr()))); | |
96 #else | |
97 LOG(WARNING) << "No muxer available"; | 108 LOG(WARNING) << "No muxer available"; |
98 return false; | 109 return false; |
99 #endif | 110 #endif |
100 | 111 |
101 if (video_tracks.isEmpty()) { | 112 DCHECK(!webm_muxer_); |
102 // TODO(mcasas): Add audio_tracks and update the code in this function | 113 |
103 // correspondingly, see http://crbug.com/528519. As of now, only video | 114 if (video_tracks.isEmpty() && audio_tracks.isEmpty()) { |
104 // tracks are supported. | 115 LOG(WARNING) << __FUNCTION__ << ": no media tracks."; |
105 LOG(WARNING) << "Recording no video tracks is not implemented"; | |
106 return false; | 116 return false; |
107 } | 117 } |
108 // TODO(mcasas): The muxer API supports only one video track. Extend it to | |
109 // several video tracks, see http://crbug.com/528523. | |
110 LOG_IF(WARNING, video_tracks.size() > 1u) << "Recording multiple video" | |
111 << " tracks is not implemented. Only recording first video track."; | |
112 const blink::WebMediaStreamTrack& video_track = video_tracks[0]; | |
113 if (video_track.isNull()) | |
114 return false; | |
115 | 118 |
116 const VideoTrackRecorder::OnEncodedVideoCB on_encoded_video_cb = | 119 webm_muxer_.reset(new media::WebmMuxer( |
117 media::BindToCurrentLoop(base::Bind(&MediaRecorderHandler::OnEncodedVideo, | 120 use_vp9_ ? media::kCodecVP9 : media::kCodecVP8, |
118 weak_factory_.GetWeakPtr())); | 121 video_tracks.size() > 0, audio_tracks.size() > 0, |
| 122 base::Bind(&MediaRecorderHandler::WriteData, |
| 123 weak_factory_.GetWeakPtr()))); |
119 | 124 |
120 video_recorders_.push_back(new VideoTrackRecorder(use_vp9_, | 125 if (!video_tracks.isEmpty()) { |
121 video_track, | 126 // TODO(mcasas): The muxer API supports only one video track. Extend it to |
122 on_encoded_video_cb)); | 127 // several video tracks, see http://crbug.com/528523. |
| 128 LOG_IF(WARNING, video_tracks.size() > 1u) |
| 129 << "Recording multiple video tracks is not implemented. " |
| 130 << "Only recording first video track."; |
| 131 const blink::WebMediaStreamTrack& video_track = video_tracks[0]; |
| 132 if (video_track.isNull()) |
| 133 return false; |
| 134 |
| 135 const VideoTrackRecorder::OnEncodedVideoCB on_encoded_video_cb = |
| 136 media::BindToCurrentLoop(base::Bind( |
| 137 &MediaRecorderHandler::OnEncodedVideo, weak_factory_.GetWeakPtr())); |
| 138 |
| 139 video_recorders_.push_back( |
| 140 new VideoTrackRecorder(use_vp9_, video_track, on_encoded_video_cb)); |
| 141 } |
| 142 |
| 143 if (!audio_tracks.isEmpty()) { |
| 144 // TODO(ajose): The muxer API supports only one audio track. Extend it to |
| 145 // several tracks. |
| 146 LOG_IF(WARNING, audio_tracks.size() > 1u) |
| 147 << "Recording multiple audio" |
| 148 << " tracks is not implemented. Only recording first audio track."; |
| 149 const blink::WebMediaStreamTrack& audio_track = audio_tracks[0]; |
| 150 if (audio_track.isNull()) |
| 151 return false; |
| 152 |
| 153 const AudioTrackRecorder::OnEncodedAudioCB on_encoded_audio_cb = |
| 154 media::BindToCurrentLoop(base::Bind( |
| 155 &MediaRecorderHandler::OnEncodedAudio, weak_factory_.GetWeakPtr())); |
| 156 |
| 157 audio_recorders_.push_back( |
| 158 new AudioTrackRecorder(audio_track, on_encoded_audio_cb)); |
| 159 } |
123 | 160 |
124 recording_ = true; | 161 recording_ = true; |
125 return true; | 162 return true; |
126 } | 163 } |
127 | 164 |
128 void MediaRecorderHandler::stop() { | 165 void MediaRecorderHandler::stop() { |
129 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 166 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
130 DCHECK(recording_); | 167 DCHECK(recording_); |
131 | 168 |
132 recording_ = false; | 169 recording_ = false; |
133 timeslice_ = TimeDelta::FromMilliseconds(0); | 170 timeslice_ = TimeDelta::FromMilliseconds(0); |
134 video_recorders_.clear(); | 171 video_recorders_.clear(); |
| 172 audio_recorders_.clear(); |
135 #if !defined(MEDIA_DISABLE_LIBWEBM) | 173 #if !defined(MEDIA_DISABLE_LIBWEBM) |
136 webm_muxer_.reset(); | 174 webm_muxer_.reset(); |
137 #endif | 175 #endif |
138 } | 176 } |
139 | 177 |
140 void MediaRecorderHandler::pause() { | 178 void MediaRecorderHandler::pause() { |
141 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 179 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
142 DCHECK(recording_); | 180 DCHECK(recording_); |
143 recording_ = false; | 181 recording_ = false; |
144 for (const auto& video_recorder : video_recorders_) | 182 for (const auto& video_recorder : video_recorders_) |
(...skipping 15 matching lines...) Expand all Loading... |
160 bool is_key_frame) { | 198 bool is_key_frame) { |
161 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 199 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
162 #if !defined(MEDIA_DISABLE_LIBWEBM) | 200 #if !defined(MEDIA_DISABLE_LIBWEBM) |
163 if (!webm_muxer_) | 201 if (!webm_muxer_) |
164 return; | 202 return; |
165 webm_muxer_->OnEncodedVideo(video_frame, encoded_data.Pass(), timestamp, | 203 webm_muxer_->OnEncodedVideo(video_frame, encoded_data.Pass(), timestamp, |
166 is_key_frame); | 204 is_key_frame); |
167 #endif | 205 #endif |
168 } | 206 } |
169 | 207 |
| 208 void MediaRecorderHandler::OnEncodedAudio(const media::AudioParameters& params, |
| 209 scoped_ptr<std::string> encoded_data, |
| 210 base::TimeTicks timestamp) { |
| 211 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
| 212 if (webm_muxer_) |
| 213 webm_muxer_->OnEncodedAudio(params, encoded_data.Pass(), timestamp); |
| 214 } |
| 215 |
170 void MediaRecorderHandler::WriteData(base::StringPiece data) { | 216 void MediaRecorderHandler::WriteData(base::StringPiece data) { |
171 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 217 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
172 | 218 |
173 // Non-buffered mode does not need to check timestamps. | 219 // Non-buffered mode does not need to check timestamps. |
174 if (timeslice_.is_zero()) { | 220 if (timeslice_.is_zero()) { |
175 client_->writeData(data.data(), data.length(), true /* lastInSlice */); | 221 client_->writeData(data.data(), data.length(), true /* lastInSlice */); |
176 return; | 222 return; |
177 } | 223 } |
178 | 224 |
179 const TimeTicks now = TimeTicks::Now(); | 225 const TimeTicks now = TimeTicks::Now(); |
180 const bool last_in_slice = now > slice_origin_timestamp_ + timeslice_; | 226 const bool last_in_slice = now > slice_origin_timestamp_ + timeslice_; |
181 DVLOG_IF(1, last_in_slice) << "Slice finished @ " << now; | 227 DVLOG_IF(1, last_in_slice) << "Slice finished @ " << now; |
182 if (last_in_slice) | 228 if (last_in_slice) |
183 slice_origin_timestamp_ = now; | 229 slice_origin_timestamp_ = now; |
184 client_->writeData(data.data(), data.length(), last_in_slice); | 230 client_->writeData(data.data(), data.length(), last_in_slice); |
185 } | 231 } |
186 | 232 |
187 void MediaRecorderHandler::OnVideoFrameForTesting( | 233 void MediaRecorderHandler::OnVideoFrameForTesting( |
188 const scoped_refptr<media::VideoFrame>& frame, | 234 const scoped_refptr<media::VideoFrame>& frame, |
189 const TimeTicks& timestamp) { | 235 const TimeTicks& timestamp) { |
190 for (auto* recorder : video_recorders_) | 236 for (auto* recorder : video_recorders_) |
191 recorder->OnVideoFrameForTesting(frame, timestamp); | 237 recorder->OnVideoFrameForTesting(frame, timestamp); |
192 } | 238 } |
193 | 239 |
| 240 void MediaRecorderHandler::OnAudioBusForTesting( |
| 241 const media::AudioBus& audio_bus, |
| 242 const base::TimeTicks& timestamp) { |
| 243 for (auto* recorder : audio_recorders_) |
| 244 recorder->OnData(audio_bus, timestamp); |
| 245 } |
| 246 |
| 247 void MediaRecorderHandler::SetAudioFormatForTesting( |
| 248 const media::AudioParameters& params) { |
| 249 for (auto* recorder : audio_recorders_) |
| 250 recorder->OnSetFormat(params); |
| 251 } |
| 252 |
194 } // namespace content | 253 } // namespace content |
OLD | NEW |