lib571.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "test.h"
  25. #ifdef HAVE_NETINET_IN_H
  26. # include <netinet/in.h>
  27. #endif
  28. #ifdef HAVE_NETDB_H
  29. # include <netdb.h>
  30. #endif
  31. #ifdef HAVE_ARPA_INET_H
  32. # include <arpa/inet.h>
  33. #endif
  34. #ifdef HAVE_SYS_STAT_H
  35. # include <sys/stat.h>
  36. #endif
  37. #ifdef HAVE_FCNTL_H
  38. # include <fcntl.h>
  39. #endif
  40. #include "warnless.h"
  41. #include "memdebug.h"
  42. #define RTP_PKT_CHANNEL(p) ((int)((unsigned char)((p)[1])))
  43. #define RTP_PKT_LENGTH(p) ((((int)((unsigned char)((p)[2]))) << 8) | \
  44. ((int)((unsigned char)((p)[3]))))
  45. #define RTP_DATA_SIZE 12
  46. static const char *RTP_DATA = "$_1234\n\0asdf";
  47. static int rtp_packet_count = 0;
  48. static size_t rtp_write(void *ptr, size_t size, size_t nmemb, void *stream)
  49. {
  50. char *data = (char *)ptr;
  51. int channel = RTP_PKT_CHANNEL(data);
  52. int message_size;
  53. int coded_size = RTP_PKT_LENGTH(data);
  54. size_t failure = (size && nmemb) ? 0 : 1;
  55. int i;
  56. (void)stream;
  57. message_size = curlx_uztosi(size * nmemb) - 4;
  58. printf("RTP: message size %d, channel %d\n", message_size, channel);
  59. if(message_size != coded_size) {
  60. printf("RTP embedded size (%d) does not match the write size (%d).\n",
  61. coded_size, message_size);
  62. return failure;
  63. }
  64. data += 4;
  65. for(i = 0; i < message_size; i += RTP_DATA_SIZE) {
  66. if(message_size - i > RTP_DATA_SIZE) {
  67. if(memcmp(RTP_DATA, data + i, RTP_DATA_SIZE) != 0) {
  68. printf("RTP PAYLOAD CORRUPTED [%s]\n", data + i);
  69. return failure;
  70. }
  71. }
  72. else {
  73. if(memcmp(RTP_DATA, data + i, message_size - i) != 0) {
  74. printf("RTP PAYLOAD END CORRUPTED (%d), [%s]\n",
  75. message_size - i, data + i);
  76. return failure;
  77. }
  78. }
  79. }
  80. rtp_packet_count++;
  81. fprintf(stderr, "packet count is %d\n", rtp_packet_count);
  82. return size * nmemb;
  83. }
  84. /* build request url */
  85. static char *suburl(const char *base, int i)
  86. {
  87. return curl_maprintf("%s%.4d", base, i);
  88. }
  89. int test(char *URL)
  90. {
  91. int res;
  92. CURL *curl;
  93. char *stream_uri = NULL;
  94. int request = 1;
  95. FILE *protofile = fopen(libtest_arg2, "wb");
  96. if(!protofile) {
  97. fprintf(stderr, "Couldn't open the protocol dump file\n");
  98. return TEST_ERR_MAJOR_BAD;
  99. }
  100. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  101. fprintf(stderr, "curl_global_init() failed\n");
  102. fclose(protofile);
  103. return TEST_ERR_MAJOR_BAD;
  104. }
  105. curl = curl_easy_init();
  106. if(!curl) {
  107. fprintf(stderr, "curl_easy_init() failed\n");
  108. fclose(protofile);
  109. curl_global_cleanup();
  110. return TEST_ERR_MAJOR_BAD;
  111. }
  112. test_setopt(curl, CURLOPT_URL, URL);
  113. stream_uri = suburl(URL, request++);
  114. if(!stream_uri) {
  115. res = TEST_ERR_MAJOR_BAD;
  116. goto test_cleanup;
  117. }
  118. test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
  119. free(stream_uri);
  120. stream_uri = NULL;
  121. test_setopt(curl, CURLOPT_INTERLEAVEFUNCTION, rtp_write);
  122. test_setopt(curl, CURLOPT_TIMEOUT, 3L);
  123. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  124. test_setopt(curl, CURLOPT_WRITEDATA, protofile);
  125. test_setopt(curl, CURLOPT_RTSP_TRANSPORT, "RTP/AVP/TCP;interleaved=0-1");
  126. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_SETUP);
  127. res = curl_easy_perform(curl);
  128. if(res)
  129. goto test_cleanup;
  130. /* This PLAY starts the interleave */
  131. stream_uri = suburl(URL, request++);
  132. if(!stream_uri) {
  133. res = TEST_ERR_MAJOR_BAD;
  134. goto test_cleanup;
  135. }
  136. test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
  137. free(stream_uri);
  138. stream_uri = NULL;
  139. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_PLAY);
  140. res = curl_easy_perform(curl);
  141. if(res)
  142. goto test_cleanup;
  143. /* The DESCRIBE request will try to consume data after the Content */
  144. stream_uri = suburl(URL, request++);
  145. if(!stream_uri) {
  146. res = TEST_ERR_MAJOR_BAD;
  147. goto test_cleanup;
  148. }
  149. test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
  150. free(stream_uri);
  151. stream_uri = NULL;
  152. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_DESCRIBE);
  153. res = curl_easy_perform(curl);
  154. if(res)
  155. goto test_cleanup;
  156. stream_uri = suburl(URL, request++);
  157. if(!stream_uri) {
  158. res = TEST_ERR_MAJOR_BAD;
  159. goto test_cleanup;
  160. }
  161. test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
  162. free(stream_uri);
  163. stream_uri = NULL;
  164. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_PLAY);
  165. res = curl_easy_perform(curl);
  166. if(res)
  167. goto test_cleanup;
  168. fprintf(stderr, "PLAY COMPLETE\n");
  169. /* Use Receive to get the rest of the data */
  170. while(!res && rtp_packet_count < 13) {
  171. fprintf(stderr, "LOOPY LOOP!\n");
  172. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_RECEIVE);
  173. res = curl_easy_perform(curl);
  174. }
  175. test_cleanup:
  176. free(stream_uri);
  177. if(protofile)
  178. fclose(protofile);
  179. curl_easy_cleanup(curl);
  180. curl_global_cleanup();
  181. return res;
  182. }