sndio_enc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * sndio play and grab interface
  3. * Copyright (c) 2010 Jacob Meuser
  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 <stdint.h>
  22. #include <sndio.h>
  23. #include "libavutil/internal.h"
  24. #include "libavformat/mux.h"
  25. #include "libavdevice/avdevice.h"
  26. #include "libavdevice/sndio.h"
  27. static av_cold int audio_write_header(AVFormatContext *s1)
  28. {
  29. SndioData *s = s1->priv_data;
  30. AVStream *st;
  31. int ret;
  32. st = s1->streams[0];
  33. s->sample_rate = st->codecpar->sample_rate;
  34. s->channels = st->codecpar->ch_layout.nb_channels;
  35. ret = ff_sndio_open(s1, 1, s1->url);
  36. return ret;
  37. }
  38. static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
  39. {
  40. SndioData *s = s1->priv_data;
  41. const uint8_t *buf = pkt->data;
  42. int size = pkt->size;
  43. int len, ret;
  44. while (size > 0) {
  45. len = FFMIN(s->buffer_size - s->buffer_offset, size);
  46. memcpy(s->buffer + s->buffer_offset, buf, len);
  47. buf += len;
  48. size -= len;
  49. s->buffer_offset += len;
  50. if (s->buffer_offset >= s->buffer_size) {
  51. ret = sio_write(s->hdl, s->buffer, s->buffer_size);
  52. if (ret == 0 || sio_eof(s->hdl))
  53. return AVERROR(EIO);
  54. s->softpos += ret;
  55. s->buffer_offset = 0;
  56. }
  57. }
  58. return 0;
  59. }
  60. static int audio_write_trailer(AVFormatContext *s1)
  61. {
  62. SndioData *s = s1->priv_data;
  63. sio_write(s->hdl, s->buffer, s->buffer_offset);
  64. ff_sndio_close(s);
  65. return 0;
  66. }
  67. static const AVClass sndio_muxer_class = {
  68. .class_name = "sndio outdev",
  69. .item_name = av_default_item_name,
  70. .version = LIBAVUTIL_VERSION_INT,
  71. .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT,
  72. };
  73. const FFOutputFormat ff_sndio_muxer = {
  74. .p.name = "sndio",
  75. .p.long_name = NULL_IF_CONFIG_SMALL("sndio audio playback"),
  76. .priv_data_size = sizeof(SndioData),
  77. /* XXX: we make the assumption that the soundcard accepts this format */
  78. /* XXX: find better solution with "preinit" method, needed also in
  79. other formats */
  80. .p.audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
  81. .p.video_codec = AV_CODEC_ID_NONE,
  82. .write_header = audio_write_header,
  83. .write_packet = audio_write_packet,
  84. .write_trailer = audio_write_trailer,
  85. .p.flags = AVFMT_NOFILE,
  86. .p.priv_class = &sndio_muxer_class,
  87. };