webrtc服务器janus echotest学习一

Echo测试演示的是发送给服务器网关的音频和视频,服务器会回传给你,效果如下图所示:
这里写图片描述

代码分析

这里写图片描述
在janus = new Janus()时,调用Janus(gatewayCallbacks)在其中有函数createSession
createSession创建请求,成功建立一次httpAPICall,输出Created handle: 1747107217737787
Janus.httpAPICall(server, {
verb: ‘POST’,
withCredentials: withCredentials,
body: request,
success: function(json) {
Janus.debug(json);
if(json[“janus”] !== “success”) {
Janus.error(“Ooops: ” + json[“error”].code + ” ” + json[“error”].reason); // FIXME
callbacks.error(json[“error”].reason);
return;
}
Janus.sessions[sessionId] = that;
eventHandler();
callbacks.success();
},
成功回调eventHandler以及echoest的sucesss

echoest的sucesss函数如下:
定义了插值对象。
janus.attach(
{
plugin: “janus.plugin.echotest”,
opaqueId: opaqueId,
success: function(pluginHandle) {
$(‘#details’).remove();
echotest = pluginHandle;
Janus.log(“Plugin attached! (” + echotest.getPlugin() + “, id=” + echotest.ge
var body = { “audio”: true, “video”: true };
echotest.send({“message”: body});
从上面可见输出Plugin attached! (janus.plugin.echotest, id=1747107217737787)

echotest = pluginHandle;定义为插件,其中定义许多函数。
this.attach = function(callbacks) { createHandle(callbacks)} 实际调用createHandle
pluginHandles[handleId] = pluginHandle;
并且向服务器发起一次请求,如果成功时,回调插件的success函数,即
success: function(pluginHandle) {
echotest = pluginHandle;
Janus.log(“Plugin attached! (” + echotest.getPlugin() + “, id=” + echotest.getId() + “)”);
var body = { “audio”: true, “video”: true };
Janus.debug(“Sending message (” + JSON.stringify(body) + “)”);
echotest.send({“message”: body});
在这个函数准备向服务器发送相应的信息连接请求。

通信1

var body = { “audio”: true, “video”: true };
echotest.send({“message”: body});
echotest.send 实际调用sendMessage
Sending message to plugin (handle=3650608414117457):
响应
这里写图片描述
然后调用success函数

后面通过handleEvent 收到信息,调用Onmessage函数。信息如下:
这里写图片描述

echotest.createOffer函数,实际调用prepareWebrtc
Trying a createOffer too (audio/video sendrecv
打印上面消息,并且调用httpAPICall来进行通信

function eventHandler()
作用是发出httpAPICall请求,然后请求成功执行handleEvent
handleEvent 收到信息,调用Onmessage函数。
这里写图片描述

通信2

调用echotest.createOffer,实际调用prepareWebrtc函数,其中getUserMedia
获得本地流,并且调用streamsDone。其中streamsDone 函数建立RTCPeerConnection连接,并且准备本地的sdp,并且调用onlocalstream把本地流显示出来。

在函数function createOffer(handleId, media, callbacks)中如果成功回调callbacks.success(jsep);即当收到本地sdp信息时,成功回调,准备向服务器发送sdp信息:
success: function(jsep) {
Janus.debug(“Got SDP!”);
Janus.debug(jsep);
这里写图片描述
echotest.send({“message”: body, “jsep”: jsep});
调用sendMessage用来发送消息,如果发送成功,会收到ack包。
这里写图片描述
但是服务器处理完成,主动向客户端发这送sdp信息,客户端是通过handleEvent来处理。
收到消息,成功回调函数onmessage。收到远程的SDP信息。
这里写图片描述
并且调用echotest.handleRemoteJsep({jsep: jsep});即执行prepareWebrtcPeer
prepareWebrtcPeer的作用是设置远程的sdp信息,用来建立p2p连接config.pc.setRemoteDescription。

回调streamsDone里面的以下函数来获得远程流。
config.pc.ontrack = function(event) {
pluginHandle.onremotestream(config.remoteStream);
这里写图片描述

扫描二维码关注公众号,回复: 1725363 查看本文章

并且回调onremotestream用来显示远程流。

猜你喜欢

转载自blog.csdn.net/bvngh3247/article/details/80746640
今日推荐