sndio_dec.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "libavutil/opt.h"
  25. #include "libavutil/time.h"
  26. #include "libavformat/avformat.h"
  27. #include "libavformat/demux.h"
  28. #include "libavformat/internal.h"
  29. #include "libavdevice/sndio.h"
  30. static av_cold int audio_read_header(AVFormatContext *s1)
  31. {
  32. SndioData *s = s1->priv_data;
  33. AVStream *st;
  34. int ret;
  35. st = avformat_new_stream(s1, NULL);
  36. if (!st)
  37. return AVERROR(ENOMEM);
  38. ret = ff_sndio_open(s1, 0, s1->url);
  39. if (ret < 0)
  40. return ret;
  41. /* take real parameters */
  42. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  43. st->codecpar->codec_id = s->codec_id;
  44. st->codecpar->sample_rate = s->sample_rate;
  45. st->codecpar->ch_layout.nb_channels = s->channels;
  46. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  47. return 0;
  48. }
  49. static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
  50. {
  51. SndioData *s = s1->priv_data;
  52. int64_t bdelay, cur_time;
  53. int ret;
  54. if ((ret = av_new_packet(pkt, s->buffer_size)) < 0)
  55. return ret;
  56. ret = sio_read(s->hdl, pkt->data, pkt->size);
  57. if (ret == 0 || sio_eof(s->hdl)) {
  58. av_packet_unref(pkt);
  59. return AVERROR_EOF;
  60. }
  61. pkt->size = ret;
  62. s->softpos += ret;
  63. /* compute pts of the start of the packet */
  64. cur_time = av_gettime();
  65. bdelay = ret + s->hwpos - s->softpos;
  66. /* convert to pts */
  67. pkt->pts = cur_time - ((bdelay * 1000000) /
  68. (s->bps * s->channels * s->sample_rate));
  69. return 0;
  70. }
  71. static av_cold int audio_read_close(AVFormatContext *s1)
  72. {
  73. SndioData *s = s1->priv_data;
  74. ff_sndio_close(s);
  75. return 0;
  76. }
  77. static const AVOption options[] = {
  78. { "sample_rate", "", offsetof(SndioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  79. { "channels", "", offsetof(SndioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  80. { NULL },
  81. };
  82. static const AVClass sndio_demuxer_class = {
  83. .class_name = "sndio indev",
  84. .item_name = av_default_item_name,
  85. .option = options,
  86. .version = LIBAVUTIL_VERSION_INT,
  87. .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
  88. };
  89. const FFInputFormat ff_sndio_demuxer = {
  90. .p.name = "sndio",
  91. .p.long_name = NULL_IF_CONFIG_SMALL("sndio audio capture"),
  92. .p.flags = AVFMT_NOFILE,
  93. .p.priv_class = &sndio_demuxer_class,
  94. .priv_data_size = sizeof(SndioData),
  95. .read_header = audio_read_header,
  96. .read_packet = audio_read_packet,
  97. .read_close = audio_read_close,
  98. };