OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "media/filters/reference_audio_renderer.h" | |
6 | |
7 #include <math.h> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/synchronization/waitable_event.h" | |
11 | |
12 namespace media { | |
13 | |
14 ReferenceAudioRenderer::ReferenceAudioRenderer(AudioManager* audio_manager) | |
15 : AudioRendererBase(), | |
16 audio_manager_(audio_manager), | |
17 bytes_per_second_(0), | |
18 buffer_capacity_(0) { | |
19 } | |
20 | |
21 ReferenceAudioRenderer::~ReferenceAudioRenderer() { | |
22 // Close down the audio device. | |
23 if (controller_) { | |
24 base::WaitableEvent closed_event(true, false); | |
25 controller_->Close(base::Bind(&base::WaitableEvent::Signal, | |
26 base::Unretained(&closed_event))); | |
27 closed_event.Wait(); | |
28 } | |
29 } | |
30 | |
31 void ReferenceAudioRenderer::SetPlaybackRate(float rate) { | |
32 // TODO(fbarchard): limit rate to reasonable values | |
33 AudioRendererBase::SetPlaybackRate(rate); | |
34 | |
35 if (controller_ && rate > 0.0f) | |
36 controller_->Play(); | |
37 } | |
38 | |
39 void ReferenceAudioRenderer::SetVolume(float volume) { | |
40 if (controller_) | |
41 controller_->SetVolume(volume); | |
42 } | |
43 | |
44 void ReferenceAudioRenderer::OnCreated(AudioOutputController* controller) { | |
45 NOTIMPLEMENTED(); | |
46 } | |
47 | |
48 void ReferenceAudioRenderer::OnPlaying(AudioOutputController* controller) { | |
49 NOTIMPLEMENTED(); | |
50 } | |
51 | |
52 void ReferenceAudioRenderer::OnPaused(AudioOutputController* controller) { | |
53 NOTIMPLEMENTED(); | |
54 } | |
55 | |
56 void ReferenceAudioRenderer::OnError(AudioOutputController* controller, | |
57 int error_code) { | |
58 NOTIMPLEMENTED(); | |
59 } | |
60 | |
61 void ReferenceAudioRenderer::OnMoreData(AudioOutputController* controller, | |
62 AudioBuffersState buffers_state) { | |
63 // TODO(fbarchard): Waveout_output_win.h should handle zero length buffers | |
64 // without clicking. | |
65 uint32 pending_bytes = static_cast<uint32>(ceil(buffers_state.total_bytes() * | |
66 GetPlaybackRate())); | |
67 base::TimeDelta delay = base::TimeDelta::FromMicroseconds( | |
68 base::Time::kMicrosecondsPerSecond * pending_bytes / | |
69 bytes_per_second_); | |
70 bool buffers_empty = buffers_state.pending_bytes == 0; | |
71 uint32 read = FillBuffer(buffer_.get(), buffer_capacity_, delay, | |
72 buffers_empty); | |
73 controller->EnqueueData(buffer_.get(), read); | |
74 } | |
75 | |
76 bool ReferenceAudioRenderer::OnInitialize(int bits_per_channel, | |
77 ChannelLayout channel_layout, | |
78 int sample_rate) { | |
79 int samples_per_packet = sample_rate / 10; | |
80 int hardware_buffer_size = samples_per_packet * | |
81 ChannelLayoutToChannelCount(channel_layout) * bits_per_channel / 8; | |
82 | |
83 // Allocate audio buffer based on hardware buffer size. | |
84 buffer_capacity_ = 3 * hardware_buffer_size; | |
85 buffer_.reset(new uint8[buffer_capacity_]); | |
86 | |
87 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, channel_layout, | |
88 sample_rate, bits_per_channel, samples_per_packet); | |
89 bytes_per_second_ = params.GetBytesPerSecond(); | |
90 | |
91 controller_ = AudioOutputController::Create(audio_manager_, this, params, | |
92 buffer_capacity_); | |
93 return controller_ != NULL; | |
94 } | |
95 | |
96 void ReferenceAudioRenderer::OnStop() { | |
97 if (controller_) | |
98 controller_->Pause(); | |
99 } | |
100 | |
101 } // namespace media | |
OLD | NEW |