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 using pulse::AutoPulseLock; | 16 using pulse::AutoPulseLock; |
| 17 using pulse::WaitForOperationCompletion; | 17 using pulse::WaitForOperationCompletion; |
| 18 | 18 |
| 19 // Helper macro to avoid code spam and string bloat. | |
| 20 #define RETURN_ON_FAILURE(expression, message) \ | |
|
tommi (sloooow) - chröme
2016/03/16 15:21:47
macros are discouraged and especially ones that ha
rchtara
2016/03/19 12:07:30
Done.
| |
| 21 do { \ | |
| 22 if (!(expression)) { \ | |
| 23 DLOG(ERROR) << message; \ | |
| 24 return false; \ | |
| 25 } \ | |
| 26 } while (0) | |
| 27 | |
| 19 // static, pa_stream_notify_cb | 28 // static, pa_stream_notify_cb |
| 20 void PulseAudioOutputStream::StreamNotifyCallback(pa_stream* s, void* p_this) { | 29 void PulseAudioOutputStream::StreamNotifyCallback(pa_stream* s, void* p_this) { |
| 21 PulseAudioOutputStream* stream = static_cast<PulseAudioOutputStream*>(p_this); | 30 PulseAudioOutputStream* stream = static_cast<PulseAudioOutputStream*>(p_this); |
| 22 | 31 |
| 23 // Forward unexpected failures to the AudioSourceCallback if available. All | 32 // Forward unexpected failures to the AudioSourceCallback if available. All |
| 24 // these variables are only modified under pa_threaded_mainloop_lock() so this | 33 // these variables are only modified under pa_threaded_mainloop_lock() so this |
| 25 // should be thread safe. | 34 // should be thread safe. |
| 26 if (s && stream->source_callback_ && | 35 if (s && stream->source_callback_ && |
| 27 pa_stream_get_state(s) == PA_STREAM_FAILED) { | 36 pa_stream_get_state(s) == PA_STREAM_FAILED) { |
| 28 stream->source_callback_->OnError(stream); | 37 stream->source_callback_->OnError(stream); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 54 } | 63 } |
| 55 | 64 |
| 56 PulseAudioOutputStream::~PulseAudioOutputStream() { | 65 PulseAudioOutputStream::~PulseAudioOutputStream() { |
| 57 // All internal structures should already have been freed in Close(), which | 66 // All internal structures should already have been freed in Close(), which |
| 58 // calls AudioManagerBase::ReleaseOutputStream() which deletes this object. | 67 // calls AudioManagerBase::ReleaseOutputStream() which deletes this object. |
| 59 DCHECK(!pa_stream_); | 68 DCHECK(!pa_stream_); |
| 60 DCHECK(!pa_context_); | 69 DCHECK(!pa_context_); |
| 61 DCHECK(!pa_mainloop_); | 70 DCHECK(!pa_mainloop_); |
| 62 } | 71 } |
| 63 | 72 |
| 73 bool PulseAudioOutputStream::InitializeMainloopAndContext() { | |
| 74 DCHECK(!pa_mainloop_); | |
| 75 DCHECK(!pa_context_); | |
| 76 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 77 pa_mainloop_ = pa_threaded_mainloop_new(); | |
| 78 RETURN_ON_FAILURE(pa_mainloop_, "Failed to create PulseAudio main loop."); | |
| 79 | |
| 80 pa_mainloop_api* pa_mainloop_api = pa_threaded_mainloop_get_api(pa_mainloop_); | |
| 81 std::string app_name = AudioManager::GetGlobalAppName(); | |
| 82 pa_context_ = pa_context_new( | |
| 83 pa_mainloop_api, app_name.empty() ? "Chromium" : app_name.c_str()); | |
|
tommi (sloooow) - chröme
2016/03/16 15:21:47
When do we get an empty string from GetGlobalAppNa
rchtara
2016/03/19 12:07:30
I'm just moving some code from CreateOutputStream
| |
| 84 RETURN_ON_FAILURE(pa_context_, "Failed to create PulseAudio context."); | |
| 85 | |
| 86 // A state callback must be set before calling pa_threaded_mainloop_lock() or | |
| 87 // pa_threaded_mainloop_wait() calls may lead to dead lock. | |
| 88 pa_context_set_state_callback(pa_context_, &pulse::ContextStateCallback, | |
| 89 pa_mainloop_); | |
| 90 { | |
| 91 // Lock the main loop while setting up the context. Failure to do so may | |
| 92 // lead to crashes as the PulseAudio thread tries to run before things are | |
| 93 // ready. | |
| 94 AutoPulseLock auto_lock(pa_mainloop_); | |
| 95 | |
| 96 RETURN_ON_FAILURE(pa_threaded_mainloop_start(pa_mainloop_) == 0, | |
| 97 "Failed to start PulseAudio main loop."); | |
| 98 RETURN_ON_FAILURE(pa_context_connect(pa_context_, NULL, | |
| 99 PA_CONTEXT_NOAUTOSPAWN, NULL) == 0, | |
| 100 "Failed to connect PulseAudio context."); | |
| 101 | |
| 102 // Wait until |pa_context_| is ready. pa_threaded_mainloop_wait() must be | |
| 103 // called after pa_context_get_state() in case the context is already ready, | |
| 104 // otherwise pa_threaded_mainloop_wait() will hang indefinitely. | |
| 105 while (true) { | |
| 106 pa_context_state_t context_state = pa_context_get_state(pa_context_); | |
| 107 RETURN_ON_FAILURE(PA_CONTEXT_IS_GOOD(context_state), | |
| 108 "Invalid PulseAudio context state."); | |
| 109 if (context_state == PA_CONTEXT_READY) | |
| 110 break; | |
| 111 pa_threaded_mainloop_wait(pa_mainloop_); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 return true; | |
| 116 } | |
| 117 | |
| 118 // static, used by pa_context_get_server_info. | |
| 119 void PulseAudioOutputStream::GetSystemDefaultOutputDeviceCallback( | |
| 120 pa_context* context, | |
| 121 const pa_server_info* info, | |
| 122 void* user_data) { | |
| 123 media::PulseAudioOutputStream* stream = | |
| 124 reinterpret_cast<media::PulseAudioOutputStream*>(user_data); | |
| 125 stream->default_system_device_name_ = info->default_sink_name; | |
| 126 pa_threaded_mainloop_signal(stream->pa_mainloop_, 0); | |
| 127 } | |
| 128 | |
| 129 void PulseAudioOutputStream::GetSystemDefaultOutputDevice() { | |
| 130 DCHECK(pa_mainloop_); | |
| 131 DCHECK(pa_context_); | |
| 132 pa_operation* operation = pa_context_get_server_info( | |
| 133 pa_context_, PulseAudioOutputStream::GetSystemDefaultOutputDeviceCallback, | |
| 134 this); | |
| 135 WaitForOperationCompletion(pa_mainloop_, operation); | |
| 136 } | |
| 137 | |
| 64 bool PulseAudioOutputStream::Open() { | 138 bool PulseAudioOutputStream::Open() { |
| 65 DCHECK(thread_checker_.CalledOnValidThread()); | 139 if (!InitializeMainloopAndContext()) { |
| 140 return false; | |
| 141 } | |
| 142 | |
| 143 AutoPulseLock auto_lock(pa_mainloop_); | |
| 144 | |
| 145 std::string device_name_to_use = device_id_; | |
| 146 if (device_id_ == AudioManagerBase::kDefaultDeviceId) { | |
| 147 GetSystemDefaultOutputDevice(); | |
| 148 device_name_to_use = default_system_device_name_; | |
| 149 } | |
| 150 | |
| 66 return pulse::CreateOutputStream( | 151 return pulse::CreateOutputStream( |
| 67 &pa_mainloop_, &pa_context_, &pa_stream_, params_, device_id_, | 152 pa_mainloop_, pa_context_, &pa_stream_, params_, device_name_to_use, |
| 68 AudioManager::GetGlobalAppName(), &StreamNotifyCallback, | 153 AudioManager::GetGlobalAppName(), &StreamNotifyCallback, |
| 69 &StreamRequestCallback, this); | 154 &StreamRequestCallback, this); |
| 70 } | 155 } |
| 71 | 156 |
| 72 void PulseAudioOutputStream::Reset() { | 157 void PulseAudioOutputStream::Reset() { |
| 73 if (!pa_mainloop_) { | 158 if (!pa_mainloop_) { |
| 74 DCHECK(!pa_stream_); | 159 DCHECK(!pa_stream_); |
| 75 DCHECK(!pa_context_); | 160 DCHECK(!pa_context_); |
| 76 return; | 161 return; |
| 77 } | 162 } |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 volume_ = static_cast<float>(volume); | 314 volume_ = static_cast<float>(volume); |
| 230 } | 315 } |
| 231 | 316 |
| 232 void PulseAudioOutputStream::GetVolume(double* volume) { | 317 void PulseAudioOutputStream::GetVolume(double* volume) { |
| 233 DCHECK(thread_checker_.CalledOnValidThread()); | 318 DCHECK(thread_checker_.CalledOnValidThread()); |
| 234 | 319 |
| 235 *volume = volume_; | 320 *volume = volume_; |
| 236 } | 321 } |
| 237 | 322 |
| 238 } // namespace media | 323 } // namespace media |
| OLD | NEW |