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.

192 lines
6.1KB

  1. /*
  2. * Pulseaudio input
  3. * Copyright (c) 2011 Luca Barbato <lu_zero@gentoo.org>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Pulseaudio input
  24. * @author Luca Barbato <lu_zero@gentoo.org>
  25. *
  26. * This avdevice decoder allows to capture audio from a Pulseaudio device using
  27. * the simple api.
  28. */
  29. #include <pulse/simple.h>
  30. #include <pulse/rtclock.h>
  31. #include <pulse/error.h>
  32. #include "libavformat/avformat.h"
  33. #include "libavutil/opt.h"
  34. #define DEFAULT_CODEC_ID AV_NE(CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE)
  35. typedef struct PulseData {
  36. AVClass *class;
  37. char *server;
  38. char *name;
  39. char *dev;
  40. char *stream_name;
  41. int sample_rate;
  42. int channels;
  43. int frame_size;
  44. pa_simple *s;
  45. int64_t pts;
  46. } PulseData;
  47. static pa_sample_format_t codec_id_to_pulse_format(int codec_id) {
  48. switch (codec_id) {
  49. case CODEC_ID_PCM_U8: return PA_SAMPLE_U8;
  50. case CODEC_ID_PCM_ALAW: return PA_SAMPLE_ALAW;
  51. case CODEC_ID_PCM_MULAW: return PA_SAMPLE_ULAW;
  52. case CODEC_ID_PCM_S16LE: return PA_SAMPLE_S16LE;
  53. case CODEC_ID_PCM_S16BE: return PA_SAMPLE_S16BE;
  54. case CODEC_ID_PCM_F32LE: return PA_SAMPLE_FLOAT32LE;
  55. case CODEC_ID_PCM_F32BE: return PA_SAMPLE_FLOAT32BE;
  56. case CODEC_ID_PCM_S32LE: return PA_SAMPLE_S32LE;
  57. case CODEC_ID_PCM_S32BE: return PA_SAMPLE_S32BE;
  58. case CODEC_ID_PCM_S24LE: return PA_SAMPLE_S24LE;
  59. case CODEC_ID_PCM_S24BE: return PA_SAMPLE_S24BE;
  60. default: return PA_SAMPLE_INVALID;
  61. }
  62. }
  63. static av_cold int pulse_read_header(AVFormatContext *s,
  64. AVFormatParameters *ap)
  65. {
  66. PulseData *pd = s->priv_data;
  67. AVStream *st;
  68. int ret;
  69. enum CodecID codec_id =
  70. s->audio_codec_id == CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
  71. const pa_sample_spec ss = { codec_id_to_pulse_format(codec_id),
  72. pd->sample_rate,
  73. pd->channels };
  74. pa_buffer_attr attr = { -1 };
  75. st = avformat_new_stream(s, NULL);
  76. if (!st) {
  77. av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
  78. return AVERROR(ENOMEM);
  79. }
  80. attr.fragsize = pd->frame_size * 4;
  81. pd->s = pa_simple_new(pd->server, pd->name,
  82. PA_STREAM_RECORD,
  83. pd->dev, pd->stream_name, &ss,
  84. NULL, &attr, &ret);
  85. if (!pd->s) {
  86. av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n",
  87. pa_strerror(ret));
  88. return AVERROR(EIO);
  89. }
  90. /* take real parameters */
  91. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  92. st->codec->codec_id = codec_id;
  93. st->codec->sample_rate = pd->sample_rate;
  94. st->codec->channels = pd->channels;
  95. av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  96. return 0;
  97. }
  98. static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
  99. {
  100. PulseData *pd = s->priv_data;
  101. int res;
  102. pa_usec_t latency, cur;
  103. uint64_t frame_duration =
  104. (pd->frame_size*1000000LL)/(pd->sample_rate * pd->channels);
  105. if (av_new_packet(pkt, pd->frame_size) < 0) {
  106. return AVERROR(ENOMEM);
  107. }
  108. cur = pa_rtclock_now();
  109. if ((pa_simple_read(pd->s, pkt->data, pkt->size, &res)) < 0) {
  110. av_log(s, AV_LOG_ERROR, "pa_simple_read failed: %s\n",
  111. pa_strerror(res));
  112. av_free_packet(pkt);
  113. return AVERROR(EIO);
  114. }
  115. if ((latency = pa_simple_get_latency(pd->s, &res)) == (pa_usec_t) -1) {
  116. av_log(s, AV_LOG_ERROR, "pa_simple_get_latency() failed: %s\n",
  117. pa_strerror(res));
  118. return AVERROR(EIO);
  119. }
  120. if (!pd->pts) {
  121. pd->pts -= latency;
  122. }
  123. pd->pts += frame_duration;
  124. av_log(s, AV_LOG_DEBUG, "%"PRId64" time %"PRId64","
  125. " latency %"PRId64", %"PRId64"\n",
  126. av_gettime(), cur, latency, pd->pts);
  127. pkt->pts = pd->pts;
  128. return 0;
  129. }
  130. static av_cold int pulse_close(AVFormatContext *s)
  131. {
  132. PulseData *pd = s->priv_data;
  133. pa_simple_free(pd->s);
  134. return 0;
  135. }
  136. #define OFFSET(a) offsetof(PulseData, a)
  137. #define D AV_OPT_FLAG_DECODING_PARAM
  138. static const AVOption options[] = {
  139. { "server", "pulse server name", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D },
  140. { "name", "application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = "ffmpeg"}, 0, 0, D },
  141. { "dev", "device to use", OFFSET(dev), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D },
  142. { "stream_name", "stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
  143. { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.dbl = 48000}, 1, INT_MAX, D },
  144. { "channels", "", OFFSET(channels), AV_OPT_TYPE_INT, {.dbl = 2}, 1, INT_MAX, D },
  145. { "frame_size", "", OFFSET(frame_size), AV_OPT_TYPE_INT, {.dbl = 1024}, 1, INT_MAX, D },
  146. { NULL },
  147. };
  148. static const AVClass pulse_demuxer_class = {
  149. .class_name = "Pulse demuxer",
  150. .item_name = av_default_item_name,
  151. .option = options,
  152. .version = LIBAVUTIL_VERSION_INT,
  153. };
  154. AVInputFormat ff_pulse_demuxer = {
  155. .name = "pulse",
  156. .long_name = NULL_IF_CONFIG_SMALL("Pulse audio input"),
  157. .priv_data_size = sizeof(PulseData),
  158. .read_header = pulse_read_header,
  159. .read_packet = pulse_read_packet,
  160. .read_close = pulse_close,
  161. .flags = AVFMT_NOFILE,
  162. .priv_class = &pulse_demuxer_class,
  163. };