pulse.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 using the simple API.
  24. * @author Luca Barbato <lu_zero@gentoo.org>
  25. */
  26. #include <pulse/simple.h>
  27. #include <pulse/rtclock.h>
  28. #include <pulse/error.h>
  29. #include "libavformat/avformat.h"
  30. #include "libavformat/internal.h"
  31. #include "libavutil/opt.h"
  32. #define DEFAULT_CODEC_ID AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE)
  33. typedef struct PulseData {
  34. AVClass *class;
  35. char *server;
  36. char *name;
  37. char *stream_name;
  38. int sample_rate;
  39. int channels;
  40. int frame_size;
  41. int fragment_size;
  42. pa_simple *s;
  43. int64_t pts;
  44. int64_t frame_duration;
  45. } PulseData;
  46. static pa_sample_format_t codec_id_to_pulse_format(int codec_id) {
  47. switch (codec_id) {
  48. case AV_CODEC_ID_PCM_U8: return PA_SAMPLE_U8;
  49. case AV_CODEC_ID_PCM_ALAW: return PA_SAMPLE_ALAW;
  50. case AV_CODEC_ID_PCM_MULAW: return PA_SAMPLE_ULAW;
  51. case AV_CODEC_ID_PCM_S16LE: return PA_SAMPLE_S16LE;
  52. case AV_CODEC_ID_PCM_S16BE: return PA_SAMPLE_S16BE;
  53. case AV_CODEC_ID_PCM_F32LE: return PA_SAMPLE_FLOAT32LE;
  54. case AV_CODEC_ID_PCM_F32BE: return PA_SAMPLE_FLOAT32BE;
  55. case AV_CODEC_ID_PCM_S32LE: return PA_SAMPLE_S32LE;
  56. case AV_CODEC_ID_PCM_S32BE: return PA_SAMPLE_S32BE;
  57. case AV_CODEC_ID_PCM_S24LE: return PA_SAMPLE_S24LE;
  58. case AV_CODEC_ID_PCM_S24BE: return PA_SAMPLE_S24BE;
  59. default: return PA_SAMPLE_INVALID;
  60. }
  61. }
  62. static av_cold int pulse_read_header(AVFormatContext *s)
  63. {
  64. PulseData *pd = s->priv_data;
  65. AVStream *st;
  66. char *device = NULL;
  67. int ret;
  68. enum AVCodecID codec_id =
  69. s->audio_codec_id == AV_CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
  70. const pa_sample_spec ss = { codec_id_to_pulse_format(codec_id),
  71. pd->sample_rate,
  72. pd->channels };
  73. pa_buffer_attr attr = { -1 };
  74. st = avformat_new_stream(s, NULL);
  75. if (!st) {
  76. av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
  77. return AVERROR(ENOMEM);
  78. }
  79. attr.fragsize = pd->fragment_size;
  80. if (strcmp(s->filename, "default"))
  81. device = s->filename;
  82. pd->s = pa_simple_new(pd->server, pd->name,
  83. PA_STREAM_RECORD,
  84. device, pd->stream_name, &ss,
  85. NULL, &attr, &ret);
  86. if (!pd->s) {
  87. av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n",
  88. pa_strerror(ret));
  89. return AVERROR(EIO);
  90. }
  91. /* take real parameters */
  92. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  93. st->codec->codec_id = codec_id;
  94. st->codec->sample_rate = pd->sample_rate;
  95. st->codec->channels = pd->channels;
  96. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  97. pd->pts = AV_NOPTS_VALUE;
  98. pd->frame_duration = (pd->frame_size * 1000000LL * 8) /
  99. (pd->sample_rate * pd->channels * av_get_bits_per_sample(codec_id));
  100. return 0;
  101. }
  102. static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
  103. {
  104. PulseData *pd = s->priv_data;
  105. int res;
  106. pa_usec_t latency;
  107. if (av_new_packet(pkt, pd->frame_size) < 0) {
  108. return AVERROR(ENOMEM);
  109. }
  110. if ((pa_simple_read(pd->s, pkt->data, pkt->size, &res)) < 0) {
  111. av_log(s, AV_LOG_ERROR, "pa_simple_read failed: %s\n",
  112. pa_strerror(res));
  113. av_free_packet(pkt);
  114. return AVERROR(EIO);
  115. }
  116. if ((latency = pa_simple_get_latency(pd->s, &res)) == (pa_usec_t) -1) {
  117. av_log(s, AV_LOG_ERROR, "pa_simple_get_latency() failed: %s\n",
  118. pa_strerror(res));
  119. return AVERROR(EIO);
  120. }
  121. if (pd->pts == AV_NOPTS_VALUE) {
  122. pd->pts = -latency;
  123. }
  124. pkt->pts = pd->pts;
  125. pd->pts += pd->frame_duration;
  126. return 0;
  127. }
  128. static av_cold int pulse_close(AVFormatContext *s)
  129. {
  130. PulseData *pd = s->priv_data;
  131. pa_simple_free(pd->s);
  132. return 0;
  133. }
  134. #define OFFSET(a) offsetof(PulseData, a)
  135. #define D AV_OPT_FLAG_DECODING_PARAM
  136. static const AVOption options[] = {
  137. { "server", "pulse server name", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D },
  138. { "name", "application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, D },
  139. { "stream_name", "stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
  140. { "sample_rate", "sample rate in Hz", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, D },
  141. { "channels", "number of audio channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, D },
  142. { "frame_size", "number of bytes per frame", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, D },
  143. { "fragment_size", "buffering size, affects latency and cpu usage", OFFSET(fragment_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D },
  144. { NULL },
  145. };
  146. static const AVClass pulse_demuxer_class = {
  147. .class_name = "Pulse demuxer",
  148. .item_name = av_default_item_name,
  149. .option = options,
  150. .version = LIBAVUTIL_VERSION_INT,
  151. };
  152. AVInputFormat ff_pulse_demuxer = {
  153. .name = "pulse",
  154. .long_name = NULL_IF_CONFIG_SMALL("Pulse audio input"),
  155. .priv_data_size = sizeof(PulseData),
  156. .read_header = pulse_read_header,
  157. .read_packet = pulse_read_packet,
  158. .read_close = pulse_close,
  159. .flags = AVFMT_NOFILE,
  160. .priv_class = &pulse_demuxer_class,
  161. };