oss_audio.c 8.5 KB

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