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.

134 lines
3.7KB

  1. /*
  2. * Linux audio play and grab interface
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "config.h"
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/soundcard.h>
  27. #include "libavutil/log.h"
  28. #include "libavcodec/avcodec.h"
  29. #include "libavformat/avformat.h"
  30. #include "oss.h"
  31. int ff_oss_audio_open(AVFormatContext *s1, int is_output,
  32. const char *audio_device)
  33. {
  34. OSSAudioData *s = s1->priv_data;
  35. int audio_fd;
  36. int tmp, err;
  37. char *flip = getenv("AUDIO_FLIP_LEFT");
  38. char errbuff[128];
  39. if (is_output)
  40. audio_fd = avpriv_open(audio_device, O_WRONLY);
  41. else
  42. audio_fd = avpriv_open(audio_device, O_RDONLY);
  43. if (audio_fd < 0) {
  44. av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
  45. return AVERROR(EIO);
  46. }
  47. if (flip && *flip == '1') {
  48. s->flip_left = 1;
  49. }
  50. /* non blocking mode */
  51. if (!is_output)
  52. fcntl(audio_fd, F_SETFL, O_NONBLOCK);
  53. s->frame_size = OSS_AUDIO_BLOCK_SIZE;
  54. #define CHECK_IOCTL_ERROR(event) \
  55. if (err < 0) { \
  56. av_strerror(AVERROR(errno), errbuff, sizeof(errbuff)); \
  57. av_log(s1, AV_LOG_ERROR, #event ": %s\n", errbuff); \
  58. goto fail; \
  59. }
  60. /* select format : favour native format
  61. * We don't CHECK_IOCTL_ERROR here because even if failed OSS still may be
  62. * usable. If OSS is not usable the SNDCTL_DSP_SETFMTS later is going to
  63. * fail anyway. */
  64. (void) ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
  65. #if HAVE_BIGENDIAN
  66. if (tmp & AFMT_S16_BE) {
  67. tmp = AFMT_S16_BE;
  68. } else if (tmp & AFMT_S16_LE) {
  69. tmp = AFMT_S16_LE;
  70. } else {
  71. tmp = 0;
  72. }
  73. #else
  74. if (tmp & AFMT_S16_LE) {
  75. tmp = AFMT_S16_LE;
  76. } else if (tmp & AFMT_S16_BE) {
  77. tmp = AFMT_S16_BE;
  78. } else {
  79. tmp = 0;
  80. }
  81. #endif
  82. switch(tmp) {
  83. case AFMT_S16_LE:
  84. s->codec_id = AV_CODEC_ID_PCM_S16LE;
  85. break;
  86. case AFMT_S16_BE:
  87. s->codec_id = AV_CODEC_ID_PCM_S16BE;
  88. break;
  89. default:
  90. av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
  91. close(audio_fd);
  92. return AVERROR(EIO);
  93. }
  94. err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
  95. CHECK_IOCTL_ERROR(SNDCTL_DSP_SETFMTS)
  96. tmp = (s->channels == 2);
  97. err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
  98. CHECK_IOCTL_ERROR(SNDCTL_DSP_STEREO)
  99. tmp = s->sample_rate;
  100. err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
  101. CHECK_IOCTL_ERROR(SNDCTL_DSP_SPEED)
  102. s->sample_rate = tmp; /* store real sample rate */
  103. s->fd = audio_fd;
  104. return 0;
  105. fail:
  106. close(audio_fd);
  107. return AVERROR(EIO);
  108. #undef CHECK_IOCTL_ERROR
  109. }
  110. int ff_oss_audio_close(OSSAudioData *s)
  111. {
  112. close(s->fd);
  113. return 0;
  114. }