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

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: AccessUnitQueue::GetInfo() returns result by value 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(),
97 curr.audio_extra_data.end(),
98 next.audio_extra_data.begin());
99 }
100
101 MediaCodecDecoder::ConfigStatus
102 MediaCodecAudioDecoder::ConfigureInternal() {
103 DCHECK(media_task_runner_->BelongsToCurrentThread());
104
105 DVLOG(1) << class_name() << "::" << __FUNCTION__;
106
107 media_codec_bridge_.reset(AudioCodecBridge::Create(configs_.audio_codec));
108 if (!media_codec_bridge_)
109 return CONFIG_FAILURE;
110
111 if (!(static_cast<AudioCodecBridge*>(media_codec_bridge_.get()))->Start(
112 configs_.audio_codec,
113 configs_.audio_sampling_rate,
114 configs_.audio_channels,
115 &configs_.audio_extra_data[0],
116 configs_.audio_extra_data.size(),
117 configs_.audio_codec_delay_ns,
118 configs_.audio_seek_preroll_ns,
119 true,
120 GetMediaCrypto().obj())) {
121
122 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " failed";
123
124 media_codec_bridge_.reset();
125 return CONFIG_FAILURE;
126 }
127
128 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " succeeded";
129
130 SetVolumeInternal();
131
132 bytes_per_frame_ = kBytesPerAudioOutputSample * configs_.audio_channels;
133 frame_count_ = 0;
134 ResetTimestampHelper();
135
136 return CONFIG_OK;
137 }
138
139 void MediaCodecAudioDecoder::OnOutputFormatChanged() {
140 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
141
142 DCHECK(media_codec_bridge_);
143
144 int old_sampling_rate = output_sampling_rate_;
145 output_sampling_rate_ = media_codec_bridge_->GetOutputSamplingRate();
146 if (output_sampling_rate_ != old_sampling_rate)
147 ResetTimestampHelper();
148 }
149
150 void MediaCodecAudioDecoder::Render(int buffer_index,
151 size_t size,
152 bool render_output,
153 base::TimeDelta pts,
154 bool eos_encountered) {
155 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
156
157 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts;
158
159 render_output = render_output && (size != 0u);
160
161 if (render_output) {
162 int64 head_position =
163 (static_cast<AudioCodecBridge*>(media_codec_bridge_.get()))
164 ->PlayOutputBuffer(buffer_index, size);
165
166 size_t new_frames_count = size / bytes_per_frame_;
167 frame_count_ += new_frames_count;
168 audio_timestamp_helper_->AddFrames(new_frames_count);
169 int64 frames_to_play = frame_count_ - head_position;
170 DCHECK_GE(frames_to_play, 0);
171
172 base::TimeDelta last_buffered = audio_timestamp_helper_->GetTimestamp();
173 base::TimeDelta now_playing =
174 last_buffered -
175 audio_timestamp_helper_->GetFrameDuration(frames_to_play);
176
177 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts
178 << " will play: [" << now_playing << "," << last_buffered << "]";
179
180 media_task_runner_->PostTask(
181 FROM_HERE,
182 base::Bind(update_current_time_cb_, now_playing, last_buffered));
183 }
184
185 media_codec_bridge_->ReleaseOutputBuffer(buffer_index, false);
186
187 CheckLastFrame(eos_encountered, false); // no delayed tasks
188 }
189
190 void MediaCodecAudioDecoder::SetVolumeInternal() {
191 DCHECK(media_task_runner_->BelongsToCurrentThread());
192
193 if (media_codec_bridge_) {
194 static_cast<AudioCodecBridge*>(media_codec_bridge_.get())->SetVolume(
195 volume_);
196 }
197 }
198
199 void MediaCodecAudioDecoder::ResetTimestampHelper() {
200 // Media thread or Decoder thread
201 // When this method is called on Media thread, decoder thread
202 // should not be running.
203
204 if (audio_timestamp_helper_)
205 base_timestamp_ = audio_timestamp_helper_->GetTimestamp();
206
207 audio_timestamp_helper_.reset(
208 new AudioTimestampHelper(configs_.audio_sampling_rate));
209
210 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_);
211 }
212
213 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698