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