pulse_audio_dec.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/time.h"
  32. #include "libavutil/opt.h"
  33. #include "pulse_audio_common.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 av_cold int pulse_read_header(AVFormatContext *s)
  50. {
  51. PulseData *pd = s->priv_data;
  52. AVStream *st;
  53. char *device = NULL;
  54. int ret, sample_bytes;
  55. enum AVCodecID codec_id =
  56. s->audio_codec_id == AV_CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
  57. const pa_sample_spec ss = { ff_codec_id_to_pulse_format(codec_id),
  58. pd->sample_rate,
  59. pd->channels };
  60. pa_buffer_attr attr = { -1 };
  61. st = avformat_new_stream(s, NULL);
  62. if (!st) {
  63. av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
  64. return AVERROR(ENOMEM);
  65. }
  66. attr.fragsize = pd->fragment_size;
  67. if (strcmp(s->filename, "default"))
  68. device = s->filename;
  69. pd->s = pa_simple_new(pd->server, pd->name,
  70. PA_STREAM_RECORD,
  71. device, pd->stream_name, &ss,
  72. NULL, &attr, &ret);
  73. if (!pd->s) {
  74. av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n",
  75. pa_strerror(ret));
  76. return AVERROR(EIO);
  77. }
  78. /* take real parameters */
  79. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  80. st->codec->codec_id = codec_id;
  81. st->codec->sample_rate = pd->sample_rate;
  82. st->codec->channels = pd->channels;
  83. avpriv_set_pts_info(st, 64, 1, pd->sample_rate); /* 64 bits pts in us */
  84. pd->pts = AV_NOPTS_VALUE;
  85. sample_bytes = (av_get_bits_per_sample(codec_id) >> 3) * pd->channels;
  86. if (pd->frame_size % sample_bytes) {
  87. av_log(s, AV_LOG_WARNING, "frame_size %i is not divisible by %i "
  88. "(channels * bytes_per_sample) \n", pd->frame_size, sample_bytes);
  89. }
  90. pd->frame_duration = pd->frame_size / sample_bytes;
  91. return 0;
  92. }
  93. static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
  94. {
  95. PulseData *pd = s->priv_data;
  96. int res;
  97. if (av_new_packet(pkt, pd->frame_size) < 0) {
  98. return AVERROR(ENOMEM);
  99. }
  100. if ((pa_simple_read(pd->s, pkt->data, pkt->size, &res)) < 0) {
  101. av_log(s, AV_LOG_ERROR, "pa_simple_read failed: %s\n",
  102. pa_strerror(res));
  103. av_free_packet(pkt);
  104. return AVERROR(EIO);
  105. }
  106. if (pd->pts == AV_NOPTS_VALUE) {
  107. pa_usec_t latency;
  108. if ((latency = pa_simple_get_latency(pd->s, &res)) == (pa_usec_t) -1) {
  109. av_log(s, AV_LOG_ERROR, "pa_simple_get_latency() failed: %s\n",
  110. pa_strerror(res));
  111. return AVERROR(EIO);
  112. }
  113. pd->pts = -latency;
  114. if (pd->wallclock)
  115. pd->pts += av_gettime();
  116. }
  117. pkt->pts = pd->pts;
  118. pd->pts += pd->frame_duration;
  119. return 0;
  120. }
  121. static av_cold int pulse_close(AVFormatContext *s)
  122. {
  123. PulseData *pd = s->priv_data;
  124. pa_simple_free(pd->s);
  125. return 0;
  126. }
  127. #define OFFSET(a) offsetof(PulseData, a)
  128. #define D AV_OPT_FLAG_DECODING_PARAM
  129. static const AVOption options[] = {
  130. { "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D },
  131. { "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, D },
  132. { "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
  133. { "sample_rate", "set sample rate in Hz", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, D },
  134. { "channels", "set number of audio channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, D },
  135. { "frame_size", "set number of bytes per frame", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, D },
  136. { "fragment_size", "set buffering size, affects latency and cpu usage", OFFSET(fragment_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D },
  137. { "wallclock", "set the initial pts using the current time", OFFSET(wallclock), AV_OPT_TYPE_INT, {.i64 = 1}, -1, 1, D },
  138. { NULL },
  139. };
  140. static const AVClass pulse_demuxer_class = {
  141. .class_name = "Pulse demuxer",
  142. .item_name = av_default_item_name,
  143. .option = options,
  144. .version = LIBAVUTIL_VERSION_INT,
  145. };
  146. AVInputFormat ff_pulse_demuxer = {
  147. .name = "pulse",
  148. .long_name = NULL_IF_CONFIG_SMALL("Pulse audio input"),
  149. .priv_data_size = sizeof(PulseData),
  150. .read_header = pulse_read_header,
  151. .read_packet = pulse_read_packet,
  152. .read_close = pulse_close,
  153. .flags = AVFMT_NOFILE,
  154. .priv_class = &pulse_demuxer_class,
  155. };