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.3KB

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