Optimize phishing page term feature extraction.
[chromium-blink-merge.git] / media / filters / audio_renderer_impl.cc
blobae9199329a86dfa9b9a9c80ef21fce910b30f1f0
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
3 // found in the LICENSE file.
5 #include "media/filters/audio_renderer_impl.h"
7 #include <math.h>
9 #include "base/logging.h"
10 #include "media/base/filter_host.h"
11 #include "media/audio/audio_manager.h"
13 namespace media {
15 // We'll try to fill 4096 samples per buffer, which is roughly ~92ms of audio
16 // data for a 44.1kHz audio source.
17 static const size_t kSamplesPerBuffer = 8*1024;
19 AudioRendererImpl::AudioRendererImpl()
20 : AudioRendererBase(),
21 stream_(NULL),
22 bytes_per_second_(0) {
25 AudioRendererImpl::~AudioRendererImpl() {
26 // Close down the audio device.
27 if (stream_) {
28 stream_->Stop();
29 stream_->Close();
33 void AudioRendererImpl::SetPlaybackRate(float rate) {
34 // TODO(fbarchard): limit rate to reasonable values
35 AudioRendererBase::SetPlaybackRate(rate);
37 static bool started = false;
38 if (rate > 0.0f && !started && stream_)
39 stream_->Start(this);
42 void AudioRendererImpl::SetVolume(float volume) {
43 if (stream_)
44 stream_->SetVolume(volume);
47 uint32 AudioRendererImpl::OnMoreData(
48 AudioOutputStream* stream, uint8* dest, uint32 len,
49 AudioBuffersState buffers_state) {
50 // TODO(scherkus): handle end of stream.
51 if (!stream_)
52 return 0;
54 // TODO(fbarchard): Waveout_output_win.h should handle zero length buffers
55 // without clicking.
56 uint32 pending_bytes = static_cast<uint32>(ceil(buffers_state.total_bytes() *
57 GetPlaybackRate()));
58 base::TimeDelta delay = base::TimeDelta::FromMicroseconds(
59 base::Time::kMicrosecondsPerSecond * pending_bytes /
60 bytes_per_second_);
61 bool buffers_empty = buffers_state.pending_bytes == 0;
62 return FillBuffer(dest, len, delay, buffers_empty);
65 void AudioRendererImpl::OnClose(AudioOutputStream* stream) {
66 // TODO(scherkus): implement OnClose.
67 NOTIMPLEMENTED();
70 void AudioRendererImpl::OnError(AudioOutputStream* stream, int code) {
71 // TODO(scherkus): implement OnError.
72 NOTIMPLEMENTED();
75 bool AudioRendererImpl::OnInitialize(const AudioDecoderConfig& config) {
76 AudioParameters params(config);
77 params.samples_per_packet = kSamplesPerBuffer;
79 bytes_per_second_ = params.GetBytesPerSecond();
81 // Create our audio stream.
82 stream_ = AudioManager::GetAudioManager()->MakeAudioOutputStream(params);
83 if (!stream_)
84 return false;
86 if (!stream_->Open()) {
87 stream_->Close();
88 stream_ = NULL;
90 return true;
93 void AudioRendererImpl::OnStop() {
94 if (stream_)
95 stream_->Stop();
98 } // namespace media