helloyesyes 发表于 2013-1-16 17:32:16

VC 6 RTP流媒体传输协议编程实例(jrtplib)

资源下载:
http://download.csdn.net/source/444512

实时流协议RTSP(RealTimeStreamingProtocol)是由RealNetworks和 Netscape共同提出的,该协议定义了一对多应用程序如何有效地通过IP网络传送多媒体数据。RTSP在体系结构上位于RTP(实时传输)和RTCP(实时控制)之上,它使用 TCP或RTP完成数据传输。HTTP与RTSP相比,HTTP传送HTML,而RTP传送的是多媒体数据。HTTP请求由客户机发出,服务器作出响应;使用RTSP时,客户机和服务器都可以发出请求,即RTSP可以是双向的。

实时流协议(RTSP)是应用级协议,控制实时数据的发送。RTSP提供了一个可扩展框架,使实时数据,如音频与视频,的受控、点播成为可能。

RTSP是应用级的协议,完成多媒体服务器的远程控制,控制信息的传输可以使用TCP,控制指令包括如:Setup、Play、Record、Pause、Teardwon等等。
对于流媒体应用,用户和服务器都可以发出请求,请求包括几种连接方法:持久、每个请求/响应传输一个连接、无连接。
常见的URL流媒体地址如:
rtsp://media.example.com:554

RTP 数据报组成:Header + Payload
RTCP:应用程序启动RTP会话时将同时占用两个端口,供RTP和RTCP使用。

如果有必要,RTP使用时可以有两个伴随文档:1)配置文档,定义负载的编码类型和格式。2)负载格式的规范文档。

在流传输过程中,有两类服务完成对流的转发处理:
1)译流服务器Translator,进入的流在流出时发生变化,作用之一是更好地穿越防火墙。
2)混流服务器Mixer,多个流进入,合并后变成一个流流出。

由于进入的流可能有多个源,比如视频会议,会有多个话筒和视频头等等情况,对于RTP来说,就有一个同步化源的问题,因此,RTP协议中用SSRC(Synchronization Source)字段来供Mixer实现同步功能。

Translator的一个作用是多播变成多个单播。

为了提供播放和回放功能,RTP提供时间标签+序列号,在流动的概念中,时间标签是最重要的信息。

RTP报文不提供长度和报文边界的描述。

RTP虽然是传输层协议,但没有在OSI体系中作为单独的层来使用。

RTP是目前解决流媒体实时传输问题的最好办法,如果要开发,可以选择JRTPLIB库。JRTPLIB是一个面向对象的RTP库,它完全遵循RFC 1889设计。JRTPLIB是一个用C++语言实现的RTP库,目前已经可以运行在Windows、Linux、FreeBSD、Solaris、Unix和 VxWorks等多种操作系统上。

了解更多RTP参考:
http://www.cnitblog.com/zouzheng/archive/2008/01/04/38449.html

下面的例子参考jrtplib的example1,加了解析负载的部分。
<div style="">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif//RTPClient.cpp:Definestheentrypointfortheconsoleapplication.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif//
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#include"stdafx.h"
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#include"rtpsession.h"
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#include"rtppacket.h"
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#include"rtpudpv4transmitter.h"
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#include"rtpipv4address.h"
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#include"rtpsessionparams.h"
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#include"rtperrors.h"
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#include<winsock2.h>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#include<stdlib.h>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#include<stdio.h>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#include"windows.h"
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#include<iostream>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#include<string>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingnamespacestd;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#pragmacomment(lib,"jrtplib.lib")
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#pragmacomment(lib,"jthread.lib")
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#pragmacomment(lib,"WS2_32.lib")
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifvoidcheckerror(intrtperr)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifif(rtperr<0)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstd::cout<<"ERROR:"<<RTPGetErrorString(rtperr)<<std::endl;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifexit(-1);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifintmain(intargc,char*argv[])
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif#ifdefWIN32
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifWSADATAdat;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifWSAStartup(MAKEWORD(2,2),&dat);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif#endif//WIN32
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifRTPSessionsess;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifuint16_tportbase,destport;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifuint32_tdestip;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstd::stringipstr;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifintstatus,i,num;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifBYTE*pBuffer;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifBYTE*pfBuffer;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//输入一些必要信息
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstd::cout<<"Enterlocalportbase:"<<std::endl;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstd::cin>>portbase;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstd::cout<<std::endl;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstd::cout<<"EnterthedestinationIPaddress"<<std::endl;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstd::cin>>ipstr;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdestip=inet_addr(ipstr.c_str());
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifif(destip==INADDR_NONE)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstd::cerr<<"BadIPaddressspecified"<<std::endl;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifreturn-1;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdestip=ntohl(destip);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstd::cout<<"Enterthedestinationport"<<std::endl;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstd::cin>>destport;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstd::cout<<std::endl;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstd::cout<<"Numberofpacketsyouwishtobesent:"<<std::endl;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstd::cin>>num;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//创建RTPsession
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifRTPUDPv4TransmissionParamstransparams;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifRTPSessionParamssessparams;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//IMPORTANT:ThelocaltimestampunitMUSTbeset,otherwise
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//RTCPSenderReportinfowillbecalculatedwrong
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//Inthiscase,we'llbesending10sampleseachsecond,sowe'll
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//putthetimestampunitto(1.0/10.0)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifsessparams.SetOwnTimestampUnit(1.0/10.0);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifsessparams.SetAcceptOwnPackets(true);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giftransparams.SetPortbase(portbase);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstatus=sess.Create(sessparams,&transparams);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcheckerror(status);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifRTPIPv4Addressaddr(destip,destport);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstatus=sess.AddDestination(addr);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcheckerror(status);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giffor(i=1;i<=num;i++)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprintf(" Sendingpacket%d/%d ",i,num);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//发送数据“1234567890”
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstatus=sess.SendPacket((void*)"1234567890",10,0,false,10);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcheckerror(status);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifsess.BeginDataAccess();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//checkincomingpackets
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifif(sess.GotoFirstSourceWithData())
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdo
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifRTPPacket*pack;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifwhile((pack=sess.GetNextPacket())!=NULL)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//Youcanexaminethedatahere
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprintf("Gotpacket! ");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstd::cout<<"Gotpacketwith"
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif<<"extendedsequencenumber"
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif<<pack->GetExtendedSequenceNumber()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif<<"fromSSRC"<<pack->GetSSRC()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif<<std::endl;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifintdataLength=pack->GetPayloadLength();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpfBuffer=(unsignedchar*)pack->GetPayloadData();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpBuffer=newBYTE+1];
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmemcpy(pBuffer,pfBuffer,dataLength);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpBuffer=0;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstd::cout<<pBuffer<<std::endl;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//wedon'tlongerneedthepacket,so
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//we'lldeleteit
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifsess.DeletePacket(pack);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}while(sess.GotoNextSourceWithData());
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifsess.EndDataAccess();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif#ifndefRTP_SUPPORT_THREAD
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstatus=sess.Poll();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcheckerror(status);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif#endif//RTP_SUPPORT_THREAD
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifRTPTime::Wait(RTPTime(1,0));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifsess.BYEDestroy(RTPTime(10,0),0,0);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif#ifdefWIN32
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifWSACleanup();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif#endif//WIN32
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifreturn0;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
页: [1]
查看完整版本: VC 6 RTP流媒体传输协议编程实例(jrtplib)