alsa_enc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * ALSA input and output
  3. * Copyright (c) 2007 Luca Abeni ( lucabe72 email it )
  4. * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr )
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * ALSA input and output: output
  25. * @author Luca Abeni ( lucabe72 email it )
  26. * @author Benoit Fouet ( benoit fouet free fr )
  27. *
  28. * This avdevice encoder can play audio to an ALSA (Advanced Linux
  29. * Sound Architecture) device.
  30. *
  31. * The filename parameter is the name of an ALSA PCM device capable of
  32. * capture, for example "default" or "plughw:1"; see the ALSA documentation
  33. * for naming conventions. The empty string is equivalent to "default".
  34. *
  35. * The playback period is set to the lower value available for the device,
  36. * which gives a low latency suitable for real-time playback.
  37. */
  38. #include <alsa/asoundlib.h>
  39. #include "libavutil/frame.h"
  40. #include "libavutil/internal.h"
  41. #include "libavutil/time.h"
  42. #include "libavformat/internal.h"
  43. #include "libavformat/mux.h"
  44. #include "avdevice.h"
  45. #include "alsa.h"
  46. static av_cold int audio_write_header(AVFormatContext *s1)
  47. {
  48. AlsaData *s = s1->priv_data;
  49. AVStream *st = NULL;
  50. unsigned int sample_rate;
  51. enum AVCodecID codec_id;
  52. int res;
  53. if (s1->nb_streams != 1 || s1->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
  54. av_log(s1, AV_LOG_ERROR, "Only a single audio stream is supported.\n");
  55. return AVERROR(EINVAL);
  56. }
  57. st = s1->streams[0];
  58. sample_rate = st->codecpar->sample_rate;
  59. codec_id = st->codecpar->codec_id;
  60. res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
  61. st->codecpar->ch_layout.nb_channels, &codec_id);
  62. if (sample_rate != st->codecpar->sample_rate) {
  63. av_log(s1, AV_LOG_ERROR,
  64. "sample rate %d not available, nearest is %d\n",
  65. st->codecpar->sample_rate, sample_rate);
  66. goto fail;
  67. }
  68. avpriv_set_pts_info(st, 64, 1, sample_rate);
  69. return res;
  70. fail:
  71. snd_pcm_close(s->h);
  72. return AVERROR(EIO);
  73. }
  74. static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
  75. {
  76. AlsaData *s = s1->priv_data;
  77. int res;
  78. int size = pkt->size;
  79. const uint8_t *buf = pkt->data;
  80. size /= s->frame_size;
  81. if (pkt->dts != AV_NOPTS_VALUE)
  82. s->timestamp = pkt->dts;
  83. s->timestamp += pkt->duration ? pkt->duration : size;
  84. if (s->reorder_func) {
  85. if (size > s->reorder_buf_size)
  86. if (ff_alsa_extend_reorder_buf(s, size))
  87. return AVERROR(ENOMEM);
  88. s->reorder_func(buf, s->reorder_buf, size);
  89. buf = s->reorder_buf;
  90. }
  91. while ((res = snd_pcm_writei(s->h, buf, size)) < 0) {
  92. if (res == -EAGAIN) {
  93. return AVERROR(EAGAIN);
  94. }
  95. if (ff_alsa_xrun_recover(s1, res) < 0) {
  96. av_log(s1, AV_LOG_ERROR, "ALSA write error: %s\n",
  97. snd_strerror(res));
  98. return AVERROR(EIO);
  99. }
  100. }
  101. return 0;
  102. }
  103. static int audio_write_frame(AVFormatContext *s1, int stream_index,
  104. AVFrame **frame, unsigned flags)
  105. {
  106. AlsaData *s = s1->priv_data;
  107. AVPacket pkt;
  108. /* ff_alsa_open() should have accepted only supported formats */
  109. if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
  110. return av_sample_fmt_is_planar(s1->streams[stream_index]->codecpar->format) ?
  111. AVERROR(EINVAL) : 0;
  112. /* set only used fields */
  113. pkt.data = (*frame)->data[0];
  114. pkt.size = (*frame)->nb_samples * s->frame_size;
  115. pkt.dts = (*frame)->pkt_dts;
  116. pkt.duration = (*frame)->duration;
  117. return audio_write_packet(s1, &pkt);
  118. }
  119. static void
  120. audio_get_output_timestamp(AVFormatContext *s1, int stream,
  121. int64_t *dts, int64_t *wall)
  122. {
  123. AlsaData *s = s1->priv_data;
  124. snd_pcm_sframes_t delay = 0;
  125. *wall = av_gettime();
  126. snd_pcm_delay(s->h, &delay);
  127. *dts = s->timestamp - delay;
  128. }
  129. static int audio_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
  130. {
  131. return ff_alsa_get_device_list(device_list, SND_PCM_STREAM_PLAYBACK);
  132. }
  133. static const AVClass alsa_muxer_class = {
  134. .class_name = "ALSA outdev",
  135. .item_name = av_default_item_name,
  136. .version = LIBAVUTIL_VERSION_INT,
  137. .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT,
  138. };
  139. const FFOutputFormat ff_alsa_muxer = {
  140. .p.name = "alsa",
  141. .p.long_name = NULL_IF_CONFIG_SMALL("ALSA audio output"),
  142. .priv_data_size = sizeof(AlsaData),
  143. .p.audio_codec = DEFAULT_CODEC_ID,
  144. .p.video_codec = AV_CODEC_ID_NONE,
  145. .write_header = audio_write_header,
  146. .write_packet = audio_write_packet,
  147. .write_trailer = ff_alsa_close,
  148. .write_uncoded_frame = audio_write_frame,
  149. .get_device_list = audio_get_device_list,
  150. .get_output_timestamp = audio_get_output_timestamp,
  151. .p.flags = AVFMT_NOFILE,
  152. .p.priv_class = &alsa_muxer_class,
  153. };