jrtplib中组播代码发送端和接收端的实现


如果需要转载,请注明出处,谢谢

文章主要是把rtp组播的发送端和接收端的代码实现写了出来,最后面有代码的下载地址,看了网上基本没人把jrtp组播这个功能贴出来,于是看了看源码,将其实现,希望能对各位朋友有帮助,话不多说直接贴代码,欢迎交流和评论。



</pre><pre code_snippet_id="1677192" snippet_file_name="blog_20160510_3_3909878" name="code" class="cpp">发送端
#include "rtpsession.h"
#include "rtpudpv4transmitter.h"
#include "rtpipv4address.h"
#include "rtpsessionparams.h"
#include "rtperrors.h"
#include "rtppacket.h"
#ifndef WIN32
	#include <netinet/in.h>
	#include <arpa/inet.h>
#else
	#include <winsock2.h>
	#include<windows.h>
#endif // WIN32
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>

using namespace jrtplib;
using namespace std;


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

	#ifdef WIN32
	WSADATA dat;
	WSAStartup(MAKEWORD(2,2),&dat);
	#endif // WIN32

	list<uint32_t> localip;

	RTPSession sess;
	uint16_t portbase=6666,destport=8888;
	uint32_t destip,srcip;
	string ipstr="239.255.0.1";
	string ipsrc="114.212.118.99";
	int status,i,num;
	destip=inet_addr(ipstr.c_str());//将字符串转换为32位网络字节序。
	srcip=inet_addr(ipsrc.c_str());

	if(destip==INADDR_NONE)
	{
		cout<<"bad destip address"<<endl;
		exit(-1);
	}
	destip=ntohl(destip);//将32位网络字节序转换成主机字节序
	srcip=ntohl(srcip);
	localip.push_back(srcip);
	//设置传输参数和会话参数
	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/8000);

	sessparams.SetAcceptOwnPackets(true);
	transparams.SetPortbase(portbase);
	transparams.SetLocalIPList(localip);
	transparams.SetMulticastInterfaceIP(srcip);
	transparams.Set
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
使用jrtplib发送PS流的步骤如下: 1. 初始化jrtplib库并创建一个RTPSession实例。 2. 设置RTPSession的传输协议类型为PS。 3. 设置RTPSession的传输模式为推送模式。 4. 设置RTPSession的远程地址和口号。 5. 打开RTPSession。 6. 读取PS文件并将其分成小的NAL单元。 7. 将NAL单元打包成RTP包并发送到远程地址。 8. 关闭RTPSession。 代码示例: ```c++ #include <jrtplib3/rtpsession.h> #include <jrtplib3/rtpudpv4transmitter.h> #include <jrtplib3/rtpipv4address.h> #include <jrtplib3/rtptimeutilities.h> #include <iostream> #include <fstream> using namespace jrtplib; using namespace std; int main(int argc, char* argv[]) { RTPSession session; RTPUDPv4TransmissionParams transparams; RTPSessionParams sessparams; sessparams.SetOwnTimestampUnit(1.0/90000.0); transparams.SetPortbase(8000); int status = session.Create(sessparams,&transparams); if (status < 0) { cerr << "ERROR: " << RTPGetErrorString(status) << endl; return -1; } RTPIPv4Address addr("192.168.1.100", 9000); status = session.AddDestination(addr); if (status < 0) { cerr << "ERROR: " << RTPGetErrorString(status) << endl; return -1; } session.SetDefaultPayloadType(96); session.SetDefaultMark(false); session.SetTimestampUnit(1.0/90000.0); session.SetDefaultTimestampIncrement(3600); status = session.InitSend(); if (status < 0) { cerr << "ERROR: " << RTPGetErrorString(status) << endl; return -1; } ifstream fin("test.ps", ios::binary); if (!fin.is_open()) { cerr << "ERROR: cannot open file" << endl; return -1; } char nal[1024]; int nal_len = 0; while (!fin.eof()) { fin.read(nal + nal_len, 1024 - nal_len); nal_len += fin.gcount(); char* p = nal; while (nal_len > 0) { int len = 0; while (len < nal_len && !(p[len] == 0x00 && p[len+1] == 0x00 && p[len+2] == 0x01)) len++; if (len == nal_len) { break; } len += 3; RTPTime delay(0.020); session.SendPacket((void*)p, len, 96, false, delay); p += len; nal_len -= len; } } fin.close(); session.BYEDestroy(RTPTime(10,0),0,0); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值