switch_packetizer.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  3. * Copyright (C) 2005-2020, Anthony Minessale II <anthm@freeswitch.org>
  4. *
  5. * Version: MPL 1.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Seven Du <seven@signalwire.com>
  21. * Portions created by the Initial Developer are Copyright (C)
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. *
  26. *
  27. * switch_packetizer.c unit test
  28. *
  29. */
  30. #include <test/switch_test.h>
  31. #include <switch_packetizer.h>
  32. #define SLICE_SIZE 4
  33. FST_CORE_BEGIN("conf")
  34. {
  35. FST_SUITE_BEGIN(switch_packetizer)
  36. {
  37. FST_SETUP_BEGIN()
  38. {
  39. }
  40. FST_SETUP_END()
  41. FST_TEST_BEGIN(test_packetizer_bitstream)
  42. {
  43. switch_packetizer_t *packetizer = switch_packetizer_create(SPT_H264_BITSTREAM, SLICE_SIZE);
  44. uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE];
  45. switch_frame_t frame = {0};
  46. switch_status_t status;
  47. uint8_t h264data[] = {0, 0, 0, 1, 0x67, 1, 2, 0, 0, 0, 1, 0x68, 1, 2, 0, 0, 0, 1, 0x65, 1, 2, 3, 4, 5, 6};
  48. frame.data = data;
  49. frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  50. switch_set_flag(&frame, SFF_ENCODED);
  51. status = switch_packetizer_feed(packetizer, h264data, sizeof(h264data));
  52. fst_requires(status == SWITCH_STATUS_SUCCESS);
  53. status = switch_packetizer_read(packetizer, &frame);
  54. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  55. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  56. fst_requires(frame.datalen == 3);
  57. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  58. status = switch_packetizer_read(packetizer, &frame);
  59. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  60. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  61. fst_requires(frame.datalen == 3);
  62. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  63. status = switch_packetizer_read(packetizer, &frame);
  64. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  65. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  66. fst_requires(frame.datalen == 4);
  67. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  68. status = switch_packetizer_read(packetizer, &frame);
  69. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  70. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  71. fst_requires(frame.datalen == 4);
  72. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  73. status = switch_packetizer_read(packetizer, &frame);
  74. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  75. fst_requires(status == SWITCH_STATUS_SUCCESS);
  76. fst_requires(frame.datalen == 4);
  77. switch_packetizer_close(&packetizer);
  78. }
  79. FST_TEST_END()
  80. FST_TEST_BEGIN(test_packetizer_sized_bitstream_has_sps_pps)
  81. {
  82. switch_packetizer_t *packetizer = switch_packetizer_create(SPT_H264_SIZED_BITSTREAM, SLICE_SIZE);
  83. uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE];
  84. switch_frame_t frame = {0};
  85. switch_status_t status;
  86. uint8_t h264data[] = {0, 0, 0, 3, 0x67, 1, 2, 0, 0, 0, 3, 0x68, 1, 2, 0, 0, 0, 7, 0x65, 1, 2, 3, 4, 5, 6};
  87. frame.data = data;
  88. frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  89. switch_set_flag(&frame, SFF_ENCODED);
  90. status = switch_packetizer_feed(packetizer, h264data, sizeof(h264data));
  91. fst_requires(status == SWITCH_STATUS_SUCCESS);
  92. status = switch_packetizer_read(packetizer, &frame);
  93. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  94. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  95. fst_requires(frame.datalen == 3);
  96. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  97. status = switch_packetizer_read(packetizer, &frame);
  98. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  99. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  100. fst_requires(frame.datalen == 3);
  101. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  102. status = switch_packetizer_read(packetizer, &frame);
  103. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  104. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  105. fst_requires(frame.datalen == 4);
  106. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  107. status = switch_packetizer_read(packetizer, &frame);
  108. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  109. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  110. fst_requires(frame.datalen == 4);
  111. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  112. status = switch_packetizer_read(packetizer, &frame);
  113. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  114. fst_requires(status == SWITCH_STATUS_SUCCESS);
  115. fst_requires(frame.datalen == 4);
  116. fst_check(frame.m == 1);
  117. switch_packetizer_close(&packetizer);
  118. }
  119. FST_TEST_END()
  120. FST_TEST_BEGIN(test_packetizer_sized_bitstream_no_sps_pps)
  121. {
  122. switch_packetizer_t *packetizer = switch_packetizer_create(SPT_H264_SIZED_BITSTREAM, SLICE_SIZE);
  123. uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE];
  124. switch_frame_t frame = {0};
  125. switch_status_t status;
  126. uint8_t h264data[] = {0, 0, 0, 3, 0x06, 1, 2, 0, 0, 0, 3, 0x09, 1, 2, 0, 0, 0, 7, 0x65, 1, 2, 3, 4, 5, 6};
  127. uint8_t extradata[] = {0x01, 0x64, 0x00, 0x1e, 0xff, 0xe1, 0x00, 0x03, 0x67, 0x64, 0x00, 0xe1, 0x00, 0x03, 0x68, 0x01, 0x02};
  128. // 1 fps 3 bytes 1pps 3 bytes
  129. frame.data = data;
  130. frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  131. switch_set_flag(&frame, SFF_ENCODED);
  132. status = switch_packetizer_feed_extradata(packetizer, extradata, sizeof(extradata));
  133. fst_requires(status == SWITCH_STATUS_SUCCESS);
  134. status = switch_packetizer_feed(packetizer, h264data, sizeof(h264data));
  135. fst_requires(status == SWITCH_STATUS_SUCCESS);
  136. status = switch_packetizer_read(packetizer, &frame);
  137. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  138. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  139. fst_requires(frame.datalen == 3);
  140. fst_check((*(uint8_t *)frame.data & 0x1f) == 0x06);
  141. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  142. status = switch_packetizer_read(packetizer, &frame);
  143. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u %x\n", frame.datalen, *(uint8_t *)frame.data);
  144. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  145. fst_requires(frame.datalen == 3);
  146. fst_check((*(uint8_t *)frame.data & 0x1f) == 0x09);
  147. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  148. status = switch_packetizer_read(packetizer, &frame);
  149. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u %x\n", frame.datalen, *(uint8_t *)frame.data);
  150. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  151. fst_requires(frame.datalen == 3);
  152. fst_check((*(uint8_t *)frame.data & 0x1f) == 0x07);
  153. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  154. status = switch_packetizer_read(packetizer, &frame);
  155. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u %x\n", frame.datalen, *(uint8_t *)frame.data);
  156. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  157. fst_requires(frame.datalen == 3);
  158. fst_check((*(uint8_t *)frame.data & 0x1f) == 0x08);
  159. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  160. status = switch_packetizer_read(packetizer, &frame);
  161. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u %x\n", frame.datalen, *(uint8_t *)frame.data);
  162. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  163. fst_requires(frame.datalen == 4);
  164. fst_check((*(uint8_t *)frame.data & 0x1f) == 0x1c);
  165. fst_check(frame.m == 0);
  166. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  167. status = switch_packetizer_read(packetizer, &frame);
  168. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u %x\n", frame.datalen, *(uint8_t *)frame.data);
  169. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  170. fst_requires(frame.datalen == 4);
  171. fst_check((*(uint8_t *)frame.data & 0x1f) == 0x1c);
  172. fst_check(frame.m == 0);
  173. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  174. status = switch_packetizer_read(packetizer, &frame);
  175. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u %x\n", frame.datalen, *(uint8_t *)frame.data);
  176. fst_requires(status == SWITCH_STATUS_SUCCESS);
  177. fst_requires(frame.datalen == 4);
  178. fst_check((*(uint8_t *)frame.data & 0x1f) == 0x1c);
  179. fst_check(frame.m == 1);
  180. switch_packetizer_close(&packetizer);
  181. }
  182. FST_TEST_END()
  183. FST_TEST_BEGIN(test_packetizer_invalid)
  184. {
  185. switch_packetizer_t *packetizer = switch_packetizer_create(SPT_H264_BITSTREAM, SLICE_SIZE);
  186. uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE];
  187. switch_frame_t frame = {0};
  188. switch_status_t status;
  189. uint8_t h264data[] = {0, 0, 2, 9, 0x67, 1, 2, 0, 0, 0, 0, 0x68, 1, 2, 0, 0, 0, 0, 0x65, 1, 2, 3, 4, 5, 6};
  190. frame.data = data;
  191. frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  192. switch_set_flag(&frame, SFF_ENCODED);
  193. status = switch_packetizer_feed(packetizer, h264data, sizeof(h264data));
  194. fst_requires(status == SWITCH_STATUS_SUCCESS);
  195. status = switch_packetizer_read(packetizer, &frame);
  196. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "status = %d datalen = %u\n", status, frame.datalen);
  197. fst_requires(status == SWITCH_STATUS_FALSE);
  198. switch_packetizer_close(&packetizer);
  199. }
  200. FST_TEST_END()
  201. FST_TEARDOWN_BEGIN()
  202. {
  203. }
  204. FST_TEARDOWN_END()
  205. }
  206. FST_SUITE_END()
  207. }
  208. FST_CORE_END()