Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/base/android/media_codec_audio_decoder.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 | |
|
qinmin
2015/05/26 00:49:18
remove extra line
| |
| 10 #include "media/base/android/media_codec_bridge.h" | |
| 11 #include "media/base/audio_timestamp_helper.h" | |
| 12 #include "media/base/demuxer_stream.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // Use 16bit PCM for audio output. Keep this value in sync with the output | |
| 17 // format we passed to AudioTrack in MediaCodecBridge. | |
| 18 const int kBytesPerAudioOutputSample = 2; | |
| 19 } | |
| 20 | |
| 21 namespace media { | |
| 22 | |
| 23 MediaCodecAudioDecoder::MediaCodecAudioDecoder( | |
| 24 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, | |
| 25 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, | |
| 26 const base::Closure& request_data_cb, | |
| 27 const base::Closure& starvation_cb, | |
| 28 const base::Closure& stop_done_cb, | |
| 29 const base::Closure& error_cb, | |
| 30 const SetTimeCallback& update_current_time_cb) | |
| 31 : MediaCodecDecoder(media_task_runner, | |
| 32 ui_task_runner, | |
| 33 request_data_cb, | |
| 34 starvation_cb, | |
| 35 stop_done_cb, | |
| 36 error_cb, | |
| 37 "AudioDecoder"), | |
| 38 volume_(-1.0), | |
| 39 bytes_per_frame_(0), | |
| 40 frame_count_(0), | |
| 41 update_current_time_cb_(update_current_time_cb) { | |
| 42 } | |
| 43 | |
| 44 MediaCodecAudioDecoder::~MediaCodecAudioDecoder() { | |
| 45 DVLOG(1) << "AudioDecoder::~AudioDecoder()"; | |
| 46 } | |
| 47 | |
| 48 void MediaCodecAudioDecoder::SetVolume(double volume) { | |
| 49 // Media thread | |
| 50 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 51 | |
| 52 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << volume; | |
| 53 | |
| 54 volume_ = volume; | |
| 55 SetVolumeInternal(); | |
| 56 } | |
| 57 | |
| 58 bool MediaCodecAudioDecoder::HasStream() const { | |
| 59 // Media thread | |
| 60 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 61 | |
| 62 return configs_.audio_codec != kUnknownAudioCodec; | |
| 63 } | |
| 64 | |
| 65 void MediaCodecAudioDecoder::Flush() { | |
| 66 // Media thread | |
| 67 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 68 | |
| 69 MediaCodecDecoder::Flush(); | |
| 70 frame_count_ = 0; | |
| 71 } | |
| 72 | |
| 73 bool MediaCodecAudioDecoder::IsCodecReconfigureNeeded( | |
| 74 const DemuxerConfigs& curr, | |
| 75 const DemuxerConfigs& next) const { | |
| 76 return curr.audio_codec != next.audio_codec || | |
| 77 curr.audio_channels != next.audio_channels || | |
| 78 curr.audio_sampling_rate != next.audio_sampling_rate || | |
| 79 next.is_audio_encrypted != next.is_audio_encrypted || | |
| 80 curr.audio_extra_data.size() != next.audio_extra_data.size() || | |
| 81 !std::equal(curr.audio_extra_data.begin(), | |
| 82 curr.audio_extra_data.end(), | |
| 83 next.audio_extra_data.begin()); | |
| 84 } | |
| 85 | |
| 86 MediaCodecDecoder::ConfigStatus | |
| 87 MediaCodecAudioDecoder::ConfigureInternal() { | |
| 88 // Media thread | |
| 89 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 90 | |
| 91 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
| 92 | |
| 93 media_codec_bridge_.reset(AudioCodecBridge::Create(configs_.audio_codec)); | |
| 94 if (!media_codec_bridge_) | |
| 95 return CONFIG_FAILURE; | |
| 96 | |
| 97 if (!(static_cast<AudioCodecBridge*>(media_codec_bridge_.get()))->Start( | |
| 98 configs_.audio_codec, | |
| 99 configs_.audio_sampling_rate, | |
| 100 configs_.audio_channels, | |
| 101 &configs_.audio_extra_data[0], | |
| 102 configs_.audio_extra_data.size(), | |
| 103 configs_.audio_codec_delay_ns, | |
| 104 configs_.audio_seek_preroll_ns, | |
| 105 true, | |
| 106 GetMediaCrypto().obj())) { | |
| 107 | |
| 108 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " failed"; | |
| 109 | |
| 110 media_codec_bridge_.reset(); | |
| 111 return CONFIG_FAILURE; | |
| 112 } | |
| 113 | |
| 114 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " succeeded"; | |
| 115 | |
| 116 SetVolumeInternal(); | |
| 117 | |
| 118 bytes_per_frame_ = kBytesPerAudioOutputSample * configs_.audio_channels; | |
| 119 frame_count_ = 0; | |
| 120 ResetTimestampHelper(); | |
| 121 | |
| 122 return CONFIG_OK; | |
| 123 } | |
| 124 | |
| 125 void MediaCodecAudioDecoder::SetBaseTimestamp(base::TimeDelta base_timestamp) { | |
| 126 // Media thread | |
| 127 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 128 | |
| 129 DVLOG(1) << __FUNCTION__ << " " << base_timestamp; | |
| 130 | |
| 131 base_timestamp_ = base_timestamp; | |
| 132 if (audio_timestamp_helper_) | |
| 133 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_); | |
| 134 } | |
| 135 | |
| 136 void MediaCodecAudioDecoder::ResetTimestampHelper() { | |
| 137 // Media thread | |
| 138 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 139 | |
| 140 if (audio_timestamp_helper_) | |
| 141 base_timestamp_ = audio_timestamp_helper_->GetTimestamp(); | |
| 142 | |
| 143 audio_timestamp_helper_.reset( | |
| 144 new AudioTimestampHelper(configs_.audio_sampling_rate)); | |
| 145 | |
| 146 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_); | |
| 147 } | |
| 148 | |
| 149 void MediaCodecAudioDecoder::SetVolumeInternal() { | |
| 150 // Media thread | |
| 151 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 152 | |
| 153 if (media_codec_bridge_) { | |
| 154 static_cast<AudioCodecBridge*>(media_codec_bridge_.get())->SetVolume( | |
| 155 volume_); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 void MediaCodecAudioDecoder::Render(int buffer_index, | |
| 160 size_t size, | |
| 161 bool render_output, | |
| 162 base::TimeDelta pts, | |
| 163 bool eos_encountered) { | |
| 164 // Decoder thread | |
| 165 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
| 166 | |
| 167 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts; | |
| 168 | |
| 169 render_output = render_output && (size != 0u); | |
| 170 | |
| 171 if (render_output) { | |
| 172 int64 head_position = (static_cast<AudioCodecBridge*>( | |
| 173 media_codec_bridge_.get()))->PlayOutputBuffer( | |
| 174 buffer_index, size); | |
| 175 | |
| 176 size_t new_frames_count = size / bytes_per_frame_; | |
| 177 frame_count_ += new_frames_count; | |
| 178 audio_timestamp_helper_->AddFrames(new_frames_count); | |
| 179 int64 frames_to_play = frame_count_ - head_position; | |
| 180 DCHECK_GE(frames_to_play, 0); | |
| 181 | |
| 182 base::TimeDelta last_buffered = audio_timestamp_helper_->GetTimestamp(); | |
| 183 base::TimeDelta now_playing = | |
| 184 last_buffered - | |
| 185 audio_timestamp_helper_->GetFrameDuration(frames_to_play); | |
| 186 | |
| 187 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts | |
| 188 << " will play: [" << now_playing << "," << last_buffered << "]"; | |
| 189 | |
| 190 update_current_time_cb_.Run(now_playing, last_buffered); | |
| 191 } | |
| 192 | |
| 193 media_codec_bridge_->ReleaseOutputBuffer(buffer_index, false); | |
| 194 | |
| 195 CheckLastFrame(eos_encountered, false); // no delayed tasks | |
| 196 } | |
| 197 | |
| 198 } // namespace media | |
| OLD | NEW |