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.

331 lines
7.8KB

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