lib571.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, 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 http://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 <curl/mprintf.h>
  39. #include "warnless.h"
  40. #include "memdebug.h"
  41. #define RTP_PKT_CHANNEL(p) ((int)((unsigned char)((p)[1])))
  42. #define RTP_PKT_LENGTH(p) ((((int)((unsigned char)((p)[2]))) << 8) | \
  43. ((int)((unsigned char)((p)[3]))))
  44. #define RTP_DATA_SIZE 12
  45. static const char *RTP_DATA = "$_1234\n\0asdf";
  46. static int rtp_packet_count = 0;
  47. static size_t rtp_write(void *ptr, size_t size, size_t nmemb, void *stream) {
  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. } else {
  70. if (memcmp(RTP_DATA, data + i, message_size - i) != 0) {
  71. printf("RTP PAYLOAD END CORRUPTED (%d), [%s]\n",
  72. message_size - i, data + i);
  73. return failure;
  74. }
  75. }
  76. }
  77. rtp_packet_count++;
  78. fprintf(stderr, "packet count is %d\n", rtp_packet_count);
  79. return size * nmemb;
  80. }
  81. /* build request url */
  82. static char *suburl(const char *base, int i)
  83. {
  84. return curl_maprintf("%s%.4d", base, i);
  85. }
  86. int test(char *URL)
  87. {
  88. int res;
  89. CURL *curl;
  90. char *stream_uri = NULL;
  91. int request=1;
  92. FILE *protofile = NULL;
  93. 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. if ((curl = curl_easy_init()) == NULL) {
  104. fprintf(stderr, "curl_easy_init() failed\n");
  105. fclose(protofile);
  106. curl_global_cleanup();
  107. return TEST_ERR_MAJOR_BAD;
  108. }
  109. test_setopt(curl, CURLOPT_URL, URL);
  110. if((stream_uri = suburl(URL, request++)) == NULL) {
  111. res = TEST_ERR_MAJOR_BAD;
  112. goto test_cleanup;
  113. }
  114. test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
  115. free(stream_uri);
  116. stream_uri = NULL;
  117. test_setopt(curl, CURLOPT_INTERLEAVEFUNCTION, rtp_write);
  118. test_setopt(curl, CURLOPT_TIMEOUT, 3);
  119. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  120. test_setopt(curl, CURLOPT_WRITEDATA, protofile);
  121. test_setopt(curl, CURLOPT_RTSP_TRANSPORT, "RTP/AVP/TCP;interleaved=0-1");
  122. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_SETUP);
  123. res = curl_easy_perform(curl);
  124. if(res)
  125. goto test_cleanup;
  126. /* This PLAY starts the interleave */
  127. if((stream_uri = suburl(URL, request++)) == NULL) {
  128. res = TEST_ERR_MAJOR_BAD;
  129. goto test_cleanup;
  130. }
  131. test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
  132. free(stream_uri);
  133. stream_uri = NULL;
  134. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_PLAY);
  135. res = curl_easy_perform(curl);
  136. if(res)
  137. goto test_cleanup;
  138. /* The DESCRIBE request will try to consume data after the Content */
  139. if((stream_uri = suburl(URL, request++)) == NULL) {
  140. res = TEST_ERR_MAJOR_BAD;
  141. goto test_cleanup;
  142. }
  143. test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
  144. free(stream_uri);
  145. stream_uri = NULL;
  146. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_DESCRIBE);
  147. res = curl_easy_perform(curl);
  148. if(res)
  149. goto test_cleanup;
  150. if((stream_uri = suburl(URL, request++)) == NULL) {
  151. res = TEST_ERR_MAJOR_BAD;
  152. goto test_cleanup;
  153. }
  154. test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
  155. free(stream_uri);
  156. stream_uri = NULL;
  157. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_PLAY);
  158. res = curl_easy_perform(curl);
  159. if(res)
  160. goto test_cleanup;
  161. fprintf(stderr, "PLAY COMPLETE\n");
  162. /* Use Receive to get the rest of the data */
  163. while(!res && rtp_packet_count < 13) {
  164. fprintf(stderr, "LOOPY LOOP!\n");
  165. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_RECEIVE);
  166. res = curl_easy_perform(curl);
  167. }
  168. test_cleanup:
  169. if(stream_uri)
  170. free(stream_uri);
  171. if(protofile)
  172. fclose(protofile);
  173. curl_easy_cleanup(curl);
  174. curl_global_cleanup();
  175. return res;
  176. }