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