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