oss_enc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "avdevice.h"
  30. #include "libavformat/internal.h"
  31. #include "libavformat/mux.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->ch_layout.nb_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. const uint8_t *buf = pkt->data;
  52. int len, ret;
  53. int size= pkt->size;
  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. const FFOutputFormat ff_oss_muxer = {
  86. .p.name = "oss",
  87. .p.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. .p.audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
  93. .p.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. .p.flags = AVFMT_NOFILE,
  98. .p.priv_class = &oss_muxer_class,
  99. };