oss_audio.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. char errbuff[128];
  45. if (is_output)
  46. audio_fd = avpriv_open(audio_device, O_WRONLY);
  47. else
  48. audio_fd = avpriv_open(audio_device, O_RDONLY);
  49. if (audio_fd < 0) {
  50. av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
  51. return AVERROR(EIO);
  52. }
  53. if (flip && *flip == '1') {
  54. s->flip_left = 1;
  55. }
  56. /* non blocking mode */
  57. if (!is_output) {
  58. if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) < 0) {
  59. av_log(s1, AV_LOG_WARNING, "%s: Could not enable non block mode (%s)\n", audio_device, strerror(errno));
  60. }
  61. }
  62. s->frame_size = OSS_AUDIO_BLOCK_SIZE;
  63. #define CHECK_IOCTL_ERROR(event) \
  64. if (err < 0) { \
  65. av_strerror(AVERROR(errno), errbuff, sizeof(errbuff)); \
  66. av_log(s1, AV_LOG_ERROR, #event ": %s\n", errbuff); \
  67. goto fail; \
  68. }
  69. /* select format : favour native format
  70. * We don't CHECK_IOCTL_ERROR here because even if failed OSS still may be
  71. * usable. If OSS is not usable the SNDCTL_DSP_SETFMTS later is going to
  72. * fail anyway. */
  73. err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
  74. if (err < 0) {
  75. av_log(s1, AV_LOG_WARNING, "SNDCTL_DSP_GETFMTS: %s\n", strerror(errno));
  76. }
  77. #if HAVE_BIGENDIAN
  78. if (tmp & AFMT_S16_BE) {
  79. tmp = AFMT_S16_BE;
  80. } else if (tmp & AFMT_S16_LE) {
  81. tmp = AFMT_S16_LE;
  82. } else {
  83. tmp = 0;
  84. }
  85. #else
  86. if (tmp & AFMT_S16_LE) {
  87. tmp = AFMT_S16_LE;
  88. } else if (tmp & AFMT_S16_BE) {
  89. tmp = AFMT_S16_BE;
  90. } else {
  91. tmp = 0;
  92. }
  93. #endif
  94. switch(tmp) {
  95. case AFMT_S16_LE:
  96. s->codec_id = AV_CODEC_ID_PCM_S16LE;
  97. break;
  98. case AFMT_S16_BE:
  99. s->codec_id = AV_CODEC_ID_PCM_S16BE;
  100. break;
  101. default:
  102. av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
  103. close(audio_fd);
  104. return AVERROR(EIO);
  105. }
  106. err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
  107. CHECK_IOCTL_ERROR(SNDCTL_DSP_SETFMTS)
  108. tmp = (s->channels == 2);
  109. err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
  110. CHECK_IOCTL_ERROR(SNDCTL_DSP_STEREO)
  111. tmp = s->sample_rate;
  112. err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
  113. CHECK_IOCTL_ERROR(SNDCTL_DSP_SPEED)
  114. s->sample_rate = tmp; /* store real sample rate */
  115. s->fd = audio_fd;
  116. return 0;
  117. fail:
  118. close(audio_fd);
  119. return AVERROR(EIO);
  120. #undef CHECK_IOCTL_ERROR
  121. }
  122. int ff_oss_audio_close(OSSAudioData *s)
  123. {
  124. close(s->fd);
  125. return 0;
  126. }