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 | 8 |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "media/audio/audio_manager_base.h" | 10 #include "media/audio/audio_manager_base.h" |
11 #include "media/audio/audio_parameters.h" | 11 #include "media/audio/audio_parameters.h" |
12 #include "media/audio/pulse/pulse_util.h" | 12 #include "media/audio/pulse/pulse_util.h" |
13 | 13 |
14 namespace media { | 14 namespace media { |
15 | 15 |
16 using pulse::AutoPulseLock; | 16 using pulse::AutoPulseLock; |
17 using pulse::WaitForOperationCompletion; | 17 using pulse::WaitForOperationCompletion; |
18 | 18 |
19 // static, pa_stream_notify_cb | 19 // static, pa_stream_notify_cb |
(...skipping 20 matching lines...) Expand all Loading... |
40 | 40 |
41 PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params, | 41 PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params, |
42 AudioManagerBase* manager) | 42 AudioManagerBase* manager) |
43 : params_(params), | 43 : params_(params), |
44 manager_(manager), | 44 manager_(manager), |
45 pa_context_(NULL), | 45 pa_context_(NULL), |
46 pa_mainloop_(NULL), | 46 pa_mainloop_(NULL), |
47 pa_stream_(NULL), | 47 pa_stream_(NULL), |
48 volume_(1.0f), | 48 volume_(1.0f), |
49 source_callback_(NULL) { | 49 source_callback_(NULL) { |
50 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 50 DCHECK(manager_->GetTaskRunner()->BelongsToCurrentThread()); |
51 | 51 |
52 CHECK(params_.IsValid()); | 52 CHECK(params_.IsValid()); |
53 audio_bus_ = AudioBus::Create(params_); | 53 audio_bus_ = AudioBus::Create(params_); |
54 } | 54 } |
55 | 55 |
56 PulseAudioOutputStream::~PulseAudioOutputStream() { | 56 PulseAudioOutputStream::~PulseAudioOutputStream() { |
57 // All internal structures should already have been freed in Close(), which | 57 // All internal structures should already have been freed in Close(), which |
58 // calls AudioManagerBase::ReleaseOutputStream() which deletes this object. | 58 // calls AudioManagerBase::ReleaseOutputStream() which deletes this object. |
59 DCHECK(!pa_stream_); | 59 DCHECK(!pa_stream_); |
60 DCHECK(!pa_context_); | 60 DCHECK(!pa_context_); |
61 DCHECK(!pa_mainloop_); | 61 DCHECK(!pa_mainloop_); |
62 } | 62 } |
63 | 63 |
64 bool PulseAudioOutputStream::Open() { | 64 bool PulseAudioOutputStream::Open() { |
65 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 65 DCHECK(manager_->GetTaskRunner()->BelongsToCurrentThread()); |
66 return pulse::CreateOutputStream(&pa_mainloop_, &pa_context_, &pa_stream_, | 66 return pulse::CreateOutputStream(&pa_mainloop_, &pa_context_, &pa_stream_, |
67 params_, &StreamNotifyCallback, | 67 params_, &StreamNotifyCallback, |
68 &StreamRequestCallback, this); | 68 &StreamRequestCallback, this); |
69 } | 69 } |
70 | 70 |
71 void PulseAudioOutputStream::Reset() { | 71 void PulseAudioOutputStream::Reset() { |
72 if (!pa_mainloop_) { | 72 if (!pa_mainloop_) { |
73 DCHECK(!pa_stream_); | 73 DCHECK(!pa_stream_); |
74 DCHECK(!pa_context_); | 74 DCHECK(!pa_context_); |
75 return; | 75 return; |
(...skipping 24 matching lines...) Expand all Loading... |
100 pa_context_ = NULL; | 100 pa_context_ = NULL; |
101 } | 101 } |
102 } | 102 } |
103 | 103 |
104 pa_threaded_mainloop_stop(pa_mainloop_); | 104 pa_threaded_mainloop_stop(pa_mainloop_); |
105 pa_threaded_mainloop_free(pa_mainloop_); | 105 pa_threaded_mainloop_free(pa_mainloop_); |
106 pa_mainloop_ = NULL; | 106 pa_mainloop_ = NULL; |
107 } | 107 } |
108 | 108 |
109 void PulseAudioOutputStream::Close() { | 109 void PulseAudioOutputStream::Close() { |
110 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 110 DCHECK(manager_->GetTaskRunner()->BelongsToCurrentThread()); |
111 | 111 |
112 Reset(); | 112 Reset(); |
113 | 113 |
114 // Signal to the manager that we're closed and can be removed. | 114 // Signal to the manager that we're closed and can be removed. |
115 // This should be the last call in the function as it deletes "this". | 115 // This should be the last call in the function as it deletes "this". |
116 manager_->ReleaseOutputStream(this); | 116 manager_->ReleaseOutputStream(this); |
117 } | 117 } |
118 | 118 |
119 void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { | 119 void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { |
120 int bytes_remaining = requested_bytes; | 120 int bytes_remaining = requested_bytes; |
(...skipping 29 matching lines...) Expand all Loading... |
150 if (source_callback_) { | 150 if (source_callback_) { |
151 source_callback_->OnError(this); | 151 source_callback_->OnError(this); |
152 } | 152 } |
153 } | 153 } |
154 | 154 |
155 bytes_remaining -= bytes_to_fill; | 155 bytes_remaining -= bytes_to_fill; |
156 } | 156 } |
157 } | 157 } |
158 | 158 |
159 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { | 159 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { |
160 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 160 DCHECK(manager_->GetTaskRunner()->BelongsToCurrentThread()); |
161 CHECK(callback); | 161 CHECK(callback); |
162 CHECK(pa_stream_); | 162 CHECK(pa_stream_); |
163 | 163 |
164 AutoPulseLock auto_lock(pa_mainloop_); | 164 AutoPulseLock auto_lock(pa_mainloop_); |
165 | 165 |
166 // Ensure the context and stream are ready. | 166 // Ensure the context and stream are ready. |
167 if (pa_context_get_state(pa_context_) != PA_CONTEXT_READY && | 167 if (pa_context_get_state(pa_context_) != PA_CONTEXT_READY && |
168 pa_stream_get_state(pa_stream_) != PA_STREAM_READY) { | 168 pa_stream_get_state(pa_stream_) != PA_STREAM_READY) { |
169 callback->OnError(this); | 169 callback->OnError(this); |
170 return; | 170 return; |
171 } | 171 } |
172 | 172 |
173 source_callback_ = callback; | 173 source_callback_ = callback; |
174 | 174 |
175 // Uncork (resume) the stream. | 175 // Uncork (resume) the stream. |
176 pa_operation* operation = pa_stream_cork( | 176 pa_operation* operation = pa_stream_cork( |
177 pa_stream_, 0, &pulse::StreamSuccessCallback, pa_mainloop_); | 177 pa_stream_, 0, &pulse::StreamSuccessCallback, pa_mainloop_); |
178 WaitForOperationCompletion(pa_mainloop_, operation); | 178 WaitForOperationCompletion(pa_mainloop_, operation); |
179 } | 179 } |
180 | 180 |
181 void PulseAudioOutputStream::Stop() { | 181 void PulseAudioOutputStream::Stop() { |
182 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 182 DCHECK(manager_->GetTaskRunner()->BelongsToCurrentThread()); |
183 | 183 |
184 // Cork (pause) the stream. Waiting for the main loop lock will ensure | 184 // Cork (pause) the stream. Waiting for the main loop lock will ensure |
185 // outstanding callbacks have completed. | 185 // outstanding callbacks have completed. |
186 AutoPulseLock auto_lock(pa_mainloop_); | 186 AutoPulseLock auto_lock(pa_mainloop_); |
187 | 187 |
188 // Set |source_callback_| to NULL so all FulfillWriteRequest() calls which may | 188 // Set |source_callback_| to NULL so all FulfillWriteRequest() calls which may |
189 // occur while waiting on the flush and cork exit immediately. | 189 // occur while waiting on the flush and cork exit immediately. |
190 source_callback_ = NULL; | 190 source_callback_ = NULL; |
191 | 191 |
192 // Flush the stream prior to cork, doing so after will cause hangs. Write | 192 // Flush the stream prior to cork, doing so after will cause hangs. Write |
193 // callbacks are suspended while inside pa_threaded_mainloop_lock() so this | 193 // callbacks are suspended while inside pa_threaded_mainloop_lock() so this |
194 // is all thread safe. | 194 // is all thread safe. |
195 pa_operation* operation = pa_stream_flush( | 195 pa_operation* operation = pa_stream_flush( |
196 pa_stream_, &pulse::StreamSuccessCallback, pa_mainloop_); | 196 pa_stream_, &pulse::StreamSuccessCallback, pa_mainloop_); |
197 WaitForOperationCompletion(pa_mainloop_, operation); | 197 WaitForOperationCompletion(pa_mainloop_, operation); |
198 | 198 |
199 operation = pa_stream_cork(pa_stream_, 1, &pulse::StreamSuccessCallback, | 199 operation = pa_stream_cork(pa_stream_, 1, &pulse::StreamSuccessCallback, |
200 pa_mainloop_); | 200 pa_mainloop_); |
201 WaitForOperationCompletion(pa_mainloop_, operation); | 201 WaitForOperationCompletion(pa_mainloop_, operation); |
202 } | 202 } |
203 | 203 |
204 void PulseAudioOutputStream::SetVolume(double volume) { | 204 void PulseAudioOutputStream::SetVolume(double volume) { |
205 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 205 DCHECK(manager_->GetTaskRunner()->BelongsToCurrentThread()); |
206 | 206 |
207 volume_ = static_cast<float>(volume); | 207 volume_ = static_cast<float>(volume); |
208 } | 208 } |
209 | 209 |
210 void PulseAudioOutputStream::GetVolume(double* volume) { | 210 void PulseAudioOutputStream::GetVolume(double* volume) { |
211 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 211 DCHECK(manager_->GetTaskRunner()->BelongsToCurrentThread()); |
212 | 212 |
213 *volume = volume_; | 213 *volume = volume_; |
214 } | 214 } |
215 | 215 |
216 } // namespace media | 216 } // namespace media |
OLD | NEW |