util.js 4.15 KB

//操作媒体或者屏幕共享
//roomHandle -> pluginHandle
//media  -> 媒体参数
//publish -> 确认参数
//errMethod -> 失败函数执行
optionMedia = function (roomHandle, media, publish, errMethod) {
	roomHandle.createOffer({
		media: media,
		simulcast: false,
		simulcast2: false,
		success: function(jsep) {
			roomHandle.send({"message": publish, "jsep": jsep});
		},
		error: function(error) {
			errMethod();
		}
	});  
}

//销毁
//roomHandle -> pluginHandle
//room -> 房间号码
//pin -> 设置的操作密码
optionDestroy = function(roomHandle, room, pin) {
	var destroy = {"request" : "destroy","room": room, "secret": pin}
	roomHandle.send({"message": destroy, success: function(result) {
	}})	
}

//在线人数
//roomHandle -> pluginHandle
//room -> 房间号码
//method -> 成功函数执行
listparticipants = function(roomHandle, room, method) {
	var body = { "request" : "listparticipants", "room" : room};
	roomHandle.send({"message": body, success: function(result) {
		var event = result["participants"];
		method(event);
	 }})
}

 //初始化
 //server -> 服务器地址
 //janus -> 服务器会话
 //succMethod -> 成功函数执行
 //errMethod ->  失败函数执行
 //desMethod ->  销毁函数执行
 init = function(server, janus, succMethod, errMethod, desMethod) {
	Janus.init({debug: "all", callback: function() {		
	    if(!Janus.isWebrtcSupported()) {
			bootbox.alert("不支持WebRTC... ");
			return;
		}
	    janus = new Janus({
			server: server,
			success: function() {
				succMethod(janus);
			},
			error: function(error) {
				errMethod(error);
			},
			destroyed: function() {
				desMethod();
			}	
		})
	}})
}

//创建句柄
//janus -> 服务器会话
//method -> 成功函数执行
attach = function(janus, method) {
	janus.attach({
		plugin: "janus.plugin.videoroom",
		success: function(pluginHandle) {
			method(pluginHandle);
		}
	})
}

//房间是否存在
//roomhandler -> pluginHandle
//room -> 房间号码
//method -> 成功函数执行
exit =  function(roomhandler, room, method) {
	var exits = {"request": "exists", "room": room};
	roomhandler.send({"message": exits, success: function(result) {
		 var event = result["exists"];
		 method(event);
	}})
}

//创建房间
//roomhandler -> pluginHandle
//create -> create参数
//method -> 成功函数执行
creatRoom = function(roomhandler, create, method) {
	Janus.log("插件 (" + roomhandler.getPlugin() + ", id=" + roomhandler.getId() + ")");
	roomhandler.send({"message": create, success: function(result) {
		var event = result["videoroom"];
		method(event);
	}})
}


//房间集合
//roomhandler -> pluginHandle
//method -> 成功函数执行
list = function(roomhandler, method) {
	var list = {"request": "list"};
	roomhandler.send({"message": list, success: function(result) {
		 var event = result["list"];
		 method(event);
	  }
    });
}

//创建会话
//janus -> 服务器会话
creatAttach = function(janus, succMethod, errMethod, conMethod, msMethod, 
	wsMethod,omMethod, olMethod, orMethod, ocMethod) {
	janus.attach({
		plugin: "janus.plugin.videoroom",
		success: function(pluginHandle) {
			succMethod(pluginHandle);
		},
		error: function(error) {
			errMethod(error);
		},
		consentDialog: function(on) {
			conMethod(on);
		},
		mediaState: function(medium, on) {
			msMethod(medium, on);
		},
		webrtcState: function(on) {
			wsMethod(on);
		},
		onmessage: function(msg, jsep) {
			omMethod(msg, jsep);
		},
		onlocalstream: function(stream) {
			olMethod(stream);
		},
		onremotestream: function(stream) {
			orMethod(stream);
		},
		oncleanup: function() {
			ocMethod();
		}	
	})
}

//join会议
join = function(roomHandle, register) {
	roomHandle.send({"message": register});
}

//configure
configure = function(roomHandle, bitrate) {
	var configure = { "request": "configure", "bitrate": bitrate};
	roomHandle.send({"message": configure});
}

opentionAnswer = function(roomHandle, jsep, media, succMethod, errMethod) {
	roomHandle.createAnswer({
		jsep: jsep,
		media: media,
		success: function(jsep) {
			succMethod(jsep);
		},
		error: function(error) {
			errMethod(error);
		}
	})
}

jsepSend = function(roomHandle, body, jsep) {
	roomHandle.send({"message": body, "jsep": jsep});
}