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.

519 lines
16KB

  1. /*
  2. * ALSA input
  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
  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 "libavutil/avassert.h"
  31. #include "libavutil/channel_layout.h"
  32. #include "libavutil/opt.h"
  33. #include "libavformat/avformat.h"
  34. #include "libavformat/internal.h"
  35. /* XXX: we make the assumption that the soundcard accepts this format */
  36. /* XXX: find better solution with "preinit" method, needed also in
  37. other formats */
  38. #define DEFAULT_CODEC_ID AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE)
  39. #define ALSA_BUFFER_SIZE_MAX 32768
  40. typedef struct AlsaData {
  41. AVClass *class;
  42. snd_pcm_t *h;
  43. int frame_size; ///< preferred size for reads and writes
  44. int period_size; ///< bytes per sample * channels
  45. int sample_rate; ///< sample rate set by user
  46. int channels; ///< number of channels set by user
  47. void (*reorder_func)(const void *, void *, int);
  48. void *reorder_buf;
  49. int reorder_buf_size; ///< in frames
  50. } AlsaData;
  51. static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
  52. {
  53. switch(codec_id) {
  54. case AV_CODEC_ID_PCM_F64LE: return SND_PCM_FORMAT_FLOAT64_LE;
  55. case AV_CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE;
  56. case AV_CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
  57. case AV_CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
  58. case AV_CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
  59. case AV_CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
  60. case AV_CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE;
  61. case AV_CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE;
  62. case AV_CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
  63. case AV_CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
  64. case AV_CODEC_ID_PCM_U24LE: return SND_PCM_FORMAT_U24_3LE;
  65. case AV_CODEC_ID_PCM_U24BE: return SND_PCM_FORMAT_U24_3BE;
  66. case AV_CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
  67. case AV_CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
  68. case AV_CODEC_ID_PCM_U16LE: return SND_PCM_FORMAT_U16_LE;
  69. case AV_CODEC_ID_PCM_U16BE: return SND_PCM_FORMAT_U16_BE;
  70. case AV_CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8;
  71. case AV_CODEC_ID_PCM_U8: return SND_PCM_FORMAT_U8;
  72. case AV_CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW;
  73. case AV_CODEC_ID_PCM_ALAW: return SND_PCM_FORMAT_A_LAW;
  74. default: return SND_PCM_FORMAT_UNKNOWN;
  75. }
  76. }
  77. #define REORDER_OUT_50(NAME, TYPE) \
  78. static void alsa_reorder_ ## NAME ## _out_50(const void *in_v, void *out_v, int n) \
  79. { \
  80. const TYPE *in = in_v; \
  81. TYPE *out = out_v; \
  82. \
  83. while (n-- > 0) { \
  84. out[0] = in[0]; \
  85. out[1] = in[1]; \
  86. out[2] = in[3]; \
  87. out[3] = in[4]; \
  88. out[4] = in[2]; \
  89. in += 5; \
  90. out += 5; \
  91. } \
  92. }
  93. #define REORDER_OUT_51(NAME, TYPE) \
  94. static void alsa_reorder_ ## NAME ## _out_51(const void *in_v, void *out_v, int n) \
  95. { \
  96. const TYPE *in = in_v; \
  97. TYPE *out = out_v; \
  98. \
  99. while (n-- > 0) { \
  100. out[0] = in[0]; \
  101. out[1] = in[1]; \
  102. out[2] = in[4]; \
  103. out[3] = in[5]; \
  104. out[4] = in[2]; \
  105. out[5] = in[3]; \
  106. in += 6; \
  107. out += 6; \
  108. } \
  109. }
  110. #define REORDER_OUT_71(NAME, TYPE) \
  111. static void alsa_reorder_ ## NAME ## _out_71(const void *in_v, void *out_v, int n) \
  112. { \
  113. const TYPE *in = in_v; \
  114. TYPE *out = out_v; \
  115. \
  116. while (n-- > 0) { \
  117. out[0] = in[0]; \
  118. out[1] = in[1]; \
  119. out[2] = in[4]; \
  120. out[3] = in[5]; \
  121. out[4] = in[2]; \
  122. out[5] = in[3]; \
  123. out[6] = in[6]; \
  124. out[7] = in[7]; \
  125. in += 8; \
  126. out += 8; \
  127. } \
  128. }
  129. REORDER_OUT_50(int8, int8_t)
  130. REORDER_OUT_51(int8, int8_t)
  131. REORDER_OUT_71(int8, int8_t)
  132. REORDER_OUT_50(int16, int16_t)
  133. REORDER_OUT_51(int16, int16_t)
  134. REORDER_OUT_71(int16, int16_t)
  135. REORDER_OUT_50(int32, int32_t)
  136. REORDER_OUT_51(int32, int32_t)
  137. REORDER_OUT_71(int32, int32_t)
  138. REORDER_OUT_50(f32, float)
  139. REORDER_OUT_51(f32, float)
  140. REORDER_OUT_71(f32, float)
  141. #define FORMAT_I8 0
  142. #define FORMAT_I16 1
  143. #define FORMAT_I32 2
  144. #define FORMAT_F32 3
  145. #define PICK_REORDER(layout)\
  146. switch(format) {\
  147. case FORMAT_I8: s->reorder_func = alsa_reorder_int8_out_ ##layout; break;\
  148. case FORMAT_I16: s->reorder_func = alsa_reorder_int16_out_ ##layout; break;\
  149. case FORMAT_I32: s->reorder_func = alsa_reorder_int32_out_ ##layout; break;\
  150. case FORMAT_F32: s->reorder_func = alsa_reorder_f32_out_ ##layout; break;\
  151. }
  152. static av_cold int find_reorder_func(AlsaData *s, int codec_id, uint64_t layout, int out)
  153. {
  154. int format;
  155. /* reordering input is not currently supported */
  156. if (!out)
  157. return AVERROR(ENOSYS);
  158. /* reordering is not needed for QUAD or 2_2 layout */
  159. if (layout == AV_CH_LAYOUT_QUAD || layout == AV_CH_LAYOUT_2_2)
  160. return 0;
  161. switch (codec_id) {
  162. case AV_CODEC_ID_PCM_S8:
  163. case AV_CODEC_ID_PCM_U8:
  164. case AV_CODEC_ID_PCM_ALAW:
  165. case AV_CODEC_ID_PCM_MULAW: format = FORMAT_I8; break;
  166. case AV_CODEC_ID_PCM_S16LE:
  167. case AV_CODEC_ID_PCM_S16BE:
  168. case AV_CODEC_ID_PCM_U16LE:
  169. case AV_CODEC_ID_PCM_U16BE: format = FORMAT_I16; break;
  170. case AV_CODEC_ID_PCM_S32LE:
  171. case AV_CODEC_ID_PCM_S32BE:
  172. case AV_CODEC_ID_PCM_U32LE:
  173. case AV_CODEC_ID_PCM_U32BE: format = FORMAT_I32; break;
  174. case AV_CODEC_ID_PCM_F32LE:
  175. case AV_CODEC_ID_PCM_F32BE: format = FORMAT_F32; break;
  176. default: return AVERROR(ENOSYS);
  177. }
  178. if (layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0)
  179. PICK_REORDER(50)
  180. else if (layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1)
  181. PICK_REORDER(51)
  182. else if (layout == AV_CH_LAYOUT_7POINT1)
  183. PICK_REORDER(71)
  184. return s->reorder_func ? 0 : AVERROR(ENOSYS);
  185. }
  186. /**
  187. * Open an ALSA PCM.
  188. *
  189. * @param s media file handle
  190. * @param mode either SND_PCM_STREAM_CAPTURE or SND_PCM_STREAM_PLAYBACK
  191. * @param sample_rate in: requested sample rate;
  192. * out: actually selected sample rate
  193. * @param channels number of channels
  194. * @param codec_id in: requested AVCodecID or AV_CODEC_ID_NONE;
  195. * out: actually selected AVCodecID, changed only if
  196. * AV_CODEC_ID_NONE was requested
  197. *
  198. * @return 0 if OK, AVERROR_xxx on error
  199. */
  200. static av_cold int alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
  201. unsigned int *sample_rate,
  202. int channels, enum AVCodecID *codec_id)
  203. {
  204. AlsaData *s = ctx->priv_data;
  205. const char *audio_device;
  206. int res, flags = 0;
  207. snd_pcm_format_t format;
  208. snd_pcm_t *h;
  209. snd_pcm_hw_params_t *hw_params;
  210. snd_pcm_uframes_t buffer_size, period_size;
  211. uint64_t layout = ctx->streams[0]->codecpar->channel_layout;
  212. if (ctx->filename[0] == 0) audio_device = "default";
  213. else audio_device = ctx->filename;
  214. if (*codec_id == AV_CODEC_ID_NONE)
  215. *codec_id = DEFAULT_CODEC_ID;
  216. format = codec_id_to_pcm_format(*codec_id);
  217. if (format == SND_PCM_FORMAT_UNKNOWN) {
  218. av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
  219. return AVERROR(ENOSYS);
  220. }
  221. s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
  222. if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
  223. flags = SND_PCM_NONBLOCK;
  224. }
  225. res = snd_pcm_open(&h, audio_device, mode, flags);
  226. if (res < 0) {
  227. av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
  228. audio_device, snd_strerror(res));
  229. return AVERROR(EIO);
  230. }
  231. res = snd_pcm_hw_params_malloc(&hw_params);
  232. if (res < 0) {
  233. av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
  234. snd_strerror(res));
  235. goto fail1;
  236. }
  237. res = snd_pcm_hw_params_any(h, hw_params);
  238. if (res < 0) {
  239. av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
  240. snd_strerror(res));
  241. goto fail;
  242. }
  243. res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
  244. if (res < 0) {
  245. av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
  246. snd_strerror(res));
  247. goto fail;
  248. }
  249. res = snd_pcm_hw_params_set_format(h, hw_params, format);
  250. if (res < 0) {
  251. av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
  252. *codec_id, format, snd_strerror(res));
  253. goto fail;
  254. }
  255. res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
  256. if (res < 0) {
  257. av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
  258. snd_strerror(res));
  259. goto fail;
  260. }
  261. res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
  262. if (res < 0) {
  263. av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
  264. channels, snd_strerror(res));
  265. goto fail;
  266. }
  267. snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
  268. buffer_size = FFMIN(buffer_size, ALSA_BUFFER_SIZE_MAX);
  269. /* TODO: maybe use ctx->max_picture_buffer somehow */
  270. res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
  271. if (res < 0) {
  272. av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
  273. snd_strerror(res));
  274. goto fail;
  275. }
  276. snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
  277. if (!period_size)
  278. period_size = buffer_size / 4;
  279. res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
  280. if (res < 0) {
  281. av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
  282. snd_strerror(res));
  283. goto fail;
  284. }
  285. s->period_size = period_size;
  286. res = snd_pcm_hw_params(h, hw_params);
  287. if (res < 0) {
  288. av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
  289. snd_strerror(res));
  290. goto fail;
  291. }
  292. snd_pcm_hw_params_free(hw_params);
  293. if (channels > 2 && layout) {
  294. if (find_reorder_func(s, *codec_id, layout, mode == SND_PCM_STREAM_PLAYBACK) < 0) {
  295. char name[128];
  296. av_get_channel_layout_string(name, sizeof(name), channels, layout);
  297. av_log(ctx, AV_LOG_WARNING, "ALSA channel layout unknown or unimplemented for %s %s.\n",
  298. name, mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
  299. }
  300. if (s->reorder_func) {
  301. s->reorder_buf_size = buffer_size;
  302. s->reorder_buf = av_malloc(s->reorder_buf_size * s->frame_size);
  303. if (!s->reorder_buf)
  304. goto fail1;
  305. }
  306. }
  307. s->h = h;
  308. return 0;
  309. fail:
  310. snd_pcm_hw_params_free(hw_params);
  311. fail1:
  312. snd_pcm_close(h);
  313. return AVERROR(EIO);
  314. }
  315. /**
  316. * Close the ALSA PCM.
  317. *
  318. * @param s1 media file handle
  319. *
  320. * @return 0
  321. */
  322. static av_cold int alsa_close(AVFormatContext *s1)
  323. {
  324. AlsaData *s = s1->priv_data;
  325. av_freep(&s->reorder_buf);
  326. snd_pcm_close(s->h);
  327. return 0;
  328. }
  329. /**
  330. * Try to recover from ALSA buffer underrun.
  331. *
  332. * @param s1 media file handle
  333. * @param err error code reported by the previous ALSA call
  334. *
  335. * @return 0 if OK, AVERROR_xxx on error
  336. */
  337. static int alsa_xrun_recover(AVFormatContext *s1, int err)
  338. {
  339. AlsaData *s = s1->priv_data;
  340. snd_pcm_t *handle = s->h;
  341. av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
  342. if (err == -EPIPE) {
  343. err = snd_pcm_prepare(handle);
  344. if (err < 0) {
  345. av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
  346. return AVERROR(EIO);
  347. }
  348. } else if (err == -ESTRPIPE) {
  349. av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
  350. return -1;
  351. }
  352. return err;
  353. }
  354. static av_cold int audio_read_header(AVFormatContext *s1)
  355. {
  356. AlsaData *s = s1->priv_data;
  357. AVStream *st;
  358. int ret;
  359. enum AVCodecID codec_id;
  360. snd_pcm_sw_params_t *sw_params;
  361. st = avformat_new_stream(s1, NULL);
  362. if (!st) {
  363. av_log(s1, AV_LOG_ERROR, "Cannot add stream\n");
  364. return AVERROR(ENOMEM);
  365. }
  366. codec_id = s1->audio_codec_id;
  367. ret = alsa_open(s1, SND_PCM_STREAM_CAPTURE, &s->sample_rate, s->channels,
  368. &codec_id);
  369. if (ret < 0) {
  370. return AVERROR(EIO);
  371. }
  372. if (snd_pcm_type(s->h) != SND_PCM_TYPE_HW)
  373. av_log(s1, AV_LOG_WARNING,
  374. "capture with some ALSA plugins, especially dsnoop, "
  375. "may hang.\n");
  376. ret = snd_pcm_sw_params_malloc(&sw_params);
  377. if (ret < 0) {
  378. av_log(s1, AV_LOG_ERROR, "cannot allocate software parameters structure (%s)\n",
  379. snd_strerror(ret));
  380. goto fail;
  381. }
  382. snd_pcm_sw_params_current(s->h, sw_params);
  383. snd_pcm_sw_params_set_tstamp_mode(s->h, sw_params, SND_PCM_TSTAMP_ENABLE);
  384. ret = snd_pcm_sw_params(s->h, sw_params);
  385. snd_pcm_sw_params_free(sw_params);
  386. if (ret < 0) {
  387. av_log(s1, AV_LOG_ERROR, "cannot install ALSA software parameters (%s)\n",
  388. snd_strerror(ret));
  389. goto fail;
  390. }
  391. /* take real parameters */
  392. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  393. st->codecpar->codec_id = codec_id;
  394. st->codecpar->sample_rate = s->sample_rate;
  395. st->codecpar->channels = s->channels;
  396. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  397. return 0;
  398. fail:
  399. snd_pcm_close(s->h);
  400. return AVERROR(EIO);
  401. }
  402. static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
  403. {
  404. AlsaData *s = s1->priv_data;
  405. AVStream *st = s1->streams[0];
  406. int res;
  407. snd_htimestamp_t timestamp;
  408. snd_pcm_uframes_t ts_delay;
  409. if (av_new_packet(pkt, s->period_size) < 0) {
  410. return AVERROR(EIO);
  411. }
  412. while ((res = snd_pcm_readi(s->h, pkt->data, pkt->size / s->frame_size)) < 0) {
  413. if (res == -EAGAIN) {
  414. av_packet_unref(pkt);
  415. return AVERROR(EAGAIN);
  416. }
  417. if (alsa_xrun_recover(s1, res) < 0) {
  418. av_log(s1, AV_LOG_ERROR, "ALSA read error: %s\n",
  419. snd_strerror(res));
  420. av_packet_unref(pkt);
  421. return AVERROR(EIO);
  422. }
  423. }
  424. snd_pcm_htimestamp(s->h, &ts_delay, &timestamp);
  425. ts_delay += res;
  426. pkt->pts = timestamp.tv_sec * 1000000LL
  427. + (timestamp.tv_nsec * st->codecpar->sample_rate
  428. - (int64_t)ts_delay * 1000000000LL + st->codecpar->sample_rate * 500LL)
  429. / (st->codecpar->sample_rate * 1000LL);
  430. pkt->size = res * s->frame_size;
  431. return 0;
  432. }
  433. static const AVOption options[] = {
  434. { "sample_rate", "", offsetof(AlsaData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  435. { "channels", "", offsetof(AlsaData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  436. { NULL },
  437. };
  438. static const AVClass alsa_demuxer_class = {
  439. .class_name = "ALSA demuxer",
  440. .item_name = av_default_item_name,
  441. .option = options,
  442. .version = LIBAVUTIL_VERSION_INT,
  443. };
  444. AVInputFormat ff_alsa_demuxer = {
  445. .name = "alsa",
  446. .long_name = NULL_IF_CONFIG_SMALL("ALSA audio input"),
  447. .priv_data_size = sizeof(AlsaData),
  448. .read_header = audio_read_header,
  449. .read_packet = audio_read_packet,
  450. .read_close = alsa_close,
  451. .flags = AVFMT_NOFILE,
  452. .priv_class = &alsa_demuxer_class,
  453. };