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.

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