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.

118 lines
3.5KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 allows to 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/internal.h"
  40. #include "libavformat/avformat.h"
  41. #include "alsa.h"
  42. static av_cold int audio_write_header(AVFormatContext *s1)
  43. {
  44. AlsaData *s = s1->priv_data;
  45. AVStream *st;
  46. unsigned int sample_rate;
  47. enum AVCodecID codec_id;
  48. int res;
  49. st = s1->streams[0];
  50. sample_rate = st->codecpar->sample_rate;
  51. codec_id = st->codecpar->codec_id;
  52. res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
  53. st->codecpar->channels, &codec_id);
  54. if (sample_rate != st->codecpar->sample_rate) {
  55. av_log(s1, AV_LOG_ERROR,
  56. "sample rate %d not available, nearest is %d\n",
  57. st->codecpar->sample_rate, sample_rate);
  58. goto fail;
  59. }
  60. return res;
  61. fail:
  62. snd_pcm_close(s->h);
  63. return AVERROR(EIO);
  64. }
  65. static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
  66. {
  67. AlsaData *s = s1->priv_data;
  68. int res;
  69. int size = pkt->size;
  70. uint8_t *buf = pkt->data;
  71. size /= s->frame_size;
  72. if (s->reorder_func) {
  73. if (size > s->reorder_buf_size)
  74. if (ff_alsa_extend_reorder_buf(s, size))
  75. return AVERROR(ENOMEM);
  76. s->reorder_func(buf, s->reorder_buf, size);
  77. buf = s->reorder_buf;
  78. }
  79. while ((res = snd_pcm_writei(s->h, buf, size)) < 0) {
  80. if (res == -EAGAIN) {
  81. return AVERROR(EAGAIN);
  82. }
  83. if (ff_alsa_xrun_recover(s1, res) < 0) {
  84. av_log(s1, AV_LOG_ERROR, "ALSA write error: %s\n",
  85. snd_strerror(res));
  86. return AVERROR(EIO);
  87. }
  88. }
  89. return 0;
  90. }
  91. AVOutputFormat ff_alsa_muxer = {
  92. .name = "alsa",
  93. .long_name = NULL_IF_CONFIG_SMALL("ALSA audio output"),
  94. .priv_data_size = sizeof(AlsaData),
  95. .audio_codec = DEFAULT_CODEC_ID,
  96. .video_codec = AV_CODEC_ID_NONE,
  97. .write_header = audio_write_header,
  98. .write_packet = audio_write_packet,
  99. .write_trailer = ff_alsa_close,
  100. .flags = AVFMT_NOFILE,
  101. };