alsa.c 14 KB

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