This source file includes following definitions.
- GetNextBuffer
- channels_
- Open
- SetupBuffers
- FreeBuffers
- Start
- Stop
- Close
- GetMaxVolume
- SetVolume
- GetVolume
- SetAutomaticGainControl
- GetAutomaticGainControl
- HandleError
- QueueNextPacket
- GetDeviceId
- WaveCallback
#include "media/audio/win/wavein_input_win.h"
#pragma comment(lib, "winmm.lib")
#include "base/logging.h"
#include "media/audio/audio_io.h"
#include "media/audio/win/audio_manager_win.h"
#include "media/audio/win/device_enumeration_win.h"
namespace media {
static WAVEHDR* GetNextBuffer(WAVEHDR* current) {
return reinterpret_cast<WAVEHDR*>(current->dwUser);
}
PCMWaveInAudioInputStream::PCMWaveInAudioInputStream(
AudioManagerWin* manager, const AudioParameters& params, int num_buffers,
const std::string& device_id)
: state_(kStateEmpty),
manager_(manager),
device_id_(device_id),
wavein_(NULL),
callback_(NULL),
num_buffers_(num_buffers),
buffer_(NULL),
channels_(params.channels()) {
DCHECK_GT(num_buffers_, 0);
format_.wFormatTag = WAVE_FORMAT_PCM;
format_.nChannels = params.channels() > 2 ? 2 : params.channels();
format_.nSamplesPerSec = params.sample_rate();
format_.wBitsPerSample = params.bits_per_sample();
format_.cbSize = 0;
format_.nBlockAlign = (format_.nChannels * format_.wBitsPerSample) / 8;
format_.nAvgBytesPerSec = format_.nBlockAlign * format_.nSamplesPerSec;
buffer_size_ = params.frames_per_buffer() * format_.nBlockAlign;
if (!buffer_size_)
buffer_size_ = format_.nAvgBytesPerSec / 10;
stopped_event_.Set(::CreateEventW(NULL, FALSE, FALSE, NULL));
}
PCMWaveInAudioInputStream::~PCMWaveInAudioInputStream() {
DCHECK(NULL == wavein_);
}
bool PCMWaveInAudioInputStream::Open() {
DCHECK(thread_checker_.CalledOnValidThread());
if (state_ != kStateEmpty)
return false;
if (num_buffers_ < 2 || num_buffers_ > 10)
return false;
UINT device_id = WAVE_MAPPER;
if (!GetDeviceId(&device_id)) {
return false;
}
MMRESULT result = MMSYSERR_NOERROR;
result = ::waveInOpen(&wavein_, device_id, &format_,
reinterpret_cast<DWORD_PTR>(WaveCallback),
reinterpret_cast<DWORD_PTR>(this),
CALLBACK_FUNCTION);
if (result != MMSYSERR_NOERROR)
return false;
SetupBuffers();
state_ = kStateReady;
return true;
}
void PCMWaveInAudioInputStream::SetupBuffers() {
WAVEHDR* last = NULL;
WAVEHDR* first = NULL;
for (int ix = 0; ix != num_buffers_; ++ix) {
uint32 sz = sizeof(WAVEHDR) + buffer_size_;
buffer_ = reinterpret_cast<WAVEHDR*>(new char[sz]);
buffer_->lpData = reinterpret_cast<char*>(buffer_) + sizeof(WAVEHDR);
buffer_->dwBufferLength = buffer_size_;
buffer_->dwBytesRecorded = 0;
buffer_->dwUser = reinterpret_cast<DWORD_PTR>(last);
buffer_->dwFlags = WHDR_DONE;
buffer_->dwLoops = 0;
if (ix == 0)
first = buffer_;
last = buffer_;
::waveInPrepareHeader(wavein_, buffer_, sizeof(WAVEHDR));
}
first->dwUser = reinterpret_cast<DWORD_PTR>(last);
}
void PCMWaveInAudioInputStream::FreeBuffers() {
WAVEHDR* current = buffer_;
for (int ix = 0; ix != num_buffers_; ++ix) {
WAVEHDR* next = GetNextBuffer(current);
if (current->dwFlags & WHDR_PREPARED)
::waveInUnprepareHeader(wavein_, current, sizeof(WAVEHDR));
delete[] reinterpret_cast<char*>(current);
current = next;
}
buffer_ = NULL;
}
void PCMWaveInAudioInputStream::Start(AudioInputCallback* callback) {
DCHECK(thread_checker_.CalledOnValidThread());
if (state_ != kStateReady)
return;
DCHECK(!callback_);
callback_ = callback;
state_ = kStateRecording;
WAVEHDR* buffer = buffer_;
for (int ix = 0; ix != num_buffers_; ++ix) {
QueueNextPacket(buffer);
buffer = GetNextBuffer(buffer);
}
buffer = buffer_;
MMRESULT result = ::waveInStart(wavein_);
if (result != MMSYSERR_NOERROR) {
HandleError(result);
state_ = kStateReady;
callback_ = NULL;
}
}
void PCMWaveInAudioInputStream::Stop() {
DVLOG(1) << "PCMWaveInAudioInputStream::Stop()";
DCHECK(thread_checker_.CalledOnValidThread());
if (state_ != kStateRecording)
return;
bool already_stopped = false;
{
base::AutoLock auto_lock(lock_);
already_stopped = (callback_ == NULL);
callback_ = NULL;
}
if (already_stopped)
return;
DWORD wait = ::WaitForSingleObject(stopped_event_, INFINITE);
DCHECK_EQ(wait, WAIT_OBJECT_0);
MMRESULT res = ::waveInReset(wavein_);
DCHECK_EQ(res, static_cast<MMRESULT>(MMSYSERR_NOERROR));
state_ = kStateReady;
}
void PCMWaveInAudioInputStream::Close() {
DVLOG(1) << "PCMWaveInAudioInputStream::Close()";
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK_NE(state_, kStateRecording);
Stop();
if (wavein_) {
FreeBuffers();
MMRESULT res = ::waveInReset(wavein_);
DCHECK_EQ(res, static_cast<MMRESULT>(MMSYSERR_NOERROR));
res = ::waveInClose(wavein_);
DCHECK_EQ(res, static_cast<MMRESULT>(MMSYSERR_NOERROR));
state_ = kStateClosed;
wavein_ = NULL;
}
manager_->ReleaseInputStream(this);
}
double PCMWaveInAudioInputStream::GetMaxVolume() {
return 0.0;
}
void PCMWaveInAudioInputStream::SetVolume(double volume) {
}
double PCMWaveInAudioInputStream::GetVolume() {
return 0.0;
}
void PCMWaveInAudioInputStream::SetAutomaticGainControl(bool enabled) {
NOTIMPLEMENTED();
}
bool PCMWaveInAudioInputStream::GetAutomaticGainControl() {
NOTIMPLEMENTED();
return false;
}
void PCMWaveInAudioInputStream::HandleError(MMRESULT error) {
DLOG(WARNING) << "PCMWaveInAudio error " << error;
callback_->OnError(this);
}
void PCMWaveInAudioInputStream::QueueNextPacket(WAVEHDR *buffer) {
MMRESULT res = ::waveInAddBuffer(wavein_, buffer, sizeof(WAVEHDR));
if (res != MMSYSERR_NOERROR)
HandleError(res);
}
bool PCMWaveInAudioInputStream::GetDeviceId(UINT* device_index) {
if (device_id_ == AudioManagerBase::kDefaultDeviceId) {
*device_index = WAVE_MAPPER;
return true;
}
AudioDeviceNames device_names;
if (!media::GetInputDeviceNamesWinXP(&device_names))
return false;
if (device_names.empty())
return false;
UINT index = 0;
bool found_device = false;
AudioDeviceNames::const_iterator it = device_names.begin();
while (it != device_names.end()) {
if (it->unique_id.compare(device_id_) == 0) {
*device_index = index;
found_device = true;
break;
}
++index;
++it;
}
return found_device;
}
void PCMWaveInAudioInputStream::WaveCallback(HWAVEIN hwi, UINT msg,
DWORD_PTR instance,
DWORD_PTR param1, DWORD_PTR) {
PCMWaveInAudioInputStream* obj =
reinterpret_cast<PCMWaveInAudioInputStream*>(instance);
base::AutoLock auto_lock(obj->lock_);
if (msg == WIM_DATA) {
if (obj->callback_) {
WAVEHDR* buffer = reinterpret_cast<WAVEHDR*>(param1);
obj->callback_->OnData(obj,
reinterpret_cast<const uint8*>(buffer->lpData),
buffer->dwBytesRecorded,
buffer->dwBytesRecorded,
0.0);
obj->QueueNextPacket(buffer);
} else {
::SetEvent(obj->stopped_event_);
}
} else if (msg == WIM_CLOSE) {
} else if (msg == WIM_OPEN) {
}
}
}