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

Side by Side Diff: media/base/android/media_codec_audio_decoder.cc

Issue 1254293003: MediaCodecPlayer implementation (stage 4 - preroll) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mtplayer-browserseek
Patch Set: Save the first post-preroll audio buffer and replay it when preroll is done Created 5 years, 4 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 #include "media/base/android/media_codec_audio_decoder.h" 5 #include "media/base/android/media_codec_audio_decoder.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "media/base/android/media_codec_bridge.h" 9 #include "media/base/android/media_codec_bridge.h"
10 #include "media/base/audio_timestamp_helper.h" 10 #include "media/base/audio_timestamp_helper.h"
11 #include "media/base/demuxer_stream.h" 11 #include "media/base/demuxer_stream.h"
12 12
13 namespace { 13 namespace {
14 14
15 // Use 16bit PCM for audio output. Keep this value in sync with the output 15 // Use 16bit PCM for audio output. Keep this value in sync with the output
16 // format we passed to AudioTrack in MediaCodecBridge. 16 // format we passed to AudioTrack in MediaCodecBridge.
17 const int kBytesPerAudioOutputSample = 2; 17 const int kBytesPerAudioOutputSample = 2;
18
19 // Fake buffer index that refers to pending buffer.
20 const int kPendingBufferIndex = -1;
18 } 21 }
19 22
20 namespace media { 23 namespace media {
21 24
22 MediaCodecAudioDecoder::MediaCodecAudioDecoder( 25 MediaCodecAudioDecoder::MediaCodecAudioDecoder(
23 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, 26 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner,
24 const base::Closure& request_data_cb, 27 const base::Closure& request_data_cb,
25 const base::Closure& starvation_cb, 28 const base::Closure& starvation_cb,
29 const base::Closure& preroll_done_cb,
26 const base::Closure& stop_done_cb, 30 const base::Closure& stop_done_cb,
27 const base::Closure& error_cb, 31 const base::Closure& error_cb,
28 const SetTimeCallback& update_current_time_cb) 32 const SetTimeCallback& update_current_time_cb)
29 : MediaCodecDecoder(media_task_runner, 33 : MediaCodecDecoder(media_task_runner,
30 request_data_cb, 34 request_data_cb,
31 starvation_cb, 35 starvation_cb,
36 preroll_done_cb,
32 stop_done_cb, 37 stop_done_cb,
33 error_cb, 38 error_cb,
34 "AudioDecoder"), 39 "AudioDecoder"),
35 volume_(-1.0), 40 volume_(-1.0),
36 bytes_per_frame_(0), 41 bytes_per_frame_(0),
37 output_sampling_rate_(0), 42 output_sampling_rate_(0),
38 frame_count_(0), 43 frame_count_(0),
39 update_current_time_cb_(update_current_time_cb) { 44 update_current_time_cb_(update_current_time_cb) {
40 } 45 }
41 46
(...skipping 20 matching lines...) Expand all
62 configs_ = configs; 67 configs_ = configs;
63 if (!media_codec_bridge_) 68 if (!media_codec_bridge_)
64 output_sampling_rate_ = configs.audio_sampling_rate; 69 output_sampling_rate_ = configs.audio_sampling_rate;
65 } 70 }
66 71
67 void MediaCodecAudioDecoder::Flush() { 72 void MediaCodecAudioDecoder::Flush() {
68 DCHECK(media_task_runner_->BelongsToCurrentThread()); 73 DCHECK(media_task_runner_->BelongsToCurrentThread());
69 74
70 MediaCodecDecoder::Flush(); 75 MediaCodecDecoder::Flush();
71 frame_count_ = 0; 76 frame_count_ = 0;
77 pending_buffer_.Invalidate();
78 }
79
80 void MediaCodecAudioDecoder::ReleaseMediaCodec() {
81 DCHECK(media_task_runner_->BelongsToCurrentThread());
82
83 DVLOG(1) << class_name() << "::" << __FUNCTION__;
84
85 MediaCodecDecoder::ReleaseMediaCodec();
86 pending_buffer_.Invalidate();
72 } 87 }
73 88
74 void MediaCodecAudioDecoder::SetVolume(double volume) { 89 void MediaCodecAudioDecoder::SetVolume(double volume) {
75 DCHECK(media_task_runner_->BelongsToCurrentThread()); 90 DCHECK(media_task_runner_->BelongsToCurrentThread());
76 91
77 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << volume; 92 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << volume;
78 93
79 volume_ = volume; 94 volume_ = volume;
80 SetVolumeInternal(); 95 SetVolumeInternal();
81 } 96 }
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 DCHECK(media_codec_bridge_); 166 DCHECK(media_codec_bridge_);
152 167
153 int old_sampling_rate = output_sampling_rate_; 168 int old_sampling_rate = output_sampling_rate_;
154 output_sampling_rate_ = media_codec_bridge_->GetOutputSamplingRate(); 169 output_sampling_rate_ = media_codec_bridge_->GetOutputSamplingRate();
155 if (output_sampling_rate_ != old_sampling_rate) 170 if (output_sampling_rate_ != old_sampling_rate)
156 ResetTimestampHelper(); 171 ResetTimestampHelper();
157 } 172 }
158 173
159 void MediaCodecAudioDecoder::Render(int buffer_index, 174 void MediaCodecAudioDecoder::Render(int buffer_index,
160 size_t size, 175 size_t size,
161 bool render_output, 176 RenderMode render_mode,
162 base::TimeDelta pts, 177 base::TimeDelta pts,
163 bool eos_encountered) { 178 bool eos_encountered) {
164 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); 179 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
165 180
166 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts; 181 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts << " "
182 << AsString(render_mode);
167 183
168 render_output = render_output && (size != 0u); 184 bool do_render = false;
169 185
170 if (render_output) { 186 switch (render_mode) {
171 int64 head_position = 187 case kRenderSkip:
172 (static_cast<AudioCodecBridge*>(media_codec_bridge_.get())) 188 break;
173 ->PlayOutputBuffer(buffer_index, size);
174 189
175 size_t new_frames_count = size / bytes_per_frame_; 190 case kRenderAfterPreroll:
176 frame_count_ += new_frames_count; 191 // Do not bother to play a single last frame after preroll
177 audio_timestamp_helper_->AddFrames(new_frames_count); 192 if (!eos_encountered && size != 0u)
178 int64 frames_to_play = frame_count_ - head_position; 193 CreatePendingBuffer(buffer_index, size, pts);
qinmin 2015/07/31 18:43:10 I prefer we just move the pending buffer to java s
179 DCHECK_GE(frames_to_play, 0); 194 break;
180 195
181 base::TimeDelta last_buffered = audio_timestamp_helper_->GetTimestamp(); 196 case kRenderNow:
182 base::TimeDelta now_playing = 197 do_render = (size != 0u);
183 last_buffered - 198 break;
184 audio_timestamp_helper_->GetFrameDuration(frames_to_play); 199 }
185 200
186 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts 201 if (do_render) {
187 << " will play: [" << now_playing << "," << last_buffered << "]"; 202 if (pending_buffer_.IsValid()) {
203 PlayOutputBuffer(kPendingBufferIndex, pending_buffer_.data.size(),
204 pending_buffer_.pts);
205 pending_buffer_.Invalidate();
206 }
188 207
189 media_task_runner_->PostTask( 208 PlayOutputBuffer(buffer_index, size, pts);
190 FROM_HERE,
191 base::Bind(update_current_time_cb_, now_playing, last_buffered));
192 } 209 }
193 210
194 media_codec_bridge_->ReleaseOutputBuffer(buffer_index, false); 211 media_codec_bridge_->ReleaseOutputBuffer(buffer_index, false);
195 212
196 CheckLastFrame(eos_encountered, false); // no delayed tasks 213 CheckLastFrame(eos_encountered, false); // no delayed tasks
197 } 214 }
198 215
216 void MediaCodecAudioDecoder::CreatePendingBuffer(int buffer_index,
217 size_t size,
218 base::TimeDelta pts) {
219 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
220 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " pts:" << pts
221 << " size:" << size;
222
223 pending_buffer_.pts = pts;
224 pending_buffer_.data.resize(size); // capacity should only increase
225
226 DCHECK(media_codec_bridge_);
227 if (!media_codec_bridge_->CopyFromOutputBuffer(
228 buffer_index, 0, &pending_buffer_.data[0], size)) {
229 DVLOG(0) << class_name() << "::" << __FUNCTION__ << ": cannot copy"
230 << " from buffer index:" << buffer_index << " size:" << size;
231 pending_buffer_.Invalidate();
232 }
233 }
234
235 void MediaCodecAudioDecoder::PlayOutputBuffer(int buffer_index,
236 size_t size,
237 base::TimeDelta pts) {
238 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
239
240 AudioCodecBridge* audio_codec =
241 static_cast<AudioCodecBridge*>(media_codec_bridge_.get());
242
243 DCHECK(audio_codec);
244
245 int64 head_position =
246 (buffer_index == kPendingBufferIndex)
247 ? audio_codec->PlayOutputBuffer(&pending_buffer_.data[0], size)
248 : audio_codec->PlayOutputBuffer(buffer_index, size);
249
250 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts
251 << " head_position:" << head_position;
252
253 size_t new_frames_count = size / bytes_per_frame_;
254 frame_count_ += new_frames_count;
255 audio_timestamp_helper_->AddFrames(new_frames_count);
256
257 int64 frames_to_play = frame_count_ - head_position;
258
259 // TODO(timav): remove this DVLOG after more testing.
260 if (frames_to_play < 0) {
261 DVLOG(0) << class_name() << "::" << __FUNCTION__ << " pts:" << pts
262 << " frame_count_ < head_position (" << frame_count_ << " < "
263 << head_position << ")"
264 << " new_frames_count:" << new_frames_count;
265 }
266 DCHECK_GE(frames_to_play, 0);
267
268 base::TimeDelta last_buffered = audio_timestamp_helper_->GetTimestamp();
269 base::TimeDelta now_playing =
270 last_buffered - audio_timestamp_helper_->GetFrameDuration(frames_to_play);
271
272 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts
273 << " will play: [" << now_playing << "," << last_buffered << "]";
274
275 media_task_runner_->PostTask(
276 FROM_HERE,
277 base::Bind(update_current_time_cb_, now_playing, last_buffered));
278 }
279
199 void MediaCodecAudioDecoder::SetVolumeInternal() { 280 void MediaCodecAudioDecoder::SetVolumeInternal() {
200 DCHECK(media_task_runner_->BelongsToCurrentThread()); 281 DCHECK(media_task_runner_->BelongsToCurrentThread());
201 282
202 if (media_codec_bridge_) { 283 if (media_codec_bridge_) {
203 static_cast<AudioCodecBridge*>(media_codec_bridge_.get()) 284 static_cast<AudioCodecBridge*>(media_codec_bridge_.get())
204 ->SetVolume(volume_); 285 ->SetVolume(volume_);
205 } 286 }
206 } 287 }
207 288
208 void MediaCodecAudioDecoder::ResetTimestampHelper() { 289 void MediaCodecAudioDecoder::ResetTimestampHelper() {
209 // Media thread or Decoder thread 290 // Media thread or Decoder thread
210 // When this method is called on Media thread, decoder thread 291 // When this method is called on Media thread, decoder thread
211 // should not be running. 292 // should not be running.
212 293
213 if (audio_timestamp_helper_) 294 if (audio_timestamp_helper_)
214 base_timestamp_ = audio_timestamp_helper_->GetTimestamp(); 295 base_timestamp_ = audio_timestamp_helper_->GetTimestamp();
215 296
216 audio_timestamp_helper_.reset( 297 audio_timestamp_helper_.reset(
217 new AudioTimestampHelper(configs_.audio_sampling_rate)); 298 new AudioTimestampHelper(configs_.audio_sampling_rate));
218 299
219 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_); 300 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_);
220 } 301 }
221 302
222 } // namespace media 303 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698