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.

324 lines
8.3KB

  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 <stdlib.h>
  23. #include <stdio.h>
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #if HAVE_SOUNDCARD_H
  28. #include <soundcard.h>
  29. #else
  30. #include <sys/soundcard.h>
  31. #endif
  32. #include <unistd.h>
  33. #include <fcntl.h>
  34. #include <sys/ioctl.h>
  35. #include "libavutil/log.h"
  36. #include "libavutil/opt.h"
  37. #include "libavcodec/avcodec.h"
  38. #include "libavformat/avformat.h"
  39. #include "libavformat/internal.h"
  40. #define AUDIO_BLOCK_SIZE 4096
  41. typedef struct {
  42. AVClass *class;
  43. int fd;
  44. int sample_rate;
  45. int channels;
  46. int frame_size; /* in bytes ! */
  47. enum CodecID codec_id;
  48. unsigned int flip_left : 1;
  49. uint8_t buffer[AUDIO_BLOCK_SIZE];
  50. int buffer_ptr;
  51. } AudioData;
  52. static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
  53. {
  54. AudioData *s = s1->priv_data;
  55. int audio_fd;
  56. int tmp, err;
  57. char *flip = getenv("AUDIO_FLIP_LEFT");
  58. if (is_output)
  59. audio_fd = open(audio_device, O_WRONLY);
  60. else
  61. audio_fd = open(audio_device, O_RDONLY);
  62. if (audio_fd < 0) {
  63. av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
  64. return AVERROR(EIO);
  65. }
  66. if (flip && *flip == '1') {
  67. s->flip_left = 1;
  68. }
  69. /* non blocking mode */
  70. if (!is_output)
  71. fcntl(audio_fd, F_SETFL, O_NONBLOCK);
  72. s->frame_size = AUDIO_BLOCK_SIZE;
  73. /* select format : favour native format */
  74. err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
  75. #if HAVE_BIGENDIAN
  76. if (tmp & AFMT_S16_BE) {
  77. tmp = AFMT_S16_BE;
  78. } else if (tmp & AFMT_S16_LE) {
  79. tmp = AFMT_S16_LE;
  80. } else {
  81. tmp = 0;
  82. }
  83. #else
  84. if (tmp & AFMT_S16_LE) {
  85. tmp = AFMT_S16_LE;
  86. } else if (tmp & AFMT_S16_BE) {
  87. tmp = AFMT_S16_BE;
  88. } else {
  89. tmp = 0;
  90. }
  91. #endif
  92. switch(tmp) {
  93. case AFMT_S16_LE:
  94. s->codec_id = CODEC_ID_PCM_S16LE;
  95. break;
  96. case AFMT_S16_BE:
  97. s->codec_id = CODEC_ID_PCM_S16BE;
  98. break;
  99. default:
  100. av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
  101. close(audio_fd);
  102. return AVERROR(EIO);
  103. }
  104. err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
  105. if (err < 0) {
  106. av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
  107. goto fail;
  108. }
  109. tmp = (s->channels == 2);
  110. err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
  111. if (err < 0) {
  112. av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
  113. goto fail;
  114. }
  115. tmp = s->sample_rate;
  116. err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
  117. if (err < 0) {
  118. av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
  119. goto fail;
  120. }
  121. s->sample_rate = tmp; /* store real sample rate */
  122. s->fd = audio_fd;
  123. return 0;
  124. fail:
  125. close(audio_fd);
  126. return AVERROR(EIO);
  127. }
  128. static int audio_close(AudioData *s)
  129. {
  130. close(s->fd);
  131. return 0;
  132. }
  133. /* sound output support */
  134. static int audio_write_header(AVFormatContext *s1)
  135. {
  136. AudioData *s = s1->priv_data;
  137. AVStream *st;
  138. int ret;
  139. st = s1->streams[0];
  140. s->sample_rate = st->codec->sample_rate;
  141. s->channels = st->codec->channels;
  142. ret = audio_open(s1, 1, s1->filename);
  143. if (ret < 0) {
  144. return AVERROR(EIO);
  145. } else {
  146. return 0;
  147. }
  148. }
  149. static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
  150. {
  151. AudioData *s = s1->priv_data;
  152. int len, ret;
  153. int size= pkt->size;
  154. uint8_t *buf= pkt->data;
  155. while (size > 0) {
  156. len = FFMIN(AUDIO_BLOCK_SIZE - s->buffer_ptr, size);
  157. memcpy(s->buffer + s->buffer_ptr, buf, len);
  158. s->buffer_ptr += len;
  159. if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
  160. for(;;) {
  161. ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
  162. if (ret > 0)
  163. break;
  164. if (ret < 0 && (errno != EAGAIN && errno != EINTR))
  165. return AVERROR(EIO);
  166. }
  167. s->buffer_ptr = 0;
  168. }
  169. buf += len;
  170. size -= len;
  171. }
  172. return 0;
  173. }
  174. static int audio_write_trailer(AVFormatContext *s1)
  175. {
  176. AudioData *s = s1->priv_data;
  177. audio_close(s);
  178. return 0;
  179. }
  180. /* grab support */
  181. static int audio_read_header(AVFormatContext *s1)
  182. {
  183. AudioData *s = s1->priv_data;
  184. AVStream *st;
  185. int ret;
  186. st = avformat_new_stream(s1, NULL);
  187. if (!st) {
  188. return AVERROR(ENOMEM);
  189. }
  190. ret = audio_open(s1, 0, s1->filename);
  191. if (ret < 0) {
  192. return AVERROR(EIO);
  193. }
  194. /* take real parameters */
  195. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  196. st->codec->codec_id = s->codec_id;
  197. st->codec->sample_rate = s->sample_rate;
  198. st->codec->channels = s->channels;
  199. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  200. return 0;
  201. }
  202. static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
  203. {
  204. AudioData *s = s1->priv_data;
  205. int ret, bdelay;
  206. int64_t cur_time;
  207. struct audio_buf_info abufi;
  208. if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
  209. return ret;
  210. ret = read(s->fd, pkt->data, pkt->size);
  211. if (ret <= 0){
  212. av_free_packet(pkt);
  213. pkt->size = 0;
  214. if (ret<0) return AVERROR(errno);
  215. else return AVERROR_EOF;
  216. }
  217. pkt->size = ret;
  218. /* compute pts of the start of the packet */
  219. cur_time = av_gettime();
  220. bdelay = ret;
  221. if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
  222. bdelay += abufi.bytes;
  223. }
  224. /* subtract time represented by the number of bytes in the audio fifo */
  225. cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
  226. /* convert to wanted units */
  227. pkt->pts = cur_time;
  228. if (s->flip_left && s->channels == 2) {
  229. int i;
  230. short *p = (short *) pkt->data;
  231. for (i = 0; i < ret; i += 4) {
  232. *p = ~*p;
  233. p += 2;
  234. }
  235. }
  236. return 0;
  237. }
  238. static int audio_read_close(AVFormatContext *s1)
  239. {
  240. AudioData *s = s1->priv_data;
  241. audio_close(s);
  242. return 0;
  243. }
  244. #if CONFIG_OSS_INDEV
  245. static const AVOption options[] = {
  246. { "sample_rate", "", offsetof(AudioData, sample_rate), AV_OPT_TYPE_INT, {.dbl = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  247. { "channels", "", offsetof(AudioData, channels), AV_OPT_TYPE_INT, {.dbl = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  248. { NULL },
  249. };
  250. static const AVClass oss_demuxer_class = {
  251. .class_name = "OSS demuxer",
  252. .item_name = av_default_item_name,
  253. .option = options,
  254. .version = LIBAVUTIL_VERSION_INT,
  255. };
  256. AVInputFormat ff_oss_demuxer = {
  257. .name = "oss",
  258. .long_name = NULL_IF_CONFIG_SMALL("Open Sound System capture"),
  259. .priv_data_size = sizeof(AudioData),
  260. .read_header = audio_read_header,
  261. .read_packet = audio_read_packet,
  262. .read_close = audio_read_close,
  263. .flags = AVFMT_NOFILE,
  264. .priv_class = &oss_demuxer_class,
  265. };
  266. #endif
  267. #if CONFIG_OSS_OUTDEV
  268. AVOutputFormat ff_oss_muxer = {
  269. .name = "oss",
  270. .long_name = NULL_IF_CONFIG_SMALL("Open Sound System playback"),
  271. .priv_data_size = sizeof(AudioData),
  272. /* XXX: we make the assumption that the soundcard accepts this format */
  273. /* XXX: find better solution with "preinit" method, needed also in
  274. other formats */
  275. .audio_codec = AV_NE(CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE),
  276. .video_codec = CODEC_ID_NONE,
  277. .write_header = audio_write_header,
  278. .write_packet = audio_write_packet,
  279. .write_trailer = audio_write_trailer,
  280. .flags = AVFMT_NOFILE,
  281. };
  282. #endif