diff options
author | cpu@google.com <cpu@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-13 23:56:56 +0000 |
---|---|---|
committer | cpu@google.com <cpu@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-13 23:56:56 +0000 |
commit | 7cecfb2fa8bd8bb4254f8d83ad0ce5d23961f98c (patch) | |
tree | 5240f4628db2efff362da4c52c69b8cb29853db6 /media/audio/win | |
parent | b62d1a8c6795a9feefe4a2e688463c56ce46a6bd (diff) | |
download | chromium_src-7cecfb2fa8bd8bb4254f8d83ad0ce5d23961f98c.zip chromium_src-7cecfb2fa8bd8bb4254f8d83ad0ce5d23961f98c.tar.gz chromium_src-7cecfb2fa8bd8bb4254f8d83ad0ce5d23961f98c.tar.bz2 |
Low level windows audio support (part 1 of 2)
- Windows specific changes to AudioManager Allows testing for mock streams
This this the first part of the code that adds support for audio using waveXXX API.
Since is relatively lot of code I will do two CLs, unfortunately the complexity
is not evenly split.
Review URL: http://codereview.chromium.org/17401
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@7987 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/audio/win')
-rw-r--r-- | media/audio/win/audio_manager_win.h | 41 | ||||
-rw-r--r-- | media/audio/win/audio_output_win.cc | 97 |
2 files changed, 108 insertions, 30 deletions
diff --git a/media/audio/win/audio_manager_win.h b/media/audio/win/audio_manager_win.h new file mode 100644 index 0000000..df9e076 --- /dev/null +++ b/media/audio/win/audio_manager_win.h @@ -0,0 +1,41 @@ +// Copyright (c) 2006-2008 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. + +#ifndef MEDIA_AUDIO_AUDIO_MANAGER_WIN_H_ +#define MEDIA_AUDIO_AUDIO_MANAGER_WIN_H_ + +#include <windows.h> + +#include "base/basictypes.h" +#include "media/audio/audio_output.h" + +class PCMWaveOutAudioOutputStream; +class AudioOutputStreamMockWin; + +// Windows implementation of the AudioManager singleton. This class is internal +// to the audio output and only internal users can call methods not exposed by +// the AudioManager class. +class AudioManagerWin : public AudioManager { + public: + AudioManagerWin() {} + // Implementation of AudioManager. + virtual AudioOutputStream* MakeAudioStream(Format format, int channels, + int sample_rate, + char bits_per_sample); + virtual void MuteAll(); + virtual void UnMuteAll(); + virtual const void* GetLastMockBuffer(); + + // Windows-only methods to free a stream created in MakeAudioStream. These + // are called internally by the audio stream when it has been closed. + void ReleaseStream(PCMWaveOutAudioOutputStream* stream); + void ReleaseStream(AudioOutputStreamMockWin* stream); + + private: + virtual ~AudioManagerWin(); + DISALLOW_COPY_AND_ASSIGN(AudioManagerWin); +}; + +#endif // MEDIA_AUDIO_AUDIO_MANAGER_WIN_H_ + diff --git a/media/audio/win/audio_output_win.cc b/media/audio/win/audio_output_win.cc index 4fe06e0..5685ee7 100644 --- a/media/audio/win/audio_output_win.cc +++ b/media/audio/win/audio_output_win.cc @@ -3,22 +3,31 @@ // found in the LICENSE file. #include <windows.h> +#include <mmsystem.h> #include "base/basictypes.h" #include "media/audio/audio_output.h" +#include "media/audio/win/audio_manager_win.h" +#include "media/audio/win/waveout_output_win.h" // A do-nothing audio stream. It behaves like a regular audio stream but does // not have any side effect, except possibly the creation and tear-down of // of a thread. It is useful to test code that uses audio streams such as // audio sources. -class AudioOutputStreamWinMock : public AudioOutputStream { +class AudioOutputStreamMockWin : public AudioOutputStream { public: - AudioOutputStreamWinMock() - : callback_(NULL), + explicit AudioOutputStreamMockWin(AudioManagerWin* manager) + : manager_(manager), + callback_(NULL), buffer_(NULL), packet_size_(0), - left_volume_(0.0), - right_volume_(0.0) { + left_volume_(1.0), + right_volume_(1.0) { + } + + virtual ~AudioOutputStreamMockWin() { + delete[] buffer_; + packet_size_ = 0; } virtual bool Open(size_t packet_size) { @@ -31,6 +40,7 @@ class AudioOutputStreamWinMock : public AudioOutputStream { virtual void Start(AudioSourceCallback* callback) { callback_ = callback; + memset(buffer_, 0, packet_size_); callback_->OnMoreData(this, buffer_, packet_size_); } @@ -52,16 +62,15 @@ class AudioOutputStreamWinMock : public AudioOutputStream { virtual void Close() { callback_->OnClose(this); callback_ = NULL; - delete this; + manager_->ReleaseStream(this); } - protected: - virtual ~AudioOutputStreamWinMock() { - delete[] buffer_; - packet_size_ = 0; + char* buffer() { + return buffer_; } private: + AudioManagerWin* manager_; AudioSourceCallback* callback_; char* buffer_; size_t packet_size_; @@ -69,35 +78,63 @@ class AudioOutputStreamWinMock : public AudioOutputStream { double right_volume_; }; -class AudioManagerWin : public AudioManager { - public: - virtual AudioOutputStream* MakeAudioStream(Format format, int channels, - int sample_rate, - char bits_per_sample) { - if (format == AUDIO_MOCK) - return new AudioOutputStreamWinMock(); - return NULL; - } +namespace { +AudioOutputStreamMockWin* g_last_mock_stream = NULL; - virtual void MuteAll() { - } +void ReplaceLastMockStream(AudioOutputStreamMockWin* newer) { + if (g_last_mock_stream) + delete g_last_mock_stream; + g_last_mock_stream = newer; +} - virtual void UnMuteAll() { +} // namespace. + +// Factory for the implementations of AudioOutputStream. Two implementations +// should suffice most windows user's needs. +// - PCMWaveOutAudioOutputStream: Based on the waveOutWrite API (in progress) +// - PCMDXSoundAudioOutputStream: Based on DirectSound or XAudio (future work). + +AudioOutputStream* AudioManagerWin::MakeAudioStream(Format format, int channels, + int sample_rate, + char bits_per_sample) { + if (format == AUDIO_MOCK) { + return new AudioOutputStreamMockWin(this); + } else if (format == AUDIO_PCM_LINEAR) { + return new PCMWaveOutAudioOutputStream(this, channels, sample_rate, + bits_per_sample, WAVE_MAPPER); } + return NULL; +} - protected: - virtual ~AudioManagerWin() { - } -}; +void AudioManagerWin::ReleaseStream(PCMWaveOutAudioOutputStream* stream) { + if (stream) + delete stream; +} + +void AudioManagerWin::ReleaseStream(AudioOutputStreamMockWin *stream) { + // Note that we keep the last mock stream so GetLastMockBuffer() works. + ReplaceLastMockStream(stream); +} + +const void* AudioManagerWin::GetLastMockBuffer() { + return (g_last_mock_stream) ? g_last_mock_stream->buffer() : NULL; +} + +void AudioManagerWin::MuteAll() { +} + +void AudioManagerWin::UnMuteAll() { +} + +AudioManagerWin::~AudioManagerWin() { + ReplaceLastMockStream(NULL); +} // TODO(cpu): Decide how to manage the lifetime of the AudioManager singleton. // Right now we are leaking it. -AudioManager* GetAudioManager() { +AudioManager* AudioManager::GetAudioManager() { static AudioManagerWin* audio_manager = NULL; if (!audio_manager) audio_manager = new AudioManagerWin(); return audio_manager; } - - - |