sndio.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * sndio play and grab interface
  3. * Copyright (c) 2010 Jacob Meuser
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdint.h>
  22. #include <sndio.h>
  23. #include "avdevice.h"
  24. #include "libavdevice/sndio.h"
  25. static inline void movecb(void *addr, int delta)
  26. {
  27. SndioData *s = addr;
  28. s->hwpos += delta * s->channels * s->bps;
  29. }
  30. av_cold int ff_sndio_open(AVFormatContext *s1, int is_output,
  31. const char *audio_device)
  32. {
  33. SndioData *s = s1->priv_data;
  34. struct sio_hdl *hdl;
  35. struct sio_par par;
  36. hdl = sio_open(audio_device, is_output ? SIO_PLAY : SIO_REC, 0);
  37. if (!hdl) {
  38. av_log(s1, AV_LOG_ERROR, "Could not open sndio device\n");
  39. return AVERROR(EIO);
  40. }
  41. sio_initpar(&par);
  42. par.bits = 16;
  43. par.sig = 1;
  44. par.le = SIO_LE_NATIVE;
  45. if (is_output)
  46. par.pchan = s->channels;
  47. else
  48. par.rchan = s->channels;
  49. par.rate = s->sample_rate;
  50. if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
  51. av_log(s1, AV_LOG_ERROR, "Impossible to set sndio parameters, "
  52. "channels: %d sample rate: %d\n", s->channels, s->sample_rate);
  53. goto fail;
  54. }
  55. if (par.bits != 16 || par.sig != 1 ||
  56. (is_output && (par.pchan != s->channels)) ||
  57. (!is_output && (par.rchan != s->channels)) ||
  58. (par.rate != s->sample_rate)) {
  59. av_log(s1, AV_LOG_ERROR, "Could not set appropriate sndio parameters, "
  60. "channels: %d sample rate: %d\n", s->channels, s->sample_rate);
  61. goto fail;
  62. }
  63. s->buffer_size = par.round * par.bps *
  64. (is_output ? par.pchan : par.rchan);
  65. if (is_output) {
  66. s->buffer = av_malloc(s->buffer_size);
  67. if (!s->buffer) {
  68. av_log(s1, AV_LOG_ERROR, "Could not allocate buffer\n");
  69. goto fail;
  70. }
  71. }
  72. s->codec_id = par.le ? AV_CODEC_ID_PCM_S16LE : AV_CODEC_ID_PCM_S16BE;
  73. s->channels = is_output ? par.pchan : par.rchan;
  74. s->sample_rate = par.rate;
  75. s->bps = par.bps;
  76. sio_onmove(hdl, movecb, s);
  77. if (!sio_start(hdl)) {
  78. av_log(s1, AV_LOG_ERROR, "Could not start sndio\n");
  79. goto fail;
  80. }
  81. s->hdl = hdl;
  82. return 0;
  83. fail:
  84. av_freep(&s->buffer);
  85. if (hdl)
  86. sio_close(hdl);
  87. return AVERROR(EIO);
  88. }
  89. int ff_sndio_close(SndioData *s)
  90. {
  91. av_freep(&s->buffer);
  92. if (s->hdl)
  93. sio_close(s->hdl);
  94. return 0;
  95. }