VC 6 RTP Streaming Media Transfer Protocol programming example __ programming

Source: Internet
Author: User
Tags pack win32

Real-time Streaming protocol RTSP (Realtimestreamingprotocol) is proposed by RealNetworks and Netscape, which defines how a one-to-many application can efficiently transfer multimedia data over an IP network. RTSP is located on the architecture of RTP (real-time transmission) and RTCP (real-time control), which uses TCP or RTP to complete the data transfer. HTTP transmits HTML compared to RTSP, while RTP transmits multimedia data. HTTP requests are sent by the client, the server responds, and when the RTSP is used, both the client and the server can make requests, that is, the RTSP can be two-way.

Real-time Streaming Protocol (RTSP) is an application-level protocol, which controls the transmission of real-time data. RTSP provides an extensible framework for the control and on-demand of real-time data, such as audio and video.

RTP is currently the best solution for streaming media real-time transmission problem, if you want to develop, you can choose Jrtplib Library. Jrtplib is an object-oriented RTP library that is fully compliant with RFC 1889 design. Jrtplib is a RTP library implemented in C + + language that can now run on a variety of operating systems such as Windows, Linux, FreeBSD, Solaris, Unix, and VxWorks.

The following example refers to the example1 of Jrtplib and adds a portion of the analytic payload.


RTPClient.cpp:Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Rtpsession.h"
#include "Rtppacket.h"
#include "Rtpudpv4transmitter.h"
#include "Rtpipv4address.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:" << rtpgeterrorstring (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 some 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 RTP session
Rtpudpv4transmissionparams Transparams;
Rtpsessionparams Sessparams;

Important:the local timestamp must is set, otherwise
RTCP Sender and would be calculated wrong
In the this case, we'll be sending the samples each second and 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);

Rtpipv4address addr (destip,destport);

Status = Sess. Adddestination (addr);
CheckError (status);

for (i = 1; I <= num; i++)
{
printf ("Sending packet%d/%d", i,num);

Send Data "1234567890"
Status = Sess. Sendpacket (void *) "1234567890", 10,0,false,10);
CheckError (status);

Sess. Begindataaccess ();

Check incoming packets
if (Sess. Gotofirstsourcewithdata ())
{
Todo
{
Rtppacket *pack;

while (pack = Sess. Getnextpacket ())!= NULL)
{
Can examine the data here
printf ("Got packet!");

Std::cout << "Got packet with"
<< "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 note Modify the Use Run-time library under the code generation for each source file as the debug multithreaded DLL.

About the JRTPLIB environment, you can refer to a lot of information on the Internet, can also be downloaded from my resources, I have compiled a related lib, as long as the VC environment can be added.

The effect of executing the test program is as follows:
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 # 59262 from Ssrc 3029241192
1234567890

Sending Packet 2/5
Got Packet!
Got packet with extended sequence # 59263 from Ssrc 3029241192
1234567890

Sending Packet 3/5

Sending Packet 4/5
Got Packet!
Got packet with extended sequence # 59264 from Ssrc 3029241192
1234567890
Got Packet!
Got packet with extended sequence # 59265 from Ssrc 3029241192
1234567890

Sending Packet 5/5

The above implementation means that the program itself opened 8000 ports, and then to its own 8000 to send, so not only sent out, but also received and resolved the content. If you want to send it to another machine, you can also run the program on the other machine. Of course, you can write a special receiver.

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.