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