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.

198 lines
6.6KB

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