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