OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/base/android/audio_decoder_job.h" | 5 #include "media/base/android/audio_decoder_job.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
10 #include "media/base/android/media_codec_bridge.h" | 10 #include "media/base/android/media_codec_bridge.h" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 g_audio_decoder_thread = LAZY_INSTANCE_INITIALIZER; | 33 g_audio_decoder_thread = LAZY_INSTANCE_INITIALIZER; |
34 | 34 |
35 AudioDecoderJob::AudioDecoderJob( | 35 AudioDecoderJob::AudioDecoderJob( |
36 const base::Closure& request_data_cb, | 36 const base::Closure& request_data_cb, |
37 const base::Closure& on_demuxer_config_changed_cb) | 37 const base::Closure& on_demuxer_config_changed_cb) |
38 : MediaDecoderJob(g_audio_decoder_thread.Pointer()->message_loop_proxy(), | 38 : MediaDecoderJob(g_audio_decoder_thread.Pointer()->message_loop_proxy(), |
39 request_data_cb, | 39 request_data_cb, |
40 on_demuxer_config_changed_cb), | 40 on_demuxer_config_changed_cb), |
41 audio_codec_(kUnknownAudioCodec), | 41 audio_codec_(kUnknownAudioCodec), |
42 num_channels_(0), | 42 num_channels_(0), |
43 sampling_rate_(0), | 43 config_sampling_rate_(0), |
44 volume_(-1.0), | 44 volume_(-1.0), |
45 bytes_per_frame_(0) { | 45 bytes_per_frame_(0), |
46 output_sampling_rate_(0), | |
47 frame_count_(0) { | |
46 } | 48 } |
47 | 49 |
48 AudioDecoderJob::~AudioDecoderJob() {} | 50 AudioDecoderJob::~AudioDecoderJob() {} |
49 | 51 |
50 bool AudioDecoderJob::HasStream() const { | 52 bool AudioDecoderJob::HasStream() const { |
51 return audio_codec_ != kUnknownAudioCodec; | 53 return audio_codec_ != kUnknownAudioCodec; |
52 } | 54 } |
53 | 55 |
56 void AudioDecoderJob::Flush() { | |
57 MediaDecoderJob::Flush(); | |
58 frame_count_ = 0; | |
kjoswiak
2015/01/23 21:58:36
This behavior was overlooked before (and I haven't
| |
59 } | |
60 | |
54 void AudioDecoderJob::SetDemuxerConfigs(const DemuxerConfigs& configs) { | 61 void AudioDecoderJob::SetDemuxerConfigs(const DemuxerConfigs& configs) { |
55 // TODO(qinmin): split DemuxerConfig for audio and video separately so we | 62 // TODO(qinmin): split DemuxerConfig for audio and video separately so we |
56 // can simply store the stucture here. | 63 // can simply store the stucture here. |
57 audio_codec_ = configs.audio_codec; | 64 audio_codec_ = configs.audio_codec; |
58 num_channels_ = configs.audio_channels; | 65 num_channels_ = configs.audio_channels; |
59 sampling_rate_ = configs.audio_sampling_rate; | 66 config_sampling_rate_ = configs.audio_sampling_rate; |
60 set_is_content_encrypted(configs.is_audio_encrypted); | 67 set_is_content_encrypted(configs.is_audio_encrypted); |
61 audio_extra_data_ = configs.audio_extra_data; | 68 audio_extra_data_ = configs.audio_extra_data; |
62 bytes_per_frame_ = kBytesPerAudioOutputSample * num_channels_; | 69 bytes_per_frame_ = kBytesPerAudioOutputSample * num_channels_; |
70 if (!media_codec_bridge_) | |
71 output_sampling_rate_ = config_sampling_rate_; | |
63 } | 72 } |
64 | 73 |
65 void AudioDecoderJob::SetVolume(double volume) { | 74 void AudioDecoderJob::SetVolume(double volume) { |
66 volume_ = volume; | 75 volume_ = volume; |
67 SetVolumeInternal(); | 76 SetVolumeInternal(); |
68 } | 77 } |
69 | 78 |
70 void AudioDecoderJob::SetBaseTimestamp(base::TimeDelta base_timestamp) { | 79 void AudioDecoderJob::SetBaseTimestamp(base::TimeDelta base_timestamp) { |
71 DCHECK(!is_decoding()); | 80 DCHECK(!is_decoding()); |
72 base_timestamp_ = base_timestamp; | |
73 if (audio_timestamp_helper_) | 81 if (audio_timestamp_helper_) |
74 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_); | 82 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp); |
83 } | |
84 | |
85 void AudioDecoderJob::ResetTimestampHelper() { | |
86 base::TimeDelta base_timestamp; | |
87 if (audio_timestamp_helper_) | |
88 base_timestamp = audio_timestamp_helper_->GetTimestamp(); | |
89 audio_timestamp_helper_.reset( | |
90 new AudioTimestampHelper(output_sampling_rate_)); | |
91 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp); | |
75 } | 92 } |
76 | 93 |
77 void AudioDecoderJob::ReleaseOutputBuffer( | 94 void AudioDecoderJob::ReleaseOutputBuffer( |
78 int output_buffer_index, | 95 int output_buffer_index, |
79 size_t size, | 96 size_t size, |
80 bool render_output, | 97 bool render_output, |
81 base::TimeDelta current_presentation_timestamp, | 98 base::TimeDelta current_presentation_timestamp, |
82 const ReleaseOutputCompletionCallback& callback) { | 99 const ReleaseOutputCompletionCallback& callback) { |
83 render_output = render_output && (size != 0u); | 100 render_output = render_output && (size != 0u); |
84 if (render_output) { | 101 if (render_output) { |
85 int64 head_position = (static_cast<AudioCodecBridge*>( | 102 int64 head_position = (static_cast<AudioCodecBridge*>( |
86 media_codec_bridge_.get()))->PlayOutputBuffer( | 103 media_codec_bridge_.get()))->PlayOutputBuffer( |
87 output_buffer_index, size); | 104 output_buffer_index, size); |
88 audio_timestamp_helper_->AddFrames(size / bytes_per_frame_); | 105 size_t new_frames_count = size / bytes_per_frame_; |
89 int64 frames_to_play = | 106 frame_count_ += new_frames_count; |
90 audio_timestamp_helper_->frame_count() - head_position; | 107 audio_timestamp_helper_->AddFrames(new_frames_count); |
108 int64 frames_to_play = frame_count_ - head_position; | |
91 DCHECK_GE(frames_to_play, 0); | 109 DCHECK_GE(frames_to_play, 0); |
92 current_presentation_timestamp = | 110 current_presentation_timestamp = |
93 audio_timestamp_helper_->GetTimestamp() - | 111 audio_timestamp_helper_->GetTimestamp() - |
94 audio_timestamp_helper_->GetFrameDuration(frames_to_play); | 112 audio_timestamp_helper_->GetFrameDuration(frames_to_play); |
95 } else { | 113 } else { |
96 current_presentation_timestamp = kNoTimestamp(); | 114 current_presentation_timestamp = kNoTimestamp(); |
97 } | 115 } |
98 media_codec_bridge_->ReleaseOutputBuffer(output_buffer_index, false); | 116 media_codec_bridge_->ReleaseOutputBuffer(output_buffer_index, false); |
99 | 117 |
100 callback.Run(current_presentation_timestamp, | 118 callback.Run(current_presentation_timestamp, |
101 audio_timestamp_helper_->GetTimestamp()); | 119 audio_timestamp_helper_->GetTimestamp()); |
102 } | 120 } |
103 | 121 |
104 bool AudioDecoderJob::ComputeTimeToRender() const { | 122 bool AudioDecoderJob::ComputeTimeToRender() const { |
105 return false; | 123 return false; |
106 } | 124 } |
107 | 125 |
108 bool AudioDecoderJob::AreDemuxerConfigsChanged( | 126 bool AudioDecoderJob::AreDemuxerConfigsChanged( |
109 const DemuxerConfigs& configs) const { | 127 const DemuxerConfigs& configs) const { |
110 return audio_codec_ != configs.audio_codec || | 128 return audio_codec_ != configs.audio_codec || |
111 num_channels_ != configs.audio_channels || | 129 num_channels_ != configs.audio_channels || |
112 sampling_rate_ != configs.audio_sampling_rate || | 130 config_sampling_rate_ != configs.audio_sampling_rate || |
113 is_content_encrypted() != configs.is_audio_encrypted || | 131 is_content_encrypted() != configs.is_audio_encrypted || |
114 audio_extra_data_.size() != configs.audio_extra_data.size() || | 132 audio_extra_data_.size() != configs.audio_extra_data.size() || |
115 !std::equal(audio_extra_data_.begin(), | 133 !std::equal(audio_extra_data_.begin(), |
116 audio_extra_data_.end(), | 134 audio_extra_data_.end(), |
117 configs.audio_extra_data.begin()); | 135 configs.audio_extra_data.begin()); |
118 } | 136 } |
119 | 137 |
120 bool AudioDecoderJob::CreateMediaCodecBridgeInternal() { | 138 bool AudioDecoderJob::CreateMediaCodecBridgeInternal() { |
121 media_codec_bridge_.reset(AudioCodecBridge::Create(audio_codec_)); | 139 media_codec_bridge_.reset(AudioCodecBridge::Create(audio_codec_)); |
122 if (!media_codec_bridge_) | 140 if (!media_codec_bridge_) |
123 return false; | 141 return false; |
124 | 142 |
125 if (!(static_cast<AudioCodecBridge*>(media_codec_bridge_.get()))->Start( | 143 if (!(static_cast<AudioCodecBridge*>(media_codec_bridge_.get()))->Start( |
126 audio_codec_, sampling_rate_, num_channels_, &audio_extra_data_[0], | 144 audio_codec_, config_sampling_rate_, num_channels_, &audio_extra_data_[0], |
127 audio_extra_data_.size(), true, GetMediaCrypto().obj())) { | 145 audio_extra_data_.size(), true, GetMediaCrypto().obj())) { |
128 media_codec_bridge_.reset(); | 146 media_codec_bridge_.reset(); |
129 return false; | 147 return false; |
130 } | 148 } |
131 | 149 |
132 SetVolumeInternal(); | 150 SetVolumeInternal(); |
133 | 151 |
134 // Need to pass the base timestamp to the new decoder. | 152 // Reset values used to track codec bridge output |
135 if (audio_timestamp_helper_) | 153 frame_count_ = 0; |
136 base_timestamp_ = audio_timestamp_helper_->GetTimestamp(); | 154 ResetTimestampHelper(); |
137 audio_timestamp_helper_.reset(new AudioTimestampHelper(sampling_rate_)); | 155 |
138 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_); | |
139 return true; | 156 return true; |
140 } | 157 } |
141 | 158 |
142 void AudioDecoderJob::SetVolumeInternal() { | 159 void AudioDecoderJob::SetVolumeInternal() { |
143 if (media_codec_bridge_) { | 160 if (media_codec_bridge_) { |
144 static_cast<AudioCodecBridge*>(media_codec_bridge_.get())->SetVolume( | 161 static_cast<AudioCodecBridge*>(media_codec_bridge_.get())->SetVolume( |
145 volume_); | 162 volume_); |
146 } | 163 } |
147 } | 164 } |
148 | 165 |
166 void AudioDecoderJob::GotOutputFormatChanged() { | |
167 if (!media_codec_bridge_) | |
168 return; | |
169 | |
170 int old_sampling_rate = output_sampling_rate_; | |
171 output_sampling_rate_ = media_codec_bridge_->GetOutputSamplingRate(); | |
172 if (output_sampling_rate_ != old_sampling_rate) | |
173 ResetTimestampHelper(); | |
174 } | |
175 | |
149 } // namespace media | 176 } // namespace media |
OLD | NEW |