oss_audio.c 8.5 KB

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