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.

181 lines
6.5KB

  1. #include "rtp-payload.h"
  2. #include "rtp-profile.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #if defined(_WIN32) || defined(_WIN64)
  8. #define strcasecmp _stricmp
  9. #endif
  10. struct rtp_payload_test_t
  11. {
  12. int payload;
  13. const char* encoding;
  14. FILE* frtp;
  15. FILE* fsource;
  16. FILE* frtp2;
  17. FILE* fsource2;
  18. void* encoder;
  19. void* decoder;
  20. size_t size;
  21. uint8_t packet[64 * 1024];
  22. };
  23. static void* rtp_alloc(void* /*param*/, int bytes)
  24. {
  25. static uint8_t buffer[2 * 1024 * 1024 + 4] = { 0, 0, 0, 1, };
  26. assert(bytes <= sizeof(buffer) - 4);
  27. return buffer + 4;
  28. }
  29. static void rtp_free(void* /*param*/, void * /*packet*/)
  30. {
  31. }
  32. static int rtp_encode_packet(void* param, const void *packet, int bytes, uint32_t /*timestamp*/, int /*flags*/)
  33. {
  34. struct rtp_payload_test_t* ctx = (struct rtp_payload_test_t*)param;
  35. uint8_t size[2];
  36. size[0] = (uint8_t)((uint32_t)bytes >> 8);
  37. size[1] = (uint8_t)(uint32_t)bytes;
  38. fwrite(size, 1, sizeof(size), ctx->frtp2);
  39. fwrite(packet, 1, bytes, ctx->frtp2);
  40. return 0;
  41. }
  42. static int rtp_decode_packet(void* param, const void *packet, int bytes, uint32_t timestamp, int flags)
  43. {
  44. static const uint8_t start_code[4] = { 0, 0, 0, 1 };
  45. struct rtp_payload_test_t* ctx = (struct rtp_payload_test_t*)param;
  46. static uint8_t buffer[2 * 1024 * 1024];
  47. assert(bytes + 4 < sizeof(buffer));
  48. assert(0 == flags);
  49. size_t size = 0;
  50. if (0 == strcmp("H264", ctx->encoding) || 0 == strcmp("H265", ctx->encoding))
  51. {
  52. memcpy(buffer, start_code, sizeof(start_code));
  53. size += sizeof(start_code);
  54. }
  55. else if (0 == strcasecmp("mpeg4-generic", ctx->encoding))
  56. {
  57. int len = bytes + 7;
  58. uint8_t profile = 2;
  59. uint8_t sampling_frequency_index = 4;
  60. uint8_t channel_configuration = 2;
  61. buffer[0] = 0xFF; /* 12-syncword */
  62. buffer[1] = 0xF0 /* 12-syncword */ | (0 << 3)/*1-ID*/ | (0x00 << 2) /*2-layer*/ | 0x01 /*1-protection_absent*/;
  63. buffer[2] = ((profile - 1) << 6) | ((sampling_frequency_index & 0x0F) << 2) | ((channel_configuration >> 2) & 0x01);
  64. buffer[3] = ((channel_configuration & 0x03) << 6) | ((len >> 11) & 0x03); /*0-original_copy*/ /*0-home*/ /*0-copyright_identification_bit*/ /*0-copyright_identification_start*/
  65. buffer[4] = (uint8_t)(len >> 3);
  66. buffer[5] = ((len & 0x07) << 5) | 0x1F;
  67. buffer[6] = 0xFC | ((len / 1024) & 0x03);
  68. size = 7;
  69. }
  70. memcpy(buffer + size, packet, bytes);
  71. size += bytes;
  72. // TODO:
  73. // check media file
  74. fwrite(buffer, 1, size, ctx->fsource2);
  75. return rtp_payload_encode_input(ctx->encoder, buffer, size, timestamp);
  76. }
  77. void rtp_payload_test(int payload, const char* encoding, uint16_t seq, uint32_t ssrc, const char* rtpfile, const char* sourcefile)
  78. {
  79. struct rtp_payload_test_t ctx;
  80. ctx.payload = payload;
  81. ctx.encoding = encoding;
  82. ctx.frtp = fopen(rtpfile, "rb");
  83. ctx.fsource = fopen(sourcefile, "rb");
  84. ctx.frtp2 = fopen("out.rtp", "wb");
  85. ctx.fsource2 = fopen("out.media", "wb");
  86. rtp_packet_setsize(1456); // 1456(live555)
  87. struct rtp_payload_t handler2;
  88. handler2.alloc = rtp_alloc;
  89. handler2.free = rtp_free;
  90. handler2.packet = rtp_encode_packet;
  91. ctx.encoder = rtp_payload_encode_create(payload, encoding, seq, ssrc, &handler2, &ctx);
  92. struct rtp_payload_t handler1;
  93. handler1.alloc = rtp_alloc;
  94. handler1.free = rtp_free;
  95. handler1.packet = rtp_decode_packet;
  96. ctx.decoder = rtp_payload_decode_create(payload, encoding, &handler1, &ctx);
  97. while (1)
  98. {
  99. uint8_t s2[2];
  100. if (2 != fread(s2, 1, 2, ctx.frtp))
  101. break;
  102. ctx.size = (s2[0] << 8) | s2[1];
  103. assert(ctx.size < sizeof(ctx.packet));
  104. if (ctx.size != (int)fread(ctx.packet, 1, ctx.size, ctx.frtp))
  105. break;
  106. rtp_payload_decode_input(ctx.decoder, ctx.packet, ctx.size);
  107. }
  108. fclose(ctx.frtp);
  109. fclose(ctx.fsource);
  110. fclose(ctx.frtp2);
  111. fclose(ctx.fsource2);
  112. rtp_payload_decode_destroy(ctx.decoder);
  113. rtp_payload_encode_destroy(ctx.encoder);
  114. }
  115. void binary_diff(const char* f1, const char* f2);
  116. void rtp_payload_test()
  117. {
  118. //rtp_payload_test(33, "MP2T", 24470, 1726408532, "E:\\video\\rtp\\bipbop-gear1-all.ts.rtp", "E:\\video\\rtp\\bipbop-gear1-all.ts");
  119. //binary_diff("E:\\video\\rtp\\bipbop-gear1-all.ts.rtp", "out.rtp");
  120. //binary_diff("E:\\video\\rtp\\bipbop-gear1-all.ts", "out.media");
  121. //rtp_payload_test(96, "H264", 12686, 1957754144, "E:\\video\\rtp\\live555-test.h264.rtp", "E:\\video\\rtp\\live555-test.h264");
  122. //binary_diff("E:\\video\\rtp\\live555-test.h264.rtp", "out.rtp");
  123. //binary_diff("E:\\video\\rtp\\live555-test.h264", "out.media");
  124. //rtp_payload_test(96, "H265", 64791, 850623724, "E:\\video\\rtp\\live555-surfing.h265.rtp", "E:\\video\\rtp\\live555-surfing.h265");
  125. //binary_diff("E:\\video\\rtp\\live555-surfing.h265.rtp", "out.rtp");
  126. //binary_diff("E:\\video\\rtp\\live555-surfing.h265", "out.media");
  127. //rtp_payload_test(RTP_PAYLOAD_MPA, "", 51375, 62185158, "E:\\video\\rtp\\24kbps.mp3.rtp", "E:\\video\\rtp\\24kbps.mp3");
  128. //binary_diff("E:\\video\\rtp\\24kbps.mp3.rtp", "out.rtp");
  129. //binary_diff("E:\\video\\rtp\\24kbps.mp3", "out.media");
  130. //rtp_payload_test(RTP_PAYLOAD_MPA, "", 59489, 4023046964, "E:\\video\\rtp\\128kbps.mp3.rtp", "E:\\video\\rtp\\128kbps.mp3");
  131. //binary_diff("E:\\video\\rtp\\128kbps.mp3.rtp", "out.rtp");
  132. //binary_diff("E:\\video\\rtp\\live555-surfing.h265", "out.media");
  133. //rtp_payload_test(RTP_PAYLOAD_MPV, "", 6879, 2417761871, "E:\\video\\rtp\\movie2.mpv.rtp", "E:\\video\\rtp\\movie2.mpv");
  134. //binary_diff("E:\\video\\rtp\\movie2.mpv.rtp", "out.rtp");
  135. //binary_diff("E:\\video\\rtp\\movie2.mpv", "out.media");
  136. //rtp_payload_test(96, "MP4V-ES", 28210, 11391760, "E:\\video\\rtp\\live555-petrov.m4e.MP4V-ES.rtp", "E:\\video\\rtp\\movie2.mpv");
  137. //binary_diff("E:\\video\\rtp\\live555-petrov.m4e.MP4V-ES.rtp", "out.rtp");
  138. //binary_diff("E:\\video\\rtp\\live555-petrov.m4e", "out.media");
  139. //rtp_payload_test(96, "mpeg4-generic", 13353, 1082077255, "E:\\video\\rtp\\live555-test.aac.rtp", "E:\\video\\rtp\\live555-test.aac");
  140. //binary_diff("E:\\video\\rtp\\live555-test.aac.rtp", "out.rtp");
  141. //binary_diff("E:\\video\\rtp\\live555-test.aac", "out.media");
  142. //rtp_payload_test(96, "MP4A-LATM", 3647, 345042127, "E:\\video\\rtp\\720p.96.MP4A-LATM.rtp", "E:\\video\\rtp\\720p.96.MP4A-LATM");
  143. //binary_diff("E:\\video\\rtp\\720p.96.MP4A-LATM.rtp", "out.rtp");
  144. //binary_diff("E:\\video\\rtp\\720p.96.MP4A-LATM", "out.media");
  145. //rtp_payload_test(96, "VP8", 2948, 1447139175, "E:\\work\\media-server\\rtmp2hls\\192.168.31.132.6974.96.VP8.rtp", "E:\\work\\media-server\\rtmp2hls\\192.168.31.132.6974.96.VP8");
  146. //binary_diff("E:\\work\\media-server\\rtmp2hls\\192.168.31.132.6974.96.VP8.rtp", "out.rtp");
  147. //binary_diff("E:\\work\\media-server\\rtmp2hls\\192.168.31.132.6974.96.VP8", "out.media");
  148. }