1 // Copyright (c) 2012 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.
5 #ifndef MEDIA_AUDIO_WIN_WAVEIN_INPUT_WIN_H_
6 #define MEDIA_AUDIO_WIN_WAVEIN_INPUT_WIN_H_
13 #include "base/basictypes.h"
14 #include "base/compiler_specific.h"
15 #include "base/synchronization/lock.h"
16 #include "base/threading/thread_checker.h"
17 #include "base/win/scoped_handle.h"
18 #include "media/audio/audio_io.h"
19 #include "media/audio/audio_parameters.h"
23 class AudioManagerWin
;
25 class PCMWaveInAudioInputStream
: public AudioInputStream
{
27 // The ctor takes all the usual parameters, plus |manager| which is the
28 // the audio manager who is creating this object and |device_id| which
29 // is provided by the operating system.
30 PCMWaveInAudioInputStream(AudioManagerWin
* manager
,
31 const AudioParameters
& params
,
33 const std::string
& device_id
);
34 virtual ~PCMWaveInAudioInputStream();
36 // Implementation of AudioInputStream.
37 virtual bool Open() OVERRIDE
;
38 virtual void Start(AudioInputCallback
* callback
) OVERRIDE
;
39 virtual void Stop() OVERRIDE
;
40 virtual void Close() OVERRIDE
;
41 // TODO(henrika): Add volume support using the Audio Mixer API.
42 virtual double GetMaxVolume() OVERRIDE
;
43 virtual void SetVolume(double volume
) OVERRIDE
;
44 virtual double GetVolume() OVERRIDE
;
45 virtual void SetAutomaticGainControl(bool enabled
) OVERRIDE
;
46 virtual bool GetAutomaticGainControl() OVERRIDE
;
50 kStateEmpty
, // Initial state.
51 kStateReady
, // Device obtained and ready to record.
52 kStateRecording
, // Recording audio.
53 kStateStopping
, // Trying to stop, waiting for callback to finish.
54 kStateStopped
, // Stopped. Device was reset.
55 kStateClosed
// Device has been released.
58 // Allow unit tests to query the device ID.
59 friend class AudioInputDeviceTest
;
61 // Windows calls us back with the recorded audio data here. See msdn
62 // documentation for 'waveInProc' for details about the parameters.
63 static void CALLBACK
WaveCallback(HWAVEIN hwi
, UINT msg
, DWORD_PTR instance
,
64 DWORD_PTR param1
, DWORD_PTR param2
);
66 // If windows reports an error this function handles it and passes it to
67 // the attached AudioInputCallback::OnError().
68 void HandleError(MMRESULT error
);
70 // Allocates and prepares the memory that will be used for recording.
73 // Deallocates the memory allocated in SetupBuffers.
76 // Sends a buffer to the audio driver for recording.
77 void QueueNextPacket(WAVEHDR
* buffer
);
79 // Converts the stored device id string into an unsigned integer which
80 // can be used by waveInOpen() to open the specified capture device.
81 bool GetDeviceId(UINT
* device_index
);
83 base::ThreadChecker thread_checker_
;
85 // Reader beware. Visual C has stronger guarantees on volatile vars than
86 // most people expect. In fact, it has release semantics on write and
87 // acquire semantics on reads. See the msdn documentation.
88 volatile State state_
;
90 // The audio manager that created this input stream. We notify it when
91 // we close so it can release its own resources.
92 AudioManagerWin
* manager_
;
94 // We use the callback mostly to periodically give the recorded audio data.
95 AudioInputCallback
* callback_
;
97 // The number of buffers of size |buffer_size_| each to use.
98 const int num_buffers_
;
100 // The size in bytes of each audio buffer.
106 // Contains the unique name of the selected endpoint device.
107 // Note that AudioManagerBase::kDefaultDeviceId represents the default
108 // device role and is not a valid ID as such.
109 std::string device_id_
;
111 // Windows native structure to encode the format parameters.
112 WAVEFORMATEX format_
;
114 // Handle to the instance of the wave device.
117 // Pointer to the first allocated audio buffer. This object owns it.
120 // An event that is signaled when the callback thread is ready to stop.
121 base::win::ScopedHandle stopped_event_
;
123 // Lock used to avoid conflicts when Stop() is called during a callback.
126 DISALLOW_COPY_AND_ASSIGN(PCMWaveInAudioInputStream
);
131 #endif // MEDIA_AUDIO_WIN_WAVEIN_INPUT_WIN_H_