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
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #if defined (WIN32)
36 # include <conio.h> /* _getch() */
37 #else
38 # include <termios.h>
39 # include <unistd.h>
40
_getch(void)41 static int _getch(void)
42 {
43 struct termios oldt, newt;
44 int ch;
45 tcgetattr( STDIN_FILENO, &oldt );
46 newt = oldt;
47 newt.c_lflag &= ~( ICANON | ECHO );
48 tcsetattr( STDIN_FILENO, TCSANOW, &newt );
49 ch = getchar();
50 tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
51 return ch;
52 }
53 #endif
54
55 #include <curl/curl.h>
56
57 #define VERSION_STR "V1.0"
58
59 /* error handling macros */
60 #define my_curl_easy_setopt(A, B, C) \
61 if ((res = curl_easy_setopt((A), (B), (C))) != CURLE_OK) \
62 fprintf(stderr, "curl_easy_setopt(%s, %s, %s) failed: %d\n", \
63 #A, #B, #C, res);
64
65 #define my_curl_easy_perform(A) \
66 if ((res = curl_easy_perform((A))) != CURLE_OK) \
67 fprintf(stderr, "curl_easy_perform(%s) failed: %d\n", #A, res);
68
69
70 /* send RTSP OPTIONS request */
rtsp_options(CURL * curl,const char * uri)71 static void rtsp_options(CURL *curl, const char *uri)
72 {
73 CURLcode res = CURLE_OK;
74 printf("\nRTSP: OPTIONS %s\n", uri);
75 my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);
76 my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_OPTIONS);
77 my_curl_easy_perform(curl);
78 }
79
80
81 /* send RTSP DESCRIBE request and write sdp response to a file */
rtsp_describe(CURL * curl,const char * uri,const char * sdp_filename)82 static void rtsp_describe(CURL *curl, const char *uri,
83 const char *sdp_filename)
84 {
85 CURLcode res = CURLE_OK;
86 FILE *sdp_fp = fopen(sdp_filename, "wt");
87 printf("\nRTSP: DESCRIBE %s\n", uri);
88 if (sdp_fp == NULL) {
89 fprintf(stderr, "Could not open '%s' for writing\n", sdp_filename);
90 sdp_fp = stdout;
91 }
92 else {
93 printf("Writing SDP to '%s'\n", sdp_filename);
94 }
95 my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, sdp_fp);
96 my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_DESCRIBE);
97 my_curl_easy_perform(curl);
98 my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout);
99 if (sdp_fp != stdout) {
100 fclose(sdp_fp);
101 }
102 }
103
104 /* send RTSP SETUP request */
rtsp_setup(CURL * curl,const char * uri,const char * transport)105 static void rtsp_setup(CURL *curl, const char *uri, const char *transport)
106 {
107 CURLcode res = CURLE_OK;
108 printf("\nRTSP: SETUP %s\n", uri);
109 printf(" TRANSPORT %s\n", transport);
110 my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);
111 my_curl_easy_setopt(curl, CURLOPT_RTSP_TRANSPORT, transport);
112 my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_SETUP);
113 my_curl_easy_perform(curl);
114 }
115
116
117 /* send RTSP PLAY request */
rtsp_play(CURL * curl,const char * uri,const char * range)118 static void rtsp_play(CURL *curl, const char *uri, const char *range)
119 {
120 CURLcode res = CURLE_OK;
121 printf("\nRTSP: PLAY %s\n", uri);
122 my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);
123 my_curl_easy_setopt(curl, CURLOPT_RANGE, range);
124 my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_PLAY);
125 my_curl_easy_perform(curl);
126 }
127
128
129 /* send RTSP TEARDOWN request */
rtsp_teardown(CURL * curl,const char * uri)130 static void rtsp_teardown(CURL *curl, const char *uri)
131 {
132 CURLcode res = CURLE_OK;
133 printf("\nRTSP: TEARDOWN %s\n", uri);
134 my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_TEARDOWN);
135 my_curl_easy_perform(curl);
136 }
137
138
139 /* convert url into an sdp filename */
get_sdp_filename(const char * url,char * sdp_filename)140 static void get_sdp_filename(const char *url, char *sdp_filename)
141 {
142 const char *s = strrchr(url, '/');
143 strcpy(sdp_filename, "video.sdp");
144 if (s != NULL) {
145 s++;
146 if (s[0] != '\0') {
147 sprintf(sdp_filename, "%s.sdp", s);
148 }
149 }
150 }
151
152
153 /* scan sdp file for media control attribute */
get_media_control_attribute(const char * sdp_filename,char * control)154 static void get_media_control_attribute(const char *sdp_filename,
155 char *control)
156 {
157 int max_len = 256;
158 char *s = malloc(max_len);
159 FILE *sdp_fp = fopen(sdp_filename, "rt");
160 control[0] = '\0';
161 if (sdp_fp != NULL) {
162 while (fgets(s, max_len - 2, sdp_fp) != NULL) {
163 sscanf(s, " a = control: %s", control);
164 }
165 fclose(sdp_fp);
166 }
167 free(s);
168 }
169
170
171 /* main app */
main(int argc,char * const argv[])172 int main(int argc, char * const argv[])
173 {
174 #if 1
175 const char *transport = "RTP/AVP;unicast;client_port=1234-1235"; /* UDP */
176 #else
177 const char *transport = "RTP/AVP/TCP;unicast;client_port=1234-1235"; /* TCP */
178 #endif
179 const char *range = "0.000-";
180 int rc = EXIT_SUCCESS;
181 char *base_name = NULL;
182
183 printf("\nRTSP request %s\n", VERSION_STR);
184 printf(" Project web site: http://code.google.com/p/rtsprequest/\n");
185 printf(" Requires cURL V7.20 or greater\n\n");
186
187 /* check command line */
188 if ((argc != 2) && (argc != 3)) {
189 base_name = strrchr(argv[0], '/');
190 if (base_name == NULL) {
191 base_name = strrchr(argv[0], '\\');
192 }
193 if (base_name == NULL) {
194 base_name = argv[0];
195 } else {
196 base_name++;
197 }
198 printf("Usage: %s url [transport]\n", base_name);
199 printf(" url of video server\n");
200 printf(" transport (optional) specifier for media stream protocol\n");
201 printf(" default transport: %s\n", transport);
202 printf("Example: %s rtsp://192.168.0.2/media/video1\n\n", base_name);
203 rc = EXIT_FAILURE;
204 } else {
205 const char *url = argv[1];
206 char *uri = malloc(strlen(url) + 32);
207 char *sdp_filename = malloc(strlen(url) + 32);
208 char *control = malloc(strlen(url) + 32);
209 CURLcode res;
210 get_sdp_filename(url, sdp_filename);
211 if (argc == 3) {
212 transport = argv[2];
213 }
214
215 /* initialize curl */
216 res = curl_global_init(CURL_GLOBAL_ALL);
217 if (res == CURLE_OK) {
218 curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
219 CURL *curl;
220 fprintf(stderr, " cURL V%s loaded\n", data->version);
221
222 /* initialize this curl session */
223 curl = curl_easy_init();
224 if (curl != NULL) {
225 my_curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
226 my_curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
227 my_curl_easy_setopt(curl, CURLOPT_HEADERDATA, stdout);
228 my_curl_easy_setopt(curl, CURLOPT_URL, url);
229
230 /* request server options */
231 sprintf(uri, "%s", url);
232 rtsp_options(curl, uri);
233
234 /* request session description and write response to sdp file */
235 rtsp_describe(curl, uri, sdp_filename);
236
237 /* get media control attribute from sdp file */
238 get_media_control_attribute(sdp_filename, control);
239
240 /* setup media stream */
241 sprintf(uri, "%s/%s", url, control);
242 rtsp_setup(curl, uri, transport);
243
244 /* start playing media stream */
245 sprintf(uri, "%s/", url);
246 rtsp_play(curl, uri, range);
247 printf("Playing video, press any key to stop ...");
248 _getch();
249 printf("\n");
250
251 /* teardown session */
252 rtsp_teardown(curl, uri);
253
254 /* cleanup */
255 curl_easy_cleanup(curl);
256 curl = NULL;
257 } else {
258 fprintf(stderr, "curl_easy_init() failed\n");
259 }
260 curl_global_cleanup();
261 } else {
262 fprintf(stderr, "curl_global_init(%s) failed: %d\n",
263 "CURL_GLOBAL_ALL", res);
264 }
265 free(control);
266 free(sdp_filename);
267 free(uri);
268 }
269
270 return rc;
271 }
272