Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(243)

Side by Side Diff: media/audio/pulse/pulse_output.cc

Issue 1992203002: Use "application default" audio device as default PulseAudio device (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge fix. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/audio/pulse/pulse_output.h ('k') | media/audio/pulse/pulse_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 } 55 }
56 56
57 PulseAudioOutputStream::~PulseAudioOutputStream() { 57 PulseAudioOutputStream::~PulseAudioOutputStream() {
58 // All internal structures should already have been freed in Close(), which 58 // All internal structures should already have been freed in Close(), which
59 // calls AudioManagerBase::ReleaseOutputStream() which deletes this object. 59 // calls AudioManagerBase::ReleaseOutputStream() which deletes this object.
60 DCHECK(!pa_stream_); 60 DCHECK(!pa_stream_);
61 DCHECK(!pa_context_); 61 DCHECK(!pa_context_);
62 DCHECK(!pa_mainloop_); 62 DCHECK(!pa_mainloop_);
63 } 63 }
64 64
65 bool PulseAudioOutputStream::InitializeMainloopAndContext() {
66 DCHECK(!pa_mainloop_);
67 DCHECK(!pa_context_);
68 DCHECK(thread_checker_.CalledOnValidThread());
69 pa_mainloop_ = pa_threaded_mainloop_new();
70 if (!pa_mainloop_) {
71 DLOG(ERROR) << "Failed to create PulseAudio main loop.";
72 return false;
73 }
74
75 pa_mainloop_api* pa_mainloop_api = pa_threaded_mainloop_get_api(pa_mainloop_);
76 std::string app_name = AudioManager::GetGlobalAppName();
77 pa_context_ = pa_context_new(
78 pa_mainloop_api, app_name.empty() ? "Chromium" : app_name.c_str());
79 if (!pa_context_) {
80 DLOG(ERROR) << "Failed to create PulseAudio context.";
81 return false;
82 }
83
84 // A state callback must be set before calling pa_threaded_mainloop_lock() or
85 // pa_threaded_mainloop_wait() calls may lead to dead lock.
86 pa_context_set_state_callback(pa_context_, &pulse::ContextStateCallback,
87 pa_mainloop_);
88 {
89 // Lock the main loop while setting up the context. Failure to do so may
90 // lead to crashes as the PulseAudio thread tries to run before things are
91 // ready.
92 AutoPulseLock auto_lock(pa_mainloop_);
93
94 if (pa_threaded_mainloop_start(pa_mainloop_) != 0) {
95 DLOG(ERROR) << "Failed to start PulseAudio main loop.";
96 return false;
97 }
98
99 if (pa_context_connect(pa_context_, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) !=
100 0) {
101 DLOG(ERROR) << "Failed to connect PulseAudio context.";
102 return false;
103 }
104
105 // Wait until |pa_context_| is ready. pa_threaded_mainloop_wait() must be
106 // called after pa_context_get_state() in case the context is already ready,
107 // otherwise pa_threaded_mainloop_wait() will hang indefinitely.
108 while (true) {
109 pa_context_state_t context_state = pa_context_get_state(pa_context_);
110 if (!PA_CONTEXT_IS_GOOD(context_state)) {
111 DLOG(ERROR) << "Invalid PulseAudio context state.";
112 return false;
113 }
114
115 if (context_state == PA_CONTEXT_READY)
116 break;
117 pa_threaded_mainloop_wait(pa_mainloop_);
118 }
119 }
120
121 return true;
122 }
123
124 // static, used by pa_context_get_server_info.
125 void PulseAudioOutputStream::GetSystemDefaultOutputDeviceCallback(
126 pa_context* context,
127 const pa_server_info* info,
128 void* user_data) {
129 media::PulseAudioOutputStream* stream =
130 static_cast<media::PulseAudioOutputStream*>(user_data);
131 stream->default_system_device_name_ = info->default_sink_name;
132 pa_threaded_mainloop_signal(stream->pa_mainloop_, 0);
133 }
134
135 void PulseAudioOutputStream::GetSystemDefaultOutputDevice() {
136 DCHECK(thread_checker_.CalledOnValidThread());
137 DCHECK(pa_mainloop_);
138 DCHECK(pa_context_);
139 pa_operation* operation = pa_context_get_server_info(
140 pa_context_, PulseAudioOutputStream::GetSystemDefaultOutputDeviceCallback,
141 this);
142 WaitForOperationCompletion(pa_mainloop_, operation);
143 }
144
145 bool PulseAudioOutputStream::Open() { 65 bool PulseAudioOutputStream::Open() {
146 DCHECK(thread_checker_.CalledOnValidThread()); 66 DCHECK(thread_checker_.CalledOnValidThread());
147 if (!InitializeMainloopAndContext()) {
148 return false;
149 }
150
151 AutoPulseLock auto_lock(pa_mainloop_);
152
153 std::string device_name_to_use = device_id_;
154 if (device_id_ == AudioDeviceDescription::kDefaultDeviceId) {
155 GetSystemDefaultOutputDevice();
156 device_name_to_use = default_system_device_name_;
157 }
158
159 return pulse::CreateOutputStream( 67 return pulse::CreateOutputStream(
160 pa_mainloop_, pa_context_, &pa_stream_, params_, device_name_to_use, 68 &pa_mainloop_, &pa_context_, &pa_stream_, params_, device_id_,
161 AudioManager::GetGlobalAppName(), &StreamNotifyCallback, 69 AudioManager::GetGlobalAppName(), &StreamNotifyCallback,
162 &StreamRequestCallback, this); 70 &StreamRequestCallback, this);
163 } 71 }
164 72
165 void PulseAudioOutputStream::Reset() { 73 void PulseAudioOutputStream::Reset() {
166 if (!pa_mainloop_) { 74 if (!pa_mainloop_) {
167 DCHECK(!pa_stream_); 75 DCHECK(!pa_stream_);
168 DCHECK(!pa_context_); 76 DCHECK(!pa_context_);
169 return; 77 return;
170 } 78 }
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 volume_ = static_cast<float>(volume); 230 volume_ = static_cast<float>(volume);
323 } 231 }
324 232
325 void PulseAudioOutputStream::GetVolume(double* volume) { 233 void PulseAudioOutputStream::GetVolume(double* volume) {
326 DCHECK(thread_checker_.CalledOnValidThread()); 234 DCHECK(thread_checker_.CalledOnValidThread());
327 235
328 *volume = volume_; 236 *volume = volume_;
329 } 237 }
330 238
331 } // namespace media 239 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/pulse/pulse_output.h ('k') | media/audio/pulse/pulse_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698