Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(408)

Side by Side Diff: media/base/android/media_codec_audio_decoder.cc

Issue 1128383003: Implementation of MediaCodecPlayer stage 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Restored media_source_player_unittest.cc Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 bool MediaCodecAudioDecoder::HasStream() const {
47 // Media thread
48 DCHECK(media_task_runner_->BelongsToCurrentThread());
49
50 return configs_.audio_codec != kUnknownAudioCodec;
51 }
52
53 void MediaCodecAudioDecoder::SetDemuxerConfigs(const DemuxerConfigs& configs) {
54 // Media thread
55 DCHECK(media_task_runner_->BelongsToCurrentThread());
56
57 DVLOG(1) << class_name() << "::" << __FUNCTION__;
58
59 configs_ = configs;
60 if (!media_codec_bridge_)
61 output_sampling_rate_ = configs.audio_sampling_rate;
62 }
63
64 void MediaCodecAudioDecoder::Flush() {
65 // Media thread
66 DCHECK(media_task_runner_->BelongsToCurrentThread());
67
68 MediaCodecDecoder::Flush();
69 frame_count_ = 0;
70 }
71
72 void MediaCodecAudioDecoder::SetVolume(double volume) {
73 // Media thread
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 // Media thread
84 DCHECK(media_task_runner_->BelongsToCurrentThread());
85
86 DVLOG(1) << __FUNCTION__ << " " << base_timestamp;
87
88 base_timestamp_ = base_timestamp;
89 if (audio_timestamp_helper_)
90 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_);
91 }
92
93 bool MediaCodecAudioDecoder::IsCodecReconfigureNeeded(
94 const DemuxerConfigs& curr,
95 const DemuxerConfigs& next) const {
96 return curr.audio_codec != next.audio_codec ||
97 curr.audio_channels != next.audio_channels ||
98 curr.audio_sampling_rate != next.audio_sampling_rate ||
99 next.is_audio_encrypted != next.is_audio_encrypted ||
100 curr.audio_extra_data.size() != next.audio_extra_data.size() ||
101 !std::equal(curr.audio_extra_data.begin(),
102 curr.audio_extra_data.end(),
103 next.audio_extra_data.begin());
104 }
105
106 MediaCodecDecoder::ConfigStatus
107 MediaCodecAudioDecoder::ConfigureInternal() {
108 // Media thread
109 DCHECK(media_task_runner_->BelongsToCurrentThread());
110
111 DVLOG(1) << class_name() << "::" << __FUNCTION__;
112
113 media_codec_bridge_.reset(AudioCodecBridge::Create(configs_.audio_codec));
114 if (!media_codec_bridge_)
115 return CONFIG_FAILURE;
116
117 if (!(static_cast<AudioCodecBridge*>(media_codec_bridge_.get()))->Start(
118 configs_.audio_codec,
119 configs_.audio_sampling_rate,
120 configs_.audio_channels,
121 &configs_.audio_extra_data[0],
122 configs_.audio_extra_data.size(),
123 configs_.audio_codec_delay_ns,
124 configs_.audio_seek_preroll_ns,
125 true,
126 GetMediaCrypto().obj())) {
127
128 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " failed";
129
130 media_codec_bridge_.reset();
131 return CONFIG_FAILURE;
132 }
133
134 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " succeeded";
135
136 SetVolumeInternal();
137
138 bytes_per_frame_ = kBytesPerAudioOutputSample * configs_.audio_channels;
139 frame_count_ = 0;
140 ResetTimestampHelper();
141
142 return CONFIG_OK;
143 }
144
145 void MediaCodecAudioDecoder::OnOutputFormatChanged() {
146 // Decoder thread
147 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
148
149 DCHECK(media_codec_bridge_);
150
151 int old_sampling_rate = output_sampling_rate_;
152 output_sampling_rate_ = media_codec_bridge_->GetOutputSamplingRate();
153 if (output_sampling_rate_ != old_sampling_rate)
154 ResetTimestampHelper();
155 }
156
157 void MediaCodecAudioDecoder::Render(int buffer_index,
158 size_t size,
159 bool render_output,
160 base::TimeDelta pts,
161 bool eos_encountered) {
162 // Decoder thread
163 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
164
165 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts;
166
167 render_output = render_output && (size != 0u);
168
169 if (render_output) {
170 int64 head_position = (static_cast<AudioCodecBridge*>(
171 media_codec_bridge_.get()))->PlayOutputBuffer(
172 buffer_index, size);
watk 2015/06/05 01:00:32 Formatting is a little hard to follow. I ran clang
Tima Vaisburd 2015/06/05 04:17:46 Done.
173
174 size_t new_frames_count = size / bytes_per_frame_;
175 frame_count_ += new_frames_count;
176 audio_timestamp_helper_->AddFrames(new_frames_count);
177 int64 frames_to_play = frame_count_ - head_position;
178 DCHECK_GE(frames_to_play, 0);
179
180 base::TimeDelta last_buffered = audio_timestamp_helper_->GetTimestamp();
181 base::TimeDelta now_playing =
182 last_buffered -
183 audio_timestamp_helper_->GetFrameDuration(frames_to_play);
184
185 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts
186 << " will play: [" << now_playing << "," << last_buffered << "]";
187
188 media_task_runner_->PostTask(
189 FROM_HERE,
190 base::Bind(update_current_time_cb_, now_playing, last_buffered));
191 }
192
193 media_codec_bridge_->ReleaseOutputBuffer(buffer_index, false);
194
195 CheckLastFrame(eos_encountered, false); // no delayed tasks
196 }
197
198 void MediaCodecAudioDecoder::SetVolumeInternal() {
199 // Media thread
200 DCHECK(media_task_runner_->BelongsToCurrentThread());
201
202 if (media_codec_bridge_) {
203 static_cast<AudioCodecBridge*>(media_codec_bridge_.get())->SetVolume(
204 volume_);
205 }
206 }
207
208 void MediaCodecAudioDecoder::ResetTimestampHelper() {
209 // Media thread or Decoder thread
210 // When this method is called on Media thread, decoder thread
211 // should not be running.
212
213 if (audio_timestamp_helper_)
214 base_timestamp_ = audio_timestamp_helper_->GetTimestamp();
215
216 audio_timestamp_helper_.reset(
217 new AudioTimestampHelper(configs_.audio_sampling_rate));
218
219 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_);
220 }
221
222 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698