sndio_enc.c 3.0 KB

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