OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/base/android/media_codec_audio_decoder.h" | 5 #include "media/base/android/media_codec_audio_decoder.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "media/base/android/media_codec_bridge.h" | 9 #include "media/base/android/media_codec_bridge.h" |
10 #include "media/base/audio_timestamp_helper.h" | 10 #include "media/base/audio_timestamp_helper.h" |
11 #include "media/base/demuxer_stream.h" | 11 #include "media/base/demuxer_stream.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // Use 16bit PCM for audio output. Keep this value in sync with the output | 15 // Use 16bit PCM for audio output. Keep this value in sync with the output |
16 // format we passed to AudioTrack in MediaCodecBridge. | 16 // format we passed to AudioTrack in MediaCodecBridge. |
17 const int kBytesPerAudioOutputSample = 2; | 17 const int kBytesPerAudioOutputSample = 2; |
18 } | 18 } |
19 | 19 |
20 namespace media { | 20 namespace media { |
21 | 21 |
22 MediaCodecAudioDecoder::MediaCodecAudioDecoder( | 22 MediaCodecAudioDecoder::MediaCodecAudioDecoder( |
23 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, | 23 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, |
24 const base::Closure& request_data_cb, | 24 const base::Closure& request_data_cb, |
25 const base::Closure& starvation_cb, | 25 const base::Closure& starvation_cb, |
| 26 const base::Closure& decoder_drained_cb, |
26 const base::Closure& stop_done_cb, | 27 const base::Closure& stop_done_cb, |
27 const base::Closure& error_cb, | 28 const base::Closure& error_cb, |
28 const SetTimeCallback& update_current_time_cb) | 29 const SetTimeCallback& update_current_time_cb) |
29 : MediaCodecDecoder(media_task_runner, | 30 : MediaCodecDecoder(media_task_runner, |
30 request_data_cb, | 31 request_data_cb, |
31 starvation_cb, | 32 starvation_cb, |
| 33 decoder_drained_cb, |
32 stop_done_cb, | 34 stop_done_cb, |
33 error_cb, | 35 error_cb, |
34 "AudioDecoder"), | 36 "AudioDecoder"), |
35 volume_(-1.0), | 37 volume_(-1.0), |
36 bytes_per_frame_(0), | 38 bytes_per_frame_(0), |
37 output_sampling_rate_(0), | 39 output_sampling_rate_(0), |
38 frame_count_(0), | 40 frame_count_(0), |
39 update_current_time_cb_(update_current_time_cb) { | 41 update_current_time_cb_(update_current_time_cb) { |
40 } | 42 } |
41 | 43 |
42 MediaCodecAudioDecoder::~MediaCodecAudioDecoder() { | 44 MediaCodecAudioDecoder::~MediaCodecAudioDecoder() { |
43 DCHECK(media_task_runner_->BelongsToCurrentThread()); | 45 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
44 DVLOG(1) << "AudioDecoder::~AudioDecoder()"; | 46 DVLOG(1) << "AudioDecoder::~AudioDecoder()"; |
45 ReleaseDecoderResources(); | 47 ReleaseDecoderResources(); |
46 } | 48 } |
47 | 49 |
48 const char* MediaCodecAudioDecoder::class_name() const { | 50 const char* MediaCodecAudioDecoder::class_name() const { |
49 return "AudioDecoder"; | 51 return "AudioDecoder"; |
50 } | 52 } |
51 | 53 |
52 bool MediaCodecAudioDecoder::HasStream() const { | 54 bool MediaCodecAudioDecoder::HasStream() const { |
53 DCHECK(media_task_runner_->BelongsToCurrentThread()); | 55 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
54 | 56 |
55 return configs_.audio_codec != kUnknownAudioCodec; | 57 return configs_.audio_codec != kUnknownAudioCodec; |
56 } | 58 } |
57 | 59 |
58 void MediaCodecAudioDecoder::SetDemuxerConfigs(const DemuxerConfigs& configs) { | 60 void MediaCodecAudioDecoder::SetDemuxerConfigs(const DemuxerConfigs& configs) { |
59 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
60 | |
61 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << configs; | 61 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << configs; |
62 | 62 |
63 configs_ = configs; | 63 configs_ = configs; |
64 if (!media_codec_bridge_) | 64 if (!media_codec_bridge_) |
65 output_sampling_rate_ = configs.audio_sampling_rate; | 65 output_sampling_rate_ = configs.audio_sampling_rate; |
66 } | 66 } |
67 | 67 |
68 void MediaCodecAudioDecoder::ReleaseDecoderResources() { | 68 void MediaCodecAudioDecoder::ReleaseDecoderResources() { |
69 DCHECK(media_task_runner_->BelongsToCurrentThread()); | 69 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
70 DVLOG(1) << class_name() << "::" << __FUNCTION__; | 70 DVLOG(1) << class_name() << "::" << __FUNCTION__; |
(...skipping 24 matching lines...) Expand all Loading... |
95 // Decoder thread should not be running. | 95 // Decoder thread should not be running. |
96 | 96 |
97 DVLOG(1) << __FUNCTION__ << " " << base_timestamp; | 97 DVLOG(1) << __FUNCTION__ << " " << base_timestamp; |
98 | 98 |
99 base_timestamp_ = base_timestamp; | 99 base_timestamp_ = base_timestamp; |
100 if (audio_timestamp_helper_) | 100 if (audio_timestamp_helper_) |
101 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_); | 101 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_); |
102 } | 102 } |
103 | 103 |
104 bool MediaCodecAudioDecoder::IsCodecReconfigureNeeded( | 104 bool MediaCodecAudioDecoder::IsCodecReconfigureNeeded( |
105 const DemuxerConfigs& curr, | |
106 const DemuxerConfigs& next) const { | 105 const DemuxerConfigs& next) const { |
107 return curr.audio_codec != next.audio_codec || | 106 if (always_reconfigure_for_tests_) |
108 curr.audio_channels != next.audio_channels || | 107 return true; |
109 curr.audio_sampling_rate != next.audio_sampling_rate || | 108 |
| 109 return configs_.audio_codec != next.audio_codec || |
| 110 configs_.audio_channels != next.audio_channels || |
| 111 configs_.audio_sampling_rate != next.audio_sampling_rate || |
110 next.is_audio_encrypted != next.is_audio_encrypted || | 112 next.is_audio_encrypted != next.is_audio_encrypted || |
111 curr.audio_extra_data.size() != next.audio_extra_data.size() || | 113 configs_.audio_extra_data.size() != next.audio_extra_data.size() || |
112 !std::equal(curr.audio_extra_data.begin(), curr.audio_extra_data.end(), | 114 !std::equal(configs_.audio_extra_data.begin(), |
| 115 configs_.audio_extra_data.end(), |
113 next.audio_extra_data.begin()); | 116 next.audio_extra_data.begin()); |
114 } | 117 } |
115 | 118 |
116 MediaCodecDecoder::ConfigStatus MediaCodecAudioDecoder::ConfigureInternal() { | 119 MediaCodecDecoder::ConfigStatus MediaCodecAudioDecoder::ConfigureInternal() { |
117 DCHECK(media_task_runner_->BelongsToCurrentThread()); | 120 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
118 | 121 |
119 DVLOG(1) << class_name() << "::" << __FUNCTION__; | 122 DVLOG(1) << class_name() << "::" << __FUNCTION__; |
120 | 123 |
121 if (configs_.audio_codec == kUnknownAudioCodec) { | 124 if (configs_.audio_codec == kUnknownAudioCodec) { |
122 DVLOG(0) << class_name() << "::" << __FUNCTION__ | 125 DVLOG(0) << class_name() << "::" << __FUNCTION__ |
(...skipping 24 matching lines...) Expand all Loading... |
147 } | 150 } |
148 | 151 |
149 DVLOG(0) << class_name() << "::" << __FUNCTION__ << " succeeded"; | 152 DVLOG(0) << class_name() << "::" << __FUNCTION__ << " succeeded"; |
150 | 153 |
151 SetVolumeInternal(); | 154 SetVolumeInternal(); |
152 | 155 |
153 bytes_per_frame_ = kBytesPerAudioOutputSample * configs_.audio_channels; | 156 bytes_per_frame_ = kBytesPerAudioOutputSample * configs_.audio_channels; |
154 frame_count_ = 0; | 157 frame_count_ = 0; |
155 ResetTimestampHelper(); | 158 ResetTimestampHelper(); |
156 | 159 |
| 160 if (!codec_created_for_tests_cb_.is_null()) |
| 161 media_task_runner_->PostTask(FROM_HERE, codec_created_for_tests_cb_); |
| 162 |
157 return kConfigOk; | 163 return kConfigOk; |
158 } | 164 } |
159 | 165 |
160 void MediaCodecAudioDecoder::OnOutputFormatChanged() { | 166 void MediaCodecAudioDecoder::OnOutputFormatChanged() { |
161 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | 167 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); |
162 | 168 |
163 DCHECK(media_codec_bridge_); | 169 DCHECK(media_codec_bridge_); |
164 | 170 |
165 int old_sampling_rate = output_sampling_rate_; | 171 int old_sampling_rate = output_sampling_rate_; |
166 output_sampling_rate_ = media_codec_bridge_->GetOutputSamplingRate(); | 172 output_sampling_rate_ = media_codec_bridge_->GetOutputSamplingRate(); |
(...skipping 18 matching lines...) Expand all Loading... |
185 AudioCodecBridge* audio_codec = | 191 AudioCodecBridge* audio_codec = |
186 static_cast<AudioCodecBridge*>(media_codec_bridge_.get()); | 192 static_cast<AudioCodecBridge*>(media_codec_bridge_.get()); |
187 | 193 |
188 DCHECK(audio_codec); | 194 DCHECK(audio_codec); |
189 | 195 |
190 const bool postpone = (render_mode == kRenderAfterPreroll); | 196 const bool postpone = (render_mode == kRenderAfterPreroll); |
191 | 197 |
192 int64 head_position = | 198 int64 head_position = |
193 audio_codec->PlayOutputBuffer(buffer_index, size, offset, postpone); | 199 audio_codec->PlayOutputBuffer(buffer_index, size, offset, postpone); |
194 | 200 |
195 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts | |
196 << (postpone ? " POSTPONE" : "") | |
197 << " head_position:" << head_position; | |
198 | |
199 // Reset the base timestamp if we have not started playing. | 201 // Reset the base timestamp if we have not started playing. |
200 // SetBaseTimestamp() must be called before AddFrames() since it resets the | 202 // SetBaseTimestamp() must be called before AddFrames() since it resets the |
201 // internal frame count. | 203 // internal frame count. |
202 if (postpone && !frame_count_) | 204 if (postpone && !frame_count_) |
203 SetBaseTimestamp(pts); | 205 SetBaseTimestamp(pts); |
204 | 206 |
205 size_t new_frames_count = size / bytes_per_frame_; | 207 size_t new_frames_count = size / bytes_per_frame_; |
206 frame_count_ += new_frames_count; | 208 frame_count_ += new_frames_count; |
207 audio_timestamp_helper_->AddFrames(new_frames_count); | 209 audio_timestamp_helper_->AddFrames(new_frames_count); |
208 | 210 |
209 if (postpone) { | 211 if (postpone) { |
| 212 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts |
| 213 << " POSTPONE"; |
| 214 |
210 // Let the player adjust the start time. | 215 // Let the player adjust the start time. |
211 media_task_runner_->PostTask( | 216 media_task_runner_->PostTask( |
212 FROM_HERE, base::Bind(update_current_time_cb_, pts, pts, true)); | 217 FROM_HERE, base::Bind(update_current_time_cb_, pts, pts, true)); |
213 } else { | 218 } else { |
214 int64 frames_to_play = frame_count_ - head_position; | 219 int64 frames_to_play = frame_count_ - head_position; |
215 | 220 |
216 DCHECK_GE(frames_to_play, 0) << class_name() << "::" << __FUNCTION__ | 221 DCHECK_GE(frames_to_play, 0) << class_name() << "::" << __FUNCTION__ |
217 << " pts:" << pts | 222 << " pts:" << pts |
218 << " frame_count_:" << frame_count_ | 223 << " frame_count_:" << frame_count_ |
219 << " head_position:" << head_position; | 224 << " head_position:" << head_position; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 if (audio_timestamp_helper_) | 259 if (audio_timestamp_helper_) |
255 base_timestamp_ = audio_timestamp_helper_->GetTimestamp(); | 260 base_timestamp_ = audio_timestamp_helper_->GetTimestamp(); |
256 | 261 |
257 audio_timestamp_helper_.reset( | 262 audio_timestamp_helper_.reset( |
258 new AudioTimestampHelper(configs_.audio_sampling_rate)); | 263 new AudioTimestampHelper(configs_.audio_sampling_rate)); |
259 | 264 |
260 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_); | 265 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_); |
261 } | 266 } |
262 | 267 |
263 } // namespace media | 268 } // namespace media |
OLD | NEW |