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.

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