oss_enc.c 3.2 KB

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