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