OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/filters/reference_audio_renderer.h" | 5 #include "media/filters/reference_audio_renderer.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/bind.h" |
10 #include "media/base/filter_host.h" | |
11 #include "media/audio/audio_manager.h" | |
12 | 10 |
13 namespace media { | 11 namespace media { |
14 | 12 |
15 // This is an arbitrary number, ~184ms in a 44.1kHz stream, assuming a playback | 13 // This is an arbitrary number. Taken from audio_output_controller_unittest. |
16 // rate of 1.0. | 14 static const int kBitsPerChannel = 16; |
17 static const size_t kSamplesPerBuffer = 8 * 1024; | |
18 | 15 |
19 ReferenceAudioRenderer::ReferenceAudioRenderer() | 16 ReferenceAudioRenderer::ReferenceAudioRenderer() |
20 : AudioRendererBase(), | 17 : AudioRendererBase(), |
21 stream_(NULL), | |
22 bytes_per_second_(0) { | 18 bytes_per_second_(0) { |
23 } | 19 } |
24 | 20 |
25 ReferenceAudioRenderer::~ReferenceAudioRenderer() { | 21 ReferenceAudioRenderer::~ReferenceAudioRenderer() { |
26 // Close down the audio device. | 22 // Close down the audio device. |
27 if (stream_) { | 23 if (controller_) |
28 stream_->Stop(); | 24 controller_->Close(base::Bind(&ReferenceAudioRenderer::OnClose, this)); |
29 stream_->Close(); | |
30 } | |
31 } | 25 } |
32 | 26 |
33 void ReferenceAudioRenderer::SetPlaybackRate(float rate) { | 27 void ReferenceAudioRenderer::SetPlaybackRate(float rate) { |
34 // TODO(fbarchard): limit rate to reasonable values | 28 // TODO(fbarchard): limit rate to reasonable values |
35 AudioRendererBase::SetPlaybackRate(rate); | 29 AudioRendererBase::SetPlaybackRate(rate); |
36 | 30 |
37 static bool started = false; | 31 if (controller_ && rate > 0.0f) |
38 if (rate > 0.0f && !started && stream_) | 32 controller_->Play(); |
39 stream_->Start(this); | |
40 } | 33 } |
41 | 34 |
42 void ReferenceAudioRenderer::SetVolume(float volume) { | 35 void ReferenceAudioRenderer::SetVolume(float volume) { |
43 if (stream_) | 36 if (controller_) |
44 stream_->SetVolume(volume); | 37 controller_->SetVolume(volume); |
45 } | 38 } |
46 | 39 |
47 uint32 ReferenceAudioRenderer::OnMoreData( | 40 void ReferenceAudioRenderer::OnCreated(AudioOutputController* controller) { |
48 AudioOutputStream* stream, uint8* dest, uint32 len, | 41 NOTIMPLEMENTED(); |
49 AudioBuffersState buffers_state) { | 42 } |
50 // TODO(scherkus): handle end of stream. | |
51 if (!stream_) | |
52 return 0; | |
53 | 43 |
44 void ReferenceAudioRenderer::OnPlaying(AudioOutputController* controller) { | |
45 NOTIMPLEMENTED(); | |
46 } | |
47 | |
48 void ReferenceAudioRenderer::OnPaused(AudioOutputController* controller) { | |
49 NOTIMPLEMENTED(); | |
50 } | |
51 | |
52 void ReferenceAudioRenderer::OnError(AudioOutputController* controller, | |
53 int error_code) { | |
54 NOTIMPLEMENTED(); | |
55 } | |
56 | |
57 void ReferenceAudioRenderer::OnMoreData(AudioOutputController* controller, | |
58 AudioBuffersState buffers_state) { | |
54 // TODO(fbarchard): Waveout_output_win.h should handle zero length buffers | 59 // TODO(fbarchard): Waveout_output_win.h should handle zero length buffers |
55 // without clicking. | 60 // without clicking. |
56 uint32 pending_bytes = static_cast<uint32>(ceil(buffers_state.total_bytes() * | 61 uint32 pending_bytes = static_cast<uint32>(ceil(buffers_state.total_bytes() * |
57 GetPlaybackRate())); | 62 GetPlaybackRate())); |
58 base::TimeDelta delay = base::TimeDelta::FromMicroseconds( | 63 base::TimeDelta delay = base::TimeDelta::FromMicroseconds( |
59 base::Time::kMicrosecondsPerSecond * pending_bytes / | 64 base::Time::kMicrosecondsPerSecond * pending_bytes / |
60 bytes_per_second_); | 65 bytes_per_second_); |
61 bool buffers_empty = buffers_state.pending_bytes == 0; | 66 bool buffers_empty = buffers_state.pending_bytes == 0; |
62 return FillBuffer(dest, len, delay, buffers_empty); | 67 uint32 read = FillBuffer(buffer_.get(), buffer_capacity_, delay, |
68 buffers_empty); | |
69 controller->EnqueueData(buffer_.get(), read); | |
63 } | 70 } |
64 | 71 |
65 void ReferenceAudioRenderer::OnClose(AudioOutputStream* stream) { | 72 bool ReferenceAudioRenderer::OnInitialize(int bits_per_channel, |
66 // TODO(scherkus): implement OnClose. | 73 ChannelLayout channel_layout, |
74 int sample_rate) { | |
75 int samples_per_packet = sample_rate / 10; | |
76 int hardware_buffer_size = samples_per_packet * | |
77 ChannelLayoutToChannelCount(channel_layout) * kBitsPerChannel / 8; | |
scherkus (not reviewing)
2011/11/23 02:37:38
wait using bits_per_channel (passed in) doesn't wo
DaleCurtis
2011/11/23 02:41:22
Works fine; I'm an idiot. Done.
On 2011/11/23 02:
| |
78 | |
79 // Allocate audio buffer based on hardware buffer size. | |
80 buffer_capacity_ = 3 * hardware_buffer_size; | |
81 buffer_.reset(new uint8[buffer_capacity_]); | |
82 | |
83 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, channel_layout, | |
84 sample_rate, bits_per_channel, samples_per_packet); | |
85 bytes_per_second_ = params.GetBytesPerSecond(); | |
86 | |
87 controller_ = AudioOutputController::Create(this, params, buffer_capacity_); | |
88 return controller_; | |
89 } | |
90 | |
91 void ReferenceAudioRenderer::OnStop() { | |
92 if (controller_) | |
93 controller_->Pause(); | |
94 } | |
95 | |
96 void ReferenceAudioRenderer::OnClose() { | |
67 NOTIMPLEMENTED(); | 97 NOTIMPLEMENTED(); |
68 } | 98 } |
69 | 99 |
70 void ReferenceAudioRenderer::OnError(AudioOutputStream* stream, int code) { | |
71 // TODO(scherkus): implement OnError. | |
72 NOTIMPLEMENTED(); | |
73 } | |
74 | |
75 bool ReferenceAudioRenderer::OnInitialize(int bits_per_channel, | |
76 ChannelLayout channel_layout, | |
77 int sample_rate) { | |
78 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, channel_layout, | |
79 sample_rate, bits_per_channel, kSamplesPerBuffer); | |
80 | |
81 bytes_per_second_ = params.GetBytesPerSecond(); | |
82 | |
83 // Create our audio stream. | |
84 stream_ = AudioManager::GetAudioManager()->MakeAudioOutputStream(params); | |
85 if (!stream_) | |
86 return false; | |
87 | |
88 if (!stream_->Open()) { | |
89 stream_->Close(); | |
90 stream_ = NULL; | |
91 } | |
92 return true; | |
93 } | |
94 | |
95 void ReferenceAudioRenderer::OnStop() { | |
96 if (stream_) | |
97 stream_->Stop(); | |
98 } | |
99 | |
100 } // namespace media | 100 } // namespace media |
OLD | NEW |