oss_audio_enc.c 3.3 KB

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