当前位置:网站首页>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);
边栏推荐
- OracleData安装问题
- Function and working principle of controller
- To quickly download JDK, in addition to the official Oracle download, is there a download address for the latest version available in China
- Analysis of distributed transaction solution Seata golang
- !‘cat‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
- The latest examination questions and answers for the eight members (standard members) of Liaoning architecture in 2022
- Learn Taiji Maker - mqtt Chapter 2 (IV) esp8266 reserved message application
- Flexible IP network test tool -- x-launch
- Extjs图书管理系统源码 智能化图书管理系统源码
- MySQL gets the current date of the year
猜你喜欢

On the necessity of building a video surveillance convergence platform and its scenario application
![[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‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。

Distributed transaction - Final consistency scheme based on message compensation (local message table, message queue)

2022年低压电工考题及答案

测试开发必备技能:安全测试漏洞靶场实战

学习太极创客 — MQTT 第二章(四)ESP8266 保留消息应用

MySQL gets the current date of the year

Multi thread implementation rewrites run (), how to inject and use mapper file to operate database
随机推荐
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
UI自動化測試框架搭建 —— 編寫一個APP自動化
【JVM系列】JVM调优
The number of small stores in Suning has dropped sharply by 428 in one year. Zhangkangyang, the son of Zhang Jindong, is the actual controller
UI自动化测试框架搭建 —— 编写一个APP自动化
Analysis of distributed transaction solution Seata golang
Ppt production tips
Generate QR code in wechat applet
Oracledata installation problems
[Matlab bp regression prediction] GA Optimized BP regression prediction (including comparison before optimization) [including source code 1901]
整理网上蛋糕商城项目
[noip2002 popularization group] cross the river pawn
BioVendor sRAGE抗体解决方案
Learning Tai Chi Maker - mqtt Chapter II (VI) mqtt wills
基于订单流工具,我们能看到什么?
2022西式面点师(高级)考试试题模拟考试平台操作
几百行代码实现一个脚本解释器
Role of native keyword
Meta universe standard forum established
吴恩达深度学习测验题:deeplearning.ai-week1-quiz