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
23#include "test.h"
24
25#ifdef HAVE_SYS_SOCKET_H
26#  include <sys/socket.h>
27#endif
28#ifdef HAVE_NETINET_IN_H
29#  include <netinet/in.h>
30#endif
31#ifdef HAVE_NETDB_H
32#  include <netdb.h>
33#endif
34#ifdef HAVE_ARPA_INET_H
35#  include <arpa/inet.h>
36#endif
37#ifdef HAVE_SYS_STAT_H
38#  include <sys/stat.h>
39#endif
40#ifdef HAVE_FCNTL_H
41#  include <fcntl.h>
42#endif
43
44#include <curl/mprintf.h>
45
46#include "memdebug.h"
47
48#define RTP_PKT_CHANNEL(p)   ((int)((unsigned char)((p)[1])))
49
50#define RTP_PKT_LENGTH(p)  ((((int)((unsigned char)((p)[2]))) << 8) | \
51                             ((int)((unsigned char)((p)[3]))))
52
53#define RTP_DATA_SIZE 12
54static const char *RTP_DATA = "$_1234\n\0asdf";
55
56static int rtp_packet_count = 0;
57
58static size_t rtp_write(void *ptr, size_t size, size_t nmemb, void *stream) {
59  char *data = (char *)ptr;
60  int channel = RTP_PKT_CHANNEL(data);
61  int message_size = (int)(size * nmemb) - 4;
62  int coded_size = RTP_PKT_LENGTH(data);
63  size_t failure = (size * nmemb) ? 0 : 1;
64  int i;
65  (void)stream;
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    } else {
82      if (memcmp(RTP_DATA, data + i, message_size - i) != 0) {
83        printf("RTP PAYLOAD END CORRUPTED (%d), [%s]\n",
84               message_size - i, data + i);
85        return failure;
86      }
87    }
88  }
89
90  rtp_packet_count++;
91  fprintf(stderr, "packet count is %d\n", rtp_packet_count);
92
93  return size * nmemb;
94}
95
96/* build request url */
97static char *suburl(const char *base, int i)
98{
99  return curl_maprintf("%s%.4d", base, i);
100}
101
102int test(char *URL)
103{
104  int res;
105  CURL *curl;
106  char *stream_uri = NULL;
107  int request=1;
108  FILE *protofile = NULL;
109
110  protofile = fopen(libtest_arg2, "wb");
111  if(protofile == NULL) {
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  if ((curl = curl_easy_init()) == NULL) {
123    fprintf(stderr, "curl_easy_init() failed\n");
124    fclose(protofile);
125    curl_global_cleanup();
126    return TEST_ERR_MAJOR_BAD;
127  }
128  test_setopt(curl, CURLOPT_URL, URL);
129
130  if((stream_uri = suburl(URL, request++)) == NULL) {
131    res = TEST_ERR_MAJOR_BAD;
132    goto test_cleanup;
133  }
134  test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
135  free(stream_uri);
136  stream_uri = NULL;
137
138  test_setopt(curl, CURLOPT_INTERLEAVEFUNCTION, rtp_write);
139  test_setopt(curl, CURLOPT_TIMEOUT, 3);
140  test_setopt(curl, CURLOPT_VERBOSE, 1L);
141  test_setopt(curl, CURLOPT_WRITEDATA, protofile);
142
143  test_setopt(curl, CURLOPT_RTSP_TRANSPORT, "RTP/AVP/TCP;interleaved=0-1");
144  test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_SETUP);
145
146  res = curl_easy_perform(curl);
147  if(res)
148    goto test_cleanup;
149
150  /* This PLAY starts the interleave */
151  if((stream_uri = suburl(URL, request++)) == NULL) {
152    res = TEST_ERR_MAJOR_BAD;
153    goto test_cleanup;
154  }
155  test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
156  free(stream_uri);
157  stream_uri = NULL;
158  test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_PLAY);
159
160  res = curl_easy_perform(curl);
161  if(res)
162    goto test_cleanup;
163
164  /* The DESCRIBE request will try to consume data after the Content */
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_DESCRIBE);
173
174  res = curl_easy_perform(curl);
175  if(res)
176    goto test_cleanup;
177
178  if((stream_uri = suburl(URL, request++)) == NULL) {
179    res = TEST_ERR_MAJOR_BAD;
180    goto test_cleanup;
181  }
182  test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
183  free(stream_uri);
184  stream_uri = NULL;
185  test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_PLAY);
186
187  res = curl_easy_perform(curl);
188  if(res)
189    goto test_cleanup;
190
191  fprintf(stderr, "PLAY COMPLETE\n");
192
193  /* Use Receive to get the rest of the data */
194  while(!res && rtp_packet_count < 13) {
195    fprintf(stderr, "LOOPY LOOP!\n");
196    test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_RECEIVE);
197    res = curl_easy_perform(curl);
198  }
199
200test_cleanup:
201
202  if(stream_uri)
203    free(stream_uri);
204
205  if(protofile)
206    fclose(protofile);
207
208  curl_easy_cleanup(curl);
209  curl_global_cleanup();
210
211  return res;
212}
213
214