1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/base/android/audio_decoder_job.h"
#include "base/bind.h"
#include "base/lazy_instance.h"
#include "base/threading/thread.h"
#include "media/base/android/media_codec_bridge.h"
#include "media/base/audio_timestamp_helper.h"
namespace {
// Use 16bit PCM for audio output. Keep this value in sync with the output
// format we passed to AudioTrack in MediaCodecBridge.
const int kBytesPerAudioOutputSample = 2;
}
namespace media {
class AudioDecoderThread : public base::Thread {
public:
AudioDecoderThread() : base::Thread("MediaSource_AudioDecoderThread") {
Start();
}
};
// TODO(qinmin): Check if it is tolerable to use worker pool to handle all the
// decoding tasks so that we don't need a global thread here.
// http://crbug.com/245750
base::LazyInstance<AudioDecoderThread>::Leaky
g_audio_decoder_thread = LAZY_INSTANCE_INITIALIZER;
AudioDecoderJob::AudioDecoderJob(
const base::Closure& request_data_cb,
const base::Closure& on_demuxer_config_changed_cb)
: MediaDecoderJob(g_audio_decoder_thread.Pointer()->message_loop_proxy(),
request_data_cb,
on_demuxer_config_changed_cb),
audio_codec_(kUnknownAudioCodec),
num_channels_(0),
sampling_rate_(0),
volume_(-1.0),
bytes_per_frame_(0) {
}
AudioDecoderJob::~AudioDecoderJob() {}
bool AudioDecoderJob::HasStream() const {
return audio_codec_ != kUnknownAudioCodec;
}
void AudioDecoderJob::SetVolume(double volume) {
volume_ = volume;
SetVolumeInternal();
}
void AudioDecoderJob::SetBaseTimestamp(base::TimeDelta base_timestamp) {
DCHECK(!is_decoding());
base_timestamp_ = base_timestamp;
if (audio_timestamp_helper_)
audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_);
}
void AudioDecoderJob::ReleaseOutputBuffer(
int output_buffer_index,
size_t size,
bool render_output,
base::TimeDelta current_presentation_timestamp,
const ReleaseOutputCompletionCallback& callback) {
render_output = render_output && (size != 0u);
if (render_output) {
int64 head_position = (static_cast<AudioCodecBridge*>(
media_codec_bridge_.get()))->PlayOutputBuffer(
output_buffer_index, size);
audio_timestamp_helper_->AddFrames(size / bytes_per_frame_);
int64 frames_to_play =
audio_timestamp_helper_->frame_count() - head_position;
DCHECK_GE(frames_to_play, 0);
current_presentation_timestamp =
audio_timestamp_helper_->GetTimestamp() -
audio_timestamp_helper_->GetFrameDuration(frames_to_play);
} else {
current_presentation_timestamp = kNoTimestamp();
}
media_codec_bridge_->ReleaseOutputBuffer(output_buffer_index, false);
callback.Run(current_presentation_timestamp,
audio_timestamp_helper_->GetTimestamp());
}
bool AudioDecoderJob::ComputeTimeToRender() const {
return false;
}
void AudioDecoderJob::UpdateDemuxerConfigs(const DemuxerConfigs& configs) {
// TODO(qinmin): split DemuxerConfig for audio and video separately so we
// can simply store the stucture here.
audio_codec_ = configs.audio_codec;
num_channels_ = configs.audio_channels;
sampling_rate_ = configs.audio_sampling_rate;
set_is_content_encrypted(configs.is_audio_encrypted);
audio_extra_data_ = configs.audio_extra_data;
bytes_per_frame_ = kBytesPerAudioOutputSample * num_channels_;
}
bool AudioDecoderJob::AreDemuxerConfigsChanged(
const DemuxerConfigs& configs) const {
return audio_codec_ != configs.audio_codec ||
num_channels_ != configs.audio_channels ||
sampling_rate_ != configs.audio_sampling_rate ||
is_content_encrypted() != configs.is_audio_encrypted ||
audio_extra_data_.size() != configs.audio_extra_data.size() ||
!std::equal(audio_extra_data_.begin(),
audio_extra_data_.end(),
configs.audio_extra_data.begin());
}
bool AudioDecoderJob::CreateMediaCodecBridgeInternal() {
media_codec_bridge_.reset(AudioCodecBridge::Create(audio_codec_));
if (!media_codec_bridge_)
return false;
if (!(static_cast<AudioCodecBridge*>(media_codec_bridge_.get()))->Start(
audio_codec_, sampling_rate_, num_channels_, &audio_extra_data_[0],
audio_extra_data_.size(), true, GetMediaCrypto().obj())) {
media_codec_bridge_.reset();
return false;
}
SetVolumeInternal();
// Need to pass the base timestamp to the new decoder.
if (audio_timestamp_helper_)
base_timestamp_ = audio_timestamp_helper_->GetTimestamp();
audio_timestamp_helper_.reset(new AudioTimestampHelper(sampling_rate_));
audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_);
return true;
}
void AudioDecoderJob::SetVolumeInternal() {
if (media_codec_bridge_) {
static_cast<AudioCodecBridge*>(media_codec_bridge_.get())->SetVolume(
volume_);
}
}
} // namespace media
|