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" |
11 #include "media/base/audio_timestamp_helper.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 | 11 |
20 namespace media { | 12 namespace media { |
21 | 13 |
22 class AudioDecoderThread : public base::Thread { | 14 class AudioDecoderThread : public base::Thread { |
23 public: | 15 public: |
24 AudioDecoderThread() : base::Thread("MediaSource_AudioDecoderThread") { | 16 AudioDecoderThread() : base::Thread("MediaSource_AudioDecoderThread") { |
25 Start(); | 17 Start(); |
26 } | 18 } |
27 }; | 19 }; |
28 | 20 |
29 // TODO(qinmin): Check if it is tolerable to use worker pool to handle all the | 21 // 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. | 22 // decoding tasks so that we don't need a global thread here. |
31 // http://crbug.com/245750 | 23 // http://crbug.com/245750 |
32 base::LazyInstance<AudioDecoderThread>::Leaky | 24 base::LazyInstance<AudioDecoderThread>::Leaky |
33 g_audio_decoder_thread = LAZY_INSTANCE_INITIALIZER; | 25 g_audio_decoder_thread = LAZY_INSTANCE_INITIALIZER; |
34 | 26 |
35 AudioDecoderJob* AudioDecoderJob::Create( | 27 AudioDecoderJob* AudioDecoderJob::Create( |
36 const AudioCodec audio_codec, | 28 const AudioCodec audio_codec, |
37 int sample_rate, | 29 int sample_rate, |
38 int channel_count, | 30 int channel_count, |
39 const uint8* extra_data, | 31 const uint8* extra_data, |
40 size_t extra_data_size, | 32 size_t extra_data_size, |
41 jobject media_crypto, | 33 jobject media_crypto, |
42 const base::Closure& request_data_cb) { | 34 const base::Closure& request_data_cb) { |
43 scoped_ptr<AudioCodecBridge> codec(AudioCodecBridge::Create(audio_codec)); | 35 scoped_ptr<AudioCodecBridge> codec(AudioCodecBridge::Create(audio_codec)); |
44 if (codec && codec->Start(audio_codec, sample_rate, channel_count, extra_data, | 36 if (codec && codec->Start(audio_codec, sample_rate, channel_count, extra_data, |
45 extra_data_size, true, media_crypto)) { | 37 extra_data_size, true, media_crypto)) { |
46 scoped_ptr<AudioTimestampHelper> audio_timestamp_helper( | 38 return new AudioDecoderJob(codec.Pass(), request_data_cb); |
47 new AudioTimestampHelper(sample_rate)); | |
48 return new AudioDecoderJob( | |
49 audio_timestamp_helper.Pass(), codec.Pass(), | |
50 kBytesPerAudioOutputSample * channel_count, request_data_cb); | |
51 } | 39 } |
| 40 |
52 LOG(ERROR) << "Failed to create AudioDecoderJob."; | 41 LOG(ERROR) << "Failed to create AudioDecoderJob."; |
53 return NULL; | 42 return NULL; |
54 } | 43 } |
55 | 44 |
56 AudioDecoderJob::AudioDecoderJob( | 45 AudioDecoderJob::AudioDecoderJob( |
57 scoped_ptr<AudioTimestampHelper> audio_timestamp_helper, | |
58 scoped_ptr<AudioCodecBridge> audio_codec_bridge, | 46 scoped_ptr<AudioCodecBridge> audio_codec_bridge, |
59 int bytes_per_frame, | |
60 const base::Closure& request_data_cb) | 47 const base::Closure& request_data_cb) |
61 : MediaDecoderJob(g_audio_decoder_thread.Pointer()->message_loop_proxy(), | 48 : MediaDecoderJob(g_audio_decoder_thread.Pointer()->message_loop_proxy(), |
62 audio_codec_bridge.get(), request_data_cb), | 49 audio_codec_bridge.get(), request_data_cb), |
63 bytes_per_frame_(bytes_per_frame), | 50 audio_codec_bridge_(audio_codec_bridge.Pass()) { |
64 audio_codec_bridge_(audio_codec_bridge.Pass()), | |
65 audio_timestamp_helper_(audio_timestamp_helper.Pass()) { | |
66 } | 51 } |
67 | 52 |
68 AudioDecoderJob::~AudioDecoderJob() { | 53 AudioDecoderJob::~AudioDecoderJob() { |
69 } | 54 } |
70 | 55 |
71 void AudioDecoderJob::SetVolume(double volume) { | 56 void AudioDecoderJob::SetVolume(double volume) { |
72 audio_codec_bridge_->SetVolume(volume); | 57 audio_codec_bridge_->SetVolume(volume); |
73 } | 58 } |
74 | 59 |
75 void AudioDecoderJob::SetBaseTimestamp(base::TimeDelta base_timestamp) { | |
76 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp); | |
77 } | |
78 | |
79 void AudioDecoderJob::ReleaseOutputBuffer( | 60 void AudioDecoderJob::ReleaseOutputBuffer( |
80 int output_buffer_index, | 61 int output_buffer_index, |
81 size_t size, | 62 size_t size, |
82 bool render_output, | 63 bool render_output, |
83 base::TimeDelta current_presentation_timestamp, | |
84 const ReleaseOutputCompletionCallback& callback) { | 64 const ReleaseOutputCompletionCallback& callback) { |
85 render_output = render_output && (size != 0u); | 65 size_t size_to_render = render_output ? size : 0u; |
86 if (render_output) { | 66 if (size_to_render) |
87 int64 head_position = audio_codec_bridge_->PlayOutputBuffer( | 67 audio_codec_bridge_->PlayOutputBuffer(output_buffer_index, size_to_render); |
88 output_buffer_index, size); | |
89 audio_timestamp_helper_->AddFrames(size / (bytes_per_frame_)); | |
90 int64 frames_to_play = | |
91 audio_timestamp_helper_->frame_count() - head_position; | |
92 DCHECK_GE(frames_to_play, 0); | |
93 current_presentation_timestamp = | |
94 audio_timestamp_helper_->GetTimestamp() - | |
95 audio_timestamp_helper_->GetFrameDuration(frames_to_play); | |
96 } else { | |
97 current_presentation_timestamp = kNoTimestamp(); | |
98 } | |
99 audio_codec_bridge_->ReleaseOutputBuffer(output_buffer_index, false); | 68 audio_codec_bridge_->ReleaseOutputBuffer(output_buffer_index, false); |
100 callback.Run(current_presentation_timestamp, | 69 |
101 audio_timestamp_helper_->GetTimestamp()); | 70 callback.Run(size_to_render); |
102 } | 71 } |
103 | 72 |
104 bool AudioDecoderJob::ComputeTimeToRender() const { | 73 bool AudioDecoderJob::ComputeTimeToRender() const { |
105 return false; | 74 return false; |
106 } | 75 } |
107 | 76 |
108 } // namespace media | 77 } // namespace media |
OLD | NEW |