1 /* 2 * Copyright (c) 2011, Jim Hollinger 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * * Neither the name of Jim Hollinger nor the names of its contributors 14 * may be used to endorse or promote products derived from this 15 * software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 */ 30 /* <DESC> 31 * A basic RTSP transfer 32 * </DESC> 33 */ 34 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 39 #if defined (WIN32) 40 # include <conio.h> /* _getch() */ 41 #else 42 # include <termios.h> 43 # include <unistd.h> 44 45 static int _getch(void) 46 { 47 struct termios oldt, newt; 48 int ch; 49 tcgetattr(STDIN_FILENO, &oldt); 50 newt = oldt; 51 newt.c_lflag &= ~( ICANON | ECHO); 52 tcsetattr(STDIN_FILENO, TCSANOW, &newt); 53 ch = getchar(); 54 tcsetattr(STDIN_FILENO, TCSANOW, &oldt); 55 return ch; 56 } 57 #endif 58 59 #include <curl/curl.h> 60 61 #define VERSION_STR "V1.0" 62 63 /* error handling macros */ 64 #define my_curl_easy_setopt(A, B, C) \ 65 if((res = curl_easy_setopt((A), (B), (C))) != CURLE_OK) \ 66 fprintf(stderr, "curl_easy_setopt(%s, %s, %s) failed: %d\n", \ 67 #A, #B, #C, res); 68 69 #define my_curl_easy_perform(A) \ 70 if((res = curl_easy_perform((A))) != CURLE_OK) \ 71 fprintf(stderr, "curl_easy_perform(%s) failed: %d\n", #A, res); 72 73 74 /* send RTSP OPTIONS request */ 75 static void rtsp_options(CURL *curl, const char *uri) 76 { 77 CURLcode res = CURLE_OK; 78 printf("\nRTSP: OPTIONS %s\n", uri); 79 my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri); 80 my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_OPTIONS); 81 my_curl_easy_perform(curl); 82 } 83 84 85 /* send RTSP DESCRIBE request and write sdp response to a file */ 86 static void rtsp_describe(CURL *curl, const char *uri, 87 const char *sdp_filename) 88 { 89 CURLcode res = CURLE_OK; 90 FILE *sdp_fp = fopen(sdp_filename, "wb"); 91 printf("\nRTSP: DESCRIBE %s\n", uri); 92 if(sdp_fp == NULL) { 93 fprintf(stderr, "Could not open '%s' for writing\n", sdp_filename); 94 sdp_fp = stdout; 95 } 96 else { 97 printf("Writing SDP to '%s'\n", sdp_filename); 98 } 99 my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, sdp_fp); 100 my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_DESCRIBE); 101 my_curl_easy_perform(curl); 102 my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout); 103 if(sdp_fp != stdout) { 104 fclose(sdp_fp); 105 } 106 } 107 108 /* send RTSP SETUP request */ 109 static void rtsp_setup(CURL *curl, const char *uri, const char *transport) 110 { 111 CURLcode res = CURLE_OK; 112 printf("\nRTSP: SETUP %s\n", uri); 113 printf(" TRANSPORT %s\n", transport); 114 my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri); 115 my_curl_easy_setopt(curl, CURLOPT_RTSP_TRANSPORT, transport); 116 my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_SETUP); 117 my_curl_easy_perform(curl); 118 } 119 120 121 /* send RTSP PLAY request */ 122 static void rtsp_play(CURL *curl, const char *uri, const char *range) 123 { 124 CURLcode res = CURLE_OK; 125 printf("\nRTSP: PLAY %s\n", uri); 126 my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri); 127 my_curl_easy_setopt(curl, CURLOPT_RANGE, range); 128 my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_PLAY); 129 my_curl_easy_perform(curl); 130 } 131 132 133 /* send RTSP TEARDOWN request */ 134 static void rtsp_teardown(CURL *curl, const char *uri) 135 { 136 CURLcode res = CURLE_OK; 137 printf("\nRTSP: TEARDOWN %s\n", uri); 138 my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_TEARDOWN); 139 my_curl_easy_perform(curl); 140 } 141 142 143 /* convert url into an sdp filename */ 144 static void get_sdp_filename(const char *url, char *sdp_filename, 145 size_t namelen) 146 { 147 const char *s = strrchr(url, '/'); 148 strcpy(sdp_filename, "video.sdp"); 149 if(s != NULL) { 150 s++; 151 if(s[0] != '\0') { 152 snprintf(sdp_filename, namelen, "%s.sdp", s); 153 } 154 } 155 } 156 157 158 /* scan sdp file for media control attribute */ 159 static void get_media_control_attribute(const char *sdp_filename, 160 char *control) 161 { 162 int max_len = 256; 163 char *s = malloc(max_len); 164 FILE *sdp_fp = fopen(sdp_filename, "rb"); 165 control[0] = '\0'; 166 if(sdp_fp != NULL) { 167 while(fgets(s, max_len - 2, sdp_fp) != NULL) { 168 sscanf(s, " a = control: %s", control); 169 } 170 fclose(sdp_fp); 171 } 172 free(s); 173 } 174 175 176 /* main app */ 177 int main(int argc, char * const argv[]) 178 { 179 #if 1 180 const char *transport = "RTP/AVP;unicast;client_port=1234-1235"; /* UDP */ 181 #else 182 /* TCP */ 183 const char *transport = "RTP/AVP/TCP;unicast;client_port=1234-1235"; 184 #endif 185 const char *range = "0.000-"; 186 int rc = EXIT_SUCCESS; 187 char *base_name = NULL; 188 189 printf("\nRTSP request %s\n", VERSION_STR); 190 printf(" Project web site: http://code.google.com/p/rtsprequest/\n"); 191 printf(" Requires cURL V7.20 or greater\n\n"); 192 193 /* check command line */ 194 if((argc != 2) && (argc != 3)) { 195 base_name = strrchr(argv[0], '/'); 196 if(base_name == NULL) { 197 base_name = strrchr(argv[0], '\\'); 198 } 199 if(base_name == NULL) { 200 base_name = argv[0]; 201 } 202 else { 203 base_name++; 204 } 205 printf("Usage: %s url [transport]\n", base_name); 206 printf(" url of video server\n"); 207 printf(" transport (optional) specifier for media stream" 208 " protocol\n"); 209 printf(" default transport: %s\n", transport); 210 printf("Example: %s rtsp://192.168.0.2/media/video1\n\n", base_name); 211 rc = EXIT_FAILURE; 212 } 213 else { 214 const char *url = argv[1]; 215 char *uri = malloc(strlen(url) + 32); 216 char *sdp_filename = malloc(strlen(url) + 32); 217 char *control = malloc(strlen(url) + 32); 218 CURLcode res; 219 get_sdp_filename(url, sdp_filename, strlen(url) + 32); 220 if(argc == 3) { 221 transport = argv[2]; 222 } 223 224 /* initialize curl */ 225 res = curl_global_init(CURL_GLOBAL_ALL); 226 if(res == CURLE_OK) { 227 curl_version_info_data *data = curl_version_info(CURLVERSION_NOW); 228 CURL *curl; 229 fprintf(stderr, " cURL V%s loaded\n", data->version); 230 231 /* initialize this curl session */ 232 curl = curl_easy_init(); 233 if(curl != NULL) { 234 my_curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); 235 my_curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); 236 my_curl_easy_setopt(curl, CURLOPT_HEADERDATA, stdout); 237 my_curl_easy_setopt(curl, CURLOPT_URL, url); 238 239 /* request server options */ 240 snprintf(uri, strlen(url) + 32, "%s", url); 241 rtsp_options(curl, uri); 242 243 /* request session description and write response to sdp file */ 244 rtsp_describe(curl, uri, sdp_filename); 245 246 /* get media control attribute from sdp file */ 247 get_media_control_attribute(sdp_filename, control); 248 249 /* setup media stream */ 250 snprintf(uri, strlen(url) + 32, "%s/%s", url, control); 251 rtsp_setup(curl, uri, transport); 252 253 /* start playing media stream */ 254 snprintf(uri, strlen(url) + 32, "%s/", url); 255 rtsp_play(curl, uri, range); 256 printf("Playing video, press any key to stop ..."); 257 _getch(); 258 printf("\n"); 259 260 /* teardown session */ 261 rtsp_teardown(curl, uri); 262 263 /* cleanup */ 264 curl_easy_cleanup(curl); 265 curl = NULL; 266 } 267 else { 268 fprintf(stderr, "curl_easy_init() failed\n"); 269 } 270 curl_global_cleanup(); 271 } 272 else { 273 fprintf(stderr, "curl_global_init(%s) failed: %d\n", 274 "CURL_GLOBAL_ALL", res); 275 } 276 free(control); 277 free(sdp_filename); 278 free(uri); 279 } 280 281 return rc; 282 } 283