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