VC 6 RTP streaming media transmission protocol programming instance (

Source: Internet
Author: User
Tags pack win32
Download resources:
Http://download.111cn.net/source/444512

Real-time stream protocol RTSP (RealTimeStreamingProtocol) is jointly proposed by RealNetworks and Netscape. This Protocol defines how one-to-multiple applications can effectively transmit multimedia data over an IP network. The RTSP architecture is located on RTP (real-time transmission) and RTCP (real-time control). It uses TCP or RTP for data transmission. Compared with RTSP, HTTP transmits HTML, while RTP transmits multimedia data. HTTP requests are sent by the client and the server responds. When RTSP is used, both the client and server can send requests, that is, RTSP can be bidirectional.

Real-time stream protocol (RTSP) is an application-level protocol that controls the transmission of real-time data. RTSP provides an extensible framework that makes real-time data, such as audio and video, controlled and on-demand, possible.

RTP is currently the best solution to the problem of real-time streaming media transmission. If you want to develop it, you can select the JRTPLIB Library. JRTPLIB is an object-oriented RTP database, which fully complies with RFC 1889. JRTPLIB is a RTP library implemented in C ++. It can run on Windows, Linux, FreeBSD, Solaris, Unix, and VxWorks operating systems.

For more information about RTP, refer:
Http://www.cnitblog.com/zouzheng/archive/2008/01/04/38449.html

For the example below, refer to example1 of jrtplib and add the parsing load section.


// RTPClient. cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include "rtpsession. h"
# Include "rtppacket. h"
# Include "rtpudpv4transmitter. h"
# Include "rtp00004address. h"
# Include "rtpsessionparams. h"
# Include "rtperrors. h"
# Include <winsock2.h>
# Include <stdlib. h>
# Include <stdio. h>
# Include "windows. h"
# Include <iostream>
# Include <string>
Using namespace std;
# Pragma comment (lib, "jrtplib. lib ")
# Pragma comment (lib, "jthread. lib ")
# Pragma comment (lib, "WS2_32.lib ")

Void checkerror (int rtperr)
...{
If (rtperr <0)
...{
Std: cout <"ERROR:" <rtpgterrorstring (rtperr) <std: endl;
Exit (-1 );
    }
}

Int main (int argc, char * argv [])
...{
# Ifdef WIN32
WSADATA dat;
WSAStartup (MAKEWORD (2, 2), & dat );
# Endif // WIN32
    
RTPSession sess;
Uint16_t portbase, destport;
Uint32_t destip;
Std: string ipstr;
Int status, I, num;

BYTE * pBuffer;
BYTE * pfBuffer;

// Enter necessary information
Std: cout <"Enter local portbase:" <std: endl;
Std: cin> portbase;
Std: cout <std: endl;
    
Std: cout <"Enter the destination IP address" <std: endl;
Std: cin> ipstr;
Destip = inet_addr (ipstr. c_str ());
If (destip = INADDR_NONE)
...{
Std: cerr <"Bad IP address specified" <std: endl;
Return-1;
    }
    
Destip = ntohl (destip );
    
Std: cout <"Enter the destination port" <std: endl;
Std: cin> destport;
    
Std: cout <std: endl;
Std: cout <"Number of packets you wish to be sent:" <std: endl;
Std: cin> num;
    
// Create an RTP session
RTPUDPv4TransmissionParams transparams;
RTPSessionParams sessparams;
    
// IMPORTANT: The local timestamp unit MUST be set, otherwise
// RTCP Sender Report info will be calculated wrong
// In this case, we'll be sending 10 samples each second, so we'll
// Put the timestamp unit to (1.0/10.0)
Sessparams. SetOwnTimestampUnit (1.0/10.0 );
    
Sessparams. SetAcceptOwnPackets (true );
Transparams. SetPortbase (portbase );
Status = sess. Create (sessparams, & transparams );
Checkerror (status );
    
Rtp00004address addr (destip, destport );
    
Status = sess. AddDestination (addr );
Checkerror (status );
    
For (I = 1; I <= num; I ++)
...{
Printf ("Sending packet % d/% d", I, num );
        
// Send "1234567890" of data"
Status = sess. SendPacket (void *) "1234567890", false, 10 );
Checkerror (status );
        
Sess. BeginDataAccess ();
        
// Check incoming packets
If (sess. GotoFirstSourceWithData ())
...{
Do
...{
RTPPacket * pack;
                
While (pack = sess. GetNextPacket ())! = NULL)
...{
// You can examine the data here
Printf ("Got packet! ");

Std: cout <"Got packet"
<"Extended sequence number"
<Pack-> GetExtendedSequenceNumber ()
<"From SSRC" <pack-> GetSSRC ()
<Std: endl;
                    
Int dataLength = pack-> GetPayloadLength ();
PfBuffer = (unsigned char *) pack-> GetPayloadData ();
PBuffer = new BYTE [dataLength + 1];
Memcpy (pBuffer, pfBuffer, dataLength );
PBuffer [dataLength] = 0;
Std: cout <pBuffer <std: endl;
// We don't longer need the packet, so
// We'll delete it
Sess. DeletePacket (pack );
                }
} While (sess. GotoNextSourceWithData ());
        }
        
Sess. EndDataAccess ();

# Ifndef RTP_SUPPORT_THREAD
Status = sess. Poll ();
Checkerror (status );
# Endif // RTP_SUPPORT_THREAD
        
RTPTime: Wait (RTPTime (1, 0 ));
    }
    
Sess. BYEDestroy (RTPTime (10, 0), 0, 0 );

# Ifdef WIN32
WSACleanup ();
# Endif // WIN32
Return 0;
}Compile and modify the Use run-time library under the code generation of each Source File to Debug Multithreaded DLL.
As shown in the following figure:



For the jrtplib environment, you can refer to a lot of information on the Internet or download it from my resources. I have compiled the relevant lib, as long as the VC environment is added.

The following figure shows the effect of executing the test program:
Enter local portbase:
8000
Enter the destination IP address
127.0.0.1
Enter the destination port
8000
Number of packets you wish to be sent:
5

Sending packet 1/5
Got packet!
Got packet with extended sequence number 59262 from SSRC 3029241192
1234567890

Sending packet 2/5
Got packet!
Got packet with extended sequence number 59263 from SSRC 3029241192
1234567890

Sending packet 3/5

Sending packet 4/5
Got packet!
Got packet with extended sequence number 59264 from SSRC 3029241192
1234567890
Got packet!
Got packet with extended sequence number 59265 from SSRC 3029241192
1234567890

Sending packet 5/5

The above execution means that the program opened port 8000 and then sent it to its own port 8000, so it not only sent it out, but also received and parsed the content. If you want to send the program to another machine, you can run the program on another machine. Of course, you can write another acceptor.
<
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.