1
0
mirror of https://github.com/moparisthebest/curl synced 2024-08-13 17:03:50 -04:00

rtsp.c: converted to C

Trimmed the newlines to be LF-only. Converted the source to plain C, to
use curl style indents, to compile warning-free with picky options and
fixed the minor fprintf() bug on line 245. Added to makefile.
This commit is contained in:
Daniel Stenberg 2011-08-10 10:57:50 +02:00
parent 8e2de86723
commit 657d02fbac
2 changed files with 272 additions and 254 deletions

View File

@ -4,7 +4,7 @@ check_PROGRAMS = 10-at-a-time anyauthput cookie_interface debug fileupload \
https multi-app multi-debugcallback multi-double multi-post multi-single \ https multi-app multi-debugcallback multi-double multi-post multi-single \
persistant post-callback postit2 sepheaders simple simplepost simplessl \ persistant post-callback postit2 sepheaders simple simplepost simplessl \
sendrecv httpcustomheader certinfo chkspeed ftpgetinfo ftp-wildcard \ sendrecv httpcustomheader certinfo chkspeed ftpgetinfo ftp-wildcard \
smtp-multi simplesmtp smtp-tls smtp-multi simplesmtp smtp-tls rtsp
# These examples require external dependencies that may not be commonly # These examples require external dependencies that may not be commonly
# available on POSIX systems, so don't bother attempting to compile them here. # available on POSIX systems, so don't bother attempting to compile them here.

View File

@ -1,253 +1,271 @@
/* /*
* Copyright (c) 2011, Jim Hollinger * Copyright (c) 2011, Jim Hollinger
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* * Neither the name of Jim Hollinger nor the names of its contributors * * Neither the name of Jim Hollinger nor the names of its contributors
* may be used to endorse or promote products derived from this * may be used to endorse or promote products derived from this
* software without specific prior written permission. * software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#if defined (WIN32) #if defined (WIN32)
# include <conio.h> // _getch() # include <conio.h> /* _getch() */
#else #else
# include <termios.h> # include <termios.h>
# include <unistd.h> # include <unistd.h>
int _getch(void) { static int _getch(void)
struct termios oldt, newt; {
tcgetattr( STDIN_FILENO, &oldt ); struct termios oldt, newt;
newt = oldt; int ch;
newt.c_lflag &= ~( ICANON | ECHO ); tcgetattr( STDIN_FILENO, &oldt );
tcsetattr( STDIN_FILENO, TCSANOW, &newt ); newt = oldt;
int ch = getchar(); newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &oldt ); tcsetattr( STDIN_FILENO, TCSANOW, &newt );
return ch; ch = getchar();
} tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
#endif return ch;
}
#include <curl/curl.h> #endif
#define VERSION_STR "V1.0" #include <curl/curl.h>
// error handling macros #define VERSION_STR "V1.0"
#define my_curl_easy_setopt(A, B, C) \
if ((res = curl_easy_setopt((A), (B), (C))) != CURLE_OK) \ /* error handling macros */
fprintf(stderr, "curl_easy_setopt(%s, %s, %s) failed: %d\n", #A, #B, #C, res); #define my_curl_easy_setopt(A, B, C) \
if ((res = curl_easy_setopt((A), (B), (C))) != CURLE_OK) \
#define my_curl_easy_perform(A) \ fprintf(stderr, "curl_easy_setopt(%s, %s, %s) failed: %d\n", \
if ((res = curl_easy_perform((A))) != CURLE_OK) \ #A, #B, #C, res);
fprintf(stderr, "curl_easy_perform(%s) failed: %d\n", #A, res);
#define my_curl_easy_perform(A) \
if ((res = curl_easy_perform((A))) != CURLE_OK) \
// send RTSP OPTIONS request fprintf(stderr, "curl_easy_perform(%s) failed: %d\n", #A, res);
void rtsp_options(CURL *curl, const char *uri) {
CURLcode res = CURLE_OK;
printf("\nRTSP: OPTIONS %s\n", uri); /* send RTSP OPTIONS request */
my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri); static void rtsp_options(CURL *curl, const char *uri)
my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_OPTIONS); {
my_curl_easy_perform(curl); CURLcode res = CURLE_OK;
} printf("\nRTSP: OPTIONS %s\n", uri);
my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);
my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_OPTIONS);
// send RTSP DESCRIBE request and write sdp response to a file my_curl_easy_perform(curl);
void rtsp_describe(CURL *curl, const char *uri, const char *sdp_filename) { }
CURLcode res = CURLE_OK;
printf("\nRTSP: DESCRIBE %s\n", uri);
FILE *sdp_fp = fopen(sdp_filename, "wt"); /* send RTSP DESCRIBE request and write sdp response to a file */
if (sdp_fp == NULL) { static void rtsp_describe(CURL *curl, const char *uri,
fprintf(stderr, "Could not open '%s' for writing\n", sdp_filename); const char *sdp_filename)
sdp_fp = stdout; {
} else { CURLcode res = CURLE_OK;
printf("Writing SDP to '%s'\n", sdp_filename); FILE *sdp_fp = fopen(sdp_filename, "wt");
} printf("\nRTSP: DESCRIBE %s\n", uri);
my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, sdp_fp); if (sdp_fp == NULL) {
my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_DESCRIBE); fprintf(stderr, "Could not open '%s' for writing\n", sdp_filename);
my_curl_easy_perform(curl); sdp_fp = stdout;
my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout); }
if (sdp_fp != stdout) { else {
fclose(sdp_fp); printf("Writing SDP to '%s'\n", sdp_filename);
} }
} my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, sdp_fp);
my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_DESCRIBE);
my_curl_easy_perform(curl);
// send RTSP SETUP request my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout);
void rtsp_setup(CURL *curl, const char *uri, const char *transport) { if (sdp_fp != stdout) {
CURLcode res = CURLE_OK; fclose(sdp_fp);
printf("\nRTSP: SETUP %s\n", uri); }
printf(" TRANSPORT %s\n", transport); }
my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);
my_curl_easy_setopt(curl, CURLOPT_RTSP_TRANSPORT, transport); /* send RTSP SETUP request */
my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_SETUP); static void rtsp_setup(CURL *curl, const char *uri, const char *transport)
my_curl_easy_perform(curl); {
} CURLcode res = CURLE_OK;
printf("\nRTSP: SETUP %s\n", uri);
printf(" TRANSPORT %s\n", transport);
// send RTSP PLAY request my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);
void rtsp_play(CURL *curl, const char *uri, const char *range) { my_curl_easy_setopt(curl, CURLOPT_RTSP_TRANSPORT, transport);
CURLcode res = CURLE_OK; my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_SETUP);
printf("\nRTSP: PLAY %s\n", uri); my_curl_easy_perform(curl);
my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri); }
my_curl_easy_setopt(curl, CURLOPT_RANGE, range);
my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_PLAY);
my_curl_easy_perform(curl); /* send RTSP PLAY request */
} static void rtsp_play(CURL *curl, const char *uri, const char *range)
{
CURLcode res = CURLE_OK;
// send RTSP TEARDOWN request printf("\nRTSP: PLAY %s\n", uri);
void rtsp_teardown(CURL *curl, const char *uri) { my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);
CURLcode res = CURLE_OK; my_curl_easy_setopt(curl, CURLOPT_RANGE, range);
printf("\nRTSP: TEARDOWN %s\n", uri); my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_PLAY);
my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_TEARDOWN); my_curl_easy_perform(curl);
my_curl_easy_perform(curl); }
}
/* send RTSP TEARDOWN request */
// convert url into an sdp filename static void rtsp_teardown(CURL *curl, const char *uri)
void get_sdp_filename(const char *url, char *sdp_filename) { {
strcpy(sdp_filename, "video.sdp"); CURLcode res = CURLE_OK;
const char *s = strrchr(url, '/'); printf("\nRTSP: TEARDOWN %s\n", uri);
if (s != NULL) { my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_TEARDOWN);
s++; my_curl_easy_perform(curl);
if (s[0] != '\0') { }
sprintf(sdp_filename, "%s.sdp", s);
}
} /* convert url into an sdp filename */
} static void get_sdp_filename(const char *url, char *sdp_filename)
{
const char *s = strrchr(url, '/');
// scan sdp file for media control attribute strcpy(sdp_filename, "video.sdp");
void get_media_control_attribute(const char *sdp_filename, char *control) { if (s != NULL) {
control[0] = '\0'; s++;
int max_len = 256; if (s[0] != '\0') {
char *s = new char[max_len]; sprintf(sdp_filename, "%s.sdp", s);
FILE *sdp_fp = fopen(sdp_filename, "rt"); }
if (sdp_fp != NULL) { }
while (fgets(s, max_len - 2, sdp_fp) != NULL) { }
sscanf(s, " a = control: %s", control);
}
fclose(sdp_fp); /* scan sdp file for media control attribute */
} static void get_media_control_attribute(const char *sdp_filename,
delete []s; char *control)
} {
int max_len = 256;
char *s = malloc(max_len);
// main app FILE *sdp_fp = fopen(sdp_filename, "rt");
int main(int argc, char * const argv[]) { control[0] = '\0';
const char *transport = "RTP/AVP;unicast;client_port=1234-1235"; // UDP if (sdp_fp != NULL) {
// const char *transport = "RTP/AVP/TCP;unicast;client_port=1234-1235"; // TCP while (fgets(s, max_len - 2, sdp_fp) != NULL) {
const char *range = "0.000-"; sscanf(s, " a = control: %s", control);
int rc = EXIT_SUCCESS; }
fclose(sdp_fp);
printf("\nRTSP request %s\n", VERSION_STR); }
printf(" Project web site: http://code.google.com/p/rtsprequest/\n"); free(s);
printf(" Requires cURL V7.20 or greater\n\n"); }
// check command line
char *basename = NULL; /* main app */
if ((argc != 2) && (argc != 3)) { int main(int argc, char * const argv[])
basename = strrchr(argv[0], '/'); {
if (basename == NULL) { #if 1
basename = strrchr(argv[0], '\\'); const char *transport = "RTP/AVP;unicast;client_port=1234-1235"; /* UDP */
} #else
if (basename == NULL) { const char *transport = "RTP/AVP/TCP;unicast;client_port=1234-1235"; /* TCP */
basename = argv[0]; #endif
} else { const char *range = "0.000-";
basename++; int rc = EXIT_SUCCESS;
} char *basename = NULL;
printf("Usage: %s url [transport]\n", basename);
printf(" url of video server\n"); printf("\nRTSP request %s\n", VERSION_STR);
printf(" transport (optional) specifier for media stream protocol\n"); printf(" Project web site: http://code.google.com/p/rtsprequest/\n");
printf(" default transport: %s\n", transport); printf(" Requires cURL V7.20 or greater\n\n");
printf("Example: %s rtsp://192.168.0.2/media/video1\n\n", basename);
rc = EXIT_FAILURE; /* check command line */
} else { if ((argc != 2) && (argc != 3)) {
const char *url = argv[1]; basename = strrchr(argv[0], '/');
char *uri = new char[strlen(url) + 32]; if (basename == NULL) {
char *sdp_filename = new char[strlen(url) + 32]; basename = strrchr(argv[0], '\\');
char *control = new char[strlen(url) + 32]; }
get_sdp_filename(url, sdp_filename); if (basename == NULL) {
if (argc == 3) { basename = argv[0];
transport = argv[2]; } else {
} basename++;
}
// initialize curl printf("Usage: %s url [transport]\n", basename);
CURLcode res = CURLE_OK; printf(" url of video server\n");
res = curl_global_init(CURL_GLOBAL_ALL); printf(" transport (optional) specifier for media stream protocol\n");
if (res == CURLE_OK) { printf(" default transport: %s\n", transport);
curl_version_info_data *data = curl_version_info(CURLVERSION_NOW); printf("Example: %s rtsp://192.168.0.2/media/video1\n\n", basename);
fprintf(stderr, " cURL V%s loaded\n", data->version); rc = EXIT_FAILURE;
} else {
// initialize this curl session const char *url = argv[1];
CURL *curl = curl_easy_init(); char *uri = malloc(strlen(url) + 32);
if (curl != NULL) { char *sdp_filename = malloc(strlen(url) + 32);
my_curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); char *control = malloc(strlen(url) + 32);
my_curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); CURLcode res;
my_curl_easy_setopt(curl, CURLOPT_WRITEHEADER, stdout); get_sdp_filename(url, sdp_filename);
my_curl_easy_setopt(curl, CURLOPT_URL, url); if (argc == 3) {
transport = argv[2];
// request server options }
sprintf(uri, "%s", url);
rtsp_options(curl, uri); /* initialize curl */
res = curl_global_init(CURL_GLOBAL_ALL);
// request session description and write response to sdp file if (res == CURLE_OK) {
rtsp_describe(curl, uri, sdp_filename); curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
CURL *curl;
// get media control attribute from sdp file fprintf(stderr, " cURL V%s loaded\n", data->version);
get_media_control_attribute(sdp_filename, control);
/* initialize this curl session */
// setup media stream curl = curl_easy_init();
sprintf(uri, "%s/%s", url, control); if (curl != NULL) {
rtsp_setup(curl, uri, transport); my_curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
my_curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
// start playing media stream my_curl_easy_setopt(curl, CURLOPT_WRITEHEADER, stdout);
sprintf(uri, "%s/", url); my_curl_easy_setopt(curl, CURLOPT_URL, url);
rtsp_play(curl, uri, range);
printf("Playing video, press any key to stop ..."); /* request server options */
_getch(); sprintf(uri, "%s", url);
printf("\n"); rtsp_options(curl, uri);
// teardown session /* request session description and write response to sdp file */
rtsp_teardown(curl, uri); rtsp_describe(curl, uri, sdp_filename);
// cleanup /* get media control attribute from sdp file */
curl_easy_cleanup(curl); get_media_control_attribute(sdp_filename, control);
curl = NULL;
} else { /* setup media stream */
fprintf(stderr, "curl_easy_init() failed\n"); sprintf(uri, "%s/%s", url, control);
} rtsp_setup(curl, uri, transport);
curl_global_cleanup();
} else { /* start playing media stream */
fprintf(stderr, "curl_global_init(%s) failed\n", "CURL_GLOBAL_ALL", res); sprintf(uri, "%s/", url);
} rtsp_play(curl, uri, range);
delete []control; printf("Playing video, press any key to stop ...");
delete []sdp_filename; _getch();
delete []uri; printf("\n");
}
/* teardown session */
return rc; rtsp_teardown(curl, uri);
}
/* cleanup */
curl_easy_cleanup(curl);
curl = NULL;
} else {
fprintf(stderr, "curl_easy_init() failed\n");
}
curl_global_cleanup();
} else {
fprintf(stderr, "curl_global_init(%s) failed: %d\n",
"CURL_GLOBAL_ALL", res);
}
free(control);
free(sdp_filename);
free(uri);
}
return rc;
}