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