alsa.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 "config_components.h"
  30. #include <alsa/asoundlib.h>
  31. #include "avdevice.h"
  32. #include "libavutil/avassert.h"
  33. #include "libavutil/channel_layout.h"
  34. #include "alsa.h"
  35. static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
  36. {
  37. switch(codec_id) {
  38. case AV_CODEC_ID_PCM_F64LE: return SND_PCM_FORMAT_FLOAT64_LE;
  39. case AV_CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE;
  40. case AV_CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
  41. case AV_CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
  42. case AV_CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
  43. case AV_CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
  44. case AV_CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE;
  45. case AV_CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE;
  46. case AV_CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
  47. case AV_CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
  48. case AV_CODEC_ID_PCM_U24LE: return SND_PCM_FORMAT_U24_3LE;
  49. case AV_CODEC_ID_PCM_U24BE: return SND_PCM_FORMAT_U24_3BE;
  50. case AV_CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
  51. case AV_CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
  52. case AV_CODEC_ID_PCM_U16LE: return SND_PCM_FORMAT_U16_LE;
  53. case AV_CODEC_ID_PCM_U16BE: return SND_PCM_FORMAT_U16_BE;
  54. case AV_CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8;
  55. case AV_CODEC_ID_PCM_U8: return SND_PCM_FORMAT_U8;
  56. case AV_CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW;
  57. case AV_CODEC_ID_PCM_ALAW: return SND_PCM_FORMAT_A_LAW;
  58. default: return SND_PCM_FORMAT_UNKNOWN;
  59. }
  60. }
  61. #define MAKE_REORDER_FUNC(NAME, TYPE, CHANNELS, LAYOUT, MAP) \
  62. static void alsa_reorder_ ## NAME ## _ ## LAYOUT(const void *in_v, \
  63. void *out_v, \
  64. int n) \
  65. { \
  66. const TYPE *in = in_v; \
  67. TYPE *out = out_v; \
  68. \
  69. while (n-- > 0) { \
  70. MAP \
  71. in += CHANNELS; \
  72. out += CHANNELS; \
  73. } \
  74. }
  75. #define MAKE_REORDER_FUNCS(CHANNELS, LAYOUT, MAP) \
  76. MAKE_REORDER_FUNC(int8, int8_t, CHANNELS, LAYOUT, MAP) \
  77. MAKE_REORDER_FUNC(int16, int16_t, CHANNELS, LAYOUT, MAP) \
  78. MAKE_REORDER_FUNC(int32, int32_t, CHANNELS, LAYOUT, MAP) \
  79. MAKE_REORDER_FUNC(f32, float, CHANNELS, LAYOUT, MAP)
  80. MAKE_REORDER_FUNCS(5, out_50, \
  81. out[0] = in[0]; \
  82. out[1] = in[1]; \
  83. out[2] = in[3]; \
  84. out[3] = in[4]; \
  85. out[4] = in[2]; \
  86. )
  87. MAKE_REORDER_FUNCS(6, out_51, \
  88. out[0] = in[0]; \
  89. out[1] = in[1]; \
  90. out[2] = in[4]; \
  91. out[3] = in[5]; \
  92. out[4] = in[2]; \
  93. out[5] = in[3]; \
  94. )
  95. MAKE_REORDER_FUNCS(8, out_71, \
  96. out[0] = in[0]; \
  97. out[1] = in[1]; \
  98. out[2] = in[4]; \
  99. out[3] = in[5]; \
  100. out[4] = in[2]; \
  101. out[5] = in[3]; \
  102. out[6] = in[6]; \
  103. out[7] = in[7]; \
  104. )
  105. #define FORMAT_I8 0
  106. #define FORMAT_I16 1
  107. #define FORMAT_I32 2
  108. #define FORMAT_F32 3
  109. #define PICK_REORDER(layout)\
  110. switch(format) {\
  111. case FORMAT_I8: s->reorder_func = alsa_reorder_int8_out_ ##layout; break;\
  112. case FORMAT_I16: s->reorder_func = alsa_reorder_int16_out_ ##layout; break;\
  113. case FORMAT_I32: s->reorder_func = alsa_reorder_int32_out_ ##layout; break;\
  114. case FORMAT_F32: s->reorder_func = alsa_reorder_f32_out_ ##layout; break;\
  115. }
  116. static av_cold int find_reorder_func(AlsaData *s, int codec_id, AVChannelLayout *layout, int out)
  117. {
  118. int format;
  119. /* reordering input is not currently supported */
  120. if (!out)
  121. return AVERROR(ENOSYS);
  122. /* reordering is not needed for QUAD or 2_2 layout */
  123. if (!av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_QUAD) ||
  124. !av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_2_2))
  125. return 0;
  126. switch (codec_id) {
  127. case AV_CODEC_ID_PCM_S8:
  128. case AV_CODEC_ID_PCM_U8:
  129. case AV_CODEC_ID_PCM_ALAW:
  130. case AV_CODEC_ID_PCM_MULAW: format = FORMAT_I8; break;
  131. case AV_CODEC_ID_PCM_S16LE:
  132. case AV_CODEC_ID_PCM_S16BE:
  133. case AV_CODEC_ID_PCM_U16LE:
  134. case AV_CODEC_ID_PCM_U16BE: format = FORMAT_I16; break;
  135. case AV_CODEC_ID_PCM_S32LE:
  136. case AV_CODEC_ID_PCM_S32BE:
  137. case AV_CODEC_ID_PCM_U32LE:
  138. case AV_CODEC_ID_PCM_U32BE: format = FORMAT_I32; break;
  139. case AV_CODEC_ID_PCM_F32LE:
  140. case AV_CODEC_ID_PCM_F32BE: format = FORMAT_F32; break;
  141. default: return AVERROR(ENOSYS);
  142. }
  143. if (!av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0_BACK) ||
  144. !av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0))
  145. PICK_REORDER(50)
  146. else if (!av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK) ||
  147. !av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1))
  148. PICK_REORDER(51)
  149. else if (!av_channel_layout_compare(layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_7POINT1))
  150. PICK_REORDER(71)
  151. return s->reorder_func ? 0 : AVERROR(ENOSYS);
  152. }
  153. av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
  154. unsigned int *sample_rate,
  155. int channels, enum AVCodecID *codec_id)
  156. {
  157. AlsaData *s = ctx->priv_data;
  158. AVChannelLayout *layout = &ctx->streams[0]->codecpar->ch_layout;
  159. const char *audio_device;
  160. int res, flags = 0;
  161. snd_pcm_format_t format;
  162. snd_pcm_t *h;
  163. snd_pcm_hw_params_t *hw_params;
  164. snd_pcm_uframes_t buffer_size, period_size;
  165. if (ctx->url[0] == 0) audio_device = "default";
  166. else audio_device = ctx->url;
  167. if (*codec_id == AV_CODEC_ID_NONE)
  168. *codec_id = DEFAULT_CODEC_ID;
  169. format = codec_id_to_pcm_format(*codec_id);
  170. if (format == SND_PCM_FORMAT_UNKNOWN) {
  171. av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
  172. return AVERROR(ENOSYS);
  173. }
  174. s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
  175. if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
  176. flags = SND_PCM_NONBLOCK;
  177. }
  178. res = snd_pcm_open(&h, audio_device, mode, flags);
  179. if (res < 0) {
  180. av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
  181. audio_device, snd_strerror(res));
  182. return AVERROR(EIO);
  183. }
  184. res = snd_pcm_hw_params_malloc(&hw_params);
  185. if (res < 0) {
  186. av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
  187. snd_strerror(res));
  188. goto fail1;
  189. }
  190. res = snd_pcm_hw_params_any(h, hw_params);
  191. if (res < 0) {
  192. av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
  193. snd_strerror(res));
  194. goto fail;
  195. }
  196. res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
  197. if (res < 0) {
  198. av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
  199. snd_strerror(res));
  200. goto fail;
  201. }
  202. res = snd_pcm_hw_params_set_format(h, hw_params, format);
  203. if (res < 0) {
  204. av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
  205. *codec_id, format, snd_strerror(res));
  206. goto fail;
  207. }
  208. res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
  209. if (res < 0) {
  210. av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
  211. snd_strerror(res));
  212. goto fail;
  213. }
  214. res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
  215. if (res < 0) {
  216. av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
  217. channels, snd_strerror(res));
  218. goto fail;
  219. }
  220. snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
  221. buffer_size = FFMIN(buffer_size, ALSA_BUFFER_SIZE_MAX);
  222. /* TODO: maybe use ctx->max_picture_buffer somehow */
  223. res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
  224. if (res < 0) {
  225. av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
  226. snd_strerror(res));
  227. goto fail;
  228. }
  229. snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
  230. if (!period_size)
  231. period_size = buffer_size / 4;
  232. res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
  233. if (res < 0) {
  234. av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
  235. snd_strerror(res));
  236. goto fail;
  237. }
  238. s->period_size = period_size;
  239. res = snd_pcm_hw_params(h, hw_params);
  240. if (res < 0) {
  241. av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
  242. snd_strerror(res));
  243. goto fail;
  244. }
  245. snd_pcm_hw_params_free(hw_params);
  246. if (channels > 2 && layout->order != AV_CHANNEL_ORDER_UNSPEC) {
  247. if (find_reorder_func(s, *codec_id, layout, mode == SND_PCM_STREAM_PLAYBACK) < 0) {
  248. char name[128];
  249. av_channel_layout_describe(layout, name, sizeof(name));
  250. av_log(ctx, AV_LOG_WARNING, "ALSA channel layout unknown or unimplemented for %s %s.\n",
  251. name, mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
  252. }
  253. if (s->reorder_func) {
  254. s->reorder_buf_size = buffer_size;
  255. s->reorder_buf = av_malloc_array(s->reorder_buf_size, s->frame_size);
  256. if (!s->reorder_buf)
  257. goto fail1;
  258. }
  259. }
  260. s->pkt = av_packet_alloc();
  261. if (!s->pkt)
  262. goto fail1;
  263. s->h = h;
  264. return 0;
  265. fail:
  266. snd_pcm_hw_params_free(hw_params);
  267. fail1:
  268. snd_pcm_close(h);
  269. return AVERROR(EIO);
  270. }
  271. av_cold int ff_alsa_close(AVFormatContext *s1)
  272. {
  273. AlsaData *s = s1->priv_data;
  274. if (snd_pcm_stream(s->h) == SND_PCM_STREAM_PLAYBACK) {
  275. snd_pcm_nonblock(s->h, 0);
  276. snd_pcm_drain(s->h);
  277. }
  278. av_freep(&s->reorder_buf);
  279. if (CONFIG_ALSA_INDEV)
  280. ff_timefilter_destroy(s->timefilter);
  281. snd_pcm_close(s->h);
  282. av_packet_free(&s->pkt);
  283. return 0;
  284. }
  285. int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
  286. {
  287. AlsaData *s = s1->priv_data;
  288. snd_pcm_t *handle = s->h;
  289. av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
  290. if (err == -EPIPE) {
  291. err = snd_pcm_prepare(handle);
  292. if (err < 0) {
  293. av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
  294. return AVERROR(EIO);
  295. }
  296. } else if (err == -ESTRPIPE) {
  297. av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
  298. return -1;
  299. }
  300. return err;
  301. }
  302. int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
  303. {
  304. int size = s->reorder_buf_size;
  305. void *r;
  306. av_assert0(size != 0);
  307. while (size < min_size)
  308. size *= 2;
  309. r = av_realloc_array(s->reorder_buf, size, s->frame_size);
  310. if (!r)
  311. return AVERROR(ENOMEM);
  312. s->reorder_buf = r;
  313. s->reorder_buf_size = size;
  314. return 0;
  315. }
  316. /* ported from alsa-utils/aplay.c */
  317. int ff_alsa_get_device_list(AVDeviceInfoList *device_list, snd_pcm_stream_t stream_type)
  318. {
  319. int ret = 0;
  320. void **hints, **n;
  321. char *name = NULL, *descr = NULL, *io = NULL, *tmp;
  322. AVDeviceInfo *new_device = NULL;
  323. const char *filter = stream_type == SND_PCM_STREAM_PLAYBACK ? "Output" : "Input";
  324. if (snd_device_name_hint(-1, "pcm", &hints) < 0)
  325. return AVERROR_EXTERNAL;
  326. n = hints;
  327. while (*n && !ret) {
  328. name = snd_device_name_get_hint(*n, "NAME");
  329. descr = snd_device_name_get_hint(*n, "DESC");
  330. io = snd_device_name_get_hint(*n, "IOID");
  331. if (!io || !strcmp(io, filter)) {
  332. new_device = av_mallocz(sizeof(AVDeviceInfo));
  333. if (!new_device) {
  334. ret = AVERROR(ENOMEM);
  335. goto fail;
  336. }
  337. new_device->device_name = av_strdup(name);
  338. if ((tmp = strrchr(descr, '\n')) && tmp[1])
  339. new_device->device_description = av_strdup(&tmp[1]);
  340. else
  341. new_device->device_description = av_strdup(descr);
  342. if (!new_device->device_description || !new_device->device_name) {
  343. ret = AVERROR(ENOMEM);
  344. goto fail;
  345. }
  346. if ((ret = av_dynarray_add_nofree(&device_list->devices,
  347. &device_list->nb_devices, new_device)) < 0) {
  348. goto fail;
  349. }
  350. if (!strcmp(new_device->device_name, "default"))
  351. device_list->default_device = device_list->nb_devices - 1;
  352. new_device = NULL;
  353. }
  354. fail:
  355. free(io);
  356. free(name);
  357. free(descr);
  358. n++;
  359. }
  360. if (new_device) {
  361. av_free(new_device->device_description);
  362. av_free(new_device->device_name);
  363. av_free(new_device);
  364. }
  365. snd_device_name_free_hint(hints);
  366. return ret;
  367. }