Download RTSP. c

Source: Internet
Author: User

1. [Code] [C/C ++] Code

/*
* Copyright (c) 2011, Jim Hollinger
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or
* Modification, are permitted provided that the following conditions
* Are met:
** Redistributions of source code must retain the above Copyright
* Notice, this list of conditions and the following disclawing.
** Redistributions in binary form must reproduce the above Copyright
* Notice, this list of conditions and the following disclawing in
* Documentation and/or other materials provided with the distribution.
** Neither the name of Jim Hollinger nor the names of its contributors
* May be used to endorse or promote products derived from this
* Software without specific prior written permission.
*
* This software is provided by the copyright holders and contributors
* "As is" and any express or implied warranties, including, but not
* Limited to, the implied warranties of merchantability and fitness
* A particle purpose are disclaimed. In no event shall the Copyright
* Owner or contributors be liable for any direct, indirect, incidental,
* Special, exemplary, or consequential damages (including, but not
* Limited to, procurement of substitute goods or services; loss of use,
* Data, or profits; or business interruption) however caused and on any
* Theory of liability, whether in contract, strict liability, or tort
* (Including negligence or otherwise) arising in any way out of the Use
* Of this software, even if advised of the possibility of such damage.
*
*/

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>

# If defined (win32)
# Include <conio. h>
# Else
# Include <termios. h>
# Include <unistd. h>

Static int _ getch (void)
{
Struct termios oldt, Newt;
Int ch;
Tcgetattr (stdin_fileno, & oldt );
Newt = oldt;
Newt. c_lflag & = ~ (Icanon | echo );
Tcsetattr (stdin_fileno, tcsanow, & Newt );
Ch = getchar ();
Tcsetattr (stdin_fileno, tcsanow, & oldt );
Return ch;
}
# Endif

# Include <curl/curl. h>

# Define version_str "V1.0"

/* Error handling macros */
# Define my_curl_easy_setopt (a, B, c )\
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 );

# Define my_curl_easy_perform ()\
If (RES = curl_easy_perform ()))! = Curle_ OK )\
Fprintf (stderr, "curl_easy_perform (% s) failed: % d \ n", # A, Res );


/* Send RTSP options Request */
Static void rtsp_options (curl * curl, const char * URI)
{
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, (long) curl_rtspreq_options );
My_curl_easy_perform (curl );
}


/* Send rtsp describe request and write SDP response to a file */
Static void rtsp_describe (curl * curl, const char * Uri,
Const char * sdp_filename)
{
Curlcode res = curle_ OK;
File * sdp_fp = fopen (sdp_filename, "WT ");
Printf ("\ nrtsp: Describe % s \ n", Uri );
If (sdp_fp = NULL ){
Fprintf (stderr, "cocould not open '% s' for writing \ n", sdp_filename );
Sdp_fp = stdout;
}
Else {
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, (long) curl_rtspreq_describe );
My_curl_easy_perform (curl );
My_curl_easy_setopt (curl, curlopt_writedata, stdout );
If (sdp_fp! = Stdout ){
Fclose (sdp_fp );
}
}

/* Send rtsp setup Request */
Static void rtsp_setup (curl * curl, const char * Uri, const char * Transport)
{
Curlcode res = curle_ OK;
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 );
My_curl_easy_setopt (curl, curlopt_rtsp_request, (long) curl_rtspreq_setup );
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;
Printf ("\ nrtsp: Play % s \ n", Uri );
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, (long) curl_rtspreq_play );
My_curl_easy_perform (curl );
}


/* Send RTSP teardown Request */
Static void rtsp_teardown (curl * curl, const char * URI)
{
Curlcode res = curle_ OK;
Printf ("\ nrtsp: teardown % s \ n", Uri );
My_curl_easy_setopt (curl, curlopt_rtsp_request, (long) curl_rtspreq_teardown );
My_curl_easy_perform (curl );
}


/* Convert URL into an SDP filename */
Static void get_sdp_filename (const char * URL, char * sdp_filename)
{
Const char * s = strrchr (URL ,'/');
Strcpy (sdp_filename, "video. SDP ");
If (s! = NULL ){
S ++;
If (s [0]! = '\ 0 '){
Sprintf (sdp_filename, "% S. SDP", S );
}
}
}


/* Scan SDP file for media control attribute */
Static void get_media_control_attribute (const char * sdp_filename,
Char * Control)
{
Int max_len = 256;
Char * s = malloc (max_len );
File * sdp_fp = fopen (sdp_filename, "RT ");
Control [0] = '\ 0 ';
If (sdp_fp! = NULL ){
While (fgets (S, max_len-2, sdp_fp )! = NULL ){
Sscanf (S, "A = control: % s", control );
}
Fclose (sdp_fp );
}
Free (s );
}


/* Main app */
Int main (INT argc, char * const argv [])
{
# 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 -";
Int rc = exit_success;
Char * base_name = NULL;

Printf ("\ nrtsp request % s \ n", version_str );
Printf ("project web site: http://code.google.com/p/rtsprequest/410n ");
Printf ("requires curl v7.20 or greater \ n ");

/* Check command line */
If (argc! = 2) & (argc! = 3 )){
Base_name = strrchr (argv [0], '/');
If (base_name = NULL ){
Base_name = strrchr (argv [0], '\');
}
If (base_name = NULL ){
Base_name = argv [0];
} Else {
Base_name ++;
}
Printf ("Usage: % s URL [transport] \ n", base_name );
Printf ("URL of Video Server \ n ");
Printf ("Transport (optional) specifier for media stream protocol \ n ");
Printf ("Default transport: % s \ n", transport );
Printf ("Example: % s rtsp: // 192.168.0.2/Media/video1 \ n", base_name );
Rc = exit_failure; beautiful anime pictures
} Else {http://www.huiyi8.com/dongman/weimei/
Const char * url = argv [1];
Char * uri = malloc (strlen (URL) + 32 );
Char * sdp_filename = malloc (strlen (URL) + 32 );
Char * control = malloc (strlen (URL) + 32 );
Curlcode res;
Get_sdp_filename (URL, sdp_filename );
If (argc = 3 ){
Transport = argv [2];
}

/* Initialize curl */
Res = curl_global_init (curl_global_all );
If (RES = curle_ OK ){
Curl_version_info_data * Data = curl_version_info (curlversion_now );
Curl * curl;
Fprintf (stderr, "curl v % s loaded \ n", data-> version );

/* Initialize this curl session */
Curl = curl_easy_init ();
If (curl! = NULL ){
My_curl_easy_setopt (curl, curlopt_verbose, 0l );
My_curl_easy_setopt (curl, curlopt_noprogress, 1l );
My_curl_easy_setopt (curl, curlopt_writeheader, stdout );
My_curl_easy_setopt (curl, curlopt_url, URL );

/* Request server options */
Sprintf (Uri, "% s", URL );
Rtsp_options (curl, Uri );

/* Request Session Description and write Response to SDP file */
Rtsp_describe (curl, Uri, sdp_filename );

/* Get media control attribute from SDP file */
Get_media_control_attribute (sdp_filename, control );

/* Setup media stream */
Sprintf (Uri, "% S/% s", URL, control );
Rtsp_setup (curl, Uri, transport );

/* Start playing media stream */
Sprintf (Uri, "% S/", URL );
Rtsp_play (curl, Uri, range );
Printf ("playing video, press any key to stop ...");
_ Getch ();
Printf ("\ n ");

/* Teardown session */
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;
}
// You'll also find all examples in the distribution archive, In the docs/examples // directory.

Download RTSP. c

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.