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