oss_audio.c 8.3 KB

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