Mercurial > mplayer.hg
annotate libao2/ao_pulse.c @ 26645:1b9a15fe49e2
synced with r26649
author | ptt |
---|---|
date | Tue, 06 May 2008 15:10:14 +0000 |
parents | c07abb5a5bef |
children | 07d106323e5c |
rev | line source |
---|---|
24782 | 1 /* |
2 * PulseAudio audio output driver. | |
3 * Copyright (C) 2006 Lennart Poettering | |
4 * Copyright (C) 2007 Reimar Doeffinger | |
5 * | |
6 * This file is part of MPlayer. | |
7 * | |
8 * MPlayer is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * MPlayer is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with MPlayer; if not, write to the Free Software | |
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 */ | |
22 #include <string.h> | |
23 | |
24 #include <pulse/pulseaudio.h> | |
25 | |
26 #include "config.h" | |
27 #include "libaf/af_format.h" | |
28 #include "mp_msg.h" | |
29 #include "audio_out.h" | |
30 #include "audio_out_internal.h" | |
31 | |
32 #define PULSE_CLIENT_NAME "MPlayer" | |
33 | |
34 /** General driver info */ | |
35 static ao_info_t info = { | |
36 "PulseAudio audio output", | |
37 "pulse", | |
38 "Lennart Poettering", | |
39 "" | |
40 }; | |
41 | |
42 /** PulseAudio playback stream object */ | |
43 static struct pa_stream *stream; | |
44 | |
45 /** PulseAudio connection context */ | |
46 static struct pa_context *context; | |
47 | |
48 /** Main event loop object */ | |
49 static struct pa_threaded_mainloop *mainloop; | |
50 | |
51 /** A temporary variable to store the current volume */ | |
52 static pa_cvolume volume; | |
53 | |
54 LIBAO_EXTERN(pulse) | |
55 | |
56 #define GENERIC_ERR_MSG(ctx, str) \ | |
57 mp_msg(MSGT_AO, MSGL_ERR, "AO: [pulse] "str": %s\n", \ | |
58 pa_strerror(pa_context_errno(ctx))) | |
59 | |
60 static void context_state_cb(pa_context *c, void *userdata) { | |
61 switch (pa_context_get_state(c)) { | |
62 case PA_CONTEXT_READY: | |
63 case PA_CONTEXT_TERMINATED: | |
64 case PA_CONTEXT_FAILED: | |
65 pa_threaded_mainloop_signal(mainloop, 0); | |
66 break; | |
67 } | |
68 } | |
69 | |
70 static void stream_state_cb(pa_stream *s, void *userdata) { | |
71 switch (pa_stream_get_state(s)) { | |
72 case PA_STREAM_READY: | |
73 case PA_STREAM_FAILED: | |
74 case PA_STREAM_TERMINATED: | |
75 pa_threaded_mainloop_signal(mainloop, 0); | |
76 break; | |
77 } | |
78 } | |
79 | |
80 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) { | |
81 pa_threaded_mainloop_signal(mainloop, 0); | |
82 } | |
83 | |
84 static void stream_latency_update_cb(pa_stream *s, void *userdata) { | |
85 pa_threaded_mainloop_signal(mainloop, 0); | |
86 } | |
87 | |
88 static void success_cb(pa_stream *s, int success, void *userdata) { | |
89 if (userdata) | |
90 *(int *)userdata = success; | |
91 pa_threaded_mainloop_signal(mainloop, 0); | |
92 } | |
93 | |
25383 | 94 /** |
95 * \brief waits for a pulseaudio operation to finish, frees it and | |
96 * unlocks the mainloop | |
97 * \param op operation to wait for | |
98 * \return 1 if operation has finished normally (DONE state), 0 otherwise | |
99 */ | |
24782 | 100 static int waitop(pa_operation *op) { |
101 pa_operation_state_t state; | |
102 if (!op) return 0; | |
103 state = pa_operation_get_state(op); | |
104 while (state == PA_OPERATION_RUNNING) { | |
105 pa_threaded_mainloop_wait(mainloop); | |
106 state = pa_operation_get_state(op); | |
107 } | |
108 pa_operation_unref(op); | |
109 pa_threaded_mainloop_unlock(mainloop); | |
110 return state == PA_OPERATION_DONE; | |
111 } | |
112 | |
113 static const struct format_map_s { | |
114 int mp_format; | |
115 pa_sample_format_t pa_format; | |
116 } format_maps[] = { | |
117 {AF_FORMAT_S16_LE, PA_SAMPLE_S16LE}, | |
118 {AF_FORMAT_S16_BE, PA_SAMPLE_S16BE}, | |
26603 | 119 {AF_FORMAT_S32_LE, PA_SAMPLE_S32LE}, |
120 {AF_FORMAT_S32_BE, PA_SAMPLE_S32BE}, | |
24782 | 121 {AF_FORMAT_FLOAT_LE, PA_SAMPLE_FLOAT32LE}, |
122 {AF_FORMAT_FLOAT_BE, PA_SAMPLE_FLOAT32BE}, | |
26602
d48e2d7191df
Make ao_pulse fall back to s16le format instead of just failing.
reimar
parents:
25386
diff
changeset
|
123 {AF_FORMAT_U8, PA_SAMPLE_U8}, |
24782 | 124 {AF_FORMAT_MU_LAW, PA_SAMPLE_ULAW}, |
125 {AF_FORMAT_A_LAW, PA_SAMPLE_ALAW}, | |
126 {AF_FORMAT_UNKNOWN, 0} | |
127 }; | |
128 | |
129 static int init(int rate_hz, int channels, int format, int flags) { | |
130 struct pa_sample_spec ss; | |
131 struct pa_channel_map map; | |
132 const struct format_map_s *fmt_map; | |
24921
148ca265fcb6
Change parsing to allow host == NULL and sink != NULL
reimar
parents:
24920
diff
changeset
|
133 char *devarg = NULL; |
24782 | 134 char *host = NULL; |
24919
51372a34bc7d
Make sink variable local, it is only used in one place
reimar
parents:
24782
diff
changeset
|
135 char *sink = NULL; |
24782 | 136 |
137 if (ao_subdevice) { | |
24921
148ca265fcb6
Change parsing to allow host == NULL and sink != NULL
reimar
parents:
24920
diff
changeset
|
138 devarg = strdup(ao_subdevice); |
148ca265fcb6
Change parsing to allow host == NULL and sink != NULL
reimar
parents:
24920
diff
changeset
|
139 sink = strchr(devarg, ':'); |
24920 | 140 if (sink) *sink++ = 0; |
24921
148ca265fcb6
Change parsing to allow host == NULL and sink != NULL
reimar
parents:
24920
diff
changeset
|
141 if (devarg[0]) host = devarg; |
24782 | 142 } |
143 | |
144 ss.channels = channels; | |
145 ss.rate = rate_hz; | |
146 | |
147 ao_data.samplerate = rate_hz; | |
148 ao_data.channels = channels; | |
149 | |
150 fmt_map = format_maps; | |
151 while (fmt_map->mp_format != format) { | |
152 if (fmt_map->mp_format == AF_FORMAT_UNKNOWN) { | |
26602
d48e2d7191df
Make ao_pulse fall back to s16le format instead of just failing.
reimar
parents:
25386
diff
changeset
|
153 mp_msg(MSGT_AO, MSGL_V, "AO: [pulse] Unsupported format, using default\n"); |
d48e2d7191df
Make ao_pulse fall back to s16le format instead of just failing.
reimar
parents:
25386
diff
changeset
|
154 fmt_map = format_maps; |
d48e2d7191df
Make ao_pulse fall back to s16le format instead of just failing.
reimar
parents:
25386
diff
changeset
|
155 break; |
24782 | 156 } |
157 fmt_map++; | |
158 } | |
26602
d48e2d7191df
Make ao_pulse fall back to s16le format instead of just failing.
reimar
parents:
25386
diff
changeset
|
159 ao_data.format = fmt_map->mp_format; |
24782 | 160 ss.format = fmt_map->pa_format; |
161 | |
162 if (!pa_sample_spec_valid(&ss)) { | |
163 mp_msg(MSGT_AO, MSGL_ERR, "AO: [pulse] Invalid sample spec\n"); | |
164 goto fail; | |
165 } | |
166 | |
167 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA); | |
168 ao_data.bps = pa_bytes_per_second(&ss); | |
169 | |
170 pa_cvolume_reset(&volume, ss.channels); | |
171 | |
172 if (!(mainloop = pa_threaded_mainloop_new())) { | |
173 mp_msg(MSGT_AO, MSGL_ERR, "AO: [pulse] Failed to allocate main loop\n"); | |
174 goto fail; | |
175 } | |
176 | |
177 if (!(context = pa_context_new(pa_threaded_mainloop_get_api(mainloop), PULSE_CLIENT_NAME))) { | |
178 mp_msg(MSGT_AO, MSGL_ERR, "AO: [pulse] Failed to allocate context\n"); | |
179 goto fail; | |
180 } | |
181 | |
182 pa_context_set_state_callback(context, context_state_cb, NULL); | |
183 | |
184 if (pa_context_connect(context, host, 0, NULL) < 0) | |
185 goto fail; | |
186 | |
187 pa_threaded_mainloop_lock(mainloop); | |
188 | |
189 if (pa_threaded_mainloop_start(mainloop) < 0) | |
190 goto unlock_and_fail; | |
191 | |
192 /* Wait until the context is ready */ | |
193 pa_threaded_mainloop_wait(mainloop); | |
194 | |
195 if (pa_context_get_state(context) != PA_CONTEXT_READY) | |
196 goto unlock_and_fail; | |
197 | |
198 if (!(stream = pa_stream_new(context, "audio stream", &ss, &map))) | |
199 goto unlock_and_fail; | |
200 | |
201 pa_stream_set_state_callback(stream, stream_state_cb, NULL); | |
202 pa_stream_set_write_callback(stream, stream_request_cb, NULL); | |
203 pa_stream_set_latency_update_callback(stream, stream_latency_update_cb, NULL); | |
204 | |
205 if (pa_stream_connect_playback(stream, sink, NULL, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, &volume, NULL) < 0) | |
206 goto unlock_and_fail; | |
207 | |
208 /* Wait until the stream is ready */ | |
209 pa_threaded_mainloop_wait(mainloop); | |
210 | |
211 if (pa_stream_get_state(stream) != PA_STREAM_READY) | |
212 goto unlock_and_fail; | |
213 | |
214 pa_threaded_mainloop_unlock(mainloop); | |
215 | |
24921
148ca265fcb6
Change parsing to allow host == NULL and sink != NULL
reimar
parents:
24920
diff
changeset
|
216 free(devarg); |
24782 | 217 return 1; |
218 | |
219 unlock_and_fail: | |
220 | |
221 if (mainloop) | |
222 pa_threaded_mainloop_unlock(mainloop); | |
223 | |
224 fail: | |
225 if (context) | |
226 GENERIC_ERR_MSG(context, "Init failed"); | |
24921
148ca265fcb6
Change parsing to allow host == NULL and sink != NULL
reimar
parents:
24920
diff
changeset
|
227 free(devarg); |
24782 | 228 uninit(1); |
229 return 0; | |
230 } | |
231 | |
232 /** Destroy libao driver */ | |
233 static void uninit(int immed) { | |
234 if (stream && !immed) { | |
235 pa_threaded_mainloop_lock(mainloop); | |
236 waitop(pa_stream_drain(stream, success_cb, NULL)); | |
237 } | |
238 | |
239 if (mainloop) | |
240 pa_threaded_mainloop_stop(mainloop); | |
241 | |
242 if (stream) { | |
243 pa_stream_disconnect(stream); | |
244 pa_stream_unref(stream); | |
245 stream = NULL; | |
246 } | |
247 | |
248 if (context) { | |
249 pa_context_disconnect(context); | |
250 pa_context_unref(context); | |
251 context = NULL; | |
252 } | |
253 | |
254 if (mainloop) { | |
255 pa_threaded_mainloop_free(mainloop); | |
256 mainloop = NULL; | |
257 } | |
258 } | |
259 | |
260 /** Play the specified data to the pulseaudio server */ | |
261 static int play(void* data, int len, int flags) { | |
25386
bd46b759fd35
pa_stream_write reportedly needs locking of the main loop
reimar
parents:
25385
diff
changeset
|
262 pa_threaded_mainloop_lock(mainloop); |
25385 | 263 if (pa_stream_write(stream, data, len, NULL, 0, PA_SEEK_RELATIVE) < 0) { |
264 GENERIC_ERR_MSG(context, "pa_stream_write() failed"); | |
25386
bd46b759fd35
pa_stream_write reportedly needs locking of the main loop
reimar
parents:
25385
diff
changeset
|
265 len = -1; |
25385 | 266 } |
25386
bd46b759fd35
pa_stream_write reportedly needs locking of the main loop
reimar
parents:
25385
diff
changeset
|
267 pa_threaded_mainloop_unlock(mainloop); |
24782 | 268 return len; |
269 } | |
270 | |
271 static void cork(int b) { | |
272 int success = 0; | |
273 pa_threaded_mainloop_lock(mainloop); | |
274 if (!waitop(pa_stream_cork(stream, b, success_cb, &success)) || | |
275 !success) | |
276 GENERIC_ERR_MSG(context, "pa_stream_cork() failed"); | |
277 } | |
278 | |
279 /** Pause the audio stream by corking it on the server */ | |
280 static void audio_pause(void) { | |
281 cork(1); | |
282 } | |
283 | |
284 /** Resume the audio stream by uncorking it on the server */ | |
285 static void audio_resume(void) { | |
286 cork(0); | |
287 } | |
288 | |
289 /** Reset the audio stream, i.e. flush the playback buffer on the server side */ | |
290 static void reset(void) { | |
291 int success = 0; | |
292 pa_threaded_mainloop_lock(mainloop); | |
293 if (!waitop(pa_stream_flush(stream, success_cb, &success)) || | |
294 !success) | |
295 GENERIC_ERR_MSG(context, "pa_stream_flush() failed"); | |
296 } | |
297 | |
298 /** Return number of bytes that may be written to the server without blocking */ | |
299 static int get_space(void) { | |
300 size_t l; | |
301 pa_threaded_mainloop_lock(mainloop); | |
302 l = pa_stream_writable_size(stream); | |
303 pa_threaded_mainloop_unlock(mainloop); | |
24923 | 304 return l; |
24782 | 305 } |
306 | |
307 /** Return the current latency in seconds */ | |
308 static float get_delay(void) { | |
309 pa_usec_t latency = (pa_usec_t) -1; | |
310 pa_threaded_mainloop_lock(mainloop); | |
311 while (pa_stream_get_latency(stream, &latency, NULL) < 0) { | |
312 if (pa_context_errno(context) != PA_ERR_NODATA) { | |
313 GENERIC_ERR_MSG(context, "pa_stream_get_latency() failed"); | |
314 break; | |
315 } | |
316 /* Wait until latency data is available again */ | |
317 pa_threaded_mainloop_wait(mainloop); | |
318 } | |
319 pa_threaded_mainloop_unlock(mainloop); | |
320 return latency == (pa_usec_t) -1 ? 0 : latency / 1000000.0; | |
321 } | |
322 | |
323 /** A callback function that is called when the | |
324 * pa_context_get_sink_input_info() operation completes. Saves the | |
325 * volume field of the specified structure to the global variable volume. */ | |
326 static void info_func(struct pa_context *c, const struct pa_sink_input_info *i, int is_last, void *userdata) { | |
327 if (is_last < 0) { | |
328 GENERIC_ERR_MSG(context, "Failed to get sink input info"); | |
329 return; | |
330 } | |
331 if (!i) | |
332 return; | |
333 volume = i->volume; | |
334 pa_threaded_mainloop_signal(mainloop, 0); | |
335 } | |
336 | |
337 static int control(int cmd, void *arg) { | |
338 switch (cmd) { | |
339 case AOCONTROL_GET_VOLUME: { | |
340 ao_control_vol_t *vol = arg; | |
341 uint32_t devidx = pa_stream_get_index(stream); | |
342 pa_threaded_mainloop_lock(mainloop); | |
343 if (!waitop(pa_context_get_sink_input_info(context, devidx, info_func, NULL))) { | |
344 GENERIC_ERR_MSG(context, "pa_stream_get_sink_input_info() failed"); | |
345 return CONTROL_ERROR; | |
346 } | |
347 | |
348 if (volume.channels != 2) | |
349 vol->left = vol->right = pa_cvolume_avg(&volume)*100/PA_VOLUME_NORM; | |
350 else { | |
351 vol->left = volume.values[0]*100/PA_VOLUME_NORM; | |
352 vol->right = volume.values[1]*100/PA_VOLUME_NORM; | |
353 } | |
354 | |
355 return CONTROL_OK; | |
356 } | |
357 | |
358 case AOCONTROL_SET_VOLUME: { | |
359 const ao_control_vol_t *vol = arg; | |
360 pa_operation *o; | |
361 | |
362 if (volume.channels != 2) | |
363 pa_cvolume_set(&volume, volume.channels, (pa_volume_t)vol->left*PA_VOLUME_NORM/100); | |
364 else { | |
365 volume.values[0] = (pa_volume_t)vol->left*PA_VOLUME_NORM/100; | |
366 volume.values[1] = (pa_volume_t)vol->right*PA_VOLUME_NORM/100; | |
367 } | |
368 | |
369 if (!(o = pa_context_set_sink_input_volume(context, pa_stream_get_index(stream), &volume, NULL, NULL))) { | |
370 GENERIC_ERR_MSG(context, "pa_context_set_sink_input_volume() failed"); | |
371 return CONTROL_ERROR; | |
372 } | |
373 /* We don't wait for completion here */ | |
374 pa_operation_unref(o); | |
375 return CONTROL_OK; | |
376 } | |
377 | |
378 default: | |
379 return CONTROL_UNKNOWN; | |
380 } | |
381 } |