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 "media/audio/audio_manager_base.h" | 11 #include "media/audio/audio_manager_base.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 namespace { | |
| 17 | |
| 18 // pa_context_get_server_info callback. It's used by | |
| 19 // GetSystemDefaultOutputDevice to set |default_system_device_name_| to the | |
| 20 // default system output device. | |
| 21 void GetSystemDefaultOutputDeviceCallback(pa_context* context, | |
| 22 const pa_server_info* info, | |
| 23 void* user_data) { | |
| 24 media::PulseAudioOutputStream* stream = | |
| 25 reinterpret_cast<media::PulseAudioOutputStream*>(user_data); | |
| 26 stream->SetDefaultSystemDeviceName(info->default_sink_name); | |
| 27 pa_threaded_mainloop_signal(stream->GetPAMainloop(), 0); | |
| 28 } | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 16 using pulse::AutoPulseLock; | 32 using pulse::AutoPulseLock; |
| 17 using pulse::WaitForOperationCompletion; | 33 using pulse::WaitForOperationCompletion; |
| 18 | 34 |
| 35 // Helper macro to avoid code spam and string bloat. | |
| 36 #define RETURN_ON_FAILURE(expression, message) \ | |
| 37 do { \ | |
| 38 if (!(expression)) { \ | |
| 39 DLOG(ERROR) << message; \ | |
| 40 return false; \ | |
| 41 } \ | |
| 42 } while (0) | |
| 43 | |
| 19 // static, pa_stream_notify_cb | 44 // static, pa_stream_notify_cb |
| 20 void PulseAudioOutputStream::StreamNotifyCallback(pa_stream* s, void* p_this) { | 45 void PulseAudioOutputStream::StreamNotifyCallback(pa_stream* s, void* p_this) { |
| 21 PulseAudioOutputStream* stream = static_cast<PulseAudioOutputStream*>(p_this); | 46 PulseAudioOutputStream* stream = static_cast<PulseAudioOutputStream*>(p_this); |
| 22 | 47 |
| 23 // Forward unexpected failures to the AudioSourceCallback if available. All | 48 // Forward unexpected failures to the AudioSourceCallback if available. All |
| 24 // these variables are only modified under pa_threaded_mainloop_lock() so this | 49 // these variables are only modified under pa_threaded_mainloop_lock() so this |
| 25 // should be thread safe. | 50 // should be thread safe. |
| 26 if (s && stream->source_callback_ && | 51 if (s && stream->source_callback_ && |
| 27 pa_stream_get_state(s) == PA_STREAM_FAILED) { | 52 pa_stream_get_state(s) == PA_STREAM_FAILED) { |
| 28 stream->source_callback_->OnError(stream); | 53 stream->source_callback_->OnError(stream); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 54 } | 79 } |
| 55 | 80 |
| 56 PulseAudioOutputStream::~PulseAudioOutputStream() { | 81 PulseAudioOutputStream::~PulseAudioOutputStream() { |
| 57 // All internal structures should already have been freed in Close(), which | 82 // All internal structures should already have been freed in Close(), which |
| 58 // calls AudioManagerBase::ReleaseOutputStream() which deletes this object. | 83 // calls AudioManagerBase::ReleaseOutputStream() which deletes this object. |
| 59 DCHECK(!pa_stream_); | 84 DCHECK(!pa_stream_); |
| 60 DCHECK(!pa_context_); | 85 DCHECK(!pa_context_); |
| 61 DCHECK(!pa_mainloop_); | 86 DCHECK(!pa_mainloop_); |
| 62 } | 87 } |
| 63 | 88 |
| 89 pa_threaded_mainloop* PulseAudioOutputStream::GetPAMainloop() { | |
| 90 return pa_mainloop_; | |
| 91 } | |
| 92 | |
| 93 void PulseAudioOutputStream::SetDefaultSystemDeviceName( | |
| 94 const std::string& name) { | |
| 95 base::Lock lock; | |
| 96 base::AutoLock auto_lock(lock); | |
| 97 default_system_device_name_ = name; | |
| 98 } | |
| 99 | |
| 100 bool PulseAudioOutputStream::InitializeMainloopAndContext() { | |
| 101 DCHECK(!pa_mainloop_); | |
| 102 DCHECK(!pa_context_); | |
| 103 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 104 pa_mainloop_ = pa_threaded_mainloop_new(); | |
| 105 RETURN_ON_FAILURE(pa_mainloop_, "Failed to create PulseAudio main loop."); | |
| 106 | |
| 107 pa_mainloop_api* pa_mainloop_api = pa_threaded_mainloop_get_api(pa_mainloop_); | |
| 108 std::string app_name = AudioManager::GetGlobalAppName(); | |
| 109 pa_context_ = pa_context_new( | |
| 110 pa_mainloop_api, app_name.empty() ? "Chromium" : app_name.c_str()); | |
| 111 RETURN_ON_FAILURE(pa_context_, "Failed to create PulseAudio context."); | |
| 112 | |
| 113 // A state callback must be set before calling pa_threaded_mainloop_lock() or | |
| 114 // pa_threaded_mainloop_wait() calls may lead to dead lock. | |
| 115 pa_context_set_state_callback(pa_context_, &pulse::ContextStateCallback, | |
| 116 pa_mainloop_); | |
| 117 { | |
| 118 // Lock the main loop while setting up the context. Failure to do so may | |
| 119 // lead to crashes as the PulseAudio thread tries to run before things are | |
| 120 // ready. | |
| 121 AutoPulseLock auto_lock(pa_mainloop_); | |
| 122 | |
| 123 RETURN_ON_FAILURE(pa_threaded_mainloop_start(pa_mainloop_) == 0, | |
| 124 "Failed to start PulseAudio main loop."); | |
| 125 RETURN_ON_FAILURE(pa_context_connect(pa_context_, NULL, | |
| 126 PA_CONTEXT_NOAUTOSPAWN, NULL) == 0, | |
| 127 "Failed to connect PulseAudio context."); | |
| 128 | |
| 129 // Wait until |pa_context_| is ready. pa_threaded_mainloop_wait() must be | |
| 130 // called after pa_context_get_state() in case the context is already ready, | |
| 131 // otherwise pa_threaded_mainloop_wait() will hang indefinitely. | |
| 132 while (true) { | |
| 133 pa_context_state_t context_state = pa_context_get_state(pa_context_); | |
| 134 RETURN_ON_FAILURE(PA_CONTEXT_IS_GOOD(context_state), | |
| 135 "Invalid PulseAudio context state."); | |
| 136 if (context_state == PA_CONTEXT_READY) | |
| 137 break; | |
| 138 pa_threaded_mainloop_wait(pa_mainloop_); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 return true; | |
| 143 } | |
| 144 | |
| 145 void PulseAudioOutputStream::GetSystemDefaultOutputDevice() { | |
| 146 DCHECK(pa_mainloop_); | |
| 147 DCHECK(pa_context_); | |
| 148 pa_operation* operation = pa_context_get_server_info( | |
| 149 pa_context_, GetSystemDefaultOutputDeviceCallback, this); | |
| 150 WaitForOperationCompletion(pa_mainloop_, operation); | |
| 151 } | |
| 152 | |
| 64 bool PulseAudioOutputStream::Open() { | 153 bool PulseAudioOutputStream::Open() { |
| 65 DCHECK(thread_checker_.CalledOnValidThread()); | 154 // InitializeMainloopAndContext() should only be called once as it is written |
| 155 // now. | |
| 156 if (!InitializeMainloopAndContext()) { | |
| 157 return false; | |
| 158 } | |
| 159 | |
| 160 AutoPulseLock auto_lock(pa_mainloop_); | |
| 161 | |
| 162 std::string device_name_to_use = device_id_; | |
| 163 if (device_id_ == AudioManagerBase::kDefaultDeviceId) { | |
| 164 GetSystemDefaultOutputDevice(); | |
| 165 device_name_to_use = default_system_device_name_; | |
| 166 } | |
| 167 | |
| 66 return pulse::CreateOutputStream( | 168 return pulse::CreateOutputStream( |
| 67 &pa_mainloop_, &pa_context_, &pa_stream_, params_, device_id_, | 169 pa_mainloop_, pa_context_, &pa_stream_, params_, device_name_to_use, |
| 68 AudioManager::GetGlobalAppName(), &StreamNotifyCallback, | 170 AudioManager::GetGlobalAppName(), &StreamNotifyCallback, |
| 69 &StreamRequestCallback, this); | 171 &StreamRequestCallback, this); |
| 70 } | 172 } |
| 71 | 173 |
| 72 void PulseAudioOutputStream::Reset() { | 174 void PulseAudioOutputStream::Reset() { |
| 73 if (!pa_mainloop_) { | 175 if (!pa_mainloop_) { |
| 74 DCHECK(!pa_stream_); | 176 DCHECK(!pa_stream_); |
| 75 DCHECK(!pa_context_); | 177 DCHECK(!pa_context_); |
| 76 return; | 178 return; |
| 77 } | 179 } |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 228 | 330 |
| 229 volume_ = static_cast<float>(volume); | 331 volume_ = static_cast<float>(volume); |
| 230 } | 332 } |
| 231 | 333 |
| 232 void PulseAudioOutputStream::GetVolume(double* volume) { | 334 void PulseAudioOutputStream::GetVolume(double* volume) { |
| 233 DCHECK(thread_checker_.CalledOnValidThread()); | 335 DCHECK(thread_checker_.CalledOnValidThread()); |
| 234 | 336 |
| 235 *volume = volume_; | 337 *volume = volume_; |
| 236 } | 338 } |
| 237 | 339 |
| 340 #undef RETURN_ON_FAILURE | |
|
Henrik Grunell
2016/03/11 18:54:41
This isn't necessary. Remove.
rchtara
2016/03/13 15:12:00
Done.
| |
| 341 | |
| 238 } // namespace media | 342 } // namespace media |
| OLD | NEW |