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.

349 lines
8.4KB

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