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