Chromium Code Reviews| 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 "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), |
| 44 #ifndef NDEBUG | |
| 45 num_postponed_buffers_(0), | |
| 46 #endif | |
| 39 update_current_time_cb_(update_current_time_cb) { | 47 update_current_time_cb_(update_current_time_cb) { |
| 40 } | 48 } |
| 41 | 49 |
| 42 MediaCodecAudioDecoder::~MediaCodecAudioDecoder() { | 50 MediaCodecAudioDecoder::~MediaCodecAudioDecoder() { |
| 43 DVLOG(1) << "AudioDecoder::~AudioDecoder()"; | 51 DVLOG(1) << "AudioDecoder::~AudioDecoder()"; |
| 44 ReleaseDecoderResources(); | 52 ReleaseDecoderResources(); |
| 45 } | 53 } |
| 46 | 54 |
| 47 const char* MediaCodecAudioDecoder::class_name() const { | 55 const char* MediaCodecAudioDecoder::class_name() const { |
| 48 return "AudioDecoder"; | 56 return "AudioDecoder"; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 62 configs_ = configs; | 70 configs_ = configs; |
| 63 if (!media_codec_bridge_) | 71 if (!media_codec_bridge_) |
| 64 output_sampling_rate_ = configs.audio_sampling_rate; | 72 output_sampling_rate_ = configs.audio_sampling_rate; |
| 65 } | 73 } |
| 66 | 74 |
| 67 void MediaCodecAudioDecoder::Flush() { | 75 void MediaCodecAudioDecoder::Flush() { |
| 68 DCHECK(media_task_runner_->BelongsToCurrentThread()); | 76 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 69 | 77 |
| 70 MediaCodecDecoder::Flush(); | 78 MediaCodecDecoder::Flush(); |
| 71 frame_count_ = 0; | 79 frame_count_ = 0; |
| 80 | |
| 81 #ifndef NDEBUG | |
| 82 // Maintain the number of postponed buffers corresponding to the Java side. | |
| 83 num_postponed_buffers_ = 0; | |
| 84 #endif | |
| 85 } | |
| 86 | |
| 87 void MediaCodecAudioDecoder::ReleaseMediaCodec() { | |
| 88 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 89 MediaCodecDecoder::ReleaseMediaCodec(); | |
| 90 | |
| 91 #ifndef NDEBUG | |
| 92 // Maintain the number of postponed buffers corresponding to the Java side. | |
| 93 num_postponed_buffers_ = 0; | |
| 94 #endif | |
| 72 } | 95 } |
| 73 | 96 |
| 74 void MediaCodecAudioDecoder::SetVolume(double volume) { | 97 void MediaCodecAudioDecoder::SetVolume(double volume) { |
| 75 DCHECK(media_task_runner_->BelongsToCurrentThread()); | 98 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 76 | 99 |
| 77 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << volume; | 100 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << volume; |
| 78 | 101 |
| 79 volume_ = volume; | 102 volume_ = volume; |
| 80 SetVolumeInternal(); | 103 SetVolumeInternal(); |
| 81 } | 104 } |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 DCHECK(media_codec_bridge_); | 174 DCHECK(media_codec_bridge_); |
| 152 | 175 |
| 153 int old_sampling_rate = output_sampling_rate_; | 176 int old_sampling_rate = output_sampling_rate_; |
| 154 output_sampling_rate_ = media_codec_bridge_->GetOutputSamplingRate(); | 177 output_sampling_rate_ = media_codec_bridge_->GetOutputSamplingRate(); |
| 155 if (output_sampling_rate_ != old_sampling_rate) | 178 if (output_sampling_rate_ != old_sampling_rate) |
| 156 ResetTimestampHelper(); | 179 ResetTimestampHelper(); |
| 157 } | 180 } |
| 158 | 181 |
| 159 void MediaCodecAudioDecoder::Render(int buffer_index, | 182 void MediaCodecAudioDecoder::Render(int buffer_index, |
| 160 size_t size, | 183 size_t size, |
| 161 bool render_output, | 184 RenderMode render_mode, |
| 162 base::TimeDelta pts, | 185 base::TimeDelta pts, |
| 163 bool eos_encountered) { | 186 bool eos_encountered) { |
| 164 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | 187 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); |
| 165 | 188 |
| 166 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts; | 189 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts << " " |
| 190 << AsString(render_mode); | |
| 167 | 191 |
| 168 render_output = render_output && (size != 0u); | 192 const bool do_play = (render_mode != kRenderSkip); |
| 169 | 193 |
| 170 if (render_output) { | 194 if (do_play) { |
| 195 AudioCodecBridge* audio_codec = | |
| 196 static_cast<AudioCodecBridge*>(media_codec_bridge_.get()); | |
| 197 | |
| 198 DCHECK(audio_codec); | |
| 199 | |
| 200 const bool postpone = (render_mode == kRenderAfterPreroll); | |
| 201 | |
| 202 #ifndef NDEBUG | |
| 203 // MediaCodecBrdge.java only keeps one buffer postponed buffer, but we | |
| 204 // increase |frame_count_| every time we call PlayOutputBuffer(). | |
| 205 // Check that we are in balance with MediaCodecBrdge.java. | |
| 206 // TODO(timav): It seems better to let Java side keep an array of | |
| 207 // postponed buffers and drop this check. | |
| 208 if (postpone) | |
|
Tima Vaisburd
2015/08/01 04:34:43
In this patch Java side already has the list of pe
| |
| 209 ++num_postponed_buffers_; | |
| 210 else | |
| 211 num_postponed_buffers_ = 0; | |
| 212 DCHECK_GE(1, num_postponed_buffers_); | |
| 213 #endif | |
| 214 | |
| 171 int64 head_position = | 215 int64 head_position = |
| 172 (static_cast<AudioCodecBridge*>(media_codec_bridge_.get())) | 216 audio_codec->PlayOutputBuffer(buffer_index, size, postpone); |
| 173 ->PlayOutputBuffer(buffer_index, size); | 217 |
| 218 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts | |
| 219 << (postpone ? " POSTPONE" : "") | |
| 220 << " head_position:" << head_position; | |
| 174 | 221 |
| 175 size_t new_frames_count = size / bytes_per_frame_; | 222 size_t new_frames_count = size / bytes_per_frame_; |
| 176 frame_count_ += new_frames_count; | 223 frame_count_ += new_frames_count; |
| 177 audio_timestamp_helper_->AddFrames(new_frames_count); | 224 audio_timestamp_helper_->AddFrames(new_frames_count); |
| 178 int64 frames_to_play = frame_count_ - head_position; | |
| 179 DCHECK_GE(frames_to_play, 0); | |
| 180 | 225 |
| 181 base::TimeDelta last_buffered = audio_timestamp_helper_->GetTimestamp(); | 226 if (!postpone) { |
| 182 base::TimeDelta now_playing = | 227 int64 frames_to_play = frame_count_ - head_position; |
| 183 last_buffered - | |
| 184 audio_timestamp_helper_->GetFrameDuration(frames_to_play); | |
| 185 | 228 |
| 186 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts | 229 // TODO(timav): remove this DVLOG after more testing. |
| 187 << " will play: [" << now_playing << "," << last_buffered << "]"; | 230 if (frames_to_play < 0) { |
| 231 DVLOG(0) << class_name() << "::" << __FUNCTION__ << " pts:" << pts | |
| 232 << " frame_count_ < head_position (" << frame_count_ << " < " | |
| 233 << head_position << ")" | |
| 234 << " new_frames_count:" << new_frames_count; | |
| 235 } | |
| 236 DCHECK_GE(frames_to_play, 0); | |
| 188 | 237 |
| 189 media_task_runner_->PostTask( | 238 base::TimeDelta last_buffered = audio_timestamp_helper_->GetTimestamp(); |
| 190 FROM_HERE, | 239 base::TimeDelta now_playing = |
| 191 base::Bind(update_current_time_cb_, now_playing, last_buffered)); | 240 last_buffered - |
| 241 audio_timestamp_helper_->GetFrameDuration(frames_to_play); | |
| 242 | |
| 243 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts | |
| 244 << " will play: [" << now_playing << "," << last_buffered << "]"; | |
| 245 | |
| 246 media_task_runner_->PostTask( | |
| 247 FROM_HERE, | |
| 248 base::Bind(update_current_time_cb_, now_playing, last_buffered)); | |
| 249 } | |
| 192 } | 250 } |
| 193 | 251 |
| 194 media_codec_bridge_->ReleaseOutputBuffer(buffer_index, false); | 252 media_codec_bridge_->ReleaseOutputBuffer(buffer_index, false); |
| 195 | 253 |
| 196 CheckLastFrame(eos_encountered, false); // no delayed tasks | 254 CheckLastFrame(eos_encountered, false); // no delayed tasks |
| 197 } | 255 } |
| 198 | 256 |
| 199 void MediaCodecAudioDecoder::SetVolumeInternal() { | 257 void MediaCodecAudioDecoder::SetVolumeInternal() { |
| 200 DCHECK(media_task_runner_->BelongsToCurrentThread()); | 258 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 201 | 259 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 213 if (audio_timestamp_helper_) | 271 if (audio_timestamp_helper_) |
| 214 base_timestamp_ = audio_timestamp_helper_->GetTimestamp(); | 272 base_timestamp_ = audio_timestamp_helper_->GetTimestamp(); |
| 215 | 273 |
| 216 audio_timestamp_helper_.reset( | 274 audio_timestamp_helper_.reset( |
| 217 new AudioTimestampHelper(configs_.audio_sampling_rate)); | 275 new AudioTimestampHelper(configs_.audio_sampling_rate)); |
| 218 | 276 |
| 219 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_); | 277 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_); |
| 220 } | 278 } |
| 221 | 279 |
| 222 } // namespace media | 280 } // namespace media |
| OLD | NEW |