OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MEDIA_AUDIO_WIN_WAVEIN_INPUT_WIN_H_ |
| 6 #define MEDIA_AUDIO_WIN_WAVEIN_INPUT_WIN_H_ |
| 7 |
| 8 #include <windows.h> |
| 9 #include <mmsystem.h> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/scoped_handle_win.h" |
| 13 #include "media/audio/audio_io.h" |
| 14 |
| 15 class AudioManagerWin; |
| 16 |
| 17 class PCMWaveInAudioInputStream : public AudioInputStream { |
| 18 public: |
| 19 // The ctor takes all the usual parameters, plus |manager| which is the |
| 20 // the audio manager who is creating this object and |device_id| which |
| 21 // is provided by the operating system. |
| 22 PCMWaveInAudioInputStream(AudioManagerWin* manager, int channels, |
| 23 int sampling_rate, int num_buffers, |
| 24 char bits_per_sample, uint32 samples_per_packet, |
| 25 UINT device_id); |
| 26 virtual ~PCMWaveInAudioInputStream(); |
| 27 |
| 28 // Implementation of AudioInputStream. |
| 29 virtual bool Open(); |
| 30 virtual void Start(AudioInputCallback* callback); |
| 31 virtual void Stop(); |
| 32 virtual void Close(); |
| 33 |
| 34 private: |
| 35 enum State { |
| 36 kStateEmpty, // Initial state. |
| 37 kStateReady, // Device obtained and ready to record. |
| 38 kStateRecording, // Recording audio. |
| 39 kStateStopping, // Trying to stop, waiting for callback to finish. |
| 40 kStateStopped, // Stopped. Device was reset. |
| 41 kStateClosed // Device has been released. |
| 42 }; |
| 43 |
| 44 // Windows calls us back with the recorded audio data here. See msdn |
| 45 // documentation for 'waveInProc' for details about the parameters. |
| 46 static void CALLBACK WaveCallback(HWAVEIN hwi, UINT msg, DWORD_PTR instance, |
| 47 DWORD_PTR param1, DWORD_PTR param2); |
| 48 |
| 49 // If windows reports an error this function handles it and passes it to |
| 50 // the attached AudioInputCallback::OnError(). |
| 51 void HandleError(MMRESULT error); |
| 52 |
| 53 // Allocates and prepares the memory that will be used for recording. |
| 54 void SetupBuffers(); |
| 55 |
| 56 // Deallocates the memory allocated in SetupBuffers. |
| 57 void FreeBuffers(); |
| 58 |
| 59 // Sends a buffer to the audio driver for recording. |
| 60 void QueueNextPacket(WAVEHDR* buffer); |
| 61 |
| 62 // Reader beware. Visual C has stronger guarantees on volatile vars than |
| 63 // most people expect. In fact, it has release semantics on write and |
| 64 // acquire semantics on reads. See the msdn documentation. |
| 65 volatile State state_; |
| 66 |
| 67 // The audio manager that created this input stream. We notify it when |
| 68 // we close so it can release its own resources. |
| 69 AudioManagerWin* manager_; |
| 70 |
| 71 // We use the callback mostly to periodically give the recorded audio data. |
| 72 AudioInputCallback* callback_; |
| 73 |
| 74 // The number of buffers of size |buffer_size_| each to use. |
| 75 const int num_buffers_; |
| 76 |
| 77 // The size in bytes of each audio buffer. |
| 78 uint32 buffer_size_; |
| 79 |
| 80 // Channels, 1 or 2. |
| 81 const int channels_; |
| 82 |
| 83 // The id assigned by the operating system to the selected wave output |
| 84 // hardware device. Usually this is just -1 which means 'default device'. |
| 85 UINT device_id_; |
| 86 |
| 87 // Windows native structure to encode the format parameters. |
| 88 WAVEFORMATEX format_; |
| 89 |
| 90 // Handle to the instance of the wave device. |
| 91 HWAVEIN wavein_; |
| 92 |
| 93 // Pointer to the first allocated audio buffer. This object owns it. |
| 94 WAVEHDR* buffer_; |
| 95 |
| 96 // An event that is signaled when the callback thread is ready to stop. |
| 97 ScopedHandle stopped_event_; |
| 98 |
| 99 DISALLOW_COPY_AND_ASSIGN(PCMWaveInAudioInputStream); |
| 100 }; |
| 101 |
| 102 #endif // MEDIA_AUDIO_WIN_WAVEIN_INPUT_WIN_H_ |
OLD | NEW |