diff options
author | cpu@google.com <cpu@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-13 23:57:32 +0000 |
---|---|---|
committer | cpu@google.com <cpu@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-13 23:57:32 +0000 |
commit | 999ebd77596b7e9ee3c13fef014d3b2d6637d559 (patch) | |
tree | 7de7fe03640a512272955cf9f3274035ae7be144 /media/audio/win/waveout_output_win.h | |
parent | 7cecfb2fa8bd8bb4254f8d83ad0ce5d23961f98c (diff) | |
download | chromium_src-999ebd77596b7e9ee3c13fef014d3b2d6637d559.zip chromium_src-999ebd77596b7e9ee3c13fef014d3b2d6637d559.tar.gz chromium_src-999ebd77596b7e9ee3c13fef014d3b2d6637d559.tar.bz2 |
Low level windows audio support (part 2 of 2)
- Implementation using the WaveXXXX windows API
- Unit tests for the implementation and for the previous CL that
had the sine wave simple source.
This is the harder one :) I tried to add comments to illuminate the thinking.
Review URL: http://codereview.chromium.org/17403
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@7988 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/audio/win/waveout_output_win.h')
-rw-r--r-- | media/audio/win/waveout_output_win.h | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/media/audio/win/waveout_output_win.h b/media/audio/win/waveout_output_win.h new file mode 100644 index 0000000..bdf35b3 --- /dev/null +++ b/media/audio/win/waveout_output_win.h @@ -0,0 +1,106 @@ +// 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_WAVEOUT_OUTPUT_WIN_H_ +#define MEDIA_AUDIO_WAVEOUT_OUTPUT_WIN_H_ + +#include <windows.h> +#include <mmsystem.h> + +#include "base/basictypes.h" +#include "base/scoped_handle_win.h" +#include "media/audio/audio_output.h" + +class AudioManagerWin; + +// Implements PCM audio output support for Windows using the WaveXXX API. +// While not as nice as the DirectSound-based API, it should work in all target +// operating systems regardless or DirectX version installed. It is known that +// in some machines WaveXXX based audio is better while in others DirectSound +// is better. +// +// Important: the OnXXXX functions in AudioSourceCallback are called by more +// than one thread so it is important to have some form of synchronization if +// you are keeping state in it. +class PCMWaveOutAudioOutputStream : public AudioOutputStream { + public: + // The ctor takes all the usual parameters, plus |manager| which is the + // the audio manager who is creating this object and |device_id| which + // is provided by the operating system. + PCMWaveOutAudioOutputStream(AudioManagerWin* manager, + int channels, int sampling_rate, + char bits_per_sample, UINT device_id); + virtual ~PCMWaveOutAudioOutputStream(); + + // Implementation of AudioOutputStream. + virtual bool Open(size_t packet_size); + virtual void Close(); + virtual void Start(AudioSourceCallback* callback); + virtual void Stop(); + virtual void SetVolume(double left_level, double right_level); + virtual void GetVolume(double* left_level, double* right_level); + + // Sends a buffer to the audio driver for playback. + void QueueNextPacket(WAVEHDR* buffer); + + private: + enum State { + PCMA_BRAND_NEW, // Initial state. + PCMA_READY, // Device obtained and ready to play. + PCMA_PLAYING, // Playing audio. + PCMA_STOPPING, // Trying to stop, waiting for callback to finish. + PCMA_STOPPED, // Stopped. Device was reset. + PCMA_CLOSED // Device has been released. + }; + + // Windows calls us back to feed more data to the device here. See msdn + // documentation for 'waveOutProc' for details about the parameters. + static void CALLBACK WaveCallback(HWAVEOUT hwo, UINT msg, DWORD_PTR instance, + DWORD_PTR param1, DWORD_PTR param2); + + // If windows reports an error this function handles it and passes it to + // the attached AudioSourceCallback::OnError(). + void HandleError(MMRESULT error); + // Allocates and prepares the memory that will be used for playback. Only + // two buffers are created. + void SetupBuffers(size_t rq_size); + // Deallocates the memory allocated in SetupBuffers. + void FreeBuffers(); + + // Reader beware. Visual C has stronger guarantees on volatile vars than + // most people expect. In fact, it has release semantics on write and + // acquire semantics on reads. See the msdn documentation. + volatile State state_; + + // The audio manager that created this output stream. We notify it when + // we close so it can release its own resources. + AudioManagerWin* manager_; + + // We use the callback mostly to periodically request more audio data. + AudioSourceCallback* callback_; + + // The size in bytes of each audio buffer, we usually have two of these. + size_t buffer_size_; + + // The id assigned by the operating system to the selected wave output + // hardware device. Usually this is just -1 which means 'default device'. + UINT device_id_; + + // Windows native structure to encode the format parameters. + WAVEFORMATEX format_; + + // Handle to the instance of the wave device. + HWAVEOUT waveout_; + + // Pointer to the first allocated audio buffer. This object owns it. + WAVEHDR* buffer_; + + // An event that is signaled when the callback thread is ready to stop. + ScopedHandle stopped_event_; + + DISALLOW_COPY_AND_ASSIGN(PCMWaveOutAudioOutputStream); +}; + +#endif // MEDIA_AUDIO_WAVEOUT_OUTPUT_WIN_H_ + |