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.

340 lines
8.9KB

  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. #if HAVE_SOUNDCARD_H
  28. #include <soundcard.h>
  29. #else
  30. #include <sys/soundcard.h>
  31. #endif
  32. #if HAVE_UNISTD_H
  33. #include <unistd.h>
  34. #endif
  35. #include <fcntl.h>
  36. #include <sys/ioctl.h>
  37. #include "libavutil/internal.h"
  38. #include "libavutil/log.h"
  39. #include "libavutil/opt.h"
  40. #include "libavutil/time.h"
  41. #include "libavcodec/avcodec.h"
  42. #include "avdevice.h"
  43. #include "libavformat/internal.h"
  44. #define AUDIO_BLOCK_SIZE 4096
  45. typedef struct {
  46. AVClass *class;
  47. int fd;
  48. int sample_rate;
  49. int channels;
  50. int frame_size; /* in bytes ! */
  51. enum AVCodecID codec_id;
  52. unsigned int flip_left : 1;
  53. uint8_t buffer[AUDIO_BLOCK_SIZE];
  54. int buffer_ptr;
  55. } AudioData;
  56. static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
  57. {
  58. AudioData *s = s1->priv_data;
  59. int audio_fd;
  60. int tmp, err;
  61. char *flip = getenv("AUDIO_FLIP_LEFT");
  62. if (is_output)
  63. audio_fd = avpriv_open(audio_device, O_WRONLY);
  64. else
  65. audio_fd = avpriv_open(audio_device, O_RDONLY);
  66. if (audio_fd < 0) {
  67. av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
  68. return AVERROR(EIO);
  69. }
  70. if (flip && *flip == '1') {
  71. s->flip_left = 1;
  72. }
  73. /* non blocking mode */
  74. if (!is_output) {
  75. if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) < 0) {
  76. av_log(s1, AV_LOG_WARNING, "%s: Could not enable non block mode (%s)\n", audio_device, strerror(errno));
  77. }
  78. }
  79. s->frame_size = AUDIO_BLOCK_SIZE;
  80. /* select format : favour native format */
  81. err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
  82. #if HAVE_BIGENDIAN
  83. if (tmp & AFMT_S16_BE) {
  84. tmp = AFMT_S16_BE;
  85. } else if (tmp & AFMT_S16_LE) {
  86. tmp = AFMT_S16_LE;
  87. } else {
  88. tmp = 0;
  89. }
  90. #else
  91. if (tmp & AFMT_S16_LE) {
  92. tmp = AFMT_S16_LE;
  93. } else if (tmp & AFMT_S16_BE) {
  94. tmp = AFMT_S16_BE;
  95. } else {
  96. tmp = 0;
  97. }
  98. #endif
  99. switch(tmp) {
  100. case AFMT_S16_LE:
  101. s->codec_id = AV_CODEC_ID_PCM_S16LE;
  102. break;
  103. case AFMT_S16_BE:
  104. s->codec_id = AV_CODEC_ID_PCM_S16BE;
  105. break;
  106. default:
  107. av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
  108. close(audio_fd);
  109. return AVERROR(EIO);
  110. }
  111. err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
  112. if (err < 0) {
  113. av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
  114. goto fail;
  115. }
  116. tmp = (s->channels == 2);
  117. err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
  118. if (err < 0) {
  119. av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
  120. goto fail;
  121. }
  122. tmp = s->sample_rate;
  123. err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
  124. if (err < 0) {
  125. av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
  126. goto fail;
  127. }
  128. s->sample_rate = tmp; /* store real sample rate */
  129. s->fd = audio_fd;
  130. return 0;
  131. fail:
  132. close(audio_fd);
  133. return AVERROR(EIO);
  134. }
  135. static int audio_close(AudioData *s)
  136. {
  137. close(s->fd);
  138. return 0;
  139. }
  140. /* sound output support */
  141. static int audio_write_header(AVFormatContext *s1)
  142. {
  143. AudioData *s = s1->priv_data;
  144. AVStream *st;
  145. int ret;
  146. st = s1->streams[0];
  147. s->sample_rate = st->codec->sample_rate;
  148. s->channels = st->codec->channels;
  149. ret = audio_open(s1, 1, s1->filename);
  150. if (ret < 0) {
  151. return AVERROR(EIO);
  152. } else {
  153. return 0;
  154. }
  155. }
  156. static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
  157. {
  158. AudioData *s = s1->priv_data;
  159. int len, ret;
  160. int size= pkt->size;
  161. uint8_t *buf= pkt->data;
  162. while (size > 0) {
  163. len = FFMIN(AUDIO_BLOCK_SIZE - s->buffer_ptr, 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)
  189. {
  190. AudioData *s = s1->priv_data;
  191. AVStream *st;
  192. int ret;
  193. st = avformat_new_stream(s1, NULL);
  194. if (!st) {
  195. return AVERROR(ENOMEM);
  196. }
  197. ret = audio_open(s1, 0, s1->filename);
  198. if (ret < 0) {
  199. return AVERROR(EIO);
  200. }
  201. /* take real parameters */
  202. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  203. st->codec->codec_id = s->codec_id;
  204. st->codec->sample_rate = s->sample_rate;
  205. st->codec->channels = s->channels;
  206. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  207. return 0;
  208. }
  209. static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
  210. {
  211. AudioData *s = s1->priv_data;
  212. int ret, bdelay;
  213. int64_t cur_time;
  214. struct audio_buf_info abufi;
  215. if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
  216. return ret;
  217. ret = read(s->fd, pkt->data, pkt->size);
  218. if (ret <= 0){
  219. av_free_packet(pkt);
  220. pkt->size = 0;
  221. if (ret<0) return AVERROR(errno);
  222. else return AVERROR_EOF;
  223. }
  224. pkt->size = ret;
  225. /* compute pts of the start of the packet */
  226. cur_time = av_gettime();
  227. bdelay = ret;
  228. if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
  229. bdelay += abufi.bytes;
  230. }
  231. /* subtract time represented by the number of bytes in the audio fifo */
  232. cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
  233. /* convert to wanted units */
  234. pkt->pts = cur_time;
  235. if (s->flip_left && s->channels == 2) {
  236. int i;
  237. short *p = (short *) pkt->data;
  238. for (i = 0; i < ret; i += 4) {
  239. *p = ~*p;
  240. p += 2;
  241. }
  242. }
  243. return 0;
  244. }
  245. static int audio_read_close(AVFormatContext *s1)
  246. {
  247. AudioData *s = s1->priv_data;
  248. audio_close(s);
  249. return 0;
  250. }
  251. #if CONFIG_OSS_INDEV
  252. static const AVOption options[] = {
  253. { "sample_rate", "", offsetof(AudioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  254. { "channels", "", offsetof(AudioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  255. { NULL },
  256. };
  257. static const AVClass oss_demuxer_class = {
  258. .class_name = "OSS demuxer",
  259. .item_name = av_default_item_name,
  260. .option = options,
  261. .version = LIBAVUTIL_VERSION_INT,
  262. .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
  263. };
  264. AVInputFormat ff_oss_demuxer = {
  265. .name = "oss",
  266. .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) capture"),
  267. .priv_data_size = sizeof(AudioData),
  268. .read_header = audio_read_header,
  269. .read_packet = audio_read_packet,
  270. .read_close = audio_read_close,
  271. .flags = AVFMT_NOFILE,
  272. .priv_class = &oss_demuxer_class,
  273. };
  274. #endif
  275. #if CONFIG_OSS_OUTDEV
  276. static const AVClass oss_muxer_class = {
  277. .class_name = "OSS muxer",
  278. .item_name = av_default_item_name,
  279. .version = LIBAVUTIL_VERSION_INT,
  280. .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT,
  281. };
  282. AVOutputFormat ff_oss_muxer = {
  283. .name = "oss",
  284. .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) playback"),
  285. .priv_data_size = sizeof(AudioData),
  286. /* XXX: we make the assumption that the soundcard accepts this format */
  287. /* XXX: find better solution with "preinit" method, needed also in
  288. other formats */
  289. .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
  290. .video_codec = AV_CODEC_ID_NONE,
  291. .write_header = audio_write_header,
  292. .write_packet = audio_write_packet,
  293. .write_trailer = audio_write_trailer,
  294. .flags = AVFMT_NOFILE,
  295. .priv_class = &oss_muxer_class,
  296. };
  297. #endif