Chromium Code Reviews| 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_output.h" | 5 #include "media/audio/pulse/pulse_output.h" |
| 6 | 6 |
| 7 #include <pulse/pulseaudio.h> | 7 #include <pulse/pulseaudio.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "media/audio/audio_device_description.h" | 12 #include "media/audio/audio_device_description.h" |
| 13 #include "media/audio/audio_manager_base.h" | 13 #include "media/audio/audio_manager_base.h" |
| 14 #include "media/audio/pulse/pulse_util.h" | 14 #include "media/audio/pulse/pulse_util.h" |
| 15 #include "media/base/audio_sample_types.h" | |
| 15 | 16 |
| 16 namespace media { | 17 namespace media { |
| 17 | 18 |
| 18 using pulse::AutoPulseLock; | 19 using pulse::AutoPulseLock; |
| 19 using pulse::WaitForOperationCompletion; | 20 using pulse::WaitForOperationCompletion; |
| 20 | 21 |
| 21 // static, pa_stream_notify_cb | 22 // static, pa_stream_notify_cb |
| 22 void PulseAudioOutputStream::StreamNotifyCallback(pa_stream* s, void* p_this) { | 23 void PulseAudioOutputStream::StreamNotifyCallback(pa_stream* s, void* p_this) { |
| 23 PulseAudioOutputStream* stream = static_cast<PulseAudioOutputStream*>(p_this); | 24 PulseAudioOutputStream* stream = static_cast<PulseAudioOutputStream*>(p_this); |
| 24 | 25 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 36 // static, pa_stream_request_cb_t | 37 // static, pa_stream_request_cb_t |
| 37 void PulseAudioOutputStream::StreamRequestCallback(pa_stream* s, size_t len, | 38 void PulseAudioOutputStream::StreamRequestCallback(pa_stream* s, size_t len, |
| 38 void* p_this) { | 39 void* p_this) { |
| 39 // Fulfill write request; must always result in a pa_stream_write() call. | 40 // Fulfill write request; must always result in a pa_stream_write() call. |
| 40 static_cast<PulseAudioOutputStream*>(p_this)->FulfillWriteRequest(len); | 41 static_cast<PulseAudioOutputStream*>(p_this)->FulfillWriteRequest(len); |
| 41 } | 42 } |
| 42 | 43 |
| 43 PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params, | 44 PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params, |
| 44 const std::string& device_id, | 45 const std::string& device_id, |
| 45 AudioManagerBase* manager) | 46 AudioManagerBase* manager) |
| 46 : params_(params), | 47 : params_(AudioParameters(params.format(), |
| 48 params.channel_layout(), | |
| 49 params.sample_rate(), | |
| 50 // Ignore the given bits per sample. We | |
| 51 // want 32 because we're outputting | |
| 52 // floats now. | |
|
DaleCurtis
2016/11/01 23:07:53
s/ now// -- no need to document the old state of t
Raymond Toy
2016/11/01 23:18:50
Done.
| |
| 53 32, | |
| 54 params.frames_per_buffer())), | |
| 47 device_id_(device_id), | 55 device_id_(device_id), |
| 48 manager_(manager), | 56 manager_(manager), |
| 49 pa_context_(NULL), | 57 pa_context_(NULL), |
| 50 pa_mainloop_(NULL), | 58 pa_mainloop_(NULL), |
| 51 pa_stream_(NULL), | 59 pa_stream_(NULL), |
| 52 volume_(1.0f), | 60 volume_(1.0f), |
| 53 source_callback_(NULL) { | 61 source_callback_(NULL) { |
| 54 CHECK(params_.IsValid()); | 62 CHECK(params_.IsValid()); |
| 55 audio_bus_ = AudioBus::Create(params_); | 63 audio_bus_ = AudioBus::Create(params_); |
| 56 } | 64 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 // Signal to the manager that we're closed and can be removed. | 125 // Signal to the manager that we're closed and can be removed. |
| 118 // This should be the last call in the function as it deletes "this". | 126 // This should be the last call in the function as it deletes "this". |
| 119 manager_->ReleaseOutputStream(this); | 127 manager_->ReleaseOutputStream(this); |
| 120 } | 128 } |
| 121 | 129 |
| 122 void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { | 130 void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { |
| 123 int bytes_remaining = requested_bytes; | 131 int bytes_remaining = requested_bytes; |
| 124 while (bytes_remaining > 0) { | 132 while (bytes_remaining > 0) { |
| 125 void* buffer = NULL; | 133 void* buffer = NULL; |
| 126 size_t bytes_to_fill = params_.GetBytesPerBuffer(); | 134 size_t bytes_to_fill = params_.GetBytesPerBuffer(); |
| 135 | |
|
DaleCurtis
2016/11/01 23:07:53
Remove?
Raymond Toy
2016/11/01 23:18:50
Done.
| |
| 127 CHECK_GE(pa_stream_begin_write(pa_stream_, &buffer, &bytes_to_fill), 0); | 136 CHECK_GE(pa_stream_begin_write(pa_stream_, &buffer, &bytes_to_fill), 0); |
| 128 CHECK_EQ(bytes_to_fill, static_cast<size_t>(params_.GetBytesPerBuffer())); | 137 CHECK_EQ(bytes_to_fill, static_cast<size_t>(params_.GetBytesPerBuffer())); |
| 129 | 138 |
| 130 // NOTE: |bytes_to_fill| may be larger than |requested_bytes| now, this is | 139 // NOTE: |bytes_to_fill| may be larger than |requested_bytes| now, this is |
| 131 // okay since pa_stream_begin_write() is the authoritative source on how | 140 // okay since pa_stream_begin_write() is the authoritative source on how |
| 132 // much can be written. | 141 // much can be written. |
| 133 | 142 |
| 134 int frames_filled = 0; | 143 int frames_filled = 0; |
| 135 if (source_callback_) { | 144 if (source_callback_) { |
| 136 const base::TimeDelta delay = pulse::GetHardwareLatency(pa_stream_); | 145 const base::TimeDelta delay = pulse::GetHardwareLatency(pa_stream_); |
| 137 frames_filled = source_callback_->OnMoreData( | 146 frames_filled = source_callback_->OnMoreData( |
| 138 delay, base::TimeTicks::Now(), 0, audio_bus_.get()); | 147 delay, base::TimeTicks::Now(), 0, audio_bus_.get()); |
| 139 | 148 |
| 140 // Zero any unfilled data so it plays back as silence. | 149 // Zero any unfilled data so it plays back as silence. |
| 141 if (frames_filled < audio_bus_->frames()) { | 150 if (frames_filled < audio_bus_->frames()) { |
| 142 audio_bus_->ZeroFramesPartial( | 151 audio_bus_->ZeroFramesPartial( |
| 143 frames_filled, audio_bus_->frames() - frames_filled); | 152 frames_filled, audio_bus_->frames() - frames_filled); |
| 144 } | 153 } |
| 145 | 154 |
| 146 // Note: If this ever changes to output raw float the data must be clipped | 155 // Note: If this ever changes to output raw float the data must be clipped |
| 147 // and sanitized since it may come from an untrusted source such as NaCl. | 156 // and sanitized since it may come from an untrusted source such as NaCl. |
| 148 audio_bus_->Scale(volume_); | 157 audio_bus_->Scale(volume_); |
| 149 audio_bus_->ToInterleaved( | 158 |
| 150 audio_bus_->frames(), params_.bits_per_sample() / 8, buffer); | 159 // Sanitize the samples. Replace NaN with zero, and then clamp |
|
DaleCurtis
2016/11/01 23:07:53
This should already be done as part of ToInterleav
Raymond Toy
2016/11/01 23:18:50
Dunno. I didn't look. I was just going by the comm
| |
| 160 // everything to the range [-1,1]. | |
| 161 audio_bus_->Clamp(); | |
| 162 | |
| 163 audio_bus_->ToInterleaved<Float32SampleTypeTraits>( | |
| 164 audio_bus_->frames(), reinterpret_cast<float*>(buffer)); | |
| 151 } else { | 165 } else { |
| 152 memset(buffer, 0, bytes_to_fill); | 166 memset(buffer, 0, bytes_to_fill); |
| 153 } | 167 } |
| 154 | 168 |
| 155 if (pa_stream_write(pa_stream_, buffer, bytes_to_fill, NULL, 0LL, | 169 if (pa_stream_write(pa_stream_, buffer, bytes_to_fill, NULL, 0LL, |
| 156 PA_SEEK_RELATIVE) < 0) { | 170 PA_SEEK_RELATIVE) < 0) { |
| 157 if (source_callback_) { | 171 if (source_callback_) { |
| 158 source_callback_->OnError(this); | 172 source_callback_->OnError(this); |
| 159 } | 173 } |
| 160 } | 174 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 volume_ = static_cast<float>(volume); | 247 volume_ = static_cast<float>(volume); |
| 234 } | 248 } |
| 235 | 249 |
| 236 void PulseAudioOutputStream::GetVolume(double* volume) { | 250 void PulseAudioOutputStream::GetVolume(double* volume) { |
| 237 DCHECK(thread_checker_.CalledOnValidThread()); | 251 DCHECK(thread_checker_.CalledOnValidThread()); |
| 238 | 252 |
| 239 *volume = volume_; | 253 *volume = volume_; |
| 240 } | 254 } |
| 241 | 255 |
| 242 } // namespace media | 256 } // namespace media |
| OLD | NEW |