1
0
mirror of https://github.com/moparisthebest/curl synced 2024-11-15 22:15:13 -05: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

@ -33,39 +33,43 @@
#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; struct termios oldt, newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt ); tcgetattr( STDIN_FILENO, &oldt );
newt = oldt; newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO ); newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt ); tcsetattr( STDIN_FILENO, TCSANOW, &newt );
int ch = getchar(); ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt ); tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch; return ch;
} }
#endif #endif
#include <curl/curl.h> #include <curl/curl.h>
#define VERSION_STR "V1.0" #define VERSION_STR "V1.0"
// error handling macros /* error handling macros */
#define my_curl_easy_setopt(A, B, C) \ #define my_curl_easy_setopt(A, B, C) \
if ((res = curl_easy_setopt((A), (B), (C))) != CURLE_OK) \ if ((res = curl_easy_setopt((A), (B), (C))) != CURLE_OK) \
fprintf(stderr, "curl_easy_setopt(%s, %s, %s) failed: %d\n", #A, #B, #C, res); fprintf(stderr, "curl_easy_setopt(%s, %s, %s) failed: %d\n", \
#A, #B, #C, res);
#define my_curl_easy_perform(A) \ #define my_curl_easy_perform(A) \
if ((res = curl_easy_perform((A))) != CURLE_OK) \ if ((res = curl_easy_perform((A))) != CURLE_OK) \
fprintf(stderr, "curl_easy_perform(%s) failed: %d\n", #A, res); fprintf(stderr, "curl_easy_perform(%s) failed: %d\n", #A, res);
// send RTSP OPTIONS request /* send RTSP OPTIONS request */
void rtsp_options(CURL *curl, const char *uri) { static void rtsp_options(CURL *curl, const char *uri)
{
CURLcode res = CURLE_OK; CURLcode res = CURLE_OK;
printf("\nRTSP: OPTIONS %s\n", uri); printf("\nRTSP: OPTIONS %s\n", uri);
my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri); my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);
@ -74,15 +78,18 @@ void rtsp_options(CURL *curl, const char *uri) {
} }
// send RTSP DESCRIBE request and write sdp response to a file /* send RTSP DESCRIBE request and write sdp response to a file */
void rtsp_describe(CURL *curl, const char *uri, const char *sdp_filename) { static void rtsp_describe(CURL *curl, const char *uri,
const char *sdp_filename)
{
CURLcode res = CURLE_OK; CURLcode res = CURLE_OK;
printf("\nRTSP: DESCRIBE %s\n", uri);
FILE *sdp_fp = fopen(sdp_filename, "wt"); FILE *sdp_fp = fopen(sdp_filename, "wt");
printf("\nRTSP: DESCRIBE %s\n", uri);
if (sdp_fp == NULL) { if (sdp_fp == NULL) {
fprintf(stderr, "Could not open '%s' for writing\n", sdp_filename); fprintf(stderr, "Could not open '%s' for writing\n", sdp_filename);
sdp_fp = stdout; sdp_fp = stdout;
} else { }
else {
printf("Writing SDP to '%s'\n", sdp_filename); printf("Writing SDP to '%s'\n", sdp_filename);
} }
my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, sdp_fp); my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, sdp_fp);
@ -94,9 +101,9 @@ void rtsp_describe(CURL *curl, const char *uri, const char *sdp_filename) {
} }
} }
/* send RTSP SETUP request */
// send RTSP SETUP request static void rtsp_setup(CURL *curl, const char *uri, const char *transport)
void rtsp_setup(CURL *curl, const char *uri, const char *transport) { {
CURLcode res = CURLE_OK; CURLcode res = CURLE_OK;
printf("\nRTSP: SETUP %s\n", uri); printf("\nRTSP: SETUP %s\n", uri);
printf(" TRANSPORT %s\n", transport); printf(" TRANSPORT %s\n", transport);
@ -107,8 +114,9 @@ void rtsp_setup(CURL *curl, const char *uri, const char *transport) {
} }
// send RTSP PLAY request /* send RTSP PLAY request */
void rtsp_play(CURL *curl, const char *uri, const char *range) { static void rtsp_play(CURL *curl, const char *uri, const char *range)
{
CURLcode res = CURLE_OK; CURLcode res = CURLE_OK;
printf("\nRTSP: PLAY %s\n", uri); printf("\nRTSP: PLAY %s\n", uri);
my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri); my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri);
@ -118,8 +126,9 @@ void rtsp_play(CURL *curl, const char *uri, const char *range) {
} }
// send RTSP TEARDOWN request /* send RTSP TEARDOWN request */
void rtsp_teardown(CURL *curl, const char *uri) { static void rtsp_teardown(CURL *curl, const char *uri)
{
CURLcode res = CURLE_OK; CURLcode res = CURLE_OK;
printf("\nRTSP: TEARDOWN %s\n", uri); printf("\nRTSP: TEARDOWN %s\n", uri);
my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_TEARDOWN); my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_TEARDOWN);
@ -127,10 +136,11 @@ void rtsp_teardown(CURL *curl, const char *uri) {
} }
// convert url into an sdp filename /* convert url into an sdp filename */
void get_sdp_filename(const char *url, char *sdp_filename) { static void get_sdp_filename(const char *url, char *sdp_filename)
strcpy(sdp_filename, "video.sdp"); {
const char *s = strrchr(url, '/'); const char *s = strrchr(url, '/');
strcpy(sdp_filename, "video.sdp");
if (s != NULL) { if (s != NULL) {
s++; s++;
if (s[0] != '\0') { if (s[0] != '\0') {
@ -140,35 +150,41 @@ void get_sdp_filename(const char *url, char *sdp_filename) {
} }
// scan sdp file for media control attribute /* scan sdp file for media control attribute */
void get_media_control_attribute(const char *sdp_filename, char *control) { static void get_media_control_attribute(const char *sdp_filename,
control[0] = '\0'; char *control)
{
int max_len = 256; int max_len = 256;
char *s = new char[max_len]; char *s = malloc(max_len);
FILE *sdp_fp = fopen(sdp_filename, "rt"); FILE *sdp_fp = fopen(sdp_filename, "rt");
control[0] = '\0';
if (sdp_fp != NULL) { if (sdp_fp != NULL) {
while (fgets(s, max_len - 2, sdp_fp) != NULL) { while (fgets(s, max_len - 2, sdp_fp) != NULL) {
sscanf(s, " a = control: %s", control); sscanf(s, " a = control: %s", control);
} }
fclose(sdp_fp); fclose(sdp_fp);
} }
delete []s; free(s);
} }
// main app /* main app */
int main(int argc, char * const argv[]) { int main(int argc, char * const argv[])
const char *transport = "RTP/AVP;unicast;client_port=1234-1235"; // UDP {
// const char *transport = "RTP/AVP/TCP;unicast;client_port=1234-1235"; // TCP #if 1
const char *transport = "RTP/AVP;unicast;client_port=1234-1235"; /* UDP */
#else
const char *transport = "RTP/AVP/TCP;unicast;client_port=1234-1235"; /* TCP */
#endif
const char *range = "0.000-"; const char *range = "0.000-";
int rc = EXIT_SUCCESS; int rc = EXIT_SUCCESS;
char *basename = NULL;
printf("\nRTSP request %s\n", VERSION_STR); printf("\nRTSP request %s\n", VERSION_STR);
printf(" Project web site: http://code.google.com/p/rtsprequest/\n"); printf(" Project web site: http://code.google.com/p/rtsprequest/\n");
printf(" Requires cURL V7.20 or greater\n\n"); printf(" Requires cURL V7.20 or greater\n\n");
// check command line /* check command line */
char *basename = NULL;
if ((argc != 2) && (argc != 3)) { if ((argc != 2) && (argc != 3)) {
basename = strrchr(argv[0], '/'); basename = strrchr(argv[0], '/');
if (basename == NULL) { if (basename == NULL) {
@ -187,54 +203,55 @@ int main(int argc, char * const argv[]) {
rc = EXIT_FAILURE; rc = EXIT_FAILURE;
} else { } else {
const char *url = argv[1]; const char *url = argv[1];
char *uri = new char[strlen(url) + 32]; char *uri = malloc(strlen(url) + 32);
char *sdp_filename = new char[strlen(url) + 32]; char *sdp_filename = malloc(strlen(url) + 32);
char *control = new char[strlen(url) + 32]; char *control = malloc(strlen(url) + 32);
CURLcode res;
get_sdp_filename(url, sdp_filename); get_sdp_filename(url, sdp_filename);
if (argc == 3) { if (argc == 3) {
transport = argv[2]; transport = argv[2];
} }
// initialize curl /* initialize curl */
CURLcode res = CURLE_OK;
res = curl_global_init(CURL_GLOBAL_ALL); res = curl_global_init(CURL_GLOBAL_ALL);
if (res == CURLE_OK) { if (res == CURLE_OK) {
curl_version_info_data *data = curl_version_info(CURLVERSION_NOW); curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
CURL *curl;
fprintf(stderr, " cURL V%s loaded\n", data->version); fprintf(stderr, " cURL V%s loaded\n", data->version);
// initialize this curl session /* initialize this curl session */
CURL *curl = curl_easy_init(); curl = curl_easy_init();
if (curl != NULL) { if (curl != NULL) {
my_curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); my_curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
my_curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); my_curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
my_curl_easy_setopt(curl, CURLOPT_WRITEHEADER, stdout); my_curl_easy_setopt(curl, CURLOPT_WRITEHEADER, stdout);
my_curl_easy_setopt(curl, CURLOPT_URL, url); my_curl_easy_setopt(curl, CURLOPT_URL, url);
// request server options /* request server options */
sprintf(uri, "%s", url); sprintf(uri, "%s", url);
rtsp_options(curl, uri); rtsp_options(curl, uri);
// request session description and write response to sdp file /* request session description and write response to sdp file */
rtsp_describe(curl, uri, sdp_filename); rtsp_describe(curl, uri, sdp_filename);
// get media control attribute from sdp file /* get media control attribute from sdp file */
get_media_control_attribute(sdp_filename, control); get_media_control_attribute(sdp_filename, control);
// setup media stream /* setup media stream */
sprintf(uri, "%s/%s", url, control); sprintf(uri, "%s/%s", url, control);
rtsp_setup(curl, uri, transport); rtsp_setup(curl, uri, transport);
// start playing media stream /* start playing media stream */
sprintf(uri, "%s/", url); sprintf(uri, "%s/", url);
rtsp_play(curl, uri, range); rtsp_play(curl, uri, range);
printf("Playing video, press any key to stop ..."); printf("Playing video, press any key to stop ...");
_getch(); _getch();
printf("\n"); printf("\n");
// teardown session /* teardown session */
rtsp_teardown(curl, uri); rtsp_teardown(curl, uri);
// cleanup /* cleanup */
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
curl = NULL; curl = NULL;
} else { } else {
@ -242,11 +259,12 @@ int main(int argc, char * const argv[]) {
} }
curl_global_cleanup(); curl_global_cleanup();
} else { } else {
fprintf(stderr, "curl_global_init(%s) failed\n", "CURL_GLOBAL_ALL", res); fprintf(stderr, "curl_global_init(%s) failed: %d\n",
"CURL_GLOBAL_ALL", res);
} }
delete []control; free(control);
delete []sdp_filename; free(sdp_filename);
delete []uri; free(uri);
} }
return rc; return rc;