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.

166 lines
5.3KB

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