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

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

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

Powered by Google App Engine
This is Rietveld 408576698