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.

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