alsa-audio-common.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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: 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_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. static void alsa_reorder_s16_out_51(const void *in_v, void *out_v, int n)
  42. {
  43. const int16_t *in = in_v;
  44. int16_t *out = out_v;
  45. while (n-- > 0) {
  46. out[0] = in[0];
  47. out[1] = in[1];
  48. out[2] = in[4];
  49. out[3] = in[5];
  50. out[4] = in[2];
  51. out[5] = in[3];
  52. in += 6;
  53. out += 6;
  54. }
  55. }
  56. static void alsa_reorder_s16_out_71(const void *in_v, void *out_v, int n)
  57. {
  58. const int16_t *in = in_v;
  59. int16_t *out = out_v;
  60. while (n-- > 0) {
  61. out[0] = in[0];
  62. out[1] = in[1];
  63. out[2] = in[4];
  64. out[3] = in[5];
  65. out[4] = in[2];
  66. out[5] = in[3];
  67. out[6] = in[6];
  68. out[7] = in[7];
  69. in += 8;
  70. out += 8;
  71. }
  72. }
  73. #define REORDER_DUMMY ((void *)1)
  74. static av_cold ff_reorder_func find_reorder_func(int codec_id,
  75. int64_t layout,
  76. int out)
  77. {
  78. return
  79. codec_id == CODEC_ID_PCM_S16LE || codec_id == CODEC_ID_PCM_S16BE ?
  80. layout == AV_CH_LAYOUT_QUAD ? REORDER_DUMMY :
  81. layout == AV_CH_LAYOUT_5POINT1_BACK ?
  82. out ? alsa_reorder_s16_out_51 : NULL :
  83. layout == AV_CH_LAYOUT_7POINT1 ?
  84. out ? alsa_reorder_s16_out_71 : NULL :
  85. NULL :
  86. NULL;
  87. }
  88. av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
  89. unsigned int *sample_rate,
  90. int channels, enum CodecID *codec_id)
  91. {
  92. AlsaData *s = ctx->priv_data;
  93. const char *audio_device;
  94. int res, flags = 0;
  95. snd_pcm_format_t format;
  96. snd_pcm_t *h;
  97. snd_pcm_hw_params_t *hw_params;
  98. snd_pcm_uframes_t buffer_size, period_size;
  99. int64_t layout = ctx->streams[0]->codec->channel_layout;
  100. if (ctx->filename[0] == 0) audio_device = "default";
  101. else audio_device = ctx->filename;
  102. if (*codec_id == CODEC_ID_NONE)
  103. *codec_id = DEFAULT_CODEC_ID;
  104. format = codec_id_to_pcm_format(*codec_id);
  105. if (format == SND_PCM_FORMAT_UNKNOWN) {
  106. av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
  107. return AVERROR(ENOSYS);
  108. }
  109. s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
  110. if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
  111. flags = SND_PCM_NONBLOCK;
  112. }
  113. res = snd_pcm_open(&h, audio_device, mode, flags);
  114. if (res < 0) {
  115. av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
  116. audio_device, snd_strerror(res));
  117. return AVERROR(EIO);
  118. }
  119. res = snd_pcm_hw_params_malloc(&hw_params);
  120. if (res < 0) {
  121. av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
  122. snd_strerror(res));
  123. goto fail1;
  124. }
  125. res = snd_pcm_hw_params_any(h, hw_params);
  126. if (res < 0) {
  127. av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
  128. snd_strerror(res));
  129. goto fail;
  130. }
  131. res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
  132. if (res < 0) {
  133. av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
  134. snd_strerror(res));
  135. goto fail;
  136. }
  137. res = snd_pcm_hw_params_set_format(h, hw_params, format);
  138. if (res < 0) {
  139. av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
  140. *codec_id, format, snd_strerror(res));
  141. goto fail;
  142. }
  143. res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
  144. if (res < 0) {
  145. av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
  146. snd_strerror(res));
  147. goto fail;
  148. }
  149. res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
  150. if (res < 0) {
  151. av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
  152. channels, snd_strerror(res));
  153. goto fail;
  154. }
  155. snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
  156. /* TODO: maybe use ctx->max_picture_buffer somehow */
  157. res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
  158. if (res < 0) {
  159. av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
  160. snd_strerror(res));
  161. goto fail;
  162. }
  163. snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
  164. res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
  165. if (res < 0) {
  166. av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
  167. snd_strerror(res));
  168. goto fail;
  169. }
  170. s->period_size = period_size;
  171. res = snd_pcm_hw_params(h, hw_params);
  172. if (res < 0) {
  173. av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
  174. snd_strerror(res));
  175. goto fail;
  176. }
  177. snd_pcm_hw_params_free(hw_params);
  178. if (channels > 2 && layout) {
  179. s->reorder_func = find_reorder_func(*codec_id, layout,
  180. mode == SND_PCM_STREAM_PLAYBACK);
  181. if (s->reorder_func == REORDER_DUMMY) {
  182. s->reorder_func = NULL;
  183. } else if (s->reorder_func) {
  184. s->reorder_buf_size = buffer_size;
  185. s->reorder_buf = av_malloc(s->reorder_buf_size * s->frame_size);
  186. if (!s->reorder_buf)
  187. goto fail1;
  188. } else {
  189. char name[16];
  190. av_get_channel_layout_string(name, sizeof(name), channels, layout);
  191. av_log(ctx, AV_LOG_WARNING,
  192. "ALSA channel layout unknown or unimplemented for %s %s.\n",
  193. name,
  194. mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
  195. }
  196. }
  197. s->h = h;
  198. return 0;
  199. fail:
  200. snd_pcm_hw_params_free(hw_params);
  201. fail1:
  202. snd_pcm_close(h);
  203. return AVERROR(EIO);
  204. }
  205. av_cold int ff_alsa_close(AVFormatContext *s1)
  206. {
  207. AlsaData *s = s1->priv_data;
  208. av_freep(s->reorder_buf);
  209. snd_pcm_close(s->h);
  210. return 0;
  211. }
  212. int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
  213. {
  214. AlsaData *s = s1->priv_data;
  215. snd_pcm_t *handle = s->h;
  216. av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
  217. if (err == -EPIPE) {
  218. err = snd_pcm_prepare(handle);
  219. if (err < 0) {
  220. av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
  221. return AVERROR(EIO);
  222. }
  223. } else if (err == -ESTRPIPE) {
  224. av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
  225. return -1;
  226. }
  227. return err;
  228. }
  229. int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
  230. {
  231. int size = s->reorder_buf_size;
  232. void *r;
  233. while (size < min_size)
  234. size *= 2;
  235. r = av_realloc(s->reorder_buf, size * s->frame_size);
  236. if (!r)
  237. return AVERROR(ENOMEM);
  238. s->reorder_buf = r;
  239. s->reorder_buf_size = size;
  240. return 0;
  241. }