You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.3KB

  1. /*
  2. * RTP MPEG2TS depacketizer
  3. * Copyright (c) 2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/attributes.h"
  22. #include "mpegts.h"
  23. #include "rtpdec_formats.h"
  24. struct PayloadContext {
  25. struct MpegTSContext *ts;
  26. int read_buf_index;
  27. int read_buf_size;
  28. uint8_t buf[RTP_MAX_PACKET_LENGTH];
  29. };
  30. static void mpegts_close_context(PayloadContext *data)
  31. {
  32. if (!data)
  33. return;
  34. if (data->ts)
  35. ff_mpegts_parse_close(data->ts);
  36. }
  37. static av_cold int mpegts_init(AVFormatContext *ctx, int st_index,
  38. PayloadContext *data)
  39. {
  40. data->ts = ff_mpegts_parse_open(ctx);
  41. if (!data->ts)
  42. return AVERROR(ENOMEM);
  43. return 0;
  44. }
  45. static int mpegts_handle_packet(AVFormatContext *ctx, PayloadContext *data,
  46. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  47. const uint8_t *buf, int len, uint16_t seq,
  48. int flags)
  49. {
  50. int ret;
  51. // We don't want to use the RTP timestamps at all. If the mpegts demuxer
  52. // doesn't set any pts/dts, the generic rtpdec code shouldn't try to
  53. // fill it in either, since the mpegts and RTP timestamps are in totally
  54. // different ranges.
  55. *timestamp = RTP_NOTS_VALUE;
  56. if (!buf) {
  57. if (data->read_buf_index >= data->read_buf_size)
  58. return AVERROR(EAGAIN);
  59. ret = ff_mpegts_parse_packet(data->ts, pkt, data->buf + data->read_buf_index,
  60. data->read_buf_size - data->read_buf_index);
  61. if (ret < 0)
  62. return AVERROR(EAGAIN);
  63. data->read_buf_index += ret;
  64. if (data->read_buf_index < data->read_buf_size)
  65. return 1;
  66. else
  67. return 0;
  68. }
  69. ret = ff_mpegts_parse_packet(data->ts, pkt, buf, len);
  70. /* The only error that can be returned from ff_mpegts_parse_packet
  71. * is "no more data to return from the provided buffer", so return
  72. * AVERROR(EAGAIN) for all errors */
  73. if (ret < 0)
  74. return AVERROR(EAGAIN);
  75. if (ret < len) {
  76. data->read_buf_size = FFMIN(len - ret, sizeof(data->buf));
  77. memcpy(data->buf, buf + ret, data->read_buf_size);
  78. data->read_buf_index = 0;
  79. return 1;
  80. }
  81. return 0;
  82. }
  83. RTPDynamicProtocolHandler ff_mpegts_dynamic_handler = {
  84. .codec_type = AVMEDIA_TYPE_DATA,
  85. .priv_data_size = sizeof(PayloadContext),
  86. .parse_packet = mpegts_handle_packet,
  87. .init = mpegts_init,
  88. .close = mpegts_close_context,
  89. .static_payload_id = 33,
  90. };