当前位置:网站首页>metaRTC5.0 API编程指南(一)
metaRTC5.0 API编程指南(一)
2022-06-28 04:56:00 【metaRTC】
概述
metaRTC5.0版本 API进行了重构,本篇文章将介绍webrtc传输调用流程和例子。
metaRTC5.0版本提供了C++和纯C两种接口。
纯C接口YangPeerConnection
头文件:include/yangrtc/YangPeerConnection.h
typedef struct{
void* conn;
YangAVInfo* avinfo;
YangStreamConfig streamconfig;
}YangPeer;
typedef struct {
YangPeer peer;
//初始化
void (*init)(YangPeer* peer);
//从stun服务器取得外网ip和端口
int32_t (*requestStunServer)(YangPeer *peer);
//生成本端sdp
int32_t (*createOffer)(YangPeer* peer, char **psdp);
//交换sdp时,获取对端sdp后,生成本地sdp以回传回对端
int32_t (*createAnswer)(YangPeer* peer,char* answer);
//http侦听信令侦听到请求后,生成http的answer sdp
int32_t (*createHttpAnswer)(YangPeer* peer,char* answer);
//启动metaRTC开始webrtc
int32_t (*setRemoteDescription)(YangPeer* peer,char* sdp);
//封装了srs/zlm的信令交换并启动metaRTC
int32_t (*connectSfuServer)(YangPeer* peer);
//关闭metartc
int32_t (*close)(YangPeer* peer);
//是否活连接
int32_t (*isAlive)(YangPeer* peer);
//是否已经连接
int32_t (*isConnected)(YangPeer* peer);
//推音频流
int32_t (*on_audio)(YangPeer* peer,YangFrame* audioFrame);
//推视频流
int32_t (*on_video)(YangPeer* peer,YangFrame* videoFrame);
//推送datachannel消息
int32_t (*on_message)(YangPeer* peer,YangFrame* msgFrame);
//发送rtc系统消息
int32_t (*sendRtcMessage)(YangPeer* peer, YangRtcMessageType mess);
}YangPeerConnection;
C++接口YangPeerConnection2
头文件:include/yangrtc/YangPeerConnection2.h
class YangPeerConnection2 {
public:
YangPeerConnection2(YangAVInfo* avinfo,YangStreamConfig* streamConfig);
virtual ~YangPeerConnection2();
YangStreamConfig* streamConfig;
public:
//初始化
void init();
//从stun服务器取得外网ip和端口
int32_t requestStunServer();
//生成本端sdp
int32_t createOffer( char **psdp);
//交换sdp时,获取对端sdp后,生成本地sdp以回传回对端
int32_t createAnswer(char* answer);
//http侦听信令侦听到请求后,生成http的answer sdp
int32_t createHttpAnswer(char* answer);
//启动metaRTC开始webrtc
int32_t setRemoteDescription(char* sdp);
//封装了srs/zlm的信令交换并启动metaRTC
int32_t connectSfuServer();
//关闭metartc
int32_t close();
//是否活连接
int32_t isAlive();
//是否已经连接
int32_t isConnected();
//推音频流
int32_t on_audio(YangFrame* audioFrame);
//推视频流
int32_t on_video(YangFrame* videoFrame);
//推送datachannel消息
int32_t on_message(YangFrame* msgFrame);
//发送rtc系统消息
int32_t sendRtcMessage( YangRtcMessageType mess);
private:
YangPeerConnection m_conn;
};接口调用流程及例子
配置参数
参数在YangStreamConfig和YangAVInfo两个struct中
libmetartc5/src/yangp2p/YangP2pRtc.cpp中代码样例
void g_p2p_rtcrecv_sendData(void* context,YangFrame* frame){
YangP2pRtc* rtcHandle=(YangP2pRtc*)context;
rtcHandle->publishMsg(frame);
}
void g_p2p_rtcrecv_sslAlert(void* context,int32_t uid,char* type,char* desc){
if(context==NULL||type==NULL||desc==NULL) return;
YangP2pRtc* rtc=(YangP2pRtc*)context;
if(strcmp(type,"warning")==0&&strcmp(desc,"CN")==0){
rtc->removePeer(uid);
}
}
void g_p2p_rtcrecv_receiveAudio(void* user,YangFrame *audioFrame){
if(user==NULL || audioFrame==NULL) return;
YangP2pRtc* rtcHandle=(YangP2pRtc*)user;
rtcHandle->receiveAudio(audioFrame);
}
void g_p2p_rtcrecv_receiveVideo(void* user,YangFrame *videoFrame){
if(user==NULL || videoFrame==NULL) return;
YangP2pRtc* rtcHandle=(YangP2pRtc*)user;
rtcHandle->receiveVideo(videoFrame);
}
void g_p2p_rtcrecv_receiveMsg(void* user,YangFrame *msgFrame){
if(user==NULL) return;
YangP2pRtc* rtcHandle=(YangP2pRtc*)user;
rtcHandle->receiveMsg(msgFrame);
}
int32_t YangP2pRtc::connectPeer(int32_t nettype, string server,int32_t localPort,int32_t pport,string app,string stream) {
int32_t ret = 0;
YangPeerConnection* sh=(YangPeerConnection*)calloc(sizeof(YangPeerConnection),1);
strcpy(sh->peer.streamconfig.app,app.c_str());
sh->peer.streamconfig.streamOptType=Yang_Stream_Both;
strcpy(sh->peer.streamconfig.remoteIp,server.c_str());
sh->peer.streamconfig.remotePort=pport;
m_clientUid=m_uidSeq++;
strcpy(sh->peer.streamconfig.stream,stream.c_str());
sh->peer.streamconfig.uid=m_clientUid;
sh->peer.streamconfig.isServer=0;
sh->peer.streamconfig.localPort=localPort;
sh->peer.streamconfig.recvCallback.context=this;
sh->peer.streamconfig.recvCallback.receiveAudio=g_p2p_rtcrecv_receiveAudio;
sh->peer.streamconfig.recvCallback.receiveVideo=g_p2p_rtcrecv_receiveVideo;
sh->peer.streamconfig.recvCallback.receiveMsg=g_p2p_rtcrecv_receiveMsg;
}纯C接口调用例子
YangPeerConnection* sh=(YangPeerConnection*)calloc(sizeof(YangPeerConnection),1);
....配置参数..
//将参数传入YangPeerConnection memcpy(&sh->peer.streamconfig.rtcCallback,&m_context->rtcCallback,sizeof(YangRtcCallback));
sh->peer.avinfo=&m_context->avinfo;
//1.初始化
yang_create_peerConnection(sh);
sh->init(&sh->peer);
//2.生成本端sdp,srs和zlm调用不需要这一步,p2p需要
char* localSdp;
char* remoteSdp=(char*)calloc(12*1000,1);
//stun请求,连接srs和zlm不需要
if(m_context->avinfo.rtc.hasIceServer){
if(sh->requestStunServer(&sh->peer)!=Yang_Ok) yang_error("request stun server fail!");
}
sh->createOffer(&sh->peer, &localSdp);
//3.取得对端或者sfu服务器的sdp后,启动metartc
//点对点调用
ret=sh->setRemoteDescription(&sh->peer,remoteSdp);
//srs/zlm调用,里面已经封装了sdp交换和metartc启动
ret=sh->connectSfuServer(&sh->peer)
//4.执行程序后,销毁对象
sh->close(&sh->peer);
yang_destroy_peerConnection(sh);
yang_free(sh);
C++接口调用例子
....参数配置..
//1.初始化
YangPeerConnection2* sh=new YangPeerConnection2(&m_context->avinfo,&streamconfig);
sh->init();
//2.生成本端sdp,注意:srs和zlm调用不需要这一步
char* localSdp;
char* remoteSdp=(char*)calloc(12*1000,1);
//stun请求,连接srs和zlm不需要
if(m_context->avinfo.rtc.hasIceServer){
if(sh->requestStunServer()!=Yang_Ok) yang_error("request stun server fail!");
}
sh->createOffer(&localSdp);
//3.取得对端或者sfu服务器的sdp后,启动metartc
//srs/zlm调用
ret = sh->connectSfuServer();
//p2p调用
ret=sh->setRemoteDescription(remoteSdp);
//4.执行程序后,销毁对象
sh->close();
yang_delete(sh);
边栏推荐
- 大促场景下,如何做好网关高可用防护
- 氨基染料研究:Lumiprobe FAM 胺,6-异构体
- Project practice! Teach you JMeter performance test hand in hand
- Redis 的 最新windows 版本 5.0.14
- 乔布斯在斯坦福大学的演讲稿——Follow your heart
- 整理网上蛋糕商城项目
- 基于订单流工具,我们能看到什么?
- 2022电力电缆判断题模拟考试平台操作
- Blocking, non blocking, IO multiplexing select\poll\epoll
- Where does the storm go? Whose pot is the weather forecast wrong?
猜你喜欢

metaRTC5.0编程之p2p网络穿透(stun)指南

DH parameters of robotics and derivation using MATLAB symbolic operation

!‘ Cat 'is not an internal or external command, nor is it a runnable program or batch file.
![[NOIP2002 普及组] 过河卒](/img/6c/31fa210e08c7fd07691a1c5320154e.png)
[NOIP2002 普及组] 过河卒

Necessary skills for test and development: actual combat of security test vulnerability shooting range

2022年低压电工考题及答案

CI & CD must be known!

native关键字的作用

?位置怎么写才能输出true
![[Matlab bp regression prediction] GA Optimized BP regression prediction (including comparison before optimization) [including source code 1901]](/img/73/1e4c605991189acc674d85618cf0ef.png)
[Matlab bp regression prediction] GA Optimized BP regression prediction (including comparison before optimization) [including source code 1901]
随机推荐
!‘cat‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
Mise en place d'un cadre d'essai d'automatisation de l'interface utilisateur - - rédaction d'une application d'automatisation
2022年低压电工考题及答案
禁用右击、键盘打开控制台事件
native关键字的作用
Taco: a data enhancement technique for character recognition
wordpress zibll子比主题6.4.1开心版 免授权
Learning Tai Chi Maker - mqtt Chapter 2 (V) heartbeat mechanism
Cgo+gsoap+onvif learning summary: 8. Summary of arm platform cross compilation operation and common problems
C语言全局变量(c文件和h文件中的全局变量、静态全局变量)使用注意事项
CUPTI error: CUPTI could not be loaded or symbol could not be found.
学习太极创客 — MQTT 第二章(四)ESP8266 保留消息应用
Distributed transaction - Final consistency scheme based on message compensation (local message table, message queue)
学习太极创客 — MQTT 第二章(五)心跳机制
TACo:一种关于文字识别的数据增强技术
Unity delegate
Huawei's 9-year experience as a software testing director
控制器的功能和工作原理
现代交换原理MOOC部分题目整理
恭喜我自己,公众号粉丝破万