lib571.c 5.6 KB

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