]> git.sesse.net Git - ffmpeg/blob - libavdevice/pulse_audio_common.c
avformat/mp3enc: drop redundant and uninitialized variable
[ffmpeg] / libavdevice / pulse_audio_common.c
1 /*
2  * Pulseaudio common
3  * Copyright (c) 2014 Lukasz Marek
4  * Copyright (c) 2011 Luca Barbato <lu_zero@gentoo.org>
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "pulse_audio_common.h"
24 #include "libavutil/attributes.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/mem.h"
27
28 pa_sample_format_t av_cold ff_codec_id_to_pulse_format(enum AVCodecID codec_id)
29 {
30     switch (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;
43     }
44 }
45
46 enum PulseAudioLoopState {
47     PA_LOOP_INITIALIZING,
48     PA_LOOP_READY,
49     PA_LOOP_FINISHED
50 };
51
52 typedef struct PulseAudioDeviceList {
53     AVDeviceInfoList *devices;
54     int error_code;
55     int output;
56     char *default_device;
57 } PulseAudioDeviceList;
58
59 static void pa_state_cb(pa_context *c, void *userdata)
60 {
61     enum PulseAudioLoopState *loop_status = userdata;
62
63     switch  (pa_context_get_state(c)) {
64     case PA_CONTEXT_FAILED:
65     case PA_CONTEXT_TERMINATED:
66         *loop_status = PA_LOOP_FINISHED;
67         break;
68     case PA_CONTEXT_READY:
69         *loop_status = PA_LOOP_READY;
70         break;
71     default:
72         break;
73     }
74 }
75
76 static void pulse_add_detected_device(PulseAudioDeviceList *info,
77                                       const char *name, const char *description)
78 {
79     int ret;
80     AVDeviceInfo *new_device = NULL;
81
82     if (info->error_code)
83         return;
84
85     new_device = av_mallocz(sizeof(AVDeviceInfo));
86     if (!new_device) {
87         info->error_code = AVERROR(ENOMEM);
88         return;
89     }
90
91     new_device->device_description = av_strdup(description);
92     new_device->device_name = av_strdup(name);
93
94     if (!new_device->device_description || !new_device->device_name) {
95         info->error_code = AVERROR(ENOMEM);
96         goto fail;
97     }
98
99     if ((ret = av_dynarray_add_nofree(&info->devices->devices,
100                                       &info->devices->nb_devices, new_device)) < 0) {
101         info->error_code = ret;
102         goto fail;
103     }
104     return;
105
106   fail:
107     av_free(new_device->device_description);
108     av_free(new_device->device_name);
109     av_free(new_device);
110
111 }
112
113 static void pulse_audio_source_device_cb(pa_context *c, const pa_source_info *dev,
114                                          int eol, void *userdata)
115 {
116     if (!eol)
117         pulse_add_detected_device(userdata, dev->name, dev->description);
118 }
119
120 static void pulse_audio_sink_device_cb(pa_context *c, const pa_sink_info *dev,
121                                        int eol, void *userdata)
122 {
123     if (!eol)
124         pulse_add_detected_device(userdata, dev->name, dev->description);
125 }
126
127 static void pulse_server_info_cb(pa_context *c, const pa_server_info *i, void *userdata)
128 {
129     PulseAudioDeviceList *info = userdata;
130     if (info->output)
131         info->default_device = av_strdup(i->default_sink_name);
132     else
133         info->default_device = av_strdup(i->default_source_name);
134     if (!info->default_device)
135         info->error_code = AVERROR(ENOMEM);
136 }
137
138 int ff_pulse_audio_get_devices(AVDeviceInfoList *devices, const char *server, int output)
139 {
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 };
147     int i;
148
149     dev_list.output = output;
150     dev_list.devices = devices;
151     if (!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;
159         goto fail;
160     }
161     if (!(pa_ctx = pa_context_new(pa_mlapi, "Query devices"))) {
162         dev_list.error_code = AVERROR(ENOMEM);
163         goto fail;
164     }
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;
168         goto fail;
169     }
170
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;
175         goto fail;
176     }
177
178     if (output)
179         pa_op = pa_context_get_sink_info_list(pa_ctx, pulse_audio_sink_device_cb, &dev_list);
180     else
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)
188         goto fail;
189
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)
197         goto fail;
198
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;
203             break;
204         }
205     }
206
207   fail:
208     av_free(dev_list.default_device);
209     if(pa_ctx)
210         pa_context_disconnect(pa_ctx);
211     if (pa_ctx)
212         pa_context_unref(pa_ctx);
213     if (pa_ml)
214         pa_mainloop_free(pa_ml);
215     return dev_list.error_code;
216 }