pulse_audio_enc.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2013 Lukasz Marek <lukasz.m.luki@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <pulse/simple.h>
  21. #include <pulse/error.h>
  22. #include "libavformat/avformat.h"
  23. #include "libavformat/internal.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/time.h"
  26. #include "libavutil/log.h"
  27. #include "pulse_audio_common.h"
  28. typedef struct PulseData {
  29. AVClass *class;
  30. const char *server;
  31. const char *name;
  32. const char *stream_name;
  33. const char *device;
  34. pa_simple *pa;
  35. int64_t timestamp;
  36. int buffer_size;
  37. int buffer_duration;
  38. } PulseData;
  39. static av_cold int pulse_write_header(AVFormatContext *h)
  40. {
  41. PulseData *s = h->priv_data;
  42. AVStream *st = NULL;
  43. int ret;
  44. pa_sample_spec ss;
  45. pa_buffer_attr attr = { -1, -1, -1, -1, -1 };
  46. const char *stream_name = s->stream_name;
  47. if (h->nb_streams != 1 || h->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
  48. av_log(s, AV_LOG_ERROR, "Only a single audio stream is supported.\n");
  49. return AVERROR(EINVAL);
  50. }
  51. st = h->streams[0];
  52. if (!stream_name) {
  53. if (h->filename[0])
  54. stream_name = h->filename;
  55. else
  56. stream_name = "Playback";
  57. }
  58. if (s->buffer_duration) {
  59. int64_t bytes = s->buffer_duration;
  60. bytes *= st->codec->channels * st->codec->sample_rate *
  61. av_get_bytes_per_sample(st->codec->sample_fmt);
  62. bytes /= 1000;
  63. attr.tlength = FFMAX(s->buffer_size, av_clip64(bytes, 0, UINT32_MAX - 1));
  64. av_log(s, AV_LOG_DEBUG,
  65. "Buffer duration: %ums recalculated into %"PRId64" bytes buffer.\n",
  66. s->buffer_duration, bytes);
  67. av_log(s, AV_LOG_DEBUG, "Real buffer length is %u bytes\n", attr.tlength);
  68. } else if (s->buffer_size)
  69. attr.tlength = s->buffer_size;
  70. ss.format = ff_codec_id_to_pulse_format(st->codec->codec_id);
  71. ss.rate = st->codec->sample_rate;
  72. ss.channels = st->codec->channels;
  73. s->pa = pa_simple_new(s->server, // Server
  74. s->name, // Application name
  75. PA_STREAM_PLAYBACK,
  76. s->device, // Device
  77. stream_name, // Description of a stream
  78. &ss, // Sample format
  79. NULL, // Use default channel map
  80. &attr, // Buffering attributes
  81. &ret); // Result
  82. if (!s->pa) {
  83. av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n", pa_strerror(ret));
  84. return AVERROR(EIO);
  85. }
  86. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  87. return 0;
  88. }
  89. static av_cold int pulse_write_trailer(AVFormatContext *h)
  90. {
  91. PulseData *s = h->priv_data;
  92. pa_simple_flush(s->pa, NULL);
  93. pa_simple_free(s->pa);
  94. s->pa = NULL;
  95. return 0;
  96. }
  97. static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
  98. {
  99. PulseData *s = h->priv_data;
  100. int error;
  101. if (!pkt) {
  102. if (pa_simple_flush(s->pa, &error) < 0) {
  103. av_log(s, AV_LOG_ERROR, "pa_simple_flush failed: %s\n", pa_strerror(error));
  104. return AVERROR(EIO);
  105. }
  106. return 1;
  107. }
  108. if (pkt->dts != AV_NOPTS_VALUE)
  109. s->timestamp = pkt->dts;
  110. if (pkt->duration) {
  111. s->timestamp += pkt->duration;
  112. } else {
  113. AVStream *st = h->streams[0];
  114. AVCodecContext *codec_ctx = st->codec;
  115. AVRational r = { 1, codec_ctx->sample_rate };
  116. int64_t samples = pkt->size / (av_get_bytes_per_sample(codec_ctx->sample_fmt) * codec_ctx->channels);
  117. s->timestamp += av_rescale_q(samples, r, st->time_base);
  118. }
  119. if (pa_simple_write(s->pa, pkt->data, pkt->size, &error) < 0) {
  120. av_log(s, AV_LOG_ERROR, "pa_simple_write failed: %s\n", pa_strerror(error));
  121. return AVERROR(EIO);
  122. }
  123. return 0;
  124. }
  125. static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
  126. {
  127. PulseData *s = h->priv_data;
  128. pa_usec_t latency = pa_simple_get_latency(s->pa, NULL);
  129. *wall = av_gettime();
  130. *dts = s->timestamp - latency;
  131. }
  132. #define OFFSET(a) offsetof(PulseData, a)
  133. #define E AV_OPT_FLAG_ENCODING_PARAM
  134. static const AVOption options[] = {
  135. { "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  136. { "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, E },
  137. { "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  138. { "device", "set device name", OFFSET(device), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  139. { "buffer_size", "set buffer size in bytes", OFFSET(buffer_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  140. { "buffer_duration", "set buffer duration in millisecs", OFFSET(buffer_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  141. { NULL }
  142. };
  143. static const AVClass pulse_muxer_class = {
  144. .class_name = "Pulse muxer",
  145. .item_name = av_default_item_name,
  146. .option = options,
  147. .version = LIBAVUTIL_VERSION_INT,
  148. };
  149. AVOutputFormat ff_pulse_muxer = {
  150. .name = "pulse",
  151. .long_name = NULL_IF_CONFIG_SMALL("Pulse audio output"),
  152. .priv_data_size = sizeof(PulseData),
  153. .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
  154. .video_codec = AV_CODEC_ID_NONE,
  155. .write_header = pulse_write_header,
  156. .write_packet = pulse_write_packet,
  157. .write_trailer = pulse_write_trailer,
  158. .get_output_timestamp = pulse_get_output_timestamp,
  159. .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
  160. .priv_class = &pulse_muxer_class,
  161. };