diff options
author | cpu@google.com <cpu@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-23 01:27:49 +0000 |
---|---|---|
committer | cpu@google.com <cpu@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-23 01:27:49 +0000 |
commit | 936f535cfbf9d82559cd54e1011f54480839b790 (patch) | |
tree | 8727353fafe26f937d8ae69c889015b71671ed2f /media/audio/simple_sources.cc | |
parent | 920603a234542c6fa484bb00ae2e8e20e7ffabbc (diff) | |
download | chromium_src-936f535cfbf9d82559cd54e1011f54480839b790.zip chromium_src-936f535cfbf9d82559cd54e1011f54480839b790.tar.gz chromium_src-936f535cfbf9d82559cd54e1011f54480839b790.tar.bz2 |
Non blocking audio source
- New interface PushAudioOutput for push model source
- New adapter class PushSource which converts from pull model to push model
- A test audio file (made by me using freeware program WavePad)
- A new test
TEST=unit test included.
Review URL: http://codereview.chromium.org/115223
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16825 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/audio/simple_sources.cc')
-rw-r--r-- | media/audio/simple_sources.cc | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/media/audio/simple_sources.cc b/media/audio/simple_sources.cc index b6fda23..6643fc1 100644 --- a/media/audio/simple_sources.cc +++ b/media/audio/simple_sources.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include <algorithm> #include <math.h> #include "base/basictypes.h" @@ -9,6 +10,9 @@ #include "media/audio/audio_output.h" #include "media/audio/simple_sources.h" +////////////////////////////////////////////////////////////////////////////// +// SineWaveAudioSource implementation. + SineWaveAudioSource::SineWaveAudioSource(Format format, int channels, double freq, double sample_freq) : format_(format), @@ -41,3 +45,70 @@ void SineWaveAudioSource::OnClose(AudioOutputStream* stream) { void SineWaveAudioSource::OnError(AudioOutputStream* stream, int code) { NOTREACHED(); } + +////////////////////////////////////////////////////////////////////////////// +// PushSource implementation. + +PushSource::PushSource(size_t packet_size) + : packet_size_(packet_size), buffered_bytes_(0) { +} + +PushSource::~PushSource() { + CleanUp(); +} + +size_t PushSource::OnMoreData(AudioOutputStream* stream, + void* dest, size_t max_size) { + Packet packet; + { + AutoLock auto_lock(lock_); + // Under lock processing in this scope. + if (!packets_.size()) + return 0; + packet = packets_.front(); + packets_.pop_front(); + buffered_bytes_ -= packet.size; + } + size_t size = std::min(max_size, packet.size); + memcpy(dest, packet.buffer, size); + delete [] packet.buffer; + return size; +} + +void PushSource::OnClose(AudioOutputStream* stream) { + CleanUp(); +} + +void PushSource::OnError(AudioOutputStream* stream, int code) { + NOTREACHED(); +} + +// TODO(cpu): Manage arbitrary large sizes. +bool PushSource::Write(const void *data, size_t len) { + if ((len == 0) || (len > packet_size_)) { + NOTREACHED(); + return false; + } + Packet packet = { new char[len], len }; + memcpy(packet.buffer, data, packet.size); + // Under lock processing here. + AutoLock auto_lock(lock_); + packets_.push_back(packet); + buffered_bytes_ += len; + return true; +} + +size_t PushSource::UnProcessedBytes() { + AutoLock auto_lock(lock_); + return buffered_bytes_; +} + +void PushSource::CleanUp() { + AutoLock auto_lock(lock_); + PacketList::const_iterator it; + for (it = packets_.begin(); it != packets_.end(); ++it) { + delete it->buffer; + buffered_bytes_ -= it->size; + } + packets_.clear(); +} |