Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / audio / win / wavein_input_win.h
bloba2c77c34c48f06dc66c3254f00cf2f8549196223
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_
8 #include <string>
10 #include <windows.h>
11 #include <mmsystem.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"
21 namespace media {
23 class AudioBus;
24 class AudioManagerWin;
26 class PCMWaveInAudioInputStream : public AudioInputStream {
27 public:
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,
33 int num_buffers,
34 const std::string& device_id);
35 ~PCMWaveInAudioInputStream() override;
37 // Implementation of AudioInputStream.
38 bool Open() override;
39 void Start(AudioInputCallback* callback) override;
40 void Stop() 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;
50 private:
51 enum State {
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.
73 void SetupBuffers();
75 // Deallocates the memory allocated in SetupBuffers.
76 void FreeBuffers();
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.
103 uint32 buffer_size_;
105 // Channels, 1 or 2.
106 const int channels_;
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.
117 HWAVEIN wavein_;
119 // Pointer to the first allocated audio buffer. This object owns it.
120 WAVEHDR* buffer_;
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.
126 base::Lock lock_;
128 // Extra audio bus used for storage of deinterleaved data for the OnData
129 // callback.
130 scoped_ptr<media::AudioBus> audio_bus_;
132 DISALLOW_COPY_AND_ASSIGN(PCMWaveInAudioInputStream);
135 } // namespace media
137 #endif // MEDIA_AUDIO_WIN_WAVEIN_INPUT_WIN_H_