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