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.

174 lines
5.6KB

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