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.

244 lines
6.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 "libavutil/opt.h"
  29. #include "libavutil/time.h"
  30. #include "libavcodec/avcodec.h"
  31. #include "libavformat/avformat.h"
  32. #include "libavformat/internal.h"
  33. #define OSS_AUDIO_BLOCK_SIZE 4096
  34. typedef struct OSSAudioData {
  35. AVClass *class;
  36. int fd;
  37. int sample_rate;
  38. int channels;
  39. int frame_size; /* in bytes ! */
  40. enum AVCodecID codec_id;
  41. unsigned int flip_left : 1;
  42. uint8_t buffer[OSS_AUDIO_BLOCK_SIZE];
  43. int buffer_ptr;
  44. } OSSAudioData;
  45. static int oss_audio_open(AVFormatContext *s1, int is_output,
  46. const char *audio_device)
  47. {
  48. OSSAudioData *s = s1->priv_data;
  49. int audio_fd;
  50. int tmp, err;
  51. char *flip = getenv("AUDIO_FLIP_LEFT");
  52. char errbuff[128];
  53. if (is_output)
  54. audio_fd = avpriv_open(audio_device, O_WRONLY);
  55. else
  56. audio_fd = avpriv_open(audio_device, O_RDONLY);
  57. if (audio_fd < 0) {
  58. av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
  59. return AVERROR(EIO);
  60. }
  61. if (flip && *flip == '1') {
  62. s->flip_left = 1;
  63. }
  64. /* non blocking mode */
  65. if (!is_output)
  66. fcntl(audio_fd, F_SETFL, O_NONBLOCK);
  67. s->frame_size = OSS_AUDIO_BLOCK_SIZE;
  68. #define CHECK_IOCTL_ERROR(event) \
  69. if (err < 0) { \
  70. av_strerror(AVERROR(errno), errbuff, sizeof(errbuff)); \
  71. av_log(s1, AV_LOG_ERROR, #event ": %s\n", errbuff); \
  72. goto fail; \
  73. }
  74. /* select format : favour native format
  75. * We don't CHECK_IOCTL_ERROR here because even if failed OSS still may be
  76. * usable. If OSS is not usable the SNDCTL_DSP_SETFMTS later is going to
  77. * fail anyway. */
  78. (void) ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
  79. #if HAVE_BIGENDIAN
  80. if (tmp & AFMT_S16_BE) {
  81. tmp = AFMT_S16_BE;
  82. } else if (tmp & AFMT_S16_LE) {
  83. tmp = AFMT_S16_LE;
  84. } else {
  85. tmp = 0;
  86. }
  87. #else
  88. if (tmp & AFMT_S16_LE) {
  89. tmp = AFMT_S16_LE;
  90. } else if (tmp & AFMT_S16_BE) {
  91. tmp = AFMT_S16_BE;
  92. } else {
  93. tmp = 0;
  94. }
  95. #endif
  96. switch(tmp) {
  97. case AFMT_S16_LE:
  98. s->codec_id = AV_CODEC_ID_PCM_S16LE;
  99. break;
  100. case AFMT_S16_BE:
  101. s->codec_id = AV_CODEC_ID_PCM_S16BE;
  102. break;
  103. default:
  104. av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
  105. close(audio_fd);
  106. return AVERROR(EIO);
  107. }
  108. err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
  109. CHECK_IOCTL_ERROR(SNDCTL_DSP_SETFMTS)
  110. tmp = (s->channels == 2);
  111. err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
  112. CHECK_IOCTL_ERROR(SNDCTL_DSP_STEREO)
  113. tmp = s->sample_rate;
  114. err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
  115. CHECK_IOCTL_ERROR(SNDCTL_DSP_SPEED)
  116. s->sample_rate = tmp; /* store real sample rate */
  117. s->fd = audio_fd;
  118. return 0;
  119. fail:
  120. close(audio_fd);
  121. return AVERROR(EIO);
  122. #undef CHECK_IOCTL_ERROR
  123. }
  124. static int audio_read_header(AVFormatContext *s1)
  125. {
  126. OSSAudioData *s = s1->priv_data;
  127. AVStream *st;
  128. int ret;
  129. st = avformat_new_stream(s1, NULL);
  130. if (!st) {
  131. return AVERROR(ENOMEM);
  132. }
  133. ret = oss_audio_open(s1, 0, s1->filename);
  134. if (ret < 0) {
  135. return AVERROR(EIO);
  136. }
  137. /* take real parameters */
  138. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  139. st->codecpar->codec_id = s->codec_id;
  140. st->codecpar->sample_rate = s->sample_rate;
  141. st->codecpar->channels = s->channels;
  142. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  143. return 0;
  144. }
  145. static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
  146. {
  147. OSSAudioData *s = s1->priv_data;
  148. int ret, bdelay;
  149. int64_t cur_time;
  150. struct audio_buf_info abufi;
  151. if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
  152. return ret;
  153. ret = read(s->fd, pkt->data, pkt->size);
  154. if (ret <= 0){
  155. av_packet_unref(pkt);
  156. pkt->size = 0;
  157. if (ret<0) return AVERROR(errno);
  158. else return AVERROR_EOF;
  159. }
  160. pkt->size = ret;
  161. /* compute pts of the start of the packet */
  162. cur_time = av_gettime();
  163. bdelay = ret;
  164. if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
  165. bdelay += abufi.bytes;
  166. }
  167. /* subtract time represented by the number of bytes in the audio fifo */
  168. cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
  169. /* convert to wanted units */
  170. pkt->pts = cur_time;
  171. if (s->flip_left && s->channels == 2) {
  172. int i;
  173. short *p = (short *) pkt->data;
  174. for (i = 0; i < ret; i += 4) {
  175. *p = ~*p;
  176. p += 2;
  177. }
  178. }
  179. return 0;
  180. }
  181. static int audio_read_close(AVFormatContext *s1)
  182. {
  183. OSSAudioData *s = s1->priv_data;
  184. close(s->fd);
  185. return 0;
  186. }
  187. static const AVOption options[] = {
  188. { "sample_rate", "", offsetof(OSSAudioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  189. { "channels", "", offsetof(OSSAudioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  190. { NULL },
  191. };
  192. static const AVClass oss_demuxer_class = {
  193. .class_name = "OSS demuxer",
  194. .item_name = av_default_item_name,
  195. .option = options,
  196. .version = LIBAVUTIL_VERSION_INT,
  197. };
  198. AVInputFormat ff_oss_demuxer = {
  199. .name = "oss",
  200. .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) capture"),
  201. .priv_data_size = sizeof(OSSAudioData),
  202. .read_header = audio_read_header,
  203. .read_packet = audio_read_packet,
  204. .read_close = audio_read_close,
  205. .flags = AVFMT_NOFILE,
  206. .priv_class = &oss_demuxer_class,
  207. };