当前位置:网站首页>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);
边栏推荐
- Standard particle swarm optimization C language program
- 学习太极创客 — MQTT 第二章(四)ESP8266 保留消息应用
- 控制器的功能和工作原理
- BioVendor sRAGE Elisa试剂盒化学性质和技术研究
- Role of native keyword
- Lumiprobe细胞成像分析:PKH26 细胞膜标记试剂盒
- 2022年G3锅炉水处理复训题库模拟考试平台操作
- 2022新版nft源码中国元宇宙数字藏品艺术品交易平台源码
- [Matlab bp regression prediction] GA Optimized BP regression prediction (including comparison before optimization) [including source code 1901]
- UI automation test framework construction - write an app automation
猜你喜欢

Taco: a data enhancement technique for character recognition

别卷!如何高质量地复现一篇论文?

Learn Taiji Maker - mqtt Chapter 2 (IV) esp8266 reserved message application

A doctor's 22 years in Huawei (full of dry goods)

Where does the storm go? Whose pot is the weather forecast wrong?

UI自动化测试框架搭建 —— 编写一个APP自动化

Mise en place d'un cadre d'essai d'automatisation de l'interface utilisateur - - rédaction d'une application d'automatisation

大促场景下,如何做好网关高可用防护

Oracledata installation problems

恭喜我自己,公众号粉丝破万
随机推荐
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
LeetCode 88:合并两个有序数组
[csp-j2020] excellent splitting
Redis 的 最新windows 版本 5.0.14
机器人学DH参数及利用matlab符号运算推导
Feign remote call fallback callback failed, no effect
程序员坐牢了,会被安排去写代码吗?
The latest examination questions and answers for the eight members (standard members) of Liaoning architecture in 2022
BioVendor sRAGE抗体解决方案
Meta universe standard forum established
创新之源 理解通透 二
UI automation test framework construction - write an app automation
Simulation questions and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022
[early knowledge of activities] list of recent activities of livevideostack
Lumiprobe细胞成像分析:PKH26 细胞膜标记试剂盒
Notepad++ -- common plug-ins
2022年最新辽宁建筑八大员(标准员)考试试题及答案
CPG 固体支持物研究:Lumiprobe通用 CPG II 型
玩转双指针
100+数据科学面试问题和答案总结 - 机器学习和深度学习