audio.c 8.2 KB

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