lib571.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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 = NULL;
  94. protofile = fopen(libtest_arg2, "wb");
  95. if(protofile == NULL) {
  96. fprintf(stderr, "Couldn't open the protocol dump file\n");
  97. return TEST_ERR_MAJOR_BAD;
  98. }
  99. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  100. fprintf(stderr, "curl_global_init() failed\n");
  101. fclose(protofile);
  102. return TEST_ERR_MAJOR_BAD;
  103. }
  104. curl = curl_easy_init();
  105. if(!curl) {
  106. fprintf(stderr, "curl_easy_init() failed\n");
  107. fclose(protofile);
  108. curl_global_cleanup();
  109. return TEST_ERR_MAJOR_BAD;
  110. }
  111. test_setopt(curl, CURLOPT_URL, URL);
  112. stream_uri = suburl(URL, request++);
  113. if(!stream_uri) {
  114. res = TEST_ERR_MAJOR_BAD;
  115. goto test_cleanup;
  116. }
  117. test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
  118. free(stream_uri);
  119. stream_uri = NULL;
  120. test_setopt(curl, CURLOPT_INTERLEAVEFUNCTION, rtp_write);
  121. test_setopt(curl, CURLOPT_TIMEOUT, 3L);
  122. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  123. test_setopt(curl, CURLOPT_WRITEDATA, protofile);
  124. test_setopt(curl, CURLOPT_RTSP_TRANSPORT, "RTP/AVP/TCP;interleaved=0-1");
  125. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_SETUP);
  126. res = curl_easy_perform(curl);
  127. if(res)
  128. goto test_cleanup;
  129. /* This PLAY starts the interleave */
  130. stream_uri = suburl(URL, request++);
  131. if(!stream_uri) {
  132. res = TEST_ERR_MAJOR_BAD;
  133. goto test_cleanup;
  134. }
  135. test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
  136. free(stream_uri);
  137. stream_uri = NULL;
  138. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_PLAY);
  139. res = curl_easy_perform(curl);
  140. if(res)
  141. goto test_cleanup;
  142. /* The DESCRIBE request will try to consume data after the Content */
  143. stream_uri = suburl(URL, request++);
  144. if(!stream_uri) {
  145. res = TEST_ERR_MAJOR_BAD;
  146. goto test_cleanup;
  147. }
  148. test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
  149. free(stream_uri);
  150. stream_uri = NULL;
  151. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_DESCRIBE);
  152. res = curl_easy_perform(curl);
  153. if(res)
  154. goto test_cleanup;
  155. stream_uri = suburl(URL, request++);
  156. if(!stream_uri) {
  157. res = TEST_ERR_MAJOR_BAD;
  158. goto test_cleanup;
  159. }
  160. test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
  161. free(stream_uri);
  162. stream_uri = NULL;
  163. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_PLAY);
  164. res = curl_easy_perform(curl);
  165. if(res)
  166. goto test_cleanup;
  167. fprintf(stderr, "PLAY COMPLETE\n");
  168. /* Use Receive to get the rest of the data */
  169. while(!res && rtp_packet_count < 13) {
  170. fprintf(stderr, "LOOPY LOOP!\n");
  171. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_RECEIVE);
  172. res = curl_easy_perform(curl);
  173. }
  174. test_cleanup:
  175. free(stream_uri);
  176. if(protofile)
  177. fclose(protofile);
  178. curl_easy_cleanup(curl);
  179. curl_global_cleanup();
  180. return res;
  181. }