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

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: Added unit tests that check A/V sync after preroll 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
« no previous file with comments | « media/base/android/media_codec_audio_decoder.h ('k') | media/base/android/media_codec_bridge.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 22 matching lines...) Expand all
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;
72 } 77 }
73 78
79 void MediaCodecAudioDecoder::ReleaseMediaCodec() {
80 DCHECK(media_task_runner_->BelongsToCurrentThread());
81 MediaCodecDecoder::ReleaseMediaCodec();
82 }
83
74 void MediaCodecAudioDecoder::SetVolume(double volume) { 84 void MediaCodecAudioDecoder::SetVolume(double volume) {
75 DCHECK(media_task_runner_->BelongsToCurrentThread()); 85 DCHECK(media_task_runner_->BelongsToCurrentThread());
76 86
77 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << volume; 87 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << volume;
78 88
79 volume_ = volume; 89 volume_ = volume;
80 SetVolumeInternal(); 90 SetVolumeInternal();
81 } 91 }
82 92
83 void MediaCodecAudioDecoder::SetBaseTimestamp(base::TimeDelta base_timestamp) { 93 void MediaCodecAudioDecoder::SetBaseTimestamp(base::TimeDelta base_timestamp) {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 DCHECK(media_codec_bridge_); 161 DCHECK(media_codec_bridge_);
152 162
153 int old_sampling_rate = output_sampling_rate_; 163 int old_sampling_rate = output_sampling_rate_;
154 output_sampling_rate_ = media_codec_bridge_->GetOutputSamplingRate(); 164 output_sampling_rate_ = media_codec_bridge_->GetOutputSamplingRate();
155 if (output_sampling_rate_ != old_sampling_rate) 165 if (output_sampling_rate_ != old_sampling_rate)
156 ResetTimestampHelper(); 166 ResetTimestampHelper();
157 } 167 }
158 168
159 void MediaCodecAudioDecoder::Render(int buffer_index, 169 void MediaCodecAudioDecoder::Render(int buffer_index,
160 size_t size, 170 size_t size,
161 bool render_output, 171 RenderMode render_mode,
162 base::TimeDelta pts, 172 base::TimeDelta pts,
163 bool eos_encountered) { 173 bool eos_encountered) {
164 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); 174 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
165 175
166 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts; 176 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts << " "
177 << AsString(render_mode);
167 178
168 render_output = render_output && (size != 0u); 179 const bool do_play = (render_mode != kRenderSkip);
169 180
170 if (render_output) { 181 if (do_play) {
182 AudioCodecBridge* audio_codec =
183 static_cast<AudioCodecBridge*>(media_codec_bridge_.get());
184
185 DCHECK(audio_codec);
186
187 const bool postpone = (render_mode == kRenderAfterPreroll);
188
171 int64 head_position = 189 int64 head_position =
172 (static_cast<AudioCodecBridge*>(media_codec_bridge_.get())) 190 audio_codec->PlayOutputBuffer(buffer_index, size, postpone);
173 ->PlayOutputBuffer(buffer_index, size); 191
192 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts
193 << (postpone ? " POSTPONE" : "")
194 << " head_position:" << head_position;
174 195
175 size_t new_frames_count = size / bytes_per_frame_; 196 size_t new_frames_count = size / bytes_per_frame_;
176 frame_count_ += new_frames_count; 197 frame_count_ += new_frames_count;
177 audio_timestamp_helper_->AddFrames(new_frames_count); 198 audio_timestamp_helper_->AddFrames(new_frames_count);
178 int64 frames_to_play = frame_count_ - head_position;
179 DCHECK_GE(frames_to_play, 0);
180 199
181 base::TimeDelta last_buffered = audio_timestamp_helper_->GetTimestamp(); 200 if (!postpone) {
182 base::TimeDelta now_playing = 201 int64 frames_to_play = frame_count_ - head_position;
183 last_buffered -
184 audio_timestamp_helper_->GetFrameDuration(frames_to_play);
185 202
186 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts 203 // TODO(timav): remove this DVLOG after more testing.
187 << " will play: [" << now_playing << "," << last_buffered << "]"; 204 if (frames_to_play < 0) {
qinmin 2015/08/05 18:54:02 simply DCHECK_GE(frames_to_play, 0) << "......"
Tima Vaisburd 2015/08/06 00:12:37 Done.
205 DVLOG(0) << class_name() << "::" << __FUNCTION__ << " pts:" << pts
206 << " frame_count_ < head_position (" << frame_count_ << " < "
207 << head_position << ")"
208 << " new_frames_count:" << new_frames_count;
209 }
210 DCHECK_GE(frames_to_play, 0);
188 211
189 media_task_runner_->PostTask( 212 base::TimeDelta last_buffered = audio_timestamp_helper_->GetTimestamp();
190 FROM_HERE, 213 base::TimeDelta now_playing =
191 base::Bind(update_current_time_cb_, now_playing, last_buffered)); 214 last_buffered -
215 audio_timestamp_helper_->GetFrameDuration(frames_to_play);
216
217 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts
218 << " will play: [" << now_playing << "," << last_buffered << "]";
219
220 media_task_runner_->PostTask(
221 FROM_HERE,
222 base::Bind(update_current_time_cb_, now_playing, last_buffered));
223 }
192 } 224 }
193 225
194 media_codec_bridge_->ReleaseOutputBuffer(buffer_index, false); 226 media_codec_bridge_->ReleaseOutputBuffer(buffer_index, false);
195 227
196 CheckLastFrame(eos_encountered, false); // no delayed tasks 228 CheckLastFrame(eos_encountered, false); // no delayed tasks
197 } 229 }
198 230
199 void MediaCodecAudioDecoder::SetVolumeInternal() { 231 void MediaCodecAudioDecoder::SetVolumeInternal() {
200 DCHECK(media_task_runner_->BelongsToCurrentThread()); 232 DCHECK(media_task_runner_->BelongsToCurrentThread());
201 233
(...skipping 11 matching lines...) Expand all
213 if (audio_timestamp_helper_) 245 if (audio_timestamp_helper_)
214 base_timestamp_ = audio_timestamp_helper_->GetTimestamp(); 246 base_timestamp_ = audio_timestamp_helper_->GetTimestamp();
215 247
216 audio_timestamp_helper_.reset( 248 audio_timestamp_helper_.reset(
217 new AudioTimestampHelper(configs_.audio_sampling_rate)); 249 new AudioTimestampHelper(configs_.audio_sampling_rate));
218 250
219 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_); 251 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_);
220 } 252 }
221 253
222 } // namespace media 254 } // namespace media
OLDNEW
« no previous file with comments | « media/base/android/media_codec_audio_decoder.h ('k') | media/base/android/media_codec_bridge.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698