diff options
author | satish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-14 08:16:50 +0000 |
---|---|---|
committer | satish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-14 08:16:50 +0000 |
commit | 9fb3102a8332d0c49dc5c49733b7eb13c1d2a0b8 (patch) | |
tree | e6b0b0e3511933d9ee8405cce8f26d7e15f30319 /media/audio/win/wavein_input_win.h | |
parent | eee3452dafdca8a97fa73ea466b6621dcff77238 (diff) | |
download | chromium_src-9fb3102a8332d0c49dc5c49733b7eb13c1d2a0b8.zip chromium_src-9fb3102a8332d0c49dc5c49733b7eb13c1d2a0b8.tar.gz chromium_src-9fb3102a8332d0c49dc5c49733b7eb13c1d2a0b8.tar.bz2 |
Add recording capability to AudioManager, and implemented on windows using the WaveIn APIs.
Implementation for other platforms will follow in future patches.
Also includes a unit test.
BUG=none
TEST=no user visible change yet, just adding audio recording backend.
Review URL: http://codereview.chromium.org/2966005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52292 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/audio/win/wavein_input_win.h')
-rw-r--r-- | media/audio/win/wavein_input_win.h | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/media/audio/win/wavein_input_win.h b/media/audio/win/wavein_input_win.h new file mode 100644 index 0000000..a4ea435 --- /dev/null +++ b/media/audio/win/wavein_input_win.h @@ -0,0 +1,102 @@ +// 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. + +#ifndef MEDIA_AUDIO_WIN_WAVEIN_INPUT_WIN_H_ +#define MEDIA_AUDIO_WIN_WAVEIN_INPUT_WIN_H_ + +#include <windows.h> +#include <mmsystem.h> + +#include "base/basictypes.h" +#include "base/scoped_handle_win.h" +#include "media/audio/audio_io.h" + +class AudioManagerWin; + +class PCMWaveInAudioInputStream : public AudioInputStream { + 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. + PCMWaveInAudioInputStream(AudioManagerWin* manager, int channels, + int sampling_rate, int num_buffers, + char bits_per_sample, uint32 samples_per_packet, + UINT device_id); + virtual ~PCMWaveInAudioInputStream(); + + // Implementation of AudioInputStream. + virtual bool Open(); + virtual void Start(AudioInputCallback* callback); + virtual void Stop(); + virtual void Close(); + + private: + enum State { + kStateEmpty, // Initial state. + kStateReady, // Device obtained and ready to record. + kStateRecording, // Recording audio. + kStateStopping, // Trying to stop, waiting for callback to finish. + kStateStopped, // Stopped. Device was reset. + kStateClosed // Device has been released. + }; + + // Windows calls us back with the recorded audio data here. See msdn + // documentation for 'waveInProc' for details about the parameters. + static void CALLBACK WaveCallback(HWAVEIN hwi, 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 AudioInputCallback::OnError(). + void HandleError(MMRESULT error); + + // Allocates and prepares the memory that will be used for recording. + void SetupBuffers(); + + // Deallocates the memory allocated in SetupBuffers. + void FreeBuffers(); + + // Sends a buffer to the audio driver for recording. + void QueueNextPacket(WAVEHDR* buffer); + + // 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 input stream. We notify it when + // we close so it can release its own resources. + AudioManagerWin* manager_; + + // We use the callback mostly to periodically give the recorded audio data. + AudioInputCallback* callback_; + + // The number of buffers of size |buffer_size_| each to use. + const int num_buffers_; + + // The size in bytes of each audio buffer. + uint32 buffer_size_; + + // Channels, 1 or 2. + const int channels_; + + // 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. + HWAVEIN wavein_; + + // 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(PCMWaveInAudioInputStream); +}; + +#endif // MEDIA_AUDIO_WIN_WAVEIN_INPUT_WIN_H_ |