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.
31 * A basic RTSP transfer
40 # include <conio.h> /* _getch() */
45 static int _getch(void)
47 struct termios oldt, newt;
49 tcgetattr(STDIN_FILENO, &oldt);
51 newt.c_lflag &= ~( ICANON | ECHO);
52 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
54 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
59 #include <curl/curl.h>
61 #define VERSION_STR "V1.0"
63 /* error handling macros */
64 #define my_curl_easy_setopt(A, B, C) \
65 res = curl_easy_setopt((A), (B), (C)); \
67 fprintf(stderr, "curl_easy_setopt(%s, %s, %s) failed: %d\n", \
70 #define my_curl_easy_perform(A) \
71 res = curl_easy_perform(A); \
73 fprintf(stderr, "curl_easy_perform(%s) failed: %d\n", #A, res);
76 /* send RTSP OPTIONS request */
77 static void rtsp_options(CURL *curl, const char *uri)
79 CURLcode res = CURLE_OK;
80 printf("\nRTSP: OPTIONS %s\n", uri);
81 my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);
82 my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_OPTIONS);
83 my_curl_easy_perform(curl);
87 /* send RTSP DESCRIBE request and write sdp response to a file */
88 static void rtsp_describe(CURL *curl, const char *uri,
89 const char *sdp_filename)
91 CURLcode res = CURLE_OK;
92 FILE *sdp_fp = fopen(sdp_filename, "wb");
93 printf("\nRTSP: DESCRIBE %s\n", uri);
95 fprintf(stderr, "Could not open '%s' for writing\n", sdp_filename);
99 printf("Writing SDP to '%s'\n", sdp_filename);
101 my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, sdp_fp);
102 my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_DESCRIBE);
103 my_curl_easy_perform(curl);
104 my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout);
105 if(sdp_fp != stdout) {
110 /* send RTSP SETUP request */
111 static void rtsp_setup(CURL *curl, const char *uri, const char *transport)
113 CURLcode res = CURLE_OK;
114 printf("\nRTSP: SETUP %s\n", uri);
115 printf(" TRANSPORT %s\n", transport);
116 my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);
117 my_curl_easy_setopt(curl, CURLOPT_RTSP_TRANSPORT, transport);
118 my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_SETUP);
119 my_curl_easy_perform(curl);
123 /* send RTSP PLAY request */
124 static void rtsp_play(CURL *curl, const char *uri, const char *range)
126 CURLcode res = CURLE_OK;
127 printf("\nRTSP: PLAY %s\n", uri);
128 my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);
129 my_curl_easy_setopt(curl, CURLOPT_RANGE, range);
130 my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_PLAY);
131 my_curl_easy_perform(curl);
133 /* switch off using range again */
134 my_curl_easy_setopt(curl, CURLOPT_RANGE, NULL);
138 /* send RTSP TEARDOWN request */
139 static void rtsp_teardown(CURL *curl, const char *uri)
141 CURLcode res = CURLE_OK;
142 printf("\nRTSP: TEARDOWN %s\n", uri);
143 my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_TEARDOWN);
144 my_curl_easy_perform(curl);
148 /* convert url into an sdp filename */
149 static void get_sdp_filename(const char *url, char *sdp_filename,
152 const char *s = strrchr(url, '/');
153 strcpy(sdp_filename, "video.sdp");
157 snprintf(sdp_filename, namelen, "%s.sdp", s);
163 /* scan sdp file for media control attribute */
164 static void get_media_control_attribute(const char *sdp_filename,
168 char *s = malloc(max_len);
169 FILE *sdp_fp = fopen(sdp_filename, "rb");
172 while(fgets(s, max_len - 2, sdp_fp) != NULL) {
173 sscanf(s, " a = control: %s", control);
182 int main(int argc, char * const argv[])
185 const char *transport = "RTP/AVP;unicast;client_port=1234-1235"; /* UDP */
188 const char *transport = "RTP/AVP/TCP;unicast;client_port=1234-1235";
190 const char *range = "0.000-";
191 int rc = EXIT_SUCCESS;
192 char *base_name = NULL;
194 printf("\nRTSP request %s\n", VERSION_STR);
195 printf(" Project web site: http://code.google.com/p/rtsprequest/\n");
196 printf(" Requires curl V7.20 or greater\n\n");
198 /* check command line */
199 if((argc != 2) && (argc != 3)) {
200 base_name = strrchr(argv[0], '/');
201 if(base_name == NULL) {
202 base_name = strrchr(argv[0], '\\');
204 if(base_name == NULL) {
210 printf("Usage: %s url [transport]\n", base_name);
211 printf(" url of video server\n");
212 printf(" transport (optional) specifier for media stream"
214 printf(" default transport: %s\n", transport);
215 printf("Example: %s rtsp://192.168.0.2/media/video1\n\n", base_name);
219 const char *url = argv[1];
220 char *uri = malloc(strlen(url) + 32);
221 char *sdp_filename = malloc(strlen(url) + 32);
222 char *control = malloc(strlen(url) + 32);
224 get_sdp_filename(url, sdp_filename, strlen(url) + 32);
229 /* initialize curl */
230 res = curl_global_init(CURL_GLOBAL_ALL);
231 if(res == CURLE_OK) {
232 curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
234 fprintf(stderr, " curl V%s loaded\n", data->version);
236 /* initialize this curl session */
237 curl = curl_easy_init();
239 my_curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
240 my_curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
241 my_curl_easy_setopt(curl, CURLOPT_HEADERDATA, stdout);
242 my_curl_easy_setopt(curl, CURLOPT_URL, url);
244 /* request server options */
245 snprintf(uri, strlen(url) + 32, "%s", url);
246 rtsp_options(curl, uri);
248 /* request session description and write response to sdp file */
249 rtsp_describe(curl, uri, sdp_filename);
251 /* get media control attribute from sdp file */
252 get_media_control_attribute(sdp_filename, control);
254 /* setup media stream */
255 snprintf(uri, strlen(url) + 32, "%s/%s", url, control);
256 rtsp_setup(curl, uri, transport);
258 /* start playing media stream */
259 snprintf(uri, strlen(url) + 32, "%s/", url);
260 rtsp_play(curl, uri, range);
261 printf("Playing video, press any key to stop ...");
265 /* teardown session */
266 rtsp_teardown(curl, uri);
269 curl_easy_cleanup(curl);
273 fprintf(stderr, "curl_easy_init() failed\n");
275 curl_global_cleanup();
278 fprintf(stderr, "curl_global_init(%s) failed: %d\n",
279 "CURL_GLOBAL_ALL", res);