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.

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