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