即時資料流通訊協定RTSP(RealTimeStreamingProtocol)是由RealNetworks和 Netscape共同提出的,該協議定義了一對多應用程式如何有效地通過IP網路傳送多媒體資料。RTSP在體繫結構上位於RTP(即時傳輸)和RTCP(即時控制)之上,它使用 TCP或RTP完成資料轉送。HTTP與RTSP相比,HTTP傳送HTML,而RTP傳送的是多媒體資料。HTTP請求由客戶機發出,伺服器作出響應;使用RTSP時,客戶機和伺服器都可以發出請求,即RTSP可以是雙向的。
即時資料流通訊協定(RTSP)是應用級協議,控制即時資料的發送。RTSP提供了一個可擴充架構,使即時資料,如音頻與視頻,的受控、點播成為可能。
RTP是目前解決流媒體即時傳輸問題的最好辦法,如果要開發,可以選擇JRTPLIB庫。JRTPLIB是一個物件導向的RTP庫,它完全遵循RFC 1889設計。JRTPLIB是一個用C++語言實現的RTP庫,目前已經可以運行在Windows、Linux、FreeBSD、Solaris、Unix和 VxWorks等多種作業系統上。
下面的例子參考jrtplib的example1,加瞭解析負載的部分。
// 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;
//輸入一些必要資訊
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;
// 建立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);
RTPIPv4Address addr(destip,destport);
status = sess.AddDestination(addr);
checkerror(status);
for (i = 1 ; i <= num ; i++)
{
printf(" Sending packet %d/%d ",i,num);
// 發送資料“1234567890”
status = sess.SendPacket((void *)"1234567890",10,0,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 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;
}
編譯註意修改每個Source File的code generation下的Use run-time library為Debug Multithreaded DLL。
關於jrtplib的環境,可以參考網上很多的資料,也可以從我的資源裡下載,我已經編譯好了相關lib,只要加的VC環境裡就可以了。
執行測試程式的效果如下:
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
上面執行的意思是程式自己開了8000連接埠,然後往自己的8000發送,所以不僅發送出去,還收到並解析出了內容。如果要往另外機器上發,另一個機器上也運行這個程式就可以了。當然可以專門再寫一個接收端。