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/pulse/pulse_input.h" | 5 #include "media/audio/pulse/pulse_input.h" |
6 | 6 |
7 #include <pulse/pulseaudio.h> | 7 #include <pulse/pulseaudio.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "media/audio/pulse/audio_manager_pulse.h" | 10 #include "media/audio/pulse/audio_manager_pulse.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 callback_(NULL), | 25 callback_(NULL), |
26 device_name_(device_name), | 26 device_name_(device_name), |
27 params_(params), | 27 params_(params), |
28 channels_(0), | 28 channels_(0), |
29 volume_(0.0), | 29 volume_(0.0), |
30 stream_started_(false), | 30 stream_started_(false), |
31 pa_mainloop_(mainloop), | 31 pa_mainloop_(mainloop), |
32 pa_context_(context), | 32 pa_context_(context), |
33 handle_(NULL), | 33 handle_(NULL), |
34 context_state_changed_(false) { | 34 context_state_changed_(false) { |
| 35 DVLOG(1) << __FUNCTION__; |
35 DCHECK(mainloop); | 36 DCHECK(mainloop); |
36 DCHECK(context); | 37 DCHECK(context); |
| 38 CHECK(params_.IsValid()); |
| 39 audio_bus_ = AudioBus::Create(params_); |
37 } | 40 } |
38 | 41 |
39 PulseAudioInputStream::~PulseAudioInputStream() { | 42 PulseAudioInputStream::~PulseAudioInputStream() { |
| 43 DVLOG(1) << __FUNCTION__; |
40 // All internal structures should already have been freed in Close(), | 44 // All internal structures should already have been freed in Close(), |
41 // which calls AudioManagerPulse::Release which deletes this object. | 45 // which calls AudioManagerPulse::Release which deletes this object. |
42 DCHECK(!handle_); | 46 DCHECK(!handle_); |
43 } | 47 } |
44 | 48 |
45 bool PulseAudioInputStream::Open() { | 49 bool PulseAudioInputStream::Open() { |
46 DCHECK(thread_checker_.CalledOnValidThread()); | 50 DCHECK(thread_checker_.CalledOnValidThread()); |
47 AutoPulseLock auto_lock(pa_mainloop_); | 51 AutoPulseLock auto_lock(pa_mainloop_); |
48 if (!pulse::CreateInputStream(pa_mainloop_, pa_context_, &handle_, params_, | 52 if (!pulse::CreateInputStream(pa_mainloop_, pa_context_, &handle_, params_, |
49 device_name_, &StreamNotifyCallback, this)) { | 53 device_name_, &StreamNotifyCallback, this)) { |
50 return false; | 54 return false; |
51 } | 55 } |
52 | 56 |
53 DCHECK(handle_); | 57 DCHECK(handle_); |
54 | 58 |
| 59 DVLOG(1) << "frames_per_buffer: " << params_.frames_per_buffer(); |
55 buffer_.reset(new media::SeekableBuffer(0, 2 * params_.GetBytesPerBuffer())); | 60 buffer_.reset(new media::SeekableBuffer(0, 2 * params_.GetBytesPerBuffer())); |
56 audio_data_buffer_.reset(new uint8[params_.GetBytesPerBuffer()]); | 61 audio_data_buffer_.reset(new uint8[params_.GetBytesPerBuffer()]); |
57 return true; | 62 return true; |
58 } | 63 } |
59 | 64 |
60 void PulseAudioInputStream::Start(AudioInputCallback* callback) { | 65 void PulseAudioInputStream::Start(AudioInputCallback* callback) { |
61 DCHECK(thread_checker_.CalledOnValidThread()); | 66 DCHECK(thread_checker_.CalledOnValidThread()); |
62 DCHECK(callback); | 67 DCHECK(callback); |
63 DCHECK(handle_); | 68 DCHECK(handle_); |
64 | 69 |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 | 270 |
266 buffer_->Append(reinterpret_cast<const uint8*>(data), length); | 271 buffer_->Append(reinterpret_cast<const uint8*>(data), length); |
267 | 272 |
268 // Checks if we still have data. | 273 // Checks if we still have data. |
269 pa_stream_drop(handle_); | 274 pa_stream_drop(handle_); |
270 } while (pa_stream_readable_size(handle_) > 0); | 275 } while (pa_stream_readable_size(handle_) > 0); |
271 | 276 |
272 int packet_size = params_.GetBytesPerBuffer(); | 277 int packet_size = params_.GetBytesPerBuffer(); |
273 while (buffer_->forward_bytes() >= packet_size) { | 278 while (buffer_->forward_bytes() >= packet_size) { |
274 buffer_->Read(audio_data_buffer_.get(), packet_size); | 279 buffer_->Read(audio_data_buffer_.get(), packet_size); |
275 callback_->OnData(this, audio_data_buffer_.get(), packet_size, | 280 audio_bus_->FromInterleaved(audio_data_buffer_.get(), |
276 hardware_delay, normalized_volume); | 281 audio_bus_->frames(), |
| 282 params_.bits_per_sample() / 8); |
| 283 callback_->OnData( |
| 284 this, audio_bus_.get(), hardware_delay, normalized_volume); |
277 | 285 |
278 if (buffer_->forward_bytes() < packet_size) | 286 if (buffer_->forward_bytes() < packet_size) |
279 break; | 287 break; |
280 | 288 |
281 // TODO(xians): Remove once PPAPI is using circular buffers. | 289 // TODO(xians): Remove once PPAPI is using circular buffers. |
282 DVLOG(1) << "OnData is being called consecutively, sleep 5ms to " | 290 DVLOG(1) << "OnData is being called consecutively, sleep 5ms to " |
283 << "wait until render consumes the data"; | 291 << "wait until render consumes the data"; |
284 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(5)); | 292 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(5)); |
285 } | 293 } |
286 | 294 |
287 pa_threaded_mainloop_signal(pa_mainloop_, 0); | 295 pa_threaded_mainloop_signal(pa_mainloop_, 0); |
288 } | 296 } |
289 | 297 |
290 } // namespace media | 298 } // namespace media |
OLD | NEW |