alsa-audio-enc.c 3.0 KB

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