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"
28 pa_sample_format_t av_cold ff_codec_id_to_pulse_format(enum AVCodecID codec_id)
31 case AV_CODEC_ID_PCM_U8: return PA_SAMPLE_U8;
32 case AV_CODEC_ID_PCM_ALAW: return PA_SAMPLE_ALAW;
33 case AV_CODEC_ID_PCM_MULAW: return PA_SAMPLE_ULAW;
34 case AV_CODEC_ID_PCM_S16LE: return PA_SAMPLE_S16LE;
35 case AV_CODEC_ID_PCM_S16BE: return PA_SAMPLE_S16BE;
36 case AV_CODEC_ID_PCM_F32LE: return PA_SAMPLE_FLOAT32LE;
37 case AV_CODEC_ID_PCM_F32BE: return PA_SAMPLE_FLOAT32BE;
38 case AV_CODEC_ID_PCM_S32LE: return PA_SAMPLE_S32LE;
39 case AV_CODEC_ID_PCM_S32BE: return PA_SAMPLE_S32BE;
40 case AV_CODEC_ID_PCM_S24LE: return PA_SAMPLE_S24LE;
41 case AV_CODEC_ID_PCM_S24BE: return PA_SAMPLE_S24BE;
42 default: return PA_SAMPLE_INVALID;
46 enum PulseAudioLoopState {
52 typedef struct PulseAudioDeviceList {
53 AVDeviceInfoList *devices;
57 } PulseAudioDeviceList;
59 static void pa_state_cb(pa_context *c, void *userdata)
61 enum PulseAudioLoopState *loop_status = userdata;
63 switch (pa_context_get_state(c)) {
64 case PA_CONTEXT_FAILED:
65 case PA_CONTEXT_TERMINATED:
66 *loop_status = PA_LOOP_FINISHED;
68 case PA_CONTEXT_READY:
69 *loop_status = PA_LOOP_READY;
76 static void pulse_add_detected_device(PulseAudioDeviceList *info,
77 const char *name, const char *description)
80 AVDeviceInfo *new_device = NULL;
85 new_device = av_mallocz(sizeof(AVDeviceInfo));
87 info->error_code = AVERROR(ENOMEM);
91 new_device->device_description = av_strdup(description);
92 new_device->device_name = av_strdup(name);
94 if (!new_device->device_description || !new_device->device_name) {
95 info->error_code = AVERROR(ENOMEM);
99 if ((ret = av_dynarray_add_nofree(&info->devices->devices,
100 &info->devices->nb_devices, new_device)) < 0) {
101 info->error_code = ret;
107 av_free(new_device->device_description);
108 av_free(new_device->device_name);
113 static void pulse_audio_source_device_cb(pa_context *c, const pa_source_info *dev,
114 int eol, void *userdata)
117 pulse_add_detected_device(userdata, dev->name, dev->description);
120 static void pulse_audio_sink_device_cb(pa_context *c, const pa_sink_info *dev,
121 int eol, void *userdata)
124 pulse_add_detected_device(userdata, dev->name, dev->description);
127 static void pulse_server_info_cb(pa_context *c, const pa_server_info *i, void *userdata)
129 PulseAudioDeviceList *info = userdata;
131 info->default_device = av_strdup(i->default_sink_name);
133 info->default_device = av_strdup(i->default_source_name);
134 if (!info->default_device)
135 info->error_code = AVERROR(ENOMEM);
138 int ff_pulse_audio_get_devices(AVDeviceInfoList *devices, const char *server, int output)
140 pa_mainloop *pa_ml = NULL;
141 pa_mainloop_api *pa_mlapi = NULL;
142 pa_operation *pa_op = NULL;
143 pa_context *pa_ctx = NULL;
144 enum pa_operation_state op_state;
145 enum PulseAudioLoopState loop_state = PA_LOOP_INITIALIZING;
146 PulseAudioDeviceList dev_list = { 0 };
149 dev_list.output = output;
150 dev_list.devices = devices;
152 return AVERROR(EINVAL);
153 devices->nb_devices = 0;
154 devices->devices = NULL;
155 if (!(pa_ml = pa_mainloop_new()))
156 return AVERROR(ENOMEM);
157 if (!(pa_mlapi = pa_mainloop_get_api(pa_ml))) {
158 dev_list.error_code = AVERROR_EXTERNAL;
161 if (!(pa_ctx = pa_context_new(pa_mlapi, "Query devices"))) {
162 dev_list.error_code = AVERROR(ENOMEM);
165 pa_context_set_state_callback(pa_ctx, pa_state_cb, &loop_state);
166 if (pa_context_connect(pa_ctx, server, 0, NULL) < 0) {
167 dev_list.error_code = AVERROR_EXTERNAL;
171 while (loop_state == PA_LOOP_INITIALIZING)
172 pa_mainloop_iterate(pa_ml, 1, NULL);
173 if (loop_state == PA_LOOP_FINISHED) {
174 dev_list.error_code = AVERROR_EXTERNAL;
179 pa_op = pa_context_get_sink_info_list(pa_ctx, pulse_audio_sink_device_cb, &dev_list);
181 pa_op = pa_context_get_source_info_list(pa_ctx, pulse_audio_source_device_cb, &dev_list);
182 while ((op_state = pa_operation_get_state(pa_op)) == PA_OPERATION_RUNNING)
183 pa_mainloop_iterate(pa_ml, 1, NULL);
184 if (op_state != PA_OPERATION_DONE)
185 dev_list.error_code = AVERROR_EXTERNAL;
186 pa_operation_unref(pa_op);
187 if (dev_list.error_code < 0)
190 pa_op = pa_context_get_server_info(pa_ctx, pulse_server_info_cb, &dev_list);
191 while ((op_state = pa_operation_get_state(pa_op)) == PA_OPERATION_RUNNING)
192 pa_mainloop_iterate(pa_ml, 1, NULL);
193 if (op_state != PA_OPERATION_DONE)
194 dev_list.error_code = AVERROR_EXTERNAL;
195 pa_operation_unref(pa_op);
196 if (dev_list.error_code < 0)
199 devices->default_device = -1;
200 for (i = 0; i < devices->nb_devices; i++) {
201 if (!strcmp(devices->devices[i]->device_name, dev_list.default_device)) {
202 devices->default_device = i;
208 av_free(dev_list.default_device);
210 pa_context_disconnect(pa_ctx);
212 pa_context_unref(pa_ctx);
214 pa_mainloop_free(pa_ml);
215 return dev_list.error_code;