oss.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #include <fcntl.h>
  27. #include <sys/ioctl.h>
  28. #include <sys/soundcard.h>
  29. #include "libavutil/log.h"
  30. #include "avdevice.h"
  31. #include "oss.h"
  32. int ff_oss_audio_open(AVFormatContext *s1, int is_output,
  33. const char *audio_device)
  34. {
  35. OSSAudioData *s = s1->priv_data;
  36. int audio_fd;
  37. int tmp, err;
  38. char *flip = getenv("AUDIO_FLIP_LEFT");
  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, av_err2str(AVERROR(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. if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) < 0) {
  53. av_log(s1, AV_LOG_WARNING, "%s: Could not enable non block mode (%s)\n", audio_device, av_err2str(AVERROR(errno)));
  54. }
  55. }
  56. s->frame_size = OSS_AUDIO_BLOCK_SIZE;
  57. #define CHECK_IOCTL_ERROR(event) \
  58. if (err < 0) { \
  59. av_log(s1, AV_LOG_ERROR, #event ": %s\n", av_err2str(AVERROR(errno)));\
  60. goto fail; \
  61. }
  62. /* select format : favour native format
  63. * We don't CHECK_IOCTL_ERROR here because even if failed OSS still may be
  64. * usable. If OSS is not usable the SNDCTL_DSP_SETFMTS later is going to
  65. * fail anyway. */
  66. err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
  67. if (err < 0) {
  68. av_log(s1, AV_LOG_WARNING, "SNDCTL_DSP_GETFMTS: %s\n", av_err2str(AVERROR(errno)));
  69. }
  70. #if HAVE_BIGENDIAN
  71. if (tmp & AFMT_S16_BE) {
  72. tmp = AFMT_S16_BE;
  73. } else if (tmp & AFMT_S16_LE) {
  74. tmp = AFMT_S16_LE;
  75. } else {
  76. tmp = 0;
  77. }
  78. #else
  79. if (tmp & AFMT_S16_LE) {
  80. tmp = AFMT_S16_LE;
  81. } else if (tmp & AFMT_S16_BE) {
  82. tmp = AFMT_S16_BE;
  83. } else {
  84. tmp = 0;
  85. }
  86. #endif
  87. switch(tmp) {
  88. case AFMT_S16_LE:
  89. s->codec_id = AV_CODEC_ID_PCM_S16LE;
  90. s->sample_size = 2;
  91. break;
  92. case AFMT_S16_BE:
  93. s->codec_id = AV_CODEC_ID_PCM_S16BE;
  94. s->sample_size = 2;
  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_SETFMT)
  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. }