docs/examples/rtsp.c
Go to the documentation of this file.
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  res = curl_easy_setopt((A), (B), (C)); \
66  if(!res) \
67  fprintf(stderr, "curl_easy_setopt(%s, %s, %s) failed: %d\n", \
68  #A, #B, #C, res);
69 
70 #define my_curl_easy_perform(A) \
71  res = curl_easy_perform(A); \
72  if(!res) \
73  fprintf(stderr, "curl_easy_perform(%s) failed: %d\n", #A, res);
74 
75 
76 /* send RTSP OPTIONS request */
77 static void rtsp_options(CURL *curl, const char *uri)
78 {
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);
84 }
85 
86 
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)
90 {
92  FILE *sdp_fp = fopen(sdp_filename, "wb");
93  printf("\nRTSP: DESCRIBE %s\n", uri);
94  if(sdp_fp == NULL) {
95  fprintf(stderr, "Could not open '%s' for writing\n", sdp_filename);
96  sdp_fp = stdout;
97  }
98  else {
99  printf("Writing SDP to '%s'\n", sdp_filename);
100  }
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) {
106  fclose(sdp_fp);
107  }
108 }
109 
110 /* send RTSP SETUP request */
111 static void rtsp_setup(CURL *curl, const char *uri, const char *transport)
112 {
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);
120 }
121 
122 
123 /* send RTSP PLAY request */
124 static void rtsp_play(CURL *curl, const char *uri, const char *range)
125 {
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);
132 }
133 
134 
135 /* send RTSP TEARDOWN request */
136 static void rtsp_teardown(CURL *curl, const char *uri)
137 {
139  printf("\nRTSP: TEARDOWN %s\n", uri);
140  my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_TEARDOWN);
141  my_curl_easy_perform(curl);
142 }
143 
144 
145 /* convert url into an sdp filename */
146 static void get_sdp_filename(const char *url, char *sdp_filename,
147  size_t namelen)
148 {
149  const char *s = strrchr(url, '/');
150  strcpy(sdp_filename, "video.sdp");
151  if(s != NULL) {
152  s++;
153  if(s[0] != '\0') {
154  snprintf(sdp_filename, namelen, "%s.sdp", s);
155  }
156  }
157 }
158 
159 
160 /* scan sdp file for media control attribute */
161 static void get_media_control_attribute(const char *sdp_filename,
162  char *control)
163 {
164  int max_len = 256;
165  char *s = malloc(max_len);
166  FILE *sdp_fp = fopen(sdp_filename, "rb");
167  control[0] = '\0';
168  if(sdp_fp != NULL) {
169  while(fgets(s, max_len - 2, sdp_fp) != NULL) {
170  sscanf(s, " a = control: %s", control);
171  }
172  fclose(sdp_fp);
173  }
174  free(s);
175 }
176 
177 
178 /* main app */
179 int main(int argc, char * const argv[])
180 {
181 #if 1
182  const char *transport = "RTP/AVP;unicast;client_port=1234-1235"; /* UDP */
183 #else
184  /* TCP */
185  const char *transport = "RTP/AVP/TCP;unicast;client_port=1234-1235";
186 #endif
187  const char *range = "0.000-";
188  int rc = EXIT_SUCCESS;
189  char *base_name = NULL;
190 
191  printf("\nRTSP request %s\n", VERSION_STR);
192  printf(" Project web site: http://code.google.com/p/rtsprequest/\n");
193  printf(" Requires curl V7.20 or greater\n\n");
194 
195  /* check command line */
196  if((argc != 2) && (argc != 3)) {
197  base_name = strrchr(argv[0], '/');
198  if(base_name == NULL) {
199  base_name = strrchr(argv[0], '\\');
200  }
201  if(base_name == NULL) {
202  base_name = argv[0];
203  }
204  else {
205  base_name++;
206  }
207  printf("Usage: %s url [transport]\n", base_name);
208  printf(" url of video server\n");
209  printf(" transport (optional) specifier for media stream"
210  " protocol\n");
211  printf(" default transport: %s\n", transport);
212  printf("Example: %s rtsp://192.168.0.2/media/video1\n\n", base_name);
213  rc = EXIT_FAILURE;
214  }
215  else {
216  const char *url = argv[1];
217  char *uri = malloc(strlen(url) + 32);
218  char *sdp_filename = malloc(strlen(url) + 32);
219  char *control = malloc(strlen(url) + 32);
220  CURLcode res;
221  get_sdp_filename(url, sdp_filename, strlen(url) + 32);
222  if(argc == 3) {
223  transport = argv[2];
224  }
225 
226  /* initialize curl */
228  if(res == CURLE_OK) {
230  CURL *curl;
231  fprintf(stderr, " curl V%s loaded\n", data->version);
232 
233  /* initialize this curl session */
234  curl = curl_easy_init();
235  if(curl != NULL) {
236  my_curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
237  my_curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
238  my_curl_easy_setopt(curl, CURLOPT_HEADERDATA, stdout);
239  my_curl_easy_setopt(curl, CURLOPT_URL, url);
240 
241  /* request server options */
242  snprintf(uri, strlen(url) + 32, "%s", url);
243  rtsp_options(curl, uri);
244 
245  /* request session description and write response to sdp file */
246  rtsp_describe(curl, uri, sdp_filename);
247 
248  /* get media control attribute from sdp file */
249  get_media_control_attribute(sdp_filename, control);
250 
251  /* setup media stream */
252  snprintf(uri, strlen(url) + 32, "%s/%s", url, control);
253  rtsp_setup(curl, uri, transport);
254 
255  /* start playing media stream */
256  snprintf(uri, strlen(url) + 32, "%s/", url);
257  rtsp_play(curl, uri, range);
258  printf("Playing video, press any key to stop ...");
259  _getch();
260  printf("\n");
261 
262  /* teardown session */
263  rtsp_teardown(curl, uri);
264 
265  /* cleanup */
266  curl_easy_cleanup(curl);
267  curl = NULL;
268  }
269  else {
270  fprintf(stderr, "curl_easy_init() failed\n");
271  }
273  }
274  else {
275  fprintf(stderr, "curl_global_init(%s) failed: %d\n",
276  "CURL_GLOBAL_ALL", res);
277  }
278  free(control);
279  free(sdp_filename);
280  free(uri);
281  }
282 
283  return rc;
284 }
#define free(ptr)
Definition: curl_memory.h:130
CURL_EXTERN curl_version_info_data * curl_version_info(CURLversion)
Definition: version.c:342
static void rtsp_teardown(CURL *curl, const char *uri)
int main(int argc, char *const argv[])
const char * version
Definition: curl.h:2607
static int _getch(void)
static void rtsp_play(CURL *curl, const char *uri, const char *range)
XmlRpcServer s
CURLcode
Definition: curl.h:454
static void get_sdp_filename(const char *url, char *sdp_filename, size_t namelen)
static int res
#define malloc(size)
Definition: curl_memory.h:124
static void rtsp_options(CURL *curl, const char *uri)
UNITTEST_START int rc
Definition: unit1301.c:31
#define printf
Definition: curl_printf.h:40
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
Definition: curl.h:455
CURL_EXTERN CURLcode curl_global_init(long flags)
curl_global_init() globally initializes curl given a bitwise set of the different features of what to...
Definition: easy.c:271
#define CURLVERSION_NOW
Definition: curl.h:2603
void CURL
Definition: curl.h:102
#define my_curl_easy_perform(A)
static void rtsp_describe(CURL *curl, const char *uri, const char *sdp_filename)
static void rtsp_setup(CURL *curl, const char *uri, const char *transport)
#define my_curl_easy_setopt(A, B, C)
#define fprintf
Definition: curl_printf.h:41
#define STDIN_FILENO
Definition: tool_main.h:32
#define snprintf
Definition: curl_printf.h:42
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
#define CURL_GLOBAL_ALL
Definition: curl.h:2519
#define VERSION_STR
static CURL * curl
Definition: sessioninfo.c:35
Definition: debug.c:29
static void get_media_control_attribute(const char *sdp_filename, char *control)


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16