You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
6.8KB

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