Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / media / audio / win / wavein_input_win.h
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/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"
20
21 namespace media {
22
23 class AudioBus;
24 class AudioManagerWin;
25
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   virtual ~PCMWaveInAudioInputStream();
36
37   // Implementation of AudioInputStream.
38   virtual bool Open() override;
39   virtual void Start(AudioInputCallback* callback) override;
40   virtual void Stop() override;
41   virtual void Close() override;
42   // TODO(henrika): Add volume support using the Audio Mixer API.
43   virtual double GetMaxVolume() override;
44   virtual void SetVolume(double volume) override;
45   virtual double GetVolume() override;
46   virtual void SetAutomaticGainControl(bool enabled) override;
47   virtual bool GetAutomaticGainControl() override;
48   virtual bool IsMuted() override;
49
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.
58   };
59
60   // Allow unit tests to query the device ID.
61   friend class AudioManagerTest;
62
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);
67
68   // If windows reports an error this function handles it and passes it to
69   // the attached AudioInputCallback::OnError().
70   void HandleError(MMRESULT error);
71
72   // Allocates and prepares the memory that will be used for recording.
73   void SetupBuffers();
74
75   // Deallocates the memory allocated in SetupBuffers.
76   void FreeBuffers();
77
78   // Sends a buffer to the audio driver for recording.
79   void QueueNextPacket(WAVEHDR* buffer);
80
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);
84
85   base::ThreadChecker thread_checker_;
86
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_;
91
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_;
95
96   // We use the callback mostly to periodically give the recorded audio data.
97   AudioInputCallback* callback_;
98
99   // The number of buffers of size |buffer_size_| each to use.
100   const int num_buffers_;
101
102   // The size in bytes of each audio buffer.
103   uint32 buffer_size_;
104
105   // Channels, 1 or 2.
106   const int channels_;
107
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_;
112
113   // Windows native structure to encode the format parameters.
114   WAVEFORMATEX format_;
115
116   // Handle to the instance of the wave device.
117   HWAVEIN wavein_;
118
119   // Pointer to the first allocated audio buffer. This object owns it.
120   WAVEHDR* buffer_;
121
122   // An event that is signaled when the callback thread is ready to stop.
123   base::win::ScopedHandle stopped_event_;
124
125   // Lock used to avoid conflicts when Stop() is called during a callback.
126   base::Lock lock_;
127
128   // Extra audio bus used for storage of deinterleaved data for the OnData
129   // callback.
130   scoped_ptr<media::AudioBus> audio_bus_;
131
132   DISALLOW_COPY_AND_ASSIGN(PCMWaveInAudioInputStream);
133 };
134
135 }  // namespace media
136
137 #endif  // MEDIA_AUDIO_WIN_WAVEIN_INPUT_WIN_H_