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.

204 lines
6.8KB

  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: 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 <alsa/asoundlib.h>
  30. #include "libavformat/avformat.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_F64LE: return SND_PCM_FORMAT_FLOAT64_LE;
  36. case CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE;
  37. case CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
  38. case CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
  39. case CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
  40. case CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
  41. case CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE;
  42. case CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE;
  43. case CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
  44. case CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
  45. case CODEC_ID_PCM_U24LE: return SND_PCM_FORMAT_U24_3LE;
  46. case CODEC_ID_PCM_U24BE: return SND_PCM_FORMAT_U24_3BE;
  47. case CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
  48. case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
  49. case CODEC_ID_PCM_U16LE: return SND_PCM_FORMAT_U16_LE;
  50. case CODEC_ID_PCM_U16BE: return SND_PCM_FORMAT_U16_BE;
  51. case CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8;
  52. case CODEC_ID_PCM_U8: return SND_PCM_FORMAT_U8;
  53. case CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW;
  54. case CODEC_ID_PCM_ALAW: return SND_PCM_FORMAT_A_LAW;
  55. default: return SND_PCM_FORMAT_UNKNOWN;
  56. }
  57. }
  58. av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
  59. unsigned int *sample_rate,
  60. int channels, enum CodecID *codec_id)
  61. {
  62. AlsaData *s = ctx->priv_data;
  63. const char *audio_device;
  64. int res, flags = 0;
  65. snd_pcm_format_t format;
  66. snd_pcm_t *h;
  67. snd_pcm_hw_params_t *hw_params;
  68. snd_pcm_uframes_t buffer_size, period_size;
  69. if (ctx->filename[0] == 0) audio_device = "default";
  70. else audio_device = ctx->filename;
  71. if (*codec_id == CODEC_ID_NONE)
  72. *codec_id = DEFAULT_CODEC_ID;
  73. format = codec_id_to_pcm_format(*codec_id);
  74. if (format == SND_PCM_FORMAT_UNKNOWN) {
  75. av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
  76. return AVERROR(ENOSYS);
  77. }
  78. s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
  79. if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
  80. flags = SND_PCM_NONBLOCK;
  81. }
  82. res = snd_pcm_open(&h, audio_device, mode, flags);
  83. if (res < 0) {
  84. av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
  85. audio_device, snd_strerror(res));
  86. return AVERROR(EIO);
  87. }
  88. res = snd_pcm_hw_params_malloc(&hw_params);
  89. if (res < 0) {
  90. av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
  91. snd_strerror(res));
  92. goto fail1;
  93. }
  94. res = snd_pcm_hw_params_any(h, hw_params);
  95. if (res < 0) {
  96. av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
  97. snd_strerror(res));
  98. goto fail;
  99. }
  100. res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
  101. if (res < 0) {
  102. av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
  103. snd_strerror(res));
  104. goto fail;
  105. }
  106. res = snd_pcm_hw_params_set_format(h, hw_params, format);
  107. if (res < 0) {
  108. av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
  109. *codec_id, format, snd_strerror(res));
  110. goto fail;
  111. }
  112. res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
  113. if (res < 0) {
  114. av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
  115. snd_strerror(res));
  116. goto fail;
  117. }
  118. res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
  119. if (res < 0) {
  120. av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
  121. channels, snd_strerror(res));
  122. goto fail;
  123. }
  124. snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
  125. /* TODO: maybe use ctx->max_picture_buffer somehow */
  126. res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
  127. if (res < 0) {
  128. av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
  129. snd_strerror(res));
  130. goto fail;
  131. }
  132. snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
  133. res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
  134. if (res < 0) {
  135. av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
  136. snd_strerror(res));
  137. goto fail;
  138. }
  139. s->period_size = period_size;
  140. res = snd_pcm_hw_params(h, hw_params);
  141. if (res < 0) {
  142. av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
  143. snd_strerror(res));
  144. goto fail;
  145. }
  146. snd_pcm_hw_params_free(hw_params);
  147. s->h = h;
  148. return 0;
  149. fail:
  150. snd_pcm_hw_params_free(hw_params);
  151. fail1:
  152. snd_pcm_close(h);
  153. return AVERROR(EIO);
  154. }
  155. av_cold int ff_alsa_close(AVFormatContext *s1)
  156. {
  157. AlsaData *s = s1->priv_data;
  158. snd_pcm_close(s->h);
  159. return 0;
  160. }
  161. int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
  162. {
  163. AlsaData *s = s1->priv_data;
  164. snd_pcm_t *handle = s->h;
  165. av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
  166. if (err == -EPIPE) {
  167. err = snd_pcm_prepare(handle);
  168. if (err < 0) {
  169. av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
  170. return AVERROR(EIO);
  171. }
  172. } else if (err == -ESTRPIPE) {
  173. av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
  174. return -1;
  175. }
  176. return err;
  177. }