• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2011 - 2021, 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 
_getch(void)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   do {                                                             \
66     res = curl_easy_setopt((A), (B), (C));                         \
67     if(res != CURLE_OK)                                            \
68       fprintf(stderr, "curl_easy_setopt(%s, %s, %s) failed: %d\n", \
69               #A, #B, #C, res);                                    \
70   } while(0)
71 
72 #define my_curl_easy_perform(A)                                         \
73   do {                                                                  \
74     res = curl_easy_perform(A);                                         \
75     if(res != CURLE_OK)                                                 \
76       fprintf(stderr, "curl_easy_perform(%s) failed: %d\n", #A, res);   \
77   } while(0)
78 
79 /* send RTSP OPTIONS request */
rtsp_options(CURL * curl,const char * uri)80 static void rtsp_options(CURL *curl, const char *uri)
81 {
82   CURLcode res = CURLE_OK;
83   printf("\nRTSP: OPTIONS %s\n", uri);
84   my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);
85   my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_OPTIONS);
86   my_curl_easy_perform(curl);
87 }
88 
89 
90 /* send RTSP DESCRIBE request and write sdp response to a file */
rtsp_describe(CURL * curl,const char * uri,const char * sdp_filename)91 static void rtsp_describe(CURL *curl, const char *uri,
92                           const char *sdp_filename)
93 {
94   CURLcode res = CURLE_OK;
95   FILE *sdp_fp = fopen(sdp_filename, "wb");
96   printf("\nRTSP: DESCRIBE %s\n", uri);
97   if(!sdp_fp) {
98     fprintf(stderr, "Could not open '%s' for writing\n", sdp_filename);
99     sdp_fp = stdout;
100   }
101   else {
102     printf("Writing SDP to '%s'\n", sdp_filename);
103   }
104   my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, sdp_fp);
105   my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_DESCRIBE);
106   my_curl_easy_perform(curl);
107   my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout);
108   if(sdp_fp != stdout) {
109     fclose(sdp_fp);
110   }
111 }
112 
113 /* send RTSP SETUP request */
rtsp_setup(CURL * curl,const char * uri,const char * transport)114 static void rtsp_setup(CURL *curl, const char *uri, const char *transport)
115 {
116   CURLcode res = CURLE_OK;
117   printf("\nRTSP: SETUP %s\n", uri);
118   printf("      TRANSPORT %s\n", transport);
119   my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);
120   my_curl_easy_setopt(curl, CURLOPT_RTSP_TRANSPORT, transport);
121   my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_SETUP);
122   my_curl_easy_perform(curl);
123 }
124 
125 
126 /* send RTSP PLAY request */
rtsp_play(CURL * curl,const char * uri,const char * range)127 static void rtsp_play(CURL *curl, const char *uri, const char *range)
128 {
129   CURLcode res = CURLE_OK;
130   printf("\nRTSP: PLAY %s\n", uri);
131   my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);
132   my_curl_easy_setopt(curl, CURLOPT_RANGE, range);
133   my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_PLAY);
134   my_curl_easy_perform(curl);
135 
136   /* switch off using range again */
137   my_curl_easy_setopt(curl, CURLOPT_RANGE, NULL);
138 }
139 
140 
141 /* send RTSP TEARDOWN request */
rtsp_teardown(CURL * curl,const char * uri)142 static void rtsp_teardown(CURL *curl, const char *uri)
143 {
144   CURLcode res = CURLE_OK;
145   printf("\nRTSP: TEARDOWN %s\n", uri);
146   my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_TEARDOWN);
147   my_curl_easy_perform(curl);
148 }
149 
150 
151 /* convert url into an sdp filename */
get_sdp_filename(const char * url,char * sdp_filename,size_t namelen)152 static void get_sdp_filename(const char *url, char *sdp_filename,
153                              size_t namelen)
154 {
155   const char *s = strrchr(url, '/');
156   strcpy(sdp_filename, "video.sdp");
157   if(s != NULL) {
158     s++;
159     if(s[0] != '\0') {
160       snprintf(sdp_filename, namelen, "%s.sdp", s);
161     }
162   }
163 }
164 
165 
166 /* scan sdp file for media control attribute */
get_media_control_attribute(const char * sdp_filename,char * control)167 static void get_media_control_attribute(const char *sdp_filename,
168                                         char *control)
169 {
170   int max_len = 256;
171   char *s = malloc(max_len);
172   FILE *sdp_fp = fopen(sdp_filename, "rb");
173   control[0] = '\0';
174   if(sdp_fp != NULL) {
175     while(fgets(s, max_len - 2, sdp_fp) != NULL) {
176       sscanf(s, " a = control: %32s", control);
177     }
178     fclose(sdp_fp);
179   }
180   free(s);
181 }
182 
183 
184 /* main app */
main(int argc,char * const argv[])185 int main(int argc, char * const argv[])
186 {
187 #if 1
188   const char *transport = "RTP/AVP;unicast;client_port=1234-1235";  /* UDP */
189 #else
190   /* TCP */
191   const char *transport = "RTP/AVP/TCP;unicast;client_port=1234-1235";
192 #endif
193   const char *range = "0.000-";
194   int rc = EXIT_SUCCESS;
195   char *base_name = NULL;
196 
197   printf("\nRTSP request %s\n", VERSION_STR);
198   printf("    Project website: "
199     "https://github.com/BackupGGCode/rtsprequest\n");
200   printf("    Requires curl V7.20 or greater\n\n");
201 
202   /* check command line */
203   if((argc != 2) && (argc != 3)) {
204     base_name = strrchr(argv[0], '/');
205     if(!base_name) {
206       base_name = strrchr(argv[0], '\\');
207     }
208     if(!base_name) {
209       base_name = argv[0];
210     }
211     else {
212       base_name++;
213     }
214     printf("Usage:   %s url [transport]\n", base_name);
215     printf("         url of video server\n");
216     printf("         transport (optional) specifier for media stream"
217            " protocol\n");
218     printf("         default transport: %s\n", transport);
219     printf("Example: %s rtsp://192.168.0.2/media/video1\n\n", base_name);
220     rc = EXIT_FAILURE;
221   }
222   else {
223     const char *url = argv[1];
224     char *uri = malloc(strlen(url) + 32);
225     char *sdp_filename = malloc(strlen(url) + 32);
226     char *control = malloc(strlen(url) + 32);
227     CURLcode res;
228     get_sdp_filename(url, sdp_filename, strlen(url) + 32);
229     if(argc == 3) {
230       transport = argv[2];
231     }
232 
233     /* initialize curl */
234     res = curl_global_init(CURL_GLOBAL_ALL);
235     if(res == CURLE_OK) {
236       curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
237       CURL *curl;
238       fprintf(stderr, "    curl V%s loaded\n", data->version);
239 
240       /* initialize this curl session */
241       curl = curl_easy_init();
242       if(curl != NULL) {
243         my_curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
244         my_curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
245         my_curl_easy_setopt(curl, CURLOPT_HEADERDATA, stdout);
246         my_curl_easy_setopt(curl, CURLOPT_URL, url);
247 
248         /* request server options */
249         snprintf(uri, strlen(url) + 32, "%s", url);
250         rtsp_options(curl, uri);
251 
252         /* request session description and write response to sdp file */
253         rtsp_describe(curl, uri, sdp_filename);
254 
255         /* get media control attribute from sdp file */
256         get_media_control_attribute(sdp_filename, control);
257 
258         /* setup media stream */
259         snprintf(uri, strlen(url) + 32, "%s/%s", url, control);
260         rtsp_setup(curl, uri, transport);
261 
262         /* start playing media stream */
263         snprintf(uri, strlen(url) + 32, "%s/", url);
264         rtsp_play(curl, uri, range);
265         printf("Playing video, press any key to stop ...");
266         _getch();
267         printf("\n");
268 
269         /* teardown session */
270         rtsp_teardown(curl, uri);
271 
272         /* cleanup */
273         curl_easy_cleanup(curl);
274         curl = NULL;
275       }
276       else {
277         fprintf(stderr, "curl_easy_init() failed\n");
278       }
279       curl_global_cleanup();
280     }
281     else {
282       fprintf(stderr, "curl_global_init(%s) failed: %d\n",
283               "CURL_GLOBAL_ALL", res);
284     }
285     free(control);
286     free(sdp_filename);
287     free(uri);
288   }
289 
290   return rc;
291 }
292