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 | |
16 // rate of 1.0. | |
17 static const size_t kSamplesPerBuffer = 8 * 1024; | |
18 | |
19 ReferenceAudioRenderer::ReferenceAudioRenderer() | 13 ReferenceAudioRenderer::ReferenceAudioRenderer() |
20 : AudioRendererBase(), | 14 : AudioRendererBase(), |
21 stream_(NULL), | |
22 bytes_per_second_(0) { | 15 bytes_per_second_(0) { |
23 } | 16 } |
24 | 17 |
25 ReferenceAudioRenderer::~ReferenceAudioRenderer() { | 18 ReferenceAudioRenderer::~ReferenceAudioRenderer() { |
26 // Close down the audio device. | 19 // Close down the audio device. |
27 if (stream_) { | 20 if (controller_) |
28 stream_->Stop(); | 21 controller_->Close(base::Bind(&ReferenceAudioRenderer::OnClose, this)); |
29 stream_->Close(); | |
30 } | |
31 } | 22 } |
32 | 23 |
33 void ReferenceAudioRenderer::SetPlaybackRate(float rate) { | 24 void ReferenceAudioRenderer::SetPlaybackRate(float rate) { |
34 // TODO(fbarchard): limit rate to reasonable values | 25 // TODO(fbarchard): limit rate to reasonable values |
35 AudioRendererBase::SetPlaybackRate(rate); | 26 AudioRendererBase::SetPlaybackRate(rate); |
36 | 27 |
37 static bool started = false; | 28 if (controller_ && rate > 0.0f) |
38 if (rate > 0.0f && !started && stream_) | 29 controller_->Play(); |
39 stream_->Start(this); | |
40 } | 30 } |
41 | 31 |
42 void ReferenceAudioRenderer::SetVolume(float volume) { | 32 void ReferenceAudioRenderer::SetVolume(float volume) { |
43 if (stream_) | 33 if (controller_) |
44 stream_->SetVolume(volume); | 34 controller_->SetVolume(volume); |
45 } | 35 } |
46 | 36 |
47 uint32 ReferenceAudioRenderer::OnMoreData( | 37 void ReferenceAudioRenderer::OnCreated(AudioOutputController* controller) { |
48 AudioOutputStream* stream, uint8* dest, uint32 len, | 38 NOTIMPLEMENTED(); |
49 AudioBuffersState buffers_state) { | 39 } |
50 // TODO(scherkus): handle end of stream. | |
51 if (!stream_) | |
52 return 0; | |
53 | 40 |
| 41 void ReferenceAudioRenderer::OnPlaying(AudioOutputController* controller) { |
| 42 NOTIMPLEMENTED(); |
| 43 } |
| 44 |
| 45 void ReferenceAudioRenderer::OnPaused(AudioOutputController* controller) { |
| 46 NOTIMPLEMENTED(); |
| 47 } |
| 48 |
| 49 void ReferenceAudioRenderer::OnError(AudioOutputController* controller, |
| 50 int error_code) { |
| 51 NOTIMPLEMENTED(); |
| 52 } |
| 53 |
| 54 void ReferenceAudioRenderer::OnMoreData(AudioOutputController* controller, |
| 55 AudioBuffersState buffers_state) { |
54 // TODO(fbarchard): Waveout_output_win.h should handle zero length buffers | 56 // TODO(fbarchard): Waveout_output_win.h should handle zero length buffers |
55 // without clicking. | 57 // without clicking. |
56 uint32 pending_bytes = static_cast<uint32>(ceil(buffers_state.total_bytes() * | 58 uint32 pending_bytes = static_cast<uint32>(ceil(buffers_state.total_bytes() * |
57 GetPlaybackRate())); | 59 GetPlaybackRate())); |
58 base::TimeDelta delay = base::TimeDelta::FromMicroseconds( | 60 base::TimeDelta delay = base::TimeDelta::FromMicroseconds( |
59 base::Time::kMicrosecondsPerSecond * pending_bytes / | 61 base::Time::kMicrosecondsPerSecond * pending_bytes / |
60 bytes_per_second_); | 62 bytes_per_second_); |
61 bool buffers_empty = buffers_state.pending_bytes == 0; | 63 bool buffers_empty = buffers_state.pending_bytes == 0; |
62 return FillBuffer(dest, len, delay, buffers_empty); | 64 uint32 read = FillBuffer(buffer_.get(), buffer_capacity_, delay, |
| 65 buffers_empty); |
| 66 controller->EnqueueData(buffer_.get(), read); |
63 } | 67 } |
64 | 68 |
65 void ReferenceAudioRenderer::OnClose(AudioOutputStream* stream) { | 69 bool ReferenceAudioRenderer::OnInitialize(int bits_per_channel, |
66 // TODO(scherkus): implement OnClose. | 70 ChannelLayout channel_layout, |
| 71 int sample_rate) { |
| 72 int samples_per_packet = sample_rate / 10; |
| 73 int hardware_buffer_size = samples_per_packet * |
| 74 ChannelLayoutToChannelCount(channel_layout) * bits_per_channel / 8; |
| 75 |
| 76 // Allocate audio buffer based on hardware buffer size. |
| 77 buffer_capacity_ = 3 * hardware_buffer_size; |
| 78 buffer_.reset(new uint8[buffer_capacity_]); |
| 79 |
| 80 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, channel_layout, |
| 81 sample_rate, bits_per_channel, samples_per_packet); |
| 82 bytes_per_second_ = params.GetBytesPerSecond(); |
| 83 |
| 84 controller_ = AudioOutputController::Create(this, params, buffer_capacity_); |
| 85 return controller_ != NULL; |
| 86 } |
| 87 |
| 88 void ReferenceAudioRenderer::OnStop() { |
| 89 if (controller_) |
| 90 controller_->Pause(); |
| 91 } |
| 92 |
| 93 void ReferenceAudioRenderer::OnClose() { |
67 NOTIMPLEMENTED(); | 94 NOTIMPLEMENTED(); |
68 } | 95 } |
69 | 96 |
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 | 97 } // namespace media |
OLD | NEW |