pulse_audio_enc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. } PulseData;
  37. static av_cold int pulse_write_header(AVFormatContext *h)
  38. {
  39. PulseData *s = h->priv_data;
  40. AVStream *st = NULL;
  41. int ret;
  42. pa_sample_spec ss;
  43. pa_buffer_attr attr = { -1, -1, -1, -1, -1 };
  44. const char *stream_name = s->stream_name;
  45. if (h->nb_streams != 1 || h->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
  46. av_log(s, AV_LOG_ERROR, "Only a single audio stream is supported.\n");
  47. return AVERROR(EINVAL);
  48. }
  49. st = h->streams[0];
  50. if (!stream_name) {
  51. if (h->filename[0])
  52. stream_name = h->filename;
  53. else
  54. stream_name = "Playback";
  55. }
  56. ss.format = codec_id_to_pulse_format(st->codec->codec_id);
  57. ss.rate = st->codec->sample_rate;
  58. ss.channels = st->codec->channels;
  59. s->pa = pa_simple_new(s->server, // Server
  60. s->name, // Application name
  61. PA_STREAM_PLAYBACK,
  62. s->device, // Device
  63. stream_name, // Description of a stream
  64. &ss, // Sample format
  65. NULL, // Use default channel map
  66. &attr, // Buffering attributes
  67. &ret); // Result
  68. if (!s->pa) {
  69. av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n", pa_strerror(ret));
  70. return AVERROR(EIO);
  71. }
  72. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  73. return 0;
  74. }
  75. static av_cold int pulse_write_trailer(AVFormatContext *h)
  76. {
  77. PulseData *s = h->priv_data;
  78. pa_simple_flush(s->pa, NULL);
  79. pa_simple_free(s->pa);
  80. s->pa = NULL;
  81. return 0;
  82. }
  83. static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
  84. {
  85. PulseData *s = h->priv_data;
  86. int error;
  87. if (!pkt) {
  88. if (pa_simple_flush(s->pa, &error) < 0) {
  89. av_log(s, AV_LOG_ERROR, "pa_simple_flush failed: %s\n", pa_strerror(error));
  90. return AVERROR(EIO);
  91. }
  92. return 0;
  93. }
  94. if (pkt->dts != AV_NOPTS_VALUE)
  95. s->timestamp = pkt->dts;
  96. if (pkt->duration) {
  97. s->timestamp += pkt->duration;
  98. } else {
  99. AVStream *st = h->streams[0];
  100. AVCodecContext *codec_ctx = st->codec;
  101. AVRational r = { 1, codec_ctx->sample_rate };
  102. int64_t samples = pkt->size / (av_get_bytes_per_sample(codec_ctx->sample_fmt) * codec_ctx->channels);
  103. s->timestamp += av_rescale_q(samples, r, st->time_base);
  104. }
  105. if (pa_simple_write(s->pa, pkt->data, pkt->size, &error) < 0) {
  106. av_log(s, AV_LOG_ERROR, "pa_simple_write failed: %s\n", pa_strerror(error));
  107. return AVERROR(EIO);
  108. }
  109. return 0;
  110. }
  111. static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
  112. {
  113. PulseData *s = h->priv_data;
  114. pa_usec_t latency = pa_simple_get_latency(s->pa, NULL);
  115. *wall = av_gettime();
  116. *dts = s->timestamp - latency;
  117. }
  118. #define OFFSET(a) offsetof(PulseData, a)
  119. #define E AV_OPT_FLAG_ENCODING_PARAM
  120. static const AVOption options[] = {
  121. { "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  122. { "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, E },
  123. { "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  124. { "device", "set device name", OFFSET(device), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  125. { NULL }
  126. };
  127. static const AVClass pulse_muxer_class = {
  128. .class_name = "Pulse muxer",
  129. .item_name = av_default_item_name,
  130. .option = options,
  131. .version = LIBAVUTIL_VERSION_INT,
  132. };
  133. AVOutputFormat ff_pulse_muxer = {
  134. .name = "pulse",
  135. .long_name = NULL_IF_CONFIG_SMALL("Pulse audio output"),
  136. .priv_data_size = sizeof(PulseData),
  137. .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
  138. .video_codec = AV_CODEC_ID_NONE,
  139. .write_header = pulse_write_header,
  140. .write_packet = pulse_write_packet,
  141. .write_trailer = pulse_write_trailer,
  142. .get_output_timestamp = pulse_get_output_timestamp,
  143. .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
  144. .priv_class = &pulse_muxer_class,
  145. };