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.

165 lines
3.1KB

  1. #include "rtp-queue.h"
  2. #include <time.h>
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <vector>
  9. #define N 10000 // count
  10. #define Q 400 // timestamp queue length, Q > RTP_MISORDER
  11. #define RTP_LOST 10 // 10%
  12. #define RTP_MISORDER 3 // 0.3%
  13. #define RTP_DUPLICATE 3 // 3%
  14. #define RTP_SEEK 5 // 0.05%
  15. static uint16_t s_input_seq[N];
  16. static uint16_t s_output_seq[N];
  17. struct rtp_queue_test_t
  18. {
  19. rtp_queue_t* q;
  20. int input_lost;
  21. int output_lost;
  22. int count_seek;
  23. int count_lost;
  24. int count_misorder;
  25. int count_duplicate;
  26. int count_input;
  27. int count_output;
  28. };
  29. static void rtp_packet_free(void* param, struct rtp_packet_t* pkt)
  30. {
  31. free(pkt); (void)param;
  32. }
  33. static struct rtp_packet_t* rtp_queue_packet_alloc(uint16_t seq, uint32_t timestamp)
  34. {
  35. struct rtp_packet_t* pkt;
  36. pkt = (struct rtp_packet_t*)calloc(1, sizeof(*pkt));
  37. if (!pkt) return pkt;
  38. pkt->rtp.seq = seq;
  39. pkt->rtp.timestamp = timestamp;
  40. return pkt;
  41. }
  42. void rtp_queue_test2(void)
  43. {
  44. std::vector<uint32_t> buckets;
  45. struct rtp_queue_test_t test;
  46. struct rtp_packet_t* pkt;
  47. memset(&test, 0, sizeof(test));
  48. test.q = rtp_queue_create(200, 90000, rtp_packet_free, NULL);
  49. //srand((unsigned int)time(NULL));
  50. srand(1);
  51. uint32_t clock = rand() * 90;
  52. printf("begin seq: %hu\n", (uint16_t)(clock / 10));
  53. for (int i = 0; i < N; i++)
  54. {
  55. int kick = rand();
  56. // [9995 ~ 9999]
  57. if (kick % 10000 > 10000 - RTP_SEEK)
  58. {
  59. clock += rand() * 90;
  60. printf("seek seq: %hu\n", (uint16_t)(clock / 10));
  61. test.count_seek++;
  62. }
  63. do
  64. {
  65. buckets.push_back(clock);
  66. clock += 10; // 100fps
  67. } while (buckets.size() < Q);
  68. uint32_t timestamp = buckets.front();
  69. uint16_t seq = (uint16_t)(timestamp / 10);
  70. buckets.erase(buckets.begin());
  71. // [0 ~ 9]
  72. if (kick % 100 < RTP_LOST)
  73. {
  74. printf("lost: %hu\n", seq);
  75. test.count_lost++;
  76. continue;
  77. }
  78. // [997 ~ 999]
  79. if (kick % 1000 > 1000 - RTP_MISORDER)
  80. {
  81. printf("misorder: %hu\n", seq);
  82. buckets.insert(buckets.begin() + (seq % 10), timestamp);
  83. test.count_misorder++;
  84. continue;
  85. }
  86. // [97 ~ 99]
  87. if (kick % 100 > 100 - RTP_DUPLICATE)
  88. {
  89. printf("duplicate: %hu\n", seq);
  90. buckets.insert(buckets.begin() + (seq % 10), timestamp);
  91. test.count_duplicate++;
  92. }
  93. pkt = rtp_queue_packet_alloc(seq, timestamp * 90);
  94. s_input_seq[test.count_input++] = pkt->rtp.seq;
  95. if (rtp_queue_write(test.q, pkt) < 1)
  96. {
  97. printf("discard: %hu\n", pkt->rtp.seq);
  98. free(pkt);
  99. continue;
  100. }
  101. pkt = rtp_queue_read(test.q);
  102. if (!pkt)
  103. continue;
  104. s_output_seq[test.count_output++] = pkt->rtp.seq;
  105. free(pkt);
  106. }
  107. rtp_queue_destroy(test.q);
  108. printf("result: \n");
  109. int j = 0;
  110. for (int i = 0; i < test.count_input; i++)
  111. {
  112. if (s_input_seq[i] == s_output_seq[j])
  113. {
  114. printf("%hu, ", s_input_seq[i]);
  115. j++;
  116. }
  117. else
  118. {
  119. // try find misorder
  120. int found = 0;
  121. for (int k = i + 1; k < test.count_input && k < i + 20; k++)
  122. {
  123. if (s_input_seq[k] == s_output_seq[j])
  124. {
  125. j++;
  126. printf("%hu, ", s_input_seq[i]);
  127. found = 1;
  128. break;
  129. }
  130. }
  131. if(0 == found)
  132. printf("[%hu], ", s_input_seq[i]);
  133. }
  134. }
  135. //assert(test.input_lost == test.output_lost);
  136. }