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.

345 lines
8.1KB

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