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:19
remove extra line
Tima Vaisburd
2015/05/28 02:00:34
Done.
| |
| 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 output_sampling_rate_(0), | |
| 41 frame_count_(0), | |
| 42 update_current_time_cb_(update_current_time_cb) { | |
| 43 } | |
| 44 | |
| 45 MediaCodecAudioDecoder::~MediaCodecAudioDecoder() { | |
| 46 DVLOG(1) << "AudioDecoder::~AudioDecoder()"; | |
| 47 } | |
| 48 | |
| 49 void MediaCodecAudioDecoder::SetVolume(double volume) { | |
| 50 // Media thread | |
| 51 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 52 | |
| 53 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << volume; | |
| 54 | |
| 55 volume_ = volume; | |
| 56 SetVolumeInternal(); | |
| 57 } | |
| 58 | |
| 59 bool MediaCodecAudioDecoder::HasStream() const { | |
| 60 // Media thread | |
| 61 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 62 | |
| 63 return configs_.audio_codec != kUnknownAudioCodec; | |
| 64 } | |
| 65 | |
| 66 void MediaCodecAudioDecoder::Flush() { | |
| 67 // Media thread | |
| 68 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 69 | |
| 70 MediaCodecDecoder::Flush(); | |
| 71 frame_count_ = 0; | |
| 72 } | |
| 73 | |
| 74 void MediaCodecAudioDecoder::SetDemuxerConfigs(const DemuxerConfigs& configs) { | |
| 75 // Media thread | |
| 76 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 77 | |
| 78 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
| 79 | |
| 80 configs_ = configs; | |
| 81 if (!media_codec_bridge_) | |
| 82 output_sampling_rate_ = configs.audio_sampling_rate; | |
| 83 } | |
| 84 | |
| 85 | |
| 86 bool MediaCodecAudioDecoder::IsCodecReconfigureNeeded( | |
| 87 const DemuxerConfigs& curr, | |
| 88 const DemuxerConfigs& next) const { | |
| 89 return curr.audio_codec != next.audio_codec || | |
| 90 curr.audio_channels != next.audio_channels || | |
| 91 curr.audio_sampling_rate != next.audio_sampling_rate || | |
| 92 next.is_audio_encrypted != next.is_audio_encrypted || | |
| 93 curr.audio_extra_data.size() != next.audio_extra_data.size() || | |
| 94 !std::equal(curr.audio_extra_data.begin(), | |
| 95 curr.audio_extra_data.end(), | |
| 96 next.audio_extra_data.begin()); | |
| 97 } | |
| 98 | |
| 99 MediaCodecDecoder::ConfigStatus | |
| 100 MediaCodecAudioDecoder::ConfigureInternal() { | |
| 101 // Media thread | |
| 102 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 103 | |
| 104 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
| 105 | |
| 106 media_codec_bridge_.reset(AudioCodecBridge::Create(configs_.audio_codec)); | |
| 107 if (!media_codec_bridge_) | |
| 108 return CONFIG_FAILURE; | |
| 109 | |
| 110 if (!(static_cast<AudioCodecBridge*>(media_codec_bridge_.get()))->Start( | |
| 111 configs_.audio_codec, | |
| 112 configs_.audio_sampling_rate, | |
| 113 configs_.audio_channels, | |
| 114 &configs_.audio_extra_data[0], | |
| 115 configs_.audio_extra_data.size(), | |
| 116 configs_.audio_codec_delay_ns, | |
| 117 configs_.audio_seek_preroll_ns, | |
| 118 true, | |
| 119 GetMediaCrypto().obj())) { | |
| 120 | |
| 121 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " failed"; | |
| 122 | |
| 123 media_codec_bridge_.reset(); | |
| 124 return CONFIG_FAILURE; | |
| 125 } | |
| 126 | |
| 127 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " succeeded"; | |
| 128 | |
| 129 SetVolumeInternal(); | |
| 130 | |
| 131 bytes_per_frame_ = kBytesPerAudioOutputSample * configs_.audio_channels; | |
| 132 frame_count_ = 0; | |
| 133 ResetTimestampHelper(); | |
| 134 | |
| 135 return CONFIG_OK; | |
| 136 } | |
| 137 | |
| 138 void MediaCodecAudioDecoder::SetBaseTimestamp(base::TimeDelta base_timestamp) { | |
| 139 // Media thread | |
| 140 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 141 | |
| 142 DVLOG(1) << __FUNCTION__ << " " << base_timestamp; | |
| 143 | |
| 144 base_timestamp_ = base_timestamp; | |
| 145 if (audio_timestamp_helper_) | |
| 146 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_); | |
| 147 } | |
| 148 | |
| 149 void MediaCodecAudioDecoder::ResetTimestampHelper() { | |
| 150 // Media thread or Decoder thread | |
| 151 // When this method is called on Media thread, decoder thread | |
| 152 // should not be running. | |
| 153 | |
| 154 if (audio_timestamp_helper_) | |
| 155 base_timestamp_ = audio_timestamp_helper_->GetTimestamp(); | |
| 156 | |
| 157 audio_timestamp_helper_.reset( | |
| 158 new AudioTimestampHelper(configs_.audio_sampling_rate)); | |
| 159 | |
| 160 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_); | |
| 161 } | |
| 162 | |
| 163 void MediaCodecAudioDecoder::SetVolumeInternal() { | |
| 164 // Media thread | |
| 165 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 166 | |
| 167 if (media_codec_bridge_) { | |
| 168 static_cast<AudioCodecBridge*>(media_codec_bridge_.get())->SetVolume( | |
| 169 volume_); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 void MediaCodecAudioDecoder::OnOutputFormatChanged() { | |
| 174 // Decoder thread | |
| 175 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
| 176 | |
| 177 DCHECK(media_codec_bridge_); | |
| 178 | |
| 179 int old_sampling_rate = output_sampling_rate_; | |
| 180 output_sampling_rate_ = media_codec_bridge_->GetOutputSamplingRate(); | |
| 181 if (output_sampling_rate_ != old_sampling_rate) | |
| 182 ResetTimestampHelper(); | |
| 183 } | |
| 184 | |
| 185 void MediaCodecAudioDecoder::Render(int buffer_index, | |
| 186 size_t size, | |
| 187 bool render_output, | |
| 188 base::TimeDelta pts, | |
| 189 bool eos_encountered) { | |
| 190 // Decoder thread | |
| 191 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
| 192 | |
| 193 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts; | |
| 194 | |
| 195 render_output = render_output && (size != 0u); | |
| 196 | |
| 197 if (render_output) { | |
| 198 int64 head_position = (static_cast<AudioCodecBridge*>( | |
| 199 media_codec_bridge_.get()))->PlayOutputBuffer( | |
| 200 buffer_index, size); | |
| 201 | |
| 202 size_t new_frames_count = size / bytes_per_frame_; | |
| 203 frame_count_ += new_frames_count; | |
| 204 audio_timestamp_helper_->AddFrames(new_frames_count); | |
| 205 int64 frames_to_play = frame_count_ - head_position; | |
| 206 DCHECK_GE(frames_to_play, 0); | |
| 207 | |
| 208 base::TimeDelta last_buffered = audio_timestamp_helper_->GetTimestamp(); | |
| 209 base::TimeDelta now_playing = | |
| 210 last_buffered - | |
| 211 audio_timestamp_helper_->GetFrameDuration(frames_to_play); | |
| 212 | |
| 213 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts | |
| 214 << " will play: [" << now_playing << "," << last_buffered << "]"; | |
| 215 | |
| 216 update_current_time_cb_.Run(now_playing, last_buffered); | |
| 217 } | |
| 218 | |
| 219 media_codec_bridge_->ReleaseOutputBuffer(buffer_index, false); | |
| 220 | |
| 221 CheckLastFrame(eos_encountered, false); // no delayed tasks | |
| 222 } | |
| 223 | |
| 224 } // namespace media | |
| OLD | NEW |