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.

142 lines
3.8KB

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