OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/audio/win/wavein_input_win.h" | 5 #include "media/audio/win/wavein_input_win.h" |
6 | 6 |
7 #pragma comment(lib, "winmm.lib") | 7 #pragma comment(lib, "winmm.lib") |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "media/audio/audio_io.h" | 10 #include "media/audio/audio_io.h" |
11 #include "media/audio/win/audio_manager_win.h" | 11 #include "media/audio/win/audio_manager_win.h" |
12 #include "media/audio/win/device_enumeration_win.h" | 12 #include "media/audio/win/device_enumeration_win.h" |
| 13 #include "media/base/audio_bus.h" |
13 | 14 |
14 namespace media { | 15 namespace media { |
15 | 16 |
16 // Our sound buffers are allocated once and kept in a linked list using the | 17 // Our sound buffers are allocated once and kept in a linked list using the |
17 // the WAVEHDR::dwUser variable. The last buffer points to the first buffer. | 18 // the WAVEHDR::dwUser variable. The last buffer points to the first buffer. |
18 static WAVEHDR* GetNextBuffer(WAVEHDR* current) { | 19 static WAVEHDR* GetNextBuffer(WAVEHDR* current) { |
19 return reinterpret_cast<WAVEHDR*>(current->dwUser); | 20 return reinterpret_cast<WAVEHDR*>(current->dwUser); |
20 } | 21 } |
21 | 22 |
22 PCMWaveInAudioInputStream::PCMWaveInAudioInputStream( | 23 PCMWaveInAudioInputStream::PCMWaveInAudioInputStream( |
23 AudioManagerWin* manager, const AudioParameters& params, int num_buffers, | 24 AudioManagerWin* manager, |
| 25 const AudioParameters& params, |
| 26 int num_buffers, |
24 const std::string& device_id) | 27 const std::string& device_id) |
25 : state_(kStateEmpty), | 28 : state_(kStateEmpty), |
26 manager_(manager), | 29 manager_(manager), |
27 device_id_(device_id), | 30 device_id_(device_id), |
28 wavein_(NULL), | 31 wavein_(NULL), |
29 callback_(NULL), | 32 callback_(NULL), |
30 num_buffers_(num_buffers), | 33 num_buffers_(num_buffers), |
31 buffer_(NULL), | 34 buffer_(NULL), |
32 channels_(params.channels()) { | 35 channels_(params.channels()), |
| 36 audio_bus_(media::AudioBus::Create(params)) { |
33 DCHECK_GT(num_buffers_, 0); | 37 DCHECK_GT(num_buffers_, 0); |
34 format_.wFormatTag = WAVE_FORMAT_PCM; | 38 format_.wFormatTag = WAVE_FORMAT_PCM; |
35 format_.nChannels = params.channels() > 2 ? 2 : params.channels(); | 39 format_.nChannels = params.channels() > 2 ? 2 : params.channels(); |
36 format_.nSamplesPerSec = params.sample_rate(); | 40 format_.nSamplesPerSec = params.sample_rate(); |
37 format_.wBitsPerSample = params.bits_per_sample(); | 41 format_.wBitsPerSample = params.bits_per_sample(); |
38 format_.cbSize = 0; | 42 format_.cbSize = 0; |
39 format_.nBlockAlign = (format_.nChannels * format_.wBitsPerSample) / 8; | 43 format_.nBlockAlign = (format_.nChannels * format_.wBitsPerSample) / 8; |
40 format_.nAvgBytesPerSec = format_.nBlockAlign * format_.nSamplesPerSec; | 44 format_.nAvgBytesPerSec = format_.nBlockAlign * format_.nSamplesPerSec; |
41 buffer_size_ = params.frames_per_buffer() * format_.nBlockAlign; | 45 buffer_size_ = params.frames_per_buffer() * format_.nBlockAlign; |
42 // If we don't have a packet size we use 100ms. | 46 // If we don't have a packet size we use 100ms. |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 if (msg == WIM_DATA) { | 287 if (msg == WIM_DATA) { |
284 // The WIM_DATA message is sent when waveform-audio data is present in | 288 // The WIM_DATA message is sent when waveform-audio data is present in |
285 // the input buffer and the buffer is being returned to the application. | 289 // the input buffer and the buffer is being returned to the application. |
286 // The message can be sent when the buffer is full or after the | 290 // The message can be sent when the buffer is full or after the |
287 // waveInReset function is called. | 291 // waveInReset function is called. |
288 if (obj->callback_) { | 292 if (obj->callback_) { |
289 // TODO(henrika): the |volume| parameter is always set to zero since | 293 // TODO(henrika): the |volume| parameter is always set to zero since |
290 // there is currently no support for controlling the microphone volume | 294 // there is currently no support for controlling the microphone volume |
291 // level. | 295 // level. |
292 WAVEHDR* buffer = reinterpret_cast<WAVEHDR*>(param1); | 296 WAVEHDR* buffer = reinterpret_cast<WAVEHDR*>(param1); |
293 obj->callback_->OnData(obj, | 297 obj->audio_bus_->FromInterleaved(reinterpret_cast<uint8*>(buffer->lpData), |
294 reinterpret_cast<const uint8*>(buffer->lpData), | 298 obj->audio_bus_->frames(), |
295 buffer->dwBytesRecorded, | 299 obj->format_.wBitsPerSample / 8); |
296 buffer->dwBytesRecorded, | 300 obj->callback_->OnData( |
297 0.0); | 301 obj, obj->audio_bus_.get(), buffer->dwBytesRecorded, 0.0); |
298 | 302 |
299 // Queue the finished buffer back with the audio driver. Since we are | 303 // Queue the finished buffer back with the audio driver. Since we are |
300 // reusing the same buffers we can get away without calling | 304 // reusing the same buffers we can get away without calling |
301 // waveInPrepareHeader. | 305 // waveInPrepareHeader. |
302 obj->QueueNextPacket(buffer); | 306 obj->QueueNextPacket(buffer); |
303 } else { | 307 } else { |
304 // Main thread has called Stop() and set |callback_| to NULL and is | 308 // Main thread has called Stop() and set |callback_| to NULL and is |
305 // now waiting to issue waveInReset which will kill this thread. | 309 // now waiting to issue waveInReset which will kill this thread. |
306 // We should not call AudioSourceCallback code anymore. | 310 // We should not call AudioSourceCallback code anymore. |
307 ::SetEvent(obj->stopped_event_); | 311 ::SetEvent(obj->stopped_event_); |
308 } | 312 } |
309 } else if (msg == WIM_CLOSE) { | 313 } else if (msg == WIM_CLOSE) { |
310 // Intentionaly no-op for now. | 314 // Intentionaly no-op for now. |
311 } else if (msg == WIM_OPEN) { | 315 } else if (msg == WIM_OPEN) { |
312 // Intentionaly no-op for now. | 316 // Intentionaly no-op for now. |
313 } | 317 } |
314 } | 318 } |
315 | 319 |
316 } // namespace media | 320 } // namespace media |
OLD | NEW |