| 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, ui_task_runner, |
| 34 request_data_cb, starvation_cb, |
| 35 stop_done_cb, error_cb, update_current_time_cb, |
| 36 "AudioDecoder"), |
| 37 volume_(-1.0), |
| 38 bytes_per_frame_(0), |
| 39 frame_count_(0) |
| 40 {} |
| 41 |
| 42 MediaCodecAudioDecoder::~MediaCodecAudioDecoder() |
| 43 { |
| 44 DVLOG(1) << "AudioDecoder::~AudioDecoder()"; |
| 45 } |
| 46 |
| 47 void MediaCodecAudioDecoder::SetVolume(double volume) { |
| 48 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << volume; |
| 49 |
| 50 volume_ = volume; |
| 51 SetVolumeInternal(); |
| 52 } |
| 53 |
| 54 bool MediaCodecAudioDecoder::HasStream() const { |
| 55 return configs_.audio_codec != kUnknownAudioCodec; |
| 56 } |
| 57 |
| 58 void MediaCodecAudioDecoder::Flush() { |
| 59 MediaCodecDecoder::Flush(); |
| 60 frame_count_ = 0; |
| 61 } |
| 62 |
| 63 bool MediaCodecAudioDecoder::IsCodecReconfigureNeeded( |
| 64 const DemuxerConfigs& curr, |
| 65 const DemuxerConfigs& next) const { |
| 66 return curr.audio_codec != next.audio_codec || |
| 67 curr.audio_channels != next.audio_channels || |
| 68 curr.audio_sampling_rate != next.audio_sampling_rate || |
| 69 next.is_audio_encrypted != next.is_audio_encrypted || |
| 70 curr.audio_extra_data.size() != next.audio_extra_data.size() || |
| 71 !std::equal(curr.audio_extra_data.begin(), |
| 72 curr.audio_extra_data.end(), |
| 73 next.audio_extra_data.begin()); |
| 74 } |
| 75 |
| 76 MediaCodecDecoder::ConfigStatus |
| 77 MediaCodecAudioDecoder::ConfigureInternal() { |
| 78 // Media thread |
| 79 DVLOG(1) << class_name() << "::" << __FUNCTION__; |
| 80 |
| 81 media_codec_bridge_.reset(AudioCodecBridge::Create(configs_.audio_codec)); |
| 82 if (!media_codec_bridge_) |
| 83 return CONFIG_FAILURE; |
| 84 |
| 85 if (!(static_cast<AudioCodecBridge*>(media_codec_bridge_.get()))->Start( |
| 86 configs_.audio_codec, |
| 87 configs_.audio_sampling_rate, |
| 88 configs_.audio_channels, |
| 89 &configs_.audio_extra_data[0], |
| 90 configs_.audio_extra_data.size(), |
| 91 configs_.audio_codec_delay_ns, |
| 92 configs_.audio_seek_preroll_ns, |
| 93 true, |
| 94 GetMediaCrypto().obj())) { |
| 95 |
| 96 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " failed"; |
| 97 |
| 98 media_codec_bridge_.reset(); |
| 99 return CONFIG_FAILURE; |
| 100 } |
| 101 |
| 102 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " succeeded"; |
| 103 |
| 104 SetVolumeInternal(); |
| 105 |
| 106 bytes_per_frame_ = kBytesPerAudioOutputSample * configs_.audio_channels; |
| 107 frame_count_ = 0; |
| 108 ResetTimestampHelper(); |
| 109 |
| 110 return CONFIG_OK; |
| 111 } |
| 112 |
| 113 void MediaCodecAudioDecoder::SetBaseTimestamp(base::TimeDelta base_timestamp) { |
| 114 // Media thread |
| 115 DVLOG(1) << __FUNCTION__ << " " << base_timestamp; |
| 116 |
| 117 base_timestamp_ = base_timestamp; |
| 118 if (audio_timestamp_helper_) |
| 119 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_); |
| 120 } |
| 121 |
| 122 void MediaCodecAudioDecoder::ResetTimestampHelper() { |
| 123 // Media thread |
| 124 if (audio_timestamp_helper_) |
| 125 base_timestamp_ = audio_timestamp_helper_->GetTimestamp(); |
| 126 |
| 127 audio_timestamp_helper_.reset( |
| 128 new AudioTimestampHelper(configs_.audio_sampling_rate)); |
| 129 |
| 130 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_); |
| 131 } |
| 132 |
| 133 void MediaCodecAudioDecoder::SetVolumeInternal() { |
| 134 // Media thread |
| 135 if (media_codec_bridge_) { |
| 136 static_cast<AudioCodecBridge*>(media_codec_bridge_.get())->SetVolume( |
| 137 volume_); |
| 138 } |
| 139 } |
| 140 |
| 141 void MediaCodecAudioDecoder::Render(int buffer_index, |
| 142 size_t size, |
| 143 bool render_output, |
| 144 base::TimeDelta pts, |
| 145 bool eos_encountered) { |
| 146 // Decoder thread |
| 147 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts; |
| 148 |
| 149 render_output = render_output && (size != 0u); |
| 150 |
| 151 if (render_output) { |
| 152 int64 head_position = (static_cast<AudioCodecBridge*>( |
| 153 media_codec_bridge_.get()))->PlayOutputBuffer( |
| 154 buffer_index, size); |
| 155 |
| 156 size_t new_frames_count = size / bytes_per_frame_; |
| 157 frame_count_ += new_frames_count; |
| 158 audio_timestamp_helper_->AddFrames(new_frames_count); |
| 159 int64 frames_to_play = frame_count_ - head_position; |
| 160 DCHECK_GE(frames_to_play, 0); |
| 161 |
| 162 base::TimeDelta last_buffered = audio_timestamp_helper_->GetTimestamp(); |
| 163 base::TimeDelta now_playing = |
| 164 last_buffered - |
| 165 audio_timestamp_helper_->GetFrameDuration(frames_to_play); |
| 166 |
| 167 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts |
| 168 << " will play: [" << now_playing << "," << last_buffered << "]"; |
| 169 |
| 170 update_current_time_cb_.Run(now_playing, last_buffered); |
| 171 } |
| 172 |
| 173 media_codec_bridge_->ReleaseOutputBuffer(buffer_index, false); |
| 174 |
| 175 if (GetState() == STOPPING || eos_encountered) { |
| 176 media_task_runner_->PostTask( |
| 177 FROM_HERE, |
| 178 base::Bind(&MediaCodecDecoder::OnLastFrameRendered, |
| 179 weak_this_, eos_encountered)); |
| 180 last_frame_posted_ = true; |
| 181 } |
| 182 } |
| 183 |
| 184 } // namespace media |
| OLD | NEW |