利用JRTPLIB库传输视频文件——发送端程序
2013-06-27 19:39
253 查看
利用JRTPLIB库传输视频文件——发送端程序
(2008-07-10 18:09:11)转载▼
标签: | 分类: 其他技术文章 |
//wangsu820@163.com 2008-06-30
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include <sys/socket.h>
#include "rtpsession.h"
#include "rtpsessionparams.h"
#include "rtpudpv4transmitter.h"
#include "rtpipv4address.h"
#include "rtperrors.h"
using namespace std;
//调试信息功能
#define DEBUG
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 FilePath" << std::endl;
}
long getfilelen( FILE *fp )
{
long cur_pos;
long len;
if( NULL == fp )
{
printf( "FILE fp is NULL\n" );
exit(-1);
}
cur_pos = ftell( fp );
fseek( fp, 0, SEEK_END );
len = ftell( fp );
fseek( fp, cur_pos, SEEK_SET );
return len;
}
int main( int argc, char **argv )
{
RTPSession rtpsess;
RTPSessionParams sessionparams;
RTPUDPv4TransmissionParams transparams;
unsigned long destip;
int destport, srcport = 8000;
char *filepath;
FILE *fp;
long filelength, templength, maxpacket, sendsize;
unsigned char *mem;
int status;
//命令行参数解析
if( argc != 4 )
{
usage();
return -1;
}
destip = inet_addr( argv[1] );
if( INADDR_NONE == destip )
{
cerr << "Bad IP Address" << endl;
return -1;
}
destip = ntohl( destip );
cout << "Destination IP Address:"
<< argv[1]
<< endl;
destport = atoi( argv[2] );
cout << "Destination Port:"
<< destport
<< endl;
strcpy( filepath, argv[3] );
cout << "FilePath:"
<< filepath
<< endl;
//创建会话
sessionparams.SetOwnTimestampUnit( 1.0 / 8000.0 );
transparams.SetPortbase( srcport );
status = rtpsess.Create( sessionparams, &transparams );
#ifdef DEBUG
cout << "check if Create Session error" << endl;
#endif
checkerror( status );
//将目标地址添加到发送地址列表中
RTPIPv4Address addr( destip, destport );
status = rtpsess.AddDestination( addr );
#ifdef DEBUG
cout << "check if AddDestination error" << endl;
#endif
checkerror( status );
//设置RTP包的默认参数
rtpsess.SetDefaultPayloadType( 0 );
rtpsess.SetDefaultMark( false );
rtpsess.SetDefaultTimestampIncrement( 10 );
//打开文件
fp = fopen( filepath, "rb" );
if( NULL == fp )
{
perror( "fopen:" );
exit(-1);
}
//获取文件大小
filelength = getfilelen( fp );
#ifdef DEBUG
printf( "the size of %s is %ldKB\n", filepath, (filelength / 1024) );
#endif
//获取所允许的最大包的大小
maxpacket = sessionparams.GetMaximumPacketSize();
#ifdef DEBUG
printf( "maximum packet size is %ldKB\n", (maxpacket / 1024) );
#endif
maxpacket -= 12;
#ifdef DEBUG
printf( "maximum payload size per packet is %ldKB(%ldB)\n", (maxpacket / 1024), maxpacket );
#endif
//分配内存空间,存放整个文件
mem = (unsigned char *)malloc( filelength );
#ifdef DEBUG
printf( "memory buffer is 0x%8x\n", (int)mem );
#endif
//读取文件内容到内存
templength = fread( mem, 1, filelength, fp );
if( templength != filelength )
{
printf( "fread error: templength=%ld, filelength=%ld\n", templength, filelength );
exit(-1);
}
#ifdef DEBUG
printf( "Have fread %ldKB\n", (templength / 1024) );
#endif
//关闭文件
fclose( fp );
sendsize = filelength;
//发送文件
if( filelength <= maxpacket )
{
#ifdef DEBUG
printf( "we will send data in a single packet\n" );
#endif
status = rtpsess.SendPacket( mem, filelength );
checkerror( status );
printf( "Sent %s...[%ldKB/%ldKB]", filepath, (filelength / 1024), (filelength / 1024) );
}
else//filelength > maxpacket文件大于最大包大小,须分包发送
{
#ifdef DEBUG
printf( "we will divide data into serval packets to send\n" );
#endif
while(1)
{
if( sendsize <= maxpacket )
{
status = rtpsess.SendPacket( mem, sendsize );
checkerror( status );
printf( "Sent %s...[%ldKB/%ldKB]\n", filepath,
(filelength / 1024), (filelength / 1024) );
break;
}
status = rtpsess.SendPacket( (void *)mem, maxpacket );
checkerror( status );
sendsize -= maxpacket;
printf( "Sent %s...[%ldKB/%ldKB]\n", filepath,
((filelength - sendsize) / 1024), (filelength / 1024) );
mem += maxpacket;
RTPTime::Wait( RTPTime( 0, 200 ) );
}
}
//离开会话
rtpsess.BYEDestroy( RTPTime( 10, 0 ), 0, 0 );
#ifdef DEBUG
cout << "GoodBye RTP Session" << endl;
#endif
return 0;
}
转载于:http://blog.sina.com.cn/s/blog_4e87ac6e01009y26.html
相关文章推荐
- 利用JRTPLIB库传输视频文件——发送端程序
- 利用socket实现简单的文件传输小程序
- 利用ssh传输文件
- 利用ssh传输文件
- 利用Socket实现多客户端传输对象和传输文件实现
- 『openframeworks』利用vlc播放视频文件
- 利用tftpd32在Linux与Windows7之间传输文件
- 探究ftp的奥妙之系统用户如何上传下载文件、vsftpd如何识别证书并利用客户端来实现远程连接传输
- java 利用socket传输文件
- 用程序自动把视频文件转为flv文件
- Xshell5下利用sftp上传下载传输文件
- LConfig:利用Lua脚本做程序的配置文件 [转]
- 非正式测试用DDS写的“视频”传输程序
- liunx 利用ssh传输文件
- 利用pscp和psftp工具,在windows和linux之间传输文件
- 利用程序代码删除文件
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务2
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务4[聊天室][使用IE浏览本页]
- 利用WebUploader实现大文件上传和视频上传
- 利用CANVAS进行文件压缩传输解析是否可行