3 * Copyright (c) 2014 Lukasz Marek
4 * Copyright (c) 2011 Luca Barbato <lu_zero@gentoo.org>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg 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 GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "pulse_audio_common.h"
24 #include "libavutil/attributes.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/avassert.h"
29 pa_sample_format_t av_cold
ff_codec_id_to_pulse_format(enum AVCodecID codec_id
)
32 case AV_CODEC_ID_PCM_U8
: return PA_SAMPLE_U8
;
33 case AV_CODEC_ID_PCM_ALAW
: return PA_SAMPLE_ALAW
;
34 case AV_CODEC_ID_PCM_MULAW
: return PA_SAMPLE_ULAW
;
35 case AV_CODEC_ID_PCM_S16LE
: return PA_SAMPLE_S16LE
;
36 case AV_CODEC_ID_PCM_S16BE
: return PA_SAMPLE_S16BE
;
37 case AV_CODEC_ID_PCM_F32LE
: return PA_SAMPLE_FLOAT32LE
;
38 case AV_CODEC_ID_PCM_F32BE
: return PA_SAMPLE_FLOAT32BE
;
39 case AV_CODEC_ID_PCM_S32LE
: return PA_SAMPLE_S32LE
;
40 case AV_CODEC_ID_PCM_S32BE
: return PA_SAMPLE_S32BE
;
41 case AV_CODEC_ID_PCM_S24LE
: return PA_SAMPLE_S24LE
;
42 case AV_CODEC_ID_PCM_S24BE
: return PA_SAMPLE_S24BE
;
43 default: return PA_SAMPLE_INVALID
;
47 enum PulseAudioContextState
{
48 PULSE_CONTEXT_INITIALIZING
,
50 PULSE_CONTEXT_FINISHED
53 typedef struct PulseAudioDeviceList
{
54 AVDeviceInfoList
*devices
;
58 } PulseAudioDeviceList
;
60 static void pa_state_cb(pa_context
*c
, void *userdata
)
62 enum PulseAudioContextState
*context_state
= userdata
;
64 switch (pa_context_get_state(c
)) {
65 case PA_CONTEXT_FAILED
:
66 case PA_CONTEXT_TERMINATED
:
67 *context_state
= PULSE_CONTEXT_FINISHED
;
69 case PA_CONTEXT_READY
:
70 *context_state
= PULSE_CONTEXT_READY
;
77 void ff_pulse_audio_disconnect_context(pa_mainloop
**pa_ml
, pa_context
**pa_ctx
)
83 pa_context_set_state_callback(*pa_ctx
, NULL
, NULL
);
84 pa_context_disconnect(*pa_ctx
);
85 pa_context_unref(*pa_ctx
);
88 pa_mainloop_free(*pa_ml
);
93 int ff_pulse_audio_connect_context(pa_mainloop
**pa_ml
, pa_context
**pa_ctx
,
94 const char *server
, const char *description
)
97 pa_mainloop_api
*pa_mlapi
= NULL
;
98 enum PulseAudioContextState context_state
= PULSE_CONTEXT_INITIALIZING
;
106 if (!(*pa_ml
= pa_mainloop_new()))
107 return AVERROR(ENOMEM
);
108 if (!(pa_mlapi
= pa_mainloop_get_api(*pa_ml
))) {
109 ret
= AVERROR_EXTERNAL
;
112 if (!(*pa_ctx
= pa_context_new(pa_mlapi
, description
))) {
113 ret
= AVERROR(ENOMEM
);
116 pa_context_set_state_callback(*pa_ctx
, pa_state_cb
, &context_state
);
117 if (pa_context_connect(*pa_ctx
, server
, 0, NULL
) < 0) {
118 ret
= AVERROR_EXTERNAL
;
122 while (context_state
== PULSE_CONTEXT_INITIALIZING
)
123 pa_mainloop_iterate(*pa_ml
, 1, NULL
);
124 if (context_state
== PULSE_CONTEXT_FINISHED
) {
125 ret
= AVERROR_EXTERNAL
;
131 ff_pulse_audio_disconnect_context(pa_ml
, pa_ctx
);
135 static void pulse_add_detected_device(PulseAudioDeviceList
*info
,
136 const char *name
, const char *description
)
139 AVDeviceInfo
*new_device
= NULL
;
141 if (info
->error_code
)
144 new_device
= av_mallocz(sizeof(AVDeviceInfo
));
146 info
->error_code
= AVERROR(ENOMEM
);
150 new_device
->device_description
= av_strdup(description
);
151 new_device
->device_name
= av_strdup(name
);
153 if (!new_device
->device_description
|| !new_device
->device_name
) {
154 info
->error_code
= AVERROR(ENOMEM
);
158 if ((ret
= av_dynarray_add_nofree(&info
->devices
->devices
,
159 &info
->devices
->nb_devices
, new_device
)) < 0) {
160 info
->error_code
= ret
;
166 av_freep(&new_device
->device_description
);
167 av_freep(&new_device
->device_name
);
172 static void pulse_audio_source_device_cb(pa_context
*c
, const pa_source_info
*dev
,
173 int eol
, void *userdata
)
176 pulse_add_detected_device(userdata
, dev
->name
, dev
->description
);
179 static void pulse_audio_sink_device_cb(pa_context
*c
, const pa_sink_info
*dev
,
180 int eol
, void *userdata
)
183 pulse_add_detected_device(userdata
, dev
->name
, dev
->description
);
186 static void pulse_server_info_cb(pa_context
*c
, const pa_server_info
*i
, void *userdata
)
188 PulseAudioDeviceList
*info
= userdata
;
190 info
->default_device
= av_strdup(i
->default_sink_name
);
192 info
->default_device
= av_strdup(i
->default_source_name
);
193 if (!info
->default_device
)
194 info
->error_code
= AVERROR(ENOMEM
);
197 int ff_pulse_audio_get_devices(AVDeviceInfoList
*devices
, const char *server
, int output
)
199 pa_mainloop
*pa_ml
= NULL
;
200 pa_operation
*pa_op
= NULL
;
201 pa_context
*pa_ctx
= NULL
;
202 enum pa_operation_state op_state
;
203 PulseAudioDeviceList dev_list
= { 0 };
206 dev_list
.output
= output
;
207 dev_list
.devices
= devices
;
209 return AVERROR(EINVAL
);
210 devices
->nb_devices
= 0;
211 devices
->devices
= NULL
;
213 if ((dev_list
.error_code
= ff_pulse_audio_connect_context(&pa_ml
, &pa_ctx
, server
, "Query devices")) < 0)
217 pa_op
= pa_context_get_sink_info_list(pa_ctx
, pulse_audio_sink_device_cb
, &dev_list
);
219 pa_op
= pa_context_get_source_info_list(pa_ctx
, pulse_audio_source_device_cb
, &dev_list
);
220 while ((op_state
= pa_operation_get_state(pa_op
)) == PA_OPERATION_RUNNING
)
221 pa_mainloop_iterate(pa_ml
, 1, NULL
);
222 if (op_state
!= PA_OPERATION_DONE
)
223 dev_list
.error_code
= AVERROR_EXTERNAL
;
224 pa_operation_unref(pa_op
);
225 if (dev_list
.error_code
< 0)
228 pa_op
= pa_context_get_server_info(pa_ctx
, pulse_server_info_cb
, &dev_list
);
229 while ((op_state
= pa_operation_get_state(pa_op
)) == PA_OPERATION_RUNNING
)
230 pa_mainloop_iterate(pa_ml
, 1, NULL
);
231 if (op_state
!= PA_OPERATION_DONE
)
232 dev_list
.error_code
= AVERROR_EXTERNAL
;
233 pa_operation_unref(pa_op
);
234 if (dev_list
.error_code
< 0)
237 devices
->default_device
= -1;
238 for (i
= 0; i
< devices
->nb_devices
; i
++) {
239 if (!strcmp(devices
->devices
[i
]->device_name
, dev_list
.default_device
)) {
240 devices
->default_device
= i
;
246 av_free(dev_list
.default_device
);
247 ff_pulse_audio_disconnect_context(&pa_ml
, &pa_ctx
);
248 return dev_list
.error_code
;