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