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

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

Issue 254473010: Refactor MSE implementation on Android to simplify the logic and improve the performance (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase after recent config IPC change Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
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 14 matching lines...) Expand all
25 Start(); 25 Start();
26 } 26 }
27 }; 27 };
28 28
29 // TODO(qinmin): Check if it is tolerable to use worker pool to handle all the 29 // TODO(qinmin): Check if it is tolerable to use worker pool to handle all the
30 // decoding tasks so that we don't need a global thread here. 30 // decoding tasks so that we don't need a global thread here.
31 // http://crbug.com/245750 31 // http://crbug.com/245750
32 base::LazyInstance<AudioDecoderThread>::Leaky 32 base::LazyInstance<AudioDecoderThread>::Leaky
33 g_audio_decoder_thread = LAZY_INSTANCE_INITIALIZER; 33 g_audio_decoder_thread = LAZY_INSTANCE_INITIALIZER;
34 34
35 AudioDecoderJob* AudioDecoderJob::Create( 35 AudioDecoderJob::AudioDecoderJob(
36 const AudioCodec audio_codec, 36 const base::Closure& request_data_cb,
37 int sample_rate, 37 const base::Closure& on_demuxer_config_changed_cb)
38 int channel_count, 38 : MediaDecoderJob(g_audio_decoder_thread.Pointer()->message_loop_proxy(),
39 const uint8* extra_data, 39 request_data_cb,
40 size_t extra_data_size, 40 on_demuxer_config_changed_cb),
41 jobject media_crypto, 41 audio_codec_(kUnknownAudioCodec),
42 const base::Closure& request_data_cb) { 42 num_channels_(0),
43 scoped_ptr<AudioCodecBridge> codec(AudioCodecBridge::Create(audio_codec)); 43 sampling_rate_(0),
44 if (codec && codec->Start(audio_codec, sample_rate, channel_count, extra_data, 44 volume_(-1.0),
45 extra_data_size, true, media_crypto)) { 45 bytes_per_frame_(0) {
46 scoped_ptr<AudioTimestampHelper> audio_timestamp_helper(
47 new AudioTimestampHelper(sample_rate));
48 return new AudioDecoderJob(
49 audio_timestamp_helper.Pass(), codec.Pass(),
50 kBytesPerAudioOutputSample * channel_count, request_data_cb);
51 }
52 LOG(ERROR) << "Failed to create AudioDecoderJob.";
53 return NULL;
54 } 46 }
55 47
56 AudioDecoderJob::AudioDecoderJob( 48 AudioDecoderJob::~AudioDecoderJob() {}
57 scoped_ptr<AudioTimestampHelper> audio_timestamp_helper,
58 scoped_ptr<AudioCodecBridge> audio_codec_bridge,
59 int bytes_per_frame,
60 const base::Closure& request_data_cb)
61 : MediaDecoderJob(g_audio_decoder_thread.Pointer()->message_loop_proxy(),
62 audio_codec_bridge.get(), request_data_cb),
63 bytes_per_frame_(bytes_per_frame),
64 audio_codec_bridge_(audio_codec_bridge.Pass()),
65 audio_timestamp_helper_(audio_timestamp_helper.Pass()) {
66 }
67 49
68 AudioDecoderJob::~AudioDecoderJob() { 50 bool AudioDecoderJob::HasStream() const {
51 return audio_codec_ != kUnknownAudioCodec;
69 } 52 }
70 53
71 void AudioDecoderJob::SetVolume(double volume) { 54 void AudioDecoderJob::SetVolume(double volume) {
72 audio_codec_bridge_->SetVolume(volume); 55 volume_ = volume;
56 SetVolumeInternal();
73 } 57 }
74 58
75 void AudioDecoderJob::SetBaseTimestamp(base::TimeDelta base_timestamp) { 59 void AudioDecoderJob::SetBaseTimestamp(base::TimeDelta base_timestamp) {
76 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp); 60 DCHECK(!is_decoding());
61 base_timestamp_ = base_timestamp;
62 if (audio_timestamp_helper_)
63 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_);
77 } 64 }
78 65
79 void AudioDecoderJob::ReleaseOutputBuffer( 66 void AudioDecoderJob::ReleaseOutputBuffer(
80 int output_buffer_index, 67 int output_buffer_index,
81 size_t size, 68 size_t size,
82 bool render_output, 69 bool render_output,
83 base::TimeDelta current_presentation_timestamp, 70 base::TimeDelta current_presentation_timestamp,
84 const ReleaseOutputCompletionCallback& callback) { 71 const ReleaseOutputCompletionCallback& callback) {
85 render_output = render_output && (size != 0u); 72 render_output = render_output && (size != 0u);
86 if (render_output) { 73 if (render_output) {
87 int64 head_position = audio_codec_bridge_->PlayOutputBuffer( 74 int64 head_position =(static_cast<AudioCodecBridge*>(
88 output_buffer_index, size); 75 media_codec_bridge_.get()))->PlayOutputBuffer(
89 audio_timestamp_helper_->AddFrames(size / (bytes_per_frame_)); 76 output_buffer_index, size);
77 audio_timestamp_helper_->AddFrames(size / bytes_per_frame_);
90 int64 frames_to_play = 78 int64 frames_to_play =
91 audio_timestamp_helper_->frame_count() - head_position; 79 audio_timestamp_helper_->frame_count() - head_position;
92 DCHECK_GE(frames_to_play, 0); 80 DCHECK_GE(frames_to_play, 0);
93 current_presentation_timestamp = 81 current_presentation_timestamp =
94 audio_timestamp_helper_->GetTimestamp() - 82 audio_timestamp_helper_->GetTimestamp() -
95 audio_timestamp_helper_->GetFrameDuration(frames_to_play); 83 audio_timestamp_helper_->GetFrameDuration(frames_to_play);
96 } else { 84 } else {
97 current_presentation_timestamp = kNoTimestamp(); 85 current_presentation_timestamp = kNoTimestamp();
98 } 86 }
99 audio_codec_bridge_->ReleaseOutputBuffer(output_buffer_index, false); 87 media_codec_bridge_->ReleaseOutputBuffer(output_buffer_index, false);
88
100 callback.Run(current_presentation_timestamp, 89 callback.Run(current_presentation_timestamp,
101 audio_timestamp_helper_->GetTimestamp()); 90 audio_timestamp_helper_->GetTimestamp());
102 } 91 }
103 92
104 bool AudioDecoderJob::ComputeTimeToRender() const { 93 bool AudioDecoderJob::ComputeTimeToRender() const {
105 return false; 94 return false;
106 } 95 }
107 96
97 void AudioDecoderJob::UpdateDemuxerConfigs(const DemuxerConfigs& configs) {
98 audio_codec_ = configs.audio_codec;
99 num_channels_ = configs.audio_channels;
100 sampling_rate_ = configs.audio_sampling_rate;
101 set_is_content_encrypted(configs.is_audio_encrypted);
102 audio_extra_data_ = configs.audio_extra_data;
103 bytes_per_frame_ = kBytesPerAudioOutputSample * num_channels_;
104 }
105
106 bool AudioDecoderJob::IsDemuxerConfigChanged(
107 const DemuxerConfigs& configs) const {
108 return audio_codec_ != configs.audio_codec ||
109 num_channels_ != configs.audio_channels ||
110 sampling_rate_ != configs.audio_sampling_rate ||
111 is_content_encrypted() != configs.is_audio_encrypted ||
112 audio_extra_data_.size() != configs.audio_extra_data.size() ||
113 !std::equal(audio_extra_data_.begin(),
114 audio_extra_data_.end(),
115 configs.audio_extra_data.begin());
116 }
117
118 bool AudioDecoderJob::CreateMediaCodecBridgeInternal() {
119 media_codec_bridge_.reset(AudioCodecBridge::Create(audio_codec_));
120 if (!media_codec_bridge_)
121 return false;
122
123 if (!(static_cast<AudioCodecBridge*>(media_codec_bridge_.get()))->Start(
124 audio_codec_, sampling_rate_, num_channels_, &audio_extra_data_[0],
125 audio_extra_data_.size(), true, GetMediaCrypto().obj())) {
126 media_codec_bridge_.reset();
127 return false;
128 }
129
130 SetVolumeInternal();
131
132 // Need to pass the base timestamp to the new decoder.
133 if (audio_timestamp_helper_)
134 base_timestamp_ = audio_timestamp_helper_->GetTimestamp();
135 audio_timestamp_helper_.reset(new AudioTimestampHelper(sampling_rate_));
136 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp_);
137 return true;
138 }
139
140 void AudioDecoderJob::SetVolumeInternal() {
141 if (media_codec_bridge_) {
142 static_cast<AudioCodecBridge*>(media_codec_bridge_.get())->SetVolume(
143 volume_);
144 }
145 }
146
108 } // namespace media 147 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698