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