Home | History | Annotate | Download | only in win
      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 
     49  private:
     50   enum State {
     51     kStateEmpty,      // Initial state.
     52     kStateReady,      // Device obtained and ready to record.
     53     kStateRecording,  // Recording audio.
     54     kStateStopping,   // Trying to stop, waiting for callback to finish.
     55     kStateStopped,    // Stopped. Device was reset.
     56     kStateClosed      // Device has been released.
     57   };
     58 
     59   // Allow unit tests to query the device ID.
     60   friend class AudioManagerTest;
     61 
     62   // Windows calls us back with the recorded audio data here. See msdn
     63   // documentation for 'waveInProc' for details about the parameters.
     64   static void CALLBACK WaveCallback(HWAVEIN hwi, UINT msg, DWORD_PTR instance,
     65                                     DWORD_PTR param1, DWORD_PTR param2);
     66 
     67   // If windows reports an error this function handles it and passes it to
     68   // the attached AudioInputCallback::OnError().
     69   void HandleError(MMRESULT error);
     70 
     71   // Allocates and prepares the memory that will be used for recording.
     72   void SetupBuffers();
     73 
     74   // Deallocates the memory allocated in SetupBuffers.
     75   void FreeBuffers();
     76 
     77   // Sends a buffer to the audio driver for recording.
     78   void QueueNextPacket(WAVEHDR* buffer);
     79 
     80   // Converts the stored device id string into an unsigned integer which
     81   // can be used by waveInOpen() to open the specified capture device.
     82   bool GetDeviceId(UINT* device_index);
     83 
     84   base::ThreadChecker thread_checker_;
     85 
     86   // Reader beware. Visual C has stronger guarantees on volatile vars than
     87   // most people expect. In fact, it has release semantics on write and
     88   // acquire semantics on reads. See the msdn documentation.
     89   volatile State state_;
     90 
     91   // The audio manager that created this input stream. We notify it when
     92   // we close so it can release its own resources.
     93   AudioManagerWin* manager_;
     94 
     95   // We use the callback mostly to periodically give the recorded audio data.
     96   AudioInputCallback* callback_;
     97 
     98   // The number of buffers of size |buffer_size_| each to use.
     99   const int num_buffers_;
    100 
    101   // The size in bytes of each audio buffer.
    102   uint32 buffer_size_;
    103 
    104   // Channels, 1 or 2.
    105   const int channels_;
    106 
    107   // Contains the unique name of the selected endpoint device.
    108   // Note that AudioManagerBase::kDefaultDeviceId represents the default
    109   // device role and is not a valid ID as such.
    110   std::string device_id_;
    111 
    112   // Windows native structure to encode the format parameters.
    113   WAVEFORMATEX format_;
    114 
    115   // Handle to the instance of the wave device.
    116   HWAVEIN wavein_;
    117 
    118   // Pointer to the first allocated audio buffer. This object owns it.
    119   WAVEHDR* buffer_;
    120 
    121   // An event that is signaled when the callback thread is ready to stop.
    122   base::win::ScopedHandle stopped_event_;
    123 
    124   // Lock used to avoid conflicts when Stop() is called during a callback.
    125   base::Lock lock_;
    126 
    127   // Extra audio bus used for storage of deinterleaved data for the OnData
    128   // callback.
    129   scoped_ptr<media::AudioBus> audio_bus_;
    130 
    131   DISALLOW_COPY_AND_ASSIGN(PCMWaveInAudioInputStream);
    132 };
    133 
    134 }  // namespace media
    135 
    136 #endif  // MEDIA_AUDIO_WIN_WAVEIN_INPUT_WIN_H_
    137