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.

99 lines
2.6KB

  1. #include "rtp-dump.h"
  2. #include "rtp-demuxer.h"
  3. #include "rtsp-demuxer.h"
  4. #include "avpkt2bs.h"
  5. #include <stdio.h>
  6. #include <assert.h>
  7. #include <stdint.h>
  8. #include <inttypes.h>
  9. #define RTP_DEMUXER 0
  10. struct rtp_dump_test_t
  11. {
  12. #if RTP_DEMUXER
  13. struct rtp_demuxer_t* demuxer;
  14. #else
  15. struct rtsp_demuxer_t* demuxer;
  16. struct avpkt2bs_t bs;
  17. #endif
  18. FILE* fp;
  19. };
  20. #if RTP_DEMUXER
  21. static int rtp_onpacket(void* param, const void* packet, int bytes, uint32_t timestamp, int flags)
  22. {
  23. int r;
  24. struct rtp_dump_test_t* ctx = (struct rtp_dump_test_t*)param;
  25. r = (int)fwrite(packet, 1, bytes, ctx->fp);
  26. assert(r == bytes);
  27. return r == bytes ? 0 : ferror(ctx->fp);
  28. }
  29. #else
  30. static int rtsp_onpacket(void* param, struct avpacket_t* pkt)
  31. {
  32. int r;
  33. struct rtp_dump_test_t* ctx = (struct rtp_dump_test_t*)param;
  34. static int64_t s_dts = 0;
  35. if (0 == s_dts)
  36. s_dts = pkt->dts;
  37. printf("[%d:0x%x] pts: %" PRId64 ", dts: %" PRId64 ", cts: %" PRId64 ", diff: %" PRId64 ", bytes: %d\n", pkt->stream->stream, (unsigned int)pkt->stream->codecid, pkt->pts, pkt->dts, pkt->pts - pkt->dts, pkt->dts - s_dts, pkt->size);
  38. s_dts = pkt->dts;
  39. r = avpkt2bs_input(&ctx->bs, pkt);
  40. fwrite(ctx->bs.ptr, 1, r, ctx->fp);
  41. //r = (int)fwrite(pkt->data, 1, pkt->size, ctx->fp);
  42. //assert(r == pkt->size);
  43. return 0;
  44. }
  45. #endif
  46. void rtp_dump_test(const char* file)
  47. {
  48. int r;
  49. uint8_t data[1500];
  50. uint32_t clock;
  51. struct rtpdump_t* dump;
  52. FILE* fp;
  53. struct rtp_dump_test_t ctx;
  54. ctx.fp = fopen("rtp.bin", "wb");
  55. dump = rtpdump_open(file, 0);
  56. #if RTP_DEMUXER
  57. ctx.demuxer = rtp_demuxer_create(0, 100, 90000, 100, "MP2P", rtp_onpacket, &ctx);
  58. #else
  59. avpkt2bs_create(&ctx.bs);
  60. ctx.demuxer = rtsp_demuxer_create(0, 100, rtsp_onpacket, &ctx);
  61. //r = rtsp_demuxer_add_payload(ctx.demuxer, 90000, 96, "PS", "");
  62. r = rtsp_demuxer_add_payload(ctx.demuxer, 90000, 100, "H264", "100 profile-level-id=420028;sprop-parameter-sets=Z0IAKOkBQHsg,aM44gA==");
  63. //r = rtsp_demuxer_add_payload(ctx.demuxer, 90000, 99, "H264", "99 packetization-mode=1;profile-level-id=4D4033; sprop-parameter-sets=Z01AM5pkAeACH/4C3AQEBQAAAwPoAAB1MOhgAJ/8AAE/8i7y40MABP/gAAn/kXeXCgA=,aO44gA==");
  64. //r = rtsp_demuxer_add_payload(ctx.demuxer, 44100, 121, "MP4A-LATM", "121 config=4000242000;cpresent=0;object=2;profile-level-id=1");
  65. assert(0 == r);
  66. #endif
  67. while (1)
  68. {
  69. r = rtpdump_read(dump, &clock, data, sizeof(data));
  70. if (r <= 0)
  71. break;
  72. #if RTP_DEMUXER
  73. r = rtp_demuxer_input(ctx.demuxer, data, r);
  74. #else
  75. r = rtsp_demuxer_input(ctx.demuxer, data, r);
  76. #endif
  77. assert(r >= 0);
  78. }
  79. #if RTP_DEMUXER
  80. rtp_demuxer_destroy(&ctx.demuxer);
  81. #else
  82. rtsp_demuxer_destroy(ctx.demuxer);
  83. avpkt2bs_destroy(&ctx.bs);
  84. #endif
  85. rtpdump_close(dump);
  86. fclose(ctx.fp);
  87. }