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.

315 lines
7.2KB

  1. /*
  2. * Linux audio play and grab interface
  3. * Copyright (c) 2000, 2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include "avformat.h"
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <linux/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;
  134. AVStream *st;
  135. int ret;
  136. s = av_mallocz(sizeof(AudioData));
  137. if (!s)
  138. return -ENOMEM;
  139. s1->priv_data = s;
  140. st = s1->streams[0];
  141. s->sample_rate = st->codec.sample_rate;
  142. s->channels = st->codec.channels;
  143. ret = audio_open(s, 1);
  144. if (ret < 0) {
  145. av_free(s);
  146. return -EIO;
  147. } else {
  148. return 0;
  149. }
  150. }
  151. static int audio_write_packet(AVFormatContext *s1, int stream_index,
  152. UINT8 *buf, int size, int force_pts)
  153. {
  154. AudioData *s = s1->priv_data;
  155. int len, ret;
  156. while (size > 0) {
  157. len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
  158. if (len > size)
  159. len = size;
  160. memcpy(s->buffer + s->buffer_ptr, buf, len);
  161. s->buffer_ptr += len;
  162. if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
  163. for(;;) {
  164. ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
  165. if (ret != 0)
  166. break;
  167. if (ret < 0 && (errno != EAGAIN && errno != EINTR))
  168. return -EIO;
  169. }
  170. s->buffer_ptr = 0;
  171. }
  172. buf += len;
  173. size -= len;
  174. }
  175. return 0;
  176. }
  177. static int audio_write_trailer(AVFormatContext *s1)
  178. {
  179. AudioData *s = s1->priv_data;
  180. audio_close(s);
  181. av_free(s);
  182. return 0;
  183. }
  184. /* grab support */
  185. static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  186. {
  187. AudioData *s;
  188. AVStream *st;
  189. int ret;
  190. if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
  191. return -1;
  192. s = av_mallocz(sizeof(AudioData));
  193. if (!s)
  194. return -ENOMEM;
  195. st = av_mallocz(sizeof(AVStream));
  196. if (!st) {
  197. av_free(s);
  198. return -ENOMEM;
  199. }
  200. s1->priv_data = s;
  201. s1->nb_streams = 1;
  202. s1->streams[0] = st;
  203. s->sample_rate = ap->sample_rate;
  204. s->channels = ap->channels;
  205. ret = audio_open(s, 0);
  206. if (ret < 0) {
  207. av_free(st);
  208. av_free(s);
  209. return -EIO;
  210. } else {
  211. /* take real parameters */
  212. st->codec.codec_type = CODEC_TYPE_AUDIO;
  213. st->codec.codec_id = s->codec_id;
  214. st->codec.sample_rate = s->sample_rate;
  215. st->codec.channels = s->channels;
  216. return 0;
  217. }
  218. }
  219. static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
  220. {
  221. AudioData *s = s1->priv_data;
  222. int ret;
  223. if (av_new_packet(pkt, s->frame_size) < 0)
  224. return -EIO;
  225. for(;;) {
  226. ret = read(s->fd, pkt->data, pkt->size);
  227. if (ret > 0)
  228. break;
  229. if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
  230. av_free_packet(pkt);
  231. pkt->size = 0;
  232. return 0;
  233. }
  234. if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
  235. av_free_packet(pkt);
  236. return -EIO;
  237. }
  238. }
  239. pkt->size = ret;
  240. if (s->flip_left && s->channels == 2) {
  241. int i;
  242. short *p = (short *) pkt->data;
  243. for (i = 0; i < ret; i += 4) {
  244. *p = ~*p;
  245. p += 2;
  246. }
  247. }
  248. return 0;
  249. }
  250. static int audio_read_close(AVFormatContext *s1)
  251. {
  252. AudioData *s = s1->priv_data;
  253. audio_close(s);
  254. av_free(s);
  255. return 0;
  256. }
  257. AVFormat audio_device_format = {
  258. "audio_device",
  259. "audio grab and output",
  260. "",
  261. "",
  262. /* XXX: we make the assumption that the soundcard accepts this format */
  263. /* XXX: find better solution with "preinit" method, needed also in
  264. other formats */
  265. #ifdef WORDS_BIGENDIAN
  266. CODEC_ID_PCM_S16BE,
  267. #else
  268. CODEC_ID_PCM_S16LE,
  269. #endif
  270. CODEC_ID_NONE,
  271. audio_write_header,
  272. audio_write_packet,
  273. audio_write_trailer,
  274. audio_read_header,
  275. audio_read_packet,
  276. audio_read_close,
  277. NULL,
  278. AVFMT_NOFILE,
  279. };