oss_audio.c 8.5 KB

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