diff options
author | satish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-15 08:34:06 +0000 |
---|---|---|
committer | satish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-15 08:34:06 +0000 |
commit | 74532a5880470f854def44a862a79af012bfd395 (patch) | |
tree | bfd5181457430fc88f39246364cb575f1593bc92 /media/audio/fake_audio_input_stream.cc | |
parent | 9ea053e87d2141867ee3429d8917809fe2e33c80 (diff) | |
download | chromium_src-74532a5880470f854def44a862a79af012bfd395.zip chromium_src-74532a5880470f854def44a862a79af012bfd395.tar.gz chromium_src-74532a5880470f854def44a862a79af012bfd395.tar.bz2 |
Add a fake audio input stream for testing purposes.
This will be used by unit tests in subsequent patches.
BUG=none
TEST=no new features added and nothing new to test
Review URL: http://codereview.chromium.org/2909015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52464 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/audio/fake_audio_input_stream.cc')
-rw-r--r-- | media/audio/fake_audio_input_stream.cc | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/media/audio/fake_audio_input_stream.cc b/media/audio/fake_audio_input_stream.cc new file mode 100644 index 0000000..9748d8e --- /dev/null +++ b/media/audio/fake_audio_input_stream.cc @@ -0,0 +1,80 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "media/audio/fake_audio_input_stream.h" + +#include "base/at_exit.h" + +using base::Time; +using base::TimeDelta; + +AudioInputStream* FakeAudioInputStream::MakeFakeStream(int channels, + int bits_per_sample, + int sampling_rate, + int samples_per_packet) { + return new FakeAudioInputStream(channels, bits_per_sample, sampling_rate, + samples_per_packet); +} + +FakeAudioInputStream::FakeAudioInputStream(int channels, int bits_per_sample, + int sampling_rate, + int samples_per_packet) + : callback_(NULL), + buffer_size_((channels * bits_per_sample * samples_per_packet) / 8), + thread_("FakeAudioRecordingThread"), + callback_interval_ms_((samples_per_packet * 1000) / sampling_rate) { + // This object is ref counted (so that it can be used with Thread, PostTask) + // but the caller expects a plain pointer. So we take a reference here and + // will Release() ourselves in Close(). + AddRef(); +} + +bool FakeAudioInputStream::Open() { + buffer_.reset(new uint8[buffer_size_]); + memset(buffer_.get(), 0, buffer_size_); + return true; +} + +void FakeAudioInputStream::Start(AudioInputCallback* callback) { + DCHECK(!thread_.IsRunning()); + callback_ = callback; + last_callback_time_ = Time::Now(); + thread_.Start(); + thread_.message_loop()->PostDelayedTask( + FROM_HERE, + NewRunnableMethod(this, &FakeAudioInputStream::DoCallback), + callback_interval_ms_); +} + +void FakeAudioInputStream::DoCallback() { + DCHECK(callback_); + callback_->OnData(this, buffer_.get(), buffer_size_); + + Time now = Time::Now(); + int64 next_callback_ms = (last_callback_time_ + + TimeDelta::FromMilliseconds(callback_interval_ms_ * 2) - + now).InMilliseconds(); + // If we are falling behind, try to catch up as much as we can in the next + // callback. + if (next_callback_ms < 0) + next_callback_ms = 0; + + last_callback_time_ = now; + thread_.message_loop()->PostDelayedTask( + FROM_HERE, + NewRunnableMethod(this, &FakeAudioInputStream::DoCallback), + next_callback_ms); +} + +void FakeAudioInputStream::Stop() { + thread_.Stop(); +} + +void FakeAudioInputStream::Close() { + if (callback_) { + callback_->OnClose(this); + callback_ = NULL; + } + Release(); // Destoys this object. +} |