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"
24 class AudioManagerWin
;
26 class PCMWaveInAudioInputStream
: public AudioInputStream
{
28 // The ctor takes all the usual parameters, plus |manager| which is the
29 // the audio manager who is creating this object and |device_id| which
30 // is provided by the operating system.
31 PCMWaveInAudioInputStream(AudioManagerWin
* manager
,
32 const AudioParameters
& params
,
34 const std::string
& device_id
);
35 ~PCMWaveInAudioInputStream() override
;
37 // Implementation of AudioInputStream.
39 void Start(AudioInputCallback
* callback
) override
;
41 void Close() override
;
42 // TODO(henrika): Add volume support using the Audio Mixer API.
43 double GetMaxVolume() override
;
44 void SetVolume(double volume
) override
;
45 double GetVolume() override
;
46 bool SetAutomaticGainControl(bool enabled
) override
;
47 bool GetAutomaticGainControl() override
;
48 bool IsMuted() override
;
52 kStateEmpty
, // Initial state.
53 kStateReady
, // Device obtained and ready to record.
54 kStateRecording
, // Recording audio.
55 kStateStopping
, // Trying to stop, waiting for callback to finish.
56 kStateStopped
, // Stopped. Device was reset.
57 kStateClosed
// Device has been released.
60 // Allow unit tests to query the device ID.
61 friend class AudioManagerTest
;
63 // Windows calls us back with the recorded audio data here. See msdn
64 // documentation for 'waveInProc' for details about the parameters.
65 static void CALLBACK
WaveCallback(HWAVEIN hwi
, UINT msg
, DWORD_PTR instance
,
66 DWORD_PTR param1
, DWORD_PTR param2
);
68 // If windows reports an error this function handles it and passes it to
69 // the attached AudioInputCallback::OnError().
70 void HandleError(MMRESULT error
);
72 // Allocates and prepares the memory that will be used for recording.
75 // Deallocates the memory allocated in SetupBuffers.
78 // Sends a buffer to the audio driver for recording.
79 void QueueNextPacket(WAVEHDR
* buffer
);
81 // Converts the stored device id string into an unsigned integer which
82 // can be used by waveInOpen() to open the specified capture device.
83 bool GetDeviceId(UINT
* device_index
);
85 base::ThreadChecker thread_checker_
;
87 // Reader beware. Visual C has stronger guarantees on volatile vars than
88 // most people expect. In fact, it has release semantics on write and
89 // acquire semantics on reads. See the msdn documentation.
90 volatile State state_
;
92 // The audio manager that created this input stream. We notify it when
93 // we close so it can release its own resources.
94 AudioManagerWin
* manager_
;
96 // We use the callback mostly to periodically give the recorded audio data.
97 AudioInputCallback
* callback_
;
99 // The number of buffers of size |buffer_size_| each to use.
100 const int num_buffers_
;
102 // The size in bytes of each audio buffer.
108 // Contains the unique name of the selected endpoint device.
109 // Note that AudioManagerBase::kDefaultDeviceId represents the default
110 // device role and is not a valid ID as such.
111 std::string device_id_
;
113 // Windows native structure to encode the format parameters.
114 WAVEFORMATEX format_
;
116 // Handle to the instance of the wave device.
119 // Pointer to the first allocated audio buffer. This object owns it.
122 // An event that is signaled when the callback thread is ready to stop.
123 base::win::ScopedHandle stopped_event_
;
125 // Lock used to avoid conflicts when Stop() is called during a callback.
128 // Extra audio bus used for storage of deinterleaved data for the OnData
130 scoped_ptr
<media::AudioBus
> audio_bus_
;
132 DISALLOW_COPY_AND_ASSIGN(PCMWaveInAudioInputStream
);
137 #endif // MEDIA_AUDIO_WIN_WAVEIN_INPUT_WIN_H_