switch_packetizer.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  51. switch_set_flag(&frame, SFF_ENCODED);
  52. status = switch_packetizer_feed(packetizer, h264data, sizeof(h264data));
  53. fst_requires(status == SWITCH_STATUS_SUCCESS);
  54. status = switch_packetizer_read(packetizer, &frame);
  55. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  56. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  57. fst_requires(frame.datalen == 3);
  58. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  59. status = switch_packetizer_read(packetizer, &frame);
  60. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  61. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  62. fst_requires(frame.datalen == 3);
  63. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  64. status = switch_packetizer_read(packetizer, &frame);
  65. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  66. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  67. fst_requires(frame.datalen == 4);
  68. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  69. status = switch_packetizer_read(packetizer, &frame);
  70. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  71. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  72. fst_requires(frame.datalen == 4);
  73. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  74. status = switch_packetizer_read(packetizer, &frame);
  75. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  76. fst_requires(status == SWITCH_STATUS_SUCCESS);
  77. fst_requires(frame.datalen == 4);
  78. switch_packetizer_close(&packetizer);
  79. }
  80. FST_TEST_END()
  81. FST_TEST_BEGIN(test_packetizer_sized_bitstream_has_sps_pps)
  82. {
  83. switch_packetizer_t *packetizer = switch_packetizer_create(SPT_H264_SIZED_BITSTREAM, SLICE_SIZE);
  84. uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE];
  85. switch_frame_t frame = {0};
  86. switch_status_t status;
  87. 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};
  88. frame.data = data;
  89. frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  90. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  91. switch_set_flag(&frame, SFF_ENCODED);
  92. status = switch_packetizer_feed(packetizer, h264data, sizeof(h264data));
  93. fst_requires(status == SWITCH_STATUS_SUCCESS);
  94. status = switch_packetizer_read(packetizer, &frame);
  95. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  96. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  97. fst_requires(frame.datalen == 3);
  98. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  99. status = switch_packetizer_read(packetizer, &frame);
  100. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  101. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  102. fst_requires(frame.datalen == 3);
  103. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  104. status = switch_packetizer_read(packetizer, &frame);
  105. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  106. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  107. fst_requires(frame.datalen == 4);
  108. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  109. status = switch_packetizer_read(packetizer, &frame);
  110. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  111. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  112. fst_requires(frame.datalen == 4);
  113. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  114. status = switch_packetizer_read(packetizer, &frame);
  115. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  116. fst_requires(status == SWITCH_STATUS_SUCCESS);
  117. fst_requires(frame.datalen == 4);
  118. fst_check(frame.m == 1);
  119. switch_packetizer_close(&packetizer);
  120. }
  121. FST_TEST_END()
  122. FST_TEST_BEGIN(test_packetizer_sized_bitstream_no_sps_pps)
  123. {
  124. switch_packetizer_t *packetizer = switch_packetizer_create(SPT_H264_SIZED_BITSTREAM, SLICE_SIZE);
  125. uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE];
  126. switch_frame_t frame = {0};
  127. switch_status_t status;
  128. 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};
  129. uint8_t extradata[] = {0x01, 0x64, 0x00, 0x1e, 0xff, 0xe1, 0x00, 0x03, 0x67, 0x64, 0x00, 0xe1, 0x00, 0x03, 0x68, 0x01, 0x02};
  130. // 1 fps 3 bytes 1pps 3 bytes
  131. frame.data = data;
  132. frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  133. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  134. switch_set_flag(&frame, SFF_ENCODED);
  135. status = switch_packetizer_feed_extradata(packetizer, extradata, sizeof(extradata));
  136. fst_requires(status == SWITCH_STATUS_SUCCESS);
  137. status = switch_packetizer_feed(packetizer, h264data, sizeof(h264data));
  138. fst_requires(status == SWITCH_STATUS_SUCCESS);
  139. status = switch_packetizer_read(packetizer, &frame);
  140. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u\n", frame.datalen);
  141. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  142. fst_requires(frame.datalen == 3);
  143. fst_check((*(uint8_t *)frame.data & 0x1f) == 0x06);
  144. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  145. status = switch_packetizer_read(packetizer, &frame);
  146. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u %x\n", frame.datalen, *(uint8_t *)frame.data);
  147. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  148. fst_requires(frame.datalen == 3);
  149. fst_check((*(uint8_t *)frame.data & 0x1f) == 0x09);
  150. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  151. status = switch_packetizer_read(packetizer, &frame);
  152. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u %x\n", frame.datalen, *(uint8_t *)frame.data);
  153. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  154. fst_requires(frame.datalen == 3);
  155. fst_check((*(uint8_t *)frame.data & 0x1f) == 0x07);
  156. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  157. status = switch_packetizer_read(packetizer, &frame);
  158. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u %x\n", frame.datalen, *(uint8_t *)frame.data);
  159. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  160. fst_requires(frame.datalen == 3);
  161. fst_check((*(uint8_t *)frame.data & 0x1f) == 0x08);
  162. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  163. status = switch_packetizer_read(packetizer, &frame);
  164. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u %x\n", frame.datalen, *(uint8_t *)frame.data);
  165. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  166. fst_requires(frame.datalen == 4);
  167. fst_check((*(uint8_t *)frame.data & 0x1f) == 0x1c);
  168. fst_check(frame.m == 0);
  169. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  170. status = switch_packetizer_read(packetizer, &frame);
  171. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u %x\n", frame.datalen, *(uint8_t *)frame.data);
  172. fst_requires(status == SWITCH_STATUS_MORE_DATA);
  173. fst_requires(frame.datalen == 4);
  174. fst_check((*(uint8_t *)frame.data & 0x1f) == 0x1c);
  175. fst_check(frame.m == 0);
  176. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  177. status = switch_packetizer_read(packetizer, &frame);
  178. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%u %x\n", frame.datalen, *(uint8_t *)frame.data);
  179. fst_requires(status == SWITCH_STATUS_SUCCESS);
  180. fst_requires(frame.datalen == 4);
  181. fst_check((*(uint8_t *)frame.data & 0x1f) == 0x1c);
  182. fst_check(frame.m == 1);
  183. switch_packetizer_close(&packetizer);
  184. }
  185. FST_TEST_END()
  186. FST_TEST_BEGIN(test_packetizer_invalid)
  187. {
  188. switch_packetizer_t *packetizer = switch_packetizer_create(SPT_H264_BITSTREAM, SLICE_SIZE);
  189. uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE];
  190. switch_frame_t frame = {0};
  191. switch_status_t status;
  192. 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};
  193. frame.data = data;
  194. frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  195. frame.datalen = SWITCH_RECOMMENDED_BUFFER_SIZE;
  196. switch_set_flag(&frame, SFF_ENCODED);
  197. status = switch_packetizer_feed(packetizer, h264data, sizeof(h264data));
  198. fst_requires(status == SWITCH_STATUS_SUCCESS);
  199. status = switch_packetizer_read(packetizer, &frame);
  200. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "status = %d datalen = %u\n", status, frame.datalen);
  201. fst_requires(status == SWITCH_STATUS_FALSE);
  202. switch_packetizer_close(&packetizer);
  203. }
  204. FST_TEST_END()
  205. FST_TEARDOWN_BEGIN()
  206. {
  207. }
  208. FST_TEARDOWN_END()
  209. }
  210. FST_SUITE_END()
  211. }
  212. FST_CORE_END()