您的位置:首页 > 编程语言 > PHP开发

利用JRTPLIB库传输视频文件——发送端程序

2011-06-14 16:09 246 查看
发送端是嵌入式开发板,芯片为ADI的blackfin系列ADSP-BF537

此次发布的发送程序和接收程序是经过测试的。

以下为发送端程序,注明下以LOCALTEST宏标注的是自发自收功能模块,现已#undef LOCALTEST了。同时在自发自收部分,我使用了Poll(),说明我没有采用Jthread库来实现多线程,而直接用单线程,即SendPacket一个数据包后,后面用Poll()来轮询是否有数据包到达。如果使用了Jthread库,则Poll()就不用了。

// JRTPLIB sender on ADSP-BF537
// wangsu820@163.com 2008-06-30
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>

#include <sys/socket.h>

#include "include/rtpsession.h"
#include "include/rtpsessionparams.h"
#include "include/rtpudpv4transmitter.h"
#include "include/rtpipv4address.h"
#include "include/rtperrors.h"

//调试信息功能

#define DEBUG
//本地测试功能
#undef LOCALTEST

#ifdef LOCALTEST
#include "include/rtppacket.h"
typedef unsigned char uint8_t;
//blackfin
typedef unsigned long size_t;
#endif

void checkerror( int errorcode )
{
if( errorcode < 0 )
{
std::cout<< "ERROR:" << RTPGetErrorString( errorcode ) << std::endl;
exit( -1 );
}
}

void usage( void )
{
std::cout << "Usage: ./bf537_send DestinationIPAddress DestinationPort" << std::endl;
}

int main( int argc, char **argv )
{
RTPSession rtpsess;
RTPSessionParams sessionparams;
RTPUDPv4TransmissionParams transparams;
unsigned long destip;
int destport, srcport = 8000;
int packetnum;
int status, i;

//命令行参数解析
if( argc != 3 )
{
usage();
return -1;
}

destip = inet_addr( argv[1] );
if( INADDR_NONE == destip )
{
std::cerr << "Bad IP Address" << std::endl;
return -1;
}
destip = ntohl( destip );
std::cout << "Destination IP Address:"
<< argv[1]
<< std::endl;

destport = atoi( argv[2] );
std::cout << "Destination Port:"
<< destport
<< std::endl;

std::cout << "How many packets you want to send?" << std::endl;
std::cin >> packetnum;

//创建会话
sessionparams.SetOwnTimestampUnit( 1.0 / 8000.0 );
#ifdef LOCALTEST
sessionparams.SetAcceptOwnPackets( true );
std::cout << "LOCALTEST,you can receive your sent data" << std::endl;
#endif
transparams.SetPortbase( srcport );
status = rtpsess.Create( sessionparams, &transparams );
#ifdef DEBUG
std::cout << "check if Create Session error" << std::endl;
#endif
checkerror( status );

//将目标地址添加到发送地址列表中
RTPIPv4Address addr( destip, destport );
status = rtpsess.AddDestination( addr );
#ifdef DEBUG
std::cout << "check if AddDestination error" << std::endl;
#endif
checkerror( status );

//设置RTP包的默认参数
rtpsess.SetDefaultPayloadType( 0 );
rtpsess.SetDefaultMark( false );
rtpsess.SetDefaultTimestampIncrement( 10 );

//循环发送数据
for( i = 1; i <= packetnum; i++ )
{
printf( "Send packet [%d/%d]......", i, packetnum );
status = rtpsess.SendPacket( ( void * )"1234567890", 10 );
checkerror( status );
printf( "OK/n" );
#ifdef LOCALTEST
//本地自己接收
rtpsess.BeginDataAccess();
#ifdef DEBUG
std::cout << "localhost begin to receive..." << std::endl;
#endif
if( rtpsess.GotoFirstSourceWithData() )
{
#ifdef DEBUG
std::cout << "Go to First Source With Data..." << std::endl;
#endif
do
{
RTPPacket *rtppack;
while( ( rtppack = rtpsess.GetNextPacket() ) != NULL )
{
//printf( "Got packet/n" );
std::cout << "Got packet,"
<< "data is:"
<<
rtppack->GetPayloadData()
<< "length is "
<<
rtppack->GetPayloadLength()
<< std::endl;
rtpsess.DeletePacket( rtppack );
}
}while( rtpsess.GotoNextSourceWithData() );
}
rtpsess.EndDataAccess();
//因为不使用poll thread,所以在接和发RTCP包时,该函数需被调用
status = rtpsess.Poll();
checkerror( status );
#endif //end LOCALTEST
RTPTime::Wait( RTPTime( 1, 0 ) );
}//end for()

//离开会话
rtpsess.BYEDestroy( RTPTime( 10, 0 ), 0, 0 );
#ifdef DEBUG
std::cout << "GoodBye RTP Session" << std::endl;
#endif
return 0;
}



转自 http://blog.sina.com.cn/s/blog_4e87ac6e01009x8q.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: