You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

354 lines
8.4KB

  1. /*
  2. * Linux audio play and grab interface
  3. * Copyright (c) 2000, 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #ifdef __OpenBSD__
  24. #include <soundcard.h>
  25. #else
  26. #include <sys/soundcard.h>
  27. #endif
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/mman.h>
  32. #include <sys/time.h>
  33. #define AUDIO_BLOCK_SIZE 4096
  34. typedef struct {
  35. int fd;
  36. int sample_rate;
  37. int channels;
  38. int frame_size; /* in bytes ! */
  39. int codec_id;
  40. int flip_left : 1;
  41. uint8_t buffer[AUDIO_BLOCK_SIZE];
  42. int buffer_ptr;
  43. } AudioData;
  44. static int audio_open(AudioData *s, int is_output, const char *audio_device)
  45. {
  46. int audio_fd;
  47. int tmp, err;
  48. char *flip = getenv("AUDIO_FLIP_LEFT");
  49. /* open linux audio device */
  50. if (!audio_device)
  51. #ifdef __OpenBSD__
  52. audio_device = "/dev/sound";
  53. #else
  54. audio_device = "/dev/dsp";
  55. #endif
  56. if (is_output)
  57. audio_fd = open(audio_device, O_WRONLY);
  58. else
  59. audio_fd = open(audio_device, O_RDONLY);
  60. if (audio_fd < 0) {
  61. perror(audio_device);
  62. return AVERROR_IO;
  63. }
  64. if (flip && *flip == '1') {
  65. s->flip_left = 1;
  66. }
  67. /* non blocking mode */
  68. if (!is_output)
  69. fcntl(audio_fd, F_SETFL, O_NONBLOCK);
  70. s->frame_size = AUDIO_BLOCK_SIZE;
  71. #if 0
  72. tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
  73. err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
  74. if (err < 0) {
  75. perror("SNDCTL_DSP_SETFRAGMENT");
  76. }
  77. #endif
  78. /* select format : favour native format */
  79. err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
  80. #ifdef WORDS_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 = CODEC_ID_PCM_S16LE;
  100. break;
  101. case AFMT_S16_BE:
  102. s->codec_id = CODEC_ID_PCM_S16BE;
  103. break;
  104. default:
  105. av_log(NULL, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
  106. close(audio_fd);
  107. return AVERROR_IO;
  108. }
  109. err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
  110. if (err < 0) {
  111. perror("SNDCTL_DSP_SETFMT");
  112. goto fail;
  113. }
  114. tmp = (s->channels == 2);
  115. err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
  116. if (err < 0) {
  117. perror("SNDCTL_DSP_STEREO");
  118. goto fail;
  119. }
  120. if (tmp)
  121. s->channels = 2;
  122. tmp = s->sample_rate;
  123. err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
  124. if (err < 0) {
  125. perror("SNDCTL_DSP_SPEED");
  126. goto fail;
  127. }
  128. s->sample_rate = tmp; /* store real sample rate */
  129. s->fd = audio_fd;
  130. return 0;
  131. fail:
  132. close(audio_fd);
  133. return AVERROR_IO;
  134. }
  135. static int audio_close(AudioData *s)
  136. {
  137. close(s->fd);
  138. return 0;
  139. }
  140. /* sound output support */
  141. static int audio_write_header(AVFormatContext *s1)
  142. {
  143. AudioData *s = s1->priv_data;
  144. AVStream *st;
  145. int ret;
  146. st = s1->streams[0];
  147. s->sample_rate = st->codec.sample_rate;
  148. s->channels = st->codec.channels;
  149. ret = audio_open(s, 1, NULL);
  150. if (ret < 0) {
  151. return AVERROR_IO;
  152. } else {
  153. return 0;
  154. }
  155. }
  156. static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
  157. {
  158. AudioData *s = s1->priv_data;
  159. int len, ret;
  160. int size= pkt->size;
  161. uint8_t *buf= pkt->data;
  162. while (size > 0) {
  163. len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
  164. if (len > size)
  165. len = size;
  166. memcpy(s->buffer + s->buffer_ptr, buf, len);
  167. s->buffer_ptr += len;
  168. if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
  169. for(;;) {
  170. ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
  171. if (ret > 0)
  172. break;
  173. if (ret < 0 && (errno != EAGAIN && errno != EINTR))
  174. return AVERROR_IO;
  175. }
  176. s->buffer_ptr = 0;
  177. }
  178. buf += len;
  179. size -= len;
  180. }
  181. return 0;
  182. }
  183. static int audio_write_trailer(AVFormatContext *s1)
  184. {
  185. AudioData *s = s1->priv_data;
  186. audio_close(s);
  187. return 0;
  188. }
  189. /* grab support */
  190. static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  191. {
  192. AudioData *s = s1->priv_data;
  193. AVStream *st;
  194. int ret;
  195. if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
  196. return -1;
  197. st = av_new_stream(s1, 0);
  198. if (!st) {
  199. return -ENOMEM;
  200. }
  201. s->sample_rate = ap->sample_rate;
  202. s->channels = ap->channels;
  203. ret = audio_open(s, 0, ap->device);
  204. if (ret < 0) {
  205. av_free(st);
  206. return AVERROR_IO;
  207. }
  208. /* take real parameters */
  209. st->codec.codec_type = CODEC_TYPE_AUDIO;
  210. st->codec.codec_id = s->codec_id;
  211. st->codec.sample_rate = s->sample_rate;
  212. st->codec.channels = s->channels;
  213. av_set_pts_info(st, 48, 1, 1000000); /* 48 bits pts in us */
  214. return 0;
  215. }
  216. static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
  217. {
  218. AudioData *s = s1->priv_data;
  219. int ret, bdelay;
  220. int64_t cur_time;
  221. struct audio_buf_info abufi;
  222. if (av_new_packet(pkt, s->frame_size) < 0)
  223. return AVERROR_IO;
  224. for(;;) {
  225. struct timeval tv;
  226. fd_set fds;
  227. tv.tv_sec = 0;
  228. tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
  229. FD_ZERO(&fds);
  230. FD_SET(s->fd, &fds);
  231. /* This will block until data is available or we get a timeout */
  232. (void) select(s->fd + 1, &fds, 0, 0, &tv);
  233. ret = read(s->fd, pkt->data, pkt->size);
  234. if (ret > 0)
  235. break;
  236. if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
  237. av_free_packet(pkt);
  238. pkt->size = 0;
  239. pkt->pts = av_gettime() & ((1LL << 48) - 1);
  240. return 0;
  241. }
  242. if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
  243. av_free_packet(pkt);
  244. return AVERROR_IO;
  245. }
  246. }
  247. pkt->size = ret;
  248. /* compute pts of the start of the packet */
  249. cur_time = av_gettime();
  250. bdelay = ret;
  251. if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
  252. bdelay += abufi.bytes;
  253. }
  254. /* substract time represented by the number of bytes in the audio fifo */
  255. cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
  256. /* convert to wanted units */
  257. pkt->pts = cur_time & ((1LL << 48) - 1);
  258. if (s->flip_left && s->channels == 2) {
  259. int i;
  260. short *p = (short *) pkt->data;
  261. for (i = 0; i < ret; i += 4) {
  262. *p = ~*p;
  263. p += 2;
  264. }
  265. }
  266. return 0;
  267. }
  268. static int audio_read_close(AVFormatContext *s1)
  269. {
  270. AudioData *s = s1->priv_data;
  271. audio_close(s);
  272. return 0;
  273. }
  274. static AVInputFormat audio_in_format = {
  275. "audio_device",
  276. "audio grab and output",
  277. sizeof(AudioData),
  278. NULL,
  279. audio_read_header,
  280. audio_read_packet,
  281. audio_read_close,
  282. .flags = AVFMT_NOFILE,
  283. };
  284. static AVOutputFormat audio_out_format = {
  285. "audio_device",
  286. "audio grab and output",
  287. "",
  288. "",
  289. sizeof(AudioData),
  290. /* XXX: we make the assumption that the soundcard accepts this format */
  291. /* XXX: find better solution with "preinit" method, needed also in
  292. other formats */
  293. #ifdef WORDS_BIGENDIAN
  294. CODEC_ID_PCM_S16BE,
  295. #else
  296. CODEC_ID_PCM_S16LE,
  297. #endif
  298. CODEC_ID_NONE,
  299. audio_write_header,
  300. audio_write_packet,
  301. audio_write_trailer,
  302. .flags = AVFMT_NOFILE,
  303. };
  304. int audio_init(void)
  305. {
  306. av_register_input_format(&audio_in_format);
  307. av_register_output_format(&audio_out_format);
  308. return 0;
  309. }