2 * Copyright (c) 2011, Jim Hollinger
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
36 # include <conio.h> /* _getch() */
41 static int _getch(void)
43 struct termios oldt, newt;
45 tcgetattr( STDIN_FILENO, &oldt );
47 newt.c_lflag &= ~( ICANON | ECHO );
48 tcsetattr( STDIN_FILENO, TCSANOW, &newt );
50 tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
55 #include <curl/curl.h>
57 #define VERSION_STR "V1.0"
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", \
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);
70 /* send RTSP OPTIONS request */
71 static void rtsp_options(CURL *curl, const char *uri)
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);
81 /* send RTSP DESCRIBE request and write sdp response to a file */
82 static void rtsp_describe(CURL *curl, const char *uri,
83 const char *sdp_filename)
85 CURLcode res = CURLE_OK;
86 FILE *sdp_fp = fopen(sdp_filename, "wt");
87 printf("\nRTSP: DESCRIBE %s\n", uri);
89 fprintf(stderr, "Could not open '%s' for writing\n", sdp_filename);
93 printf("Writing SDP to '%s'\n", sdp_filename);
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) {
104 /* send RTSP SETUP request */
105 static void rtsp_setup(CURL *curl, const char *uri, const char *transport)
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);
117 /* send RTSP PLAY request */
118 static void rtsp_play(CURL *curl, const char *uri, const char *range)
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);
129 /* send RTSP TEARDOWN request */
130 static void rtsp_teardown(CURL *curl, const char *uri)
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);
139 /* convert url into an sdp filename */
140 static void get_sdp_filename(const char *url, char *sdp_filename)
142 const char *s = strrchr(url, '/');
143 strcpy(sdp_filename, "video.sdp");
147 sprintf(sdp_filename, "%s.sdp", s);
153 /* scan sdp file for media control attribute */
154 static void get_media_control_attribute(const char *sdp_filename,
158 char *s = malloc(max_len);
159 FILE *sdp_fp = fopen(sdp_filename, "rt");
161 if (sdp_fp != NULL) {
162 while (fgets(s, max_len - 2, sdp_fp) != NULL) {
163 sscanf(s, " a = control: %s", control);
172 int main(int argc, char * const argv[])
175 const char *transport = "RTP/AVP;unicast;client_port=1234-1235"; /* UDP */
177 const char *transport = "RTP/AVP/TCP;unicast;client_port=1234-1235"; /* TCP */
179 const char *range = "0.000-";
180 int rc = EXIT_SUCCESS;
181 char *base_name = NULL;
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");
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], '\\');
193 if (base_name == NULL) {
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);
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);
210 get_sdp_filename(url, sdp_filename);
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);
220 fprintf(stderr, " cURL V%s loaded\n", data->version);
222 /* initialize this curl session */
223 curl = curl_easy_init();
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_WRITEHEADER, stdout);
228 my_curl_easy_setopt(curl, CURLOPT_URL, url);
230 /* request server options */
231 sprintf(uri, "%s", url);
232 rtsp_options(curl, uri);
234 /* request session description and write response to sdp file */
235 rtsp_describe(curl, uri, sdp_filename);
237 /* get media control attribute from sdp file */
238 get_media_control_attribute(sdp_filename, control);
240 /* setup media stream */
241 sprintf(uri, "%s/%s", url, control);
242 rtsp_setup(curl, uri, transport);
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 ...");
251 /* teardown session */
252 rtsp_teardown(curl, uri);
255 curl_easy_cleanup(curl);
258 fprintf(stderr, "curl_easy_init() failed\n");
260 curl_global_cleanup();
262 fprintf(stderr, "curl_global_init(%s) failed: %d\n",
263 "CURL_GLOBAL_ALL", res);