lib571.c 5.6 KB

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