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.

344 lines
8.2KB

  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, int stream_index,
  149. const uint8_t *buf, int size, int64_t pts)
  150. {
  151. AudioData *s = s1->priv_data;
  152. int len, ret;
  153. while (size > 0) {
  154. len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
  155. if (len > size)
  156. len = size;
  157. memcpy(s->buffer + s->buffer_ptr, buf, len);
  158. s->buffer_ptr += len;
  159. if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
  160. for(;;) {
  161. ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
  162. if (ret > 0)
  163. break;
  164. if (ret < 0 && (errno != EAGAIN && errno != EINTR))
  165. return -EIO;
  166. }
  167. s->buffer_ptr = 0;
  168. }
  169. buf += len;
  170. size -= len;
  171. }
  172. return 0;
  173. }
  174. static int audio_write_trailer(AVFormatContext *s1)
  175. {
  176. AudioData *s = s1->priv_data;
  177. audio_close(s);
  178. return 0;
  179. }
  180. /* grab support */
  181. static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  182. {
  183. AudioData *s = s1->priv_data;
  184. AVStream *st;
  185. int ret;
  186. if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
  187. return -1;
  188. st = av_new_stream(s1, 0);
  189. if (!st) {
  190. return -ENOMEM;
  191. }
  192. s->sample_rate = ap->sample_rate;
  193. s->channels = ap->channels;
  194. ret = audio_open(s, 0, ap->device);
  195. if (ret < 0) {
  196. av_free(st);
  197. return -EIO;
  198. }
  199. /* take real parameters */
  200. st->codec.codec_type = CODEC_TYPE_AUDIO;
  201. st->codec.codec_id = s->codec_id;
  202. st->codec.sample_rate = s->sample_rate;
  203. st->codec.channels = s->channels;
  204. av_set_pts_info(s1, 48, 1, 1000000); /* 48 bits pts in us */
  205. return 0;
  206. }
  207. static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
  208. {
  209. AudioData *s = s1->priv_data;
  210. int ret, bdelay;
  211. int64_t cur_time;
  212. struct audio_buf_info abufi;
  213. if (av_new_packet(pkt, s->frame_size) < 0)
  214. return -EIO;
  215. for(;;) {
  216. struct timeval tv;
  217. fd_set fds;
  218. tv.tv_sec = 0;
  219. tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
  220. FD_ZERO(&fds);
  221. FD_SET(s->fd, &fds);
  222. /* This will block until data is available or we get a timeout */
  223. (void) select(s->fd + 1, &fds, 0, 0, &tv);
  224. ret = read(s->fd, pkt->data, pkt->size);
  225. if (ret > 0)
  226. break;
  227. if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
  228. av_free_packet(pkt);
  229. pkt->size = 0;
  230. return 0;
  231. }
  232. if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
  233. av_free_packet(pkt);
  234. return -EIO;
  235. }
  236. }
  237. pkt->size = ret;
  238. /* compute pts of the start of the packet */
  239. cur_time = av_gettime();
  240. bdelay = ret;
  241. if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
  242. bdelay += abufi.bytes;
  243. }
  244. /* substract time represented by the number of bytes in the audio fifo */
  245. cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
  246. /* convert to wanted units */
  247. pkt->pts = cur_time & ((1LL << 48) - 1);
  248. if (s->flip_left && s->channels == 2) {
  249. int i;
  250. short *p = (short *) pkt->data;
  251. for (i = 0; i < ret; i += 4) {
  252. *p = ~*p;
  253. p += 2;
  254. }
  255. }
  256. return 0;
  257. }
  258. static int audio_read_close(AVFormatContext *s1)
  259. {
  260. AudioData *s = s1->priv_data;
  261. audio_close(s);
  262. return 0;
  263. }
  264. static AVInputFormat audio_in_format = {
  265. "audio_device",
  266. "audio grab and output",
  267. sizeof(AudioData),
  268. NULL,
  269. audio_read_header,
  270. audio_read_packet,
  271. audio_read_close,
  272. .flags = AVFMT_NOFILE,
  273. };
  274. static AVOutputFormat audio_out_format = {
  275. "audio_device",
  276. "audio grab and output",
  277. "",
  278. "",
  279. sizeof(AudioData),
  280. /* XXX: we make the assumption that the soundcard accepts this format */
  281. /* XXX: find better solution with "preinit" method, needed also in
  282. other formats */
  283. #ifdef WORDS_BIGENDIAN
  284. CODEC_ID_PCM_S16BE,
  285. #else
  286. CODEC_ID_PCM_S16LE,
  287. #endif
  288. CODEC_ID_NONE,
  289. audio_write_header,
  290. audio_write_packet,
  291. audio_write_trailer,
  292. .flags = AVFMT_NOFILE,
  293. };
  294. int audio_init(void)
  295. {
  296. av_register_input_format(&audio_in_format);
  297. av_register_output_format(&audio_out_format);
  298. return 0;
  299. }