alsa-audio-common.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 libavdevice/alsa-audio-common.c
  24. * ALSA input and output: common code
  25. * @author Luca Abeni ( lucabe72 email it )
  26. * @author Benoit Fouet ( benoit fouet free fr )
  27. * @author Nicolas George ( nicolas george normalesup org )
  28. */
  29. #include "libavformat/avformat.h"
  30. #include <alsa/asoundlib.h>
  31. #include "alsa-audio.h"
  32. static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
  33. {
  34. switch(codec_id) {
  35. case CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
  36. case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
  37. case CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8;
  38. default: return SND_PCM_FORMAT_UNKNOWN;
  39. }
  40. }
  41. av_cold int ff_alsa_open(AVFormatContext *ctx, int mode,
  42. unsigned int *sample_rate,
  43. int channels, int *codec_id)
  44. {
  45. AlsaData *s = ctx->priv_data;
  46. const char *audio_device;
  47. int res, flags = 0;
  48. snd_pcm_format_t format;
  49. snd_pcm_t *h;
  50. snd_pcm_hw_params_t *hw_params;
  51. snd_pcm_uframes_t buffer_size, period_size;
  52. if (ctx->filename[0] == 0) audio_device = "default";
  53. else audio_device = ctx->filename;
  54. if (*codec_id == CODEC_ID_NONE)
  55. *codec_id = DEFAULT_CODEC_ID;
  56. format = codec_id_to_pcm_format(*codec_id);
  57. if (format == SND_PCM_FORMAT_UNKNOWN) {
  58. av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
  59. return AVERROR(ENOSYS);
  60. }
  61. s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
  62. if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
  63. flags = O_NONBLOCK;
  64. }
  65. res = snd_pcm_open(&h, audio_device, mode, flags);
  66. if (res < 0) {
  67. av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
  68. audio_device, snd_strerror(res));
  69. return AVERROR_IO;
  70. }
  71. res = snd_pcm_hw_params_malloc(&hw_params);
  72. if (res < 0) {
  73. av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
  74. snd_strerror(res));
  75. goto fail1;
  76. }
  77. res = snd_pcm_hw_params_any(h, hw_params);
  78. if (res < 0) {
  79. av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
  80. snd_strerror(res));
  81. goto fail;
  82. }
  83. res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
  84. if (res < 0) {
  85. av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
  86. snd_strerror(res));
  87. goto fail;
  88. }
  89. res = snd_pcm_hw_params_set_format(h, hw_params, format);
  90. if (res < 0) {
  91. av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
  92. *codec_id, format, snd_strerror(res));
  93. goto fail;
  94. }
  95. res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
  96. if (res < 0) {
  97. av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
  98. snd_strerror(res));
  99. goto fail;
  100. }
  101. res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
  102. if (res < 0) {
  103. av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
  104. channels, snd_strerror(res));
  105. goto fail;
  106. }
  107. snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
  108. /* TODO: maybe use ctx->max_picture_buffer somehow */
  109. res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
  110. if (res < 0) {
  111. av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
  112. snd_strerror(res));
  113. goto fail;
  114. }
  115. snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
  116. res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
  117. if (res < 0) {
  118. av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
  119. snd_strerror(res));
  120. goto fail;
  121. }
  122. s->period_size = period_size;
  123. res = snd_pcm_hw_params(h, hw_params);
  124. if (res < 0) {
  125. av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
  126. snd_strerror(res));
  127. goto fail;
  128. }
  129. snd_pcm_hw_params_free(hw_params);
  130. s->h = h;
  131. return 0;
  132. fail:
  133. snd_pcm_hw_params_free(hw_params);
  134. fail1:
  135. snd_pcm_close(h);
  136. return AVERROR_IO;
  137. }
  138. av_cold int ff_alsa_close(AVFormatContext *s1)
  139. {
  140. AlsaData *s = s1->priv_data;
  141. snd_pcm_close(s->h);
  142. return 0;
  143. }
  144. int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
  145. {
  146. AlsaData *s = s1->priv_data;
  147. snd_pcm_t *handle = s->h;
  148. av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
  149. if (err == -EPIPE) {
  150. err = snd_pcm_prepare(handle);
  151. if (err < 0) {
  152. av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
  153. return AVERROR_IO;
  154. }
  155. } else if (err == -ESTRPIPE) {
  156. av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
  157. return -1;
  158. }
  159. return err;
  160. }