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