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.

155 lines
5.1KB

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