pulse_audio_common.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #include "pulse_audio_common.h"
  23. #include "libavutil/attributes.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/mem.h"
  26. #include "libavutil/avassert.h"
  27. pa_sample_format_t av_cold ff_codec_id_to_pulse_format(enum AVCodecID codec_id)
  28. {
  29. switch (codec_id) {
  30. case AV_CODEC_ID_PCM_U8: return PA_SAMPLE_U8;
  31. case AV_CODEC_ID_PCM_ALAW: return PA_SAMPLE_ALAW;
  32. case AV_CODEC_ID_PCM_MULAW: return PA_SAMPLE_ULAW;
  33. case AV_CODEC_ID_PCM_S16LE: return PA_SAMPLE_S16LE;
  34. case AV_CODEC_ID_PCM_S16BE: return PA_SAMPLE_S16BE;
  35. case AV_CODEC_ID_PCM_F32LE: return PA_SAMPLE_FLOAT32LE;
  36. case AV_CODEC_ID_PCM_F32BE: return PA_SAMPLE_FLOAT32BE;
  37. case AV_CODEC_ID_PCM_S32LE: return PA_SAMPLE_S32LE;
  38. case AV_CODEC_ID_PCM_S32BE: return PA_SAMPLE_S32BE;
  39. case AV_CODEC_ID_PCM_S24LE: return PA_SAMPLE_S24LE;
  40. case AV_CODEC_ID_PCM_S24BE: return PA_SAMPLE_S24BE;
  41. default: return PA_SAMPLE_INVALID;
  42. }
  43. }
  44. enum PulseAudioContextState {
  45. PULSE_CONTEXT_INITIALIZING,
  46. PULSE_CONTEXT_READY,
  47. PULSE_CONTEXT_FINISHED
  48. };
  49. typedef struct PulseAudioDeviceList {
  50. AVDeviceInfoList *devices;
  51. int error_code;
  52. int output;
  53. char *default_device;
  54. } PulseAudioDeviceList;
  55. static void pa_state_cb(pa_context *c, void *userdata)
  56. {
  57. enum PulseAudioContextState *context_state = userdata;
  58. switch (pa_context_get_state(c)) {
  59. case PA_CONTEXT_FAILED:
  60. case PA_CONTEXT_TERMINATED:
  61. *context_state = PULSE_CONTEXT_FINISHED;
  62. break;
  63. case PA_CONTEXT_READY:
  64. *context_state = PULSE_CONTEXT_READY;
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. void ff_pulse_audio_disconnect_context(pa_mainloop **pa_ml, pa_context **pa_ctx)
  71. {
  72. av_assert0(pa_ml);
  73. av_assert0(pa_ctx);
  74. if (*pa_ctx) {
  75. pa_context_set_state_callback(*pa_ctx, NULL, NULL);
  76. pa_context_disconnect(*pa_ctx);
  77. pa_context_unref(*pa_ctx);
  78. }
  79. if (*pa_ml)
  80. pa_mainloop_free(*pa_ml);
  81. *pa_ml = NULL;
  82. *pa_ctx = NULL;
  83. }
  84. int ff_pulse_audio_connect_context(pa_mainloop **pa_ml, pa_context **pa_ctx,
  85. const char *server, const char *description)
  86. {
  87. int ret;
  88. pa_mainloop_api *pa_mlapi = NULL;
  89. enum PulseAudioContextState context_state = PULSE_CONTEXT_INITIALIZING;
  90. av_assert0(pa_ml);
  91. av_assert0(pa_ctx);
  92. *pa_ml = NULL;
  93. *pa_ctx = NULL;
  94. if (!(*pa_ml = pa_mainloop_new()))
  95. return AVERROR(ENOMEM);
  96. if (!(pa_mlapi = pa_mainloop_get_api(*pa_ml))) {
  97. ret = AVERROR_EXTERNAL;
  98. goto fail;
  99. }
  100. if (!(*pa_ctx = pa_context_new(pa_mlapi, description))) {
  101. ret = AVERROR(ENOMEM);
  102. goto fail;
  103. }
  104. pa_context_set_state_callback(*pa_ctx, pa_state_cb, &context_state);
  105. if (pa_context_connect(*pa_ctx, server, 0, NULL) < 0) {
  106. ret = AVERROR_EXTERNAL;
  107. goto fail;
  108. }
  109. while (context_state == PULSE_CONTEXT_INITIALIZING)
  110. pa_mainloop_iterate(*pa_ml, 1, NULL);
  111. if (context_state == PULSE_CONTEXT_FINISHED) {
  112. ret = AVERROR_EXTERNAL;
  113. goto fail;
  114. }
  115. return 0;
  116. fail:
  117. ff_pulse_audio_disconnect_context(pa_ml, pa_ctx);
  118. return ret;
  119. }
  120. static void pulse_add_detected_device(PulseAudioDeviceList *info,
  121. const char *name, const char *description)
  122. {
  123. int ret;
  124. AVDeviceInfo *new_device = NULL;
  125. if (info->error_code)
  126. return;
  127. new_device = av_mallocz(sizeof(AVDeviceInfo));
  128. if (!new_device) {
  129. info->error_code = AVERROR(ENOMEM);
  130. return;
  131. }
  132. new_device->device_description = av_strdup(description);
  133. new_device->device_name = av_strdup(name);
  134. if (!new_device->device_description || !new_device->device_name) {
  135. info->error_code = AVERROR(ENOMEM);
  136. goto fail;
  137. }
  138. if ((ret = av_dynarray_add_nofree(&info->devices->devices,
  139. &info->devices->nb_devices, new_device)) < 0) {
  140. info->error_code = ret;
  141. goto fail;
  142. }
  143. return;
  144. fail:
  145. av_freep(&new_device->device_description);
  146. av_freep(&new_device->device_name);
  147. av_free(new_device);
  148. }
  149. static void pulse_audio_source_device_cb(pa_context *c, const pa_source_info *dev,
  150. int eol, void *userdata)
  151. {
  152. if (!eol)
  153. pulse_add_detected_device(userdata, dev->name, dev->description);
  154. }
  155. static void pulse_audio_sink_device_cb(pa_context *c, const pa_sink_info *dev,
  156. int eol, void *userdata)
  157. {
  158. if (!eol)
  159. pulse_add_detected_device(userdata, dev->name, dev->description);
  160. }
  161. static void pulse_server_info_cb(pa_context *c, const pa_server_info *i, void *userdata)
  162. {
  163. PulseAudioDeviceList *info = userdata;
  164. if (info->output)
  165. info->default_device = av_strdup(i->default_sink_name);
  166. else
  167. info->default_device = av_strdup(i->default_source_name);
  168. if (!info->default_device)
  169. info->error_code = AVERROR(ENOMEM);
  170. }
  171. int ff_pulse_audio_get_devices(AVDeviceInfoList *devices, const char *server, int output)
  172. {
  173. pa_mainloop *pa_ml = NULL;
  174. pa_operation *pa_op = NULL;
  175. pa_context *pa_ctx = NULL;
  176. enum pa_operation_state op_state;
  177. PulseAudioDeviceList dev_list = { 0 };
  178. int i;
  179. dev_list.output = output;
  180. dev_list.devices = devices;
  181. if (!devices)
  182. return AVERROR(EINVAL);
  183. devices->nb_devices = 0;
  184. devices->devices = NULL;
  185. if ((dev_list.error_code = ff_pulse_audio_connect_context(&pa_ml, &pa_ctx, server, "Query devices")) < 0)
  186. goto fail;
  187. if (output)
  188. pa_op = pa_context_get_sink_info_list(pa_ctx, pulse_audio_sink_device_cb, &dev_list);
  189. else
  190. pa_op = pa_context_get_source_info_list(pa_ctx, pulse_audio_source_device_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. pa_op = pa_context_get_server_info(pa_ctx, pulse_server_info_cb, &dev_list);
  199. while ((op_state = pa_operation_get_state(pa_op)) == PA_OPERATION_RUNNING)
  200. pa_mainloop_iterate(pa_ml, 1, NULL);
  201. if (op_state != PA_OPERATION_DONE)
  202. dev_list.error_code = AVERROR_EXTERNAL;
  203. pa_operation_unref(pa_op);
  204. if (dev_list.error_code < 0)
  205. goto fail;
  206. devices->default_device = -1;
  207. for (i = 0; i < devices->nb_devices; i++) {
  208. if (!strcmp(devices->devices[i]->device_name, dev_list.default_device)) {
  209. devices->default_device = i;
  210. break;
  211. }
  212. }
  213. fail:
  214. av_free(dev_list.default_device);
  215. ff_pulse_audio_disconnect_context(&pa_ml, &pa_ctx);
  216. return dev_list.error_code;
  217. }