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.

109 lines
3.0KB

  1. /*
  2. * Linux audio grab interface
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. #if HAVE_SOUNDCARD_H
  23. #include <soundcard.h>
  24. #else
  25. #include <sys/soundcard.h>
  26. #endif
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <sys/ioctl.h>
  30. #include "libavutil/internal.h"
  31. #include "libavcodec/avcodec.h"
  32. #include "libavformat/avformat.h"
  33. #include "libavformat/internal.h"
  34. #include "oss.h"
  35. static int audio_write_header(AVFormatContext *s1)
  36. {
  37. OSSAudioData *s = s1->priv_data;
  38. AVStream *st;
  39. int ret;
  40. st = s1->streams[0];
  41. s->sample_rate = st->codecpar->sample_rate;
  42. s->channels = st->codecpar->channels;
  43. ret = ff_oss_audio_open(s1, 1, s1->filename);
  44. if (ret < 0) {
  45. return AVERROR(EIO);
  46. } else {
  47. return 0;
  48. }
  49. }
  50. static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
  51. {
  52. OSSAudioData *s = s1->priv_data;
  53. int len, ret;
  54. int size= pkt->size;
  55. uint8_t *buf= pkt->data;
  56. while (size > 0) {
  57. len = FFMIN(OSS_AUDIO_BLOCK_SIZE - s->buffer_ptr, size);
  58. memcpy(s->buffer + s->buffer_ptr, buf, len);
  59. s->buffer_ptr += len;
  60. if (s->buffer_ptr >= OSS_AUDIO_BLOCK_SIZE) {
  61. for(;;) {
  62. ret = write(s->fd, s->buffer, OSS_AUDIO_BLOCK_SIZE);
  63. if (ret > 0)
  64. break;
  65. if (ret < 0 && (errno != EAGAIN && errno != EINTR))
  66. return AVERROR(EIO);
  67. }
  68. s->buffer_ptr = 0;
  69. }
  70. buf += len;
  71. size -= len;
  72. }
  73. return 0;
  74. }
  75. static int audio_write_trailer(AVFormatContext *s1)
  76. {
  77. OSSAudioData *s = s1->priv_data;
  78. ff_oss_audio_close(s);
  79. return 0;
  80. }
  81. AVOutputFormat ff_oss_muxer = {
  82. .name = "oss",
  83. .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) playback"),
  84. .priv_data_size = sizeof(OSSAudioData),
  85. /* XXX: we make the assumption that the soundcard accepts this format */
  86. /* XXX: find better solution with "preinit" method, needed also in
  87. other formats */
  88. .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
  89. .video_codec = AV_CODEC_ID_NONE,
  90. .write_header = audio_write_header,
  91. .write_packet = audio_write_packet,
  92. .write_trailer = audio_write_trailer,
  93. .flags = AVFMT_NOFILE,
  94. };