var janus=null;var sfu=null;var remoteFeeds=new Map;var opaqueId="videoroomtest-"+Janus.randomString(12);var myroom=1234;var mystream=null;var myvideoDOMContainer;var sfuIsReadyPromise=jQuery.Deferred();var webrtcPublished;function startJanus(server,myuser,turnServer,debug){switch(chatHTML5.config.codec){case"h264":myroom=1234;break;case"vp8":myroom=5678;break;case"vp9":myroom=8910;break}debug=true;if(!server){server="https://"+window.location.hostname+":8089/janus"}Janus.init({debug:debug,callback:function(){var iceServers=[];if(!turnServer||!turnServer.urls){turnServer={iceTransportPolicy:"all"}}else{turnServer.iceTransportPolicy="relay";iceServers.push({urls:turnServer.urls,username:turnServer.username,credential:turnServer.credential})}if(!Janus.isWebrtcSupported()){console.log("No WebRTC support... ");return}janus=new Janus({server:server,iceTransportPolicy:turnServer.iceTransportPolicy,iceServers:iceServers,success:function(){janus.attach({plugin:"janus.plugin.videoroom",opaqueId:opaqueId,success:function(pluginHandle){sfu=pluginHandle;Janus.log("Plugin attached! ("+sfu.getPlugin()+", id="+sfu.getId()+")");var event=new Event("janusReady");document.dispatchEvent(event);var register={request:"join",room:myroom,ptype:"publisher",display:myuser.username,id:parseInt(myuser.id)};sfu.send({message:register});sfuIsReadyPromise.resolve(true)},error:function(error){Janus.error(" -- Error attaching plugin...",error);console.log("Error attaching plugin... "+error)},consentDialog:function(on){Janus.debug("Consent dialog should be "+(on?"on":"off")+" now")},mediaState:function(medium,on){Janus.log("Janus "+(on?"started":"stopped")+" receiving our "+medium)},webrtcState:function(on){Janus.log("Janus says our WebRTC PeerConnection is "+(on?"up":"down")+" now")},onmessage:function(msg,jsep){Janus.debug(" ::: Got a message (publisher) :::");Janus.debug(msg);var event=msg["videoroom"];Janus.debug("Event: "+event);if(event!=undefined&&event!=null){if(event==="joined"){var myid=msg["id"];Janus.log("Successfully joined room "+msg["room"]+" with ID "+myid);if(msg["publishers"]!==undefined&&msg["publishers"]!==null){var list=msg["publishers"];Janus.debug("Got a list of available publishers/feeds:");Janus.debug(list);for(var f in list){var id=list[f]["id"];var display=list[f]["display"];var audio=list[f]["audio_codec"];var video=list[f]["video_codec"];Janus.debug(" >> ["+id+"] "+display+" (audio: "+audio+", video: "+video+")");console.log("1Streamid:",id,audio,video)}}}else if(event==="destroyed"){Janus.warn("The room has been destroyed!");console.log("The room has been destroyed")}else if(event==="event"){if(msg["publishers"]!==undefined&&msg["publishers"]!==null){var list=msg["publishers"];Janus.debug("Got a list of available publishers/feeds:");Janus.debug(list);for(var f in list){var id=list[f]["id"];var display=list[f]["display"];var audio=list[f]["audio_codec"];var video=list[f]["video_codec"];Janus.debug(" >> ["+id+"] "+display+" (audio: "+audio+", video: "+video+")");console.log("2Streamid:",id,audio,video)}}else if(msg["leaving"]!==undefined&&msg["leaving"]!==null){var leaving=msg["leaving"];Janus.log("Publisher left: "+leaving);var remoteFeed=null}else if(msg["unpublished"]!==undefined&&msg["unpublished"]!==null){var unpublished=msg["unpublished"];Janus.log("Publisher left: "+unpublished);if(unpublished==="ok"){var event=new Event("janusUnPublished");document.dispatchEvent(event);sfu.hangup();return}}else if(msg["error"]!==undefined&&msg["error"]!==null){if(msg["error_code"]===426){console.log("the one this demo uses as a test room")}else{console.log(msg["error"])}}}}if(jsep!==undefined&&jsep!==null){Janus.debug("Handling SDP as well...");Janus.debug(jsep);sfu.handleRemoteJsep({jsep:jsep});var audio=msg["audio_codec"];if(mystream&&mystream.getAudioTracks()&&mystream.getAudioTracks().length>0&&!audio){console.log("Our audio stream has been rejected, viewers won't hear us")}var video=msg["video_codec"];if(mystream&&mystream.getVideoTracks()&&mystream.getVideoTracks().length>0&&!video){console.log("Our video stream has been rejected, viewers won't see us")}}},onlocalstream:function(stream){Janus.debug(" ::: Got a local stream :::");mystream=stream;jQuery.event.trigger({type:"getMyStreamId",id:chatHTML5.myUser.id});var videoTracks=mystream.getVideoTracks();if(videoTracks===null||videoTracks===undefined||videoTracks.length===0){myvideoDOMContainer.html("No webcam available")}else{if(jQuery("#myVideo").length===0){var el=' ';myvideoDOMContainer.append(el);$("#myAudioCheckBox").bootstrapToggle();$("#myAudioCheckBox").parent().addClass("myAudioCheckBox")}Janus.attachMediaStream(jQuery("#myVideo").get(0),mystream);jQuery("#myVideo").get(0).muted="muted";var event=new Event("janusPublished");document.dispatchEvent(event)}},onremotestream:function(stream){},oncleanup:function(){Janus.log(" ::: Got a cleanup notification: we are unpublished now :::");mystream=null}})},error:function(error){Janus.error(error);console.log(error)},destroyed:function(){window.location.reload()}})}})}function publishStream(_myvideoDOMContainer,streamIfWatched,filenameRecord){webrtcPublished=jQuery.Deferred();myvideoDOMContainer=_myvideoDOMContainer;jQuery.when(sfuIsReadyPromise).done(function(res){publishOwnFeed(true,streamIfWatched,filenameRecord)})}function startRecordVideo(filenameRecord){publishOwnFeed(true,false,filenameRecord)}function stopRecordVideo(){unpublishOwnFeed();setTimeout(function(){publishOwnFeed(true,false,false)},3e3)}function publishOwnFeed(useAudio,streamIfWatched,filenameRecord){console.log("publishOwnFeed");sfu.createOffer({media:{audioRecv:false,videoRecv:false,audioSend:useAudio,videoSend:true},simulcast:false,success:function(jsep){console.log("publishing");webrtcPublished.resolve(true);Janus.debug("Got publisher SDP!");Janus.debug(jsep);var publish={request:"configure",audio:true,video:true};if(filenameRecord){publish.filename=filenameRecord;publish.record=true}else{publish.filename="";publish.record=false}chatHTML5.myUser.jsep=jsep;if(1==1||streamIfWatched!="1"){console.log("SendMessage PUBLISH !");sfu.send({message:publish,jsep:jsep})}},error:function(error){Janus.error("WebRTC error:",error);webrtcPublished.resolve(false);if(useAudio){publishOwnFeed(false)}else{console.log("WebRTC error... "+JSON.stringify(error));publishOwnFeed(true)}}})}function toggleMute(){var muted=sfu.isAudioMuted();Janus.log((muted?"Unmuting":"Muting")+" local stream...");if(muted)sfu.unmuteAudio();else sfu.muteAudio();muted=sfu.isAudioMuted()}function stopPublishStream(){unpublishOwnFeed()}function unpublishOwnFeed(){var unpublish={request:"unpublish"};sfu.send({message:unpublish})}function playStream(id,videoElementId,muted,video){id=parseInt(id);console.log("playStream",id);var remoteFeed=null;janus.attach({plugin:"janus.plugin.videoroom",opaqueId:opaqueId,success:function(pluginHandle){remoteFeeds.set(id,pluginHandle);remoteFeed=pluginHandle;remoteFeed.simulcastStarted=false;Janus.log("Plugin attached! ("+remoteFeed.getPlugin()+", id="+remoteFeed.getId()+")");Janus.log(" -- This is a subscriber");var listen={request:"join",room:myroom,ptype:"subscriber",feed:id};remoteFeed.videoCodec=video;remoteFeed.send({message:listen})},error:function(error){Janus.error(" -- Error attaching plugin...",error);console.log("Error attaching plugin... "+error)},onmessage:function(msg,jsep){Janus.debug(" ::: Got a message (subscriber) :::");Janus.debug(msg);var event=msg["videoroom"];Janus.debug("Event: "+event);if(msg["error"]!==undefined&&msg["error"]!==null){console.log(msg["error"])}else if(event!=undefined&&event!=null){if(event==="attached"){remoteFeed.rfid=msg["id"];remoteFeed.rfdisplay=msg["display"];Janus.log("Successfully attached to feed "+remoteFeed.rfid+" ("+remoteFeed.rfdisplay+") in room "+msg["room"])}else if(event==="event"){var substream=msg["substream"];var temporal=msg["temporal"];if(substream!==null&&substream!==undefined||temporal!==null&&temporal!==undefined){}}else{}}if(jsep!==undefined&&jsep!==null){Janus.debug("Handling SDP as well...");Janus.debug(jsep);remoteFeed.createAnswer({jsep:jsep,media:{audioSend:false,videoSend:false},success:function(jsep){Janus.debug("Got SDP!");Janus.debug(jsep);var body={request:"start",room:myroom};remoteFeed.send({message:body,jsep:jsep})},error:function(error){Janus.error("WebRTC error:",error);console.log("WebRTC error... "+JSON.stringify(error))}})}},webrtcState:function(on){Janus.log("Janus says this WebRTC PeerConnection (feed #"+remoteFeed.rfindex+") is "+(on?"up":"down")+" now")},onlocalstream:function(stream){},onremotestream:function(stream){Janus.debug("Remote feed #"+remoteFeed.rfindex);Janus.debug("Remote feed #"+remoteFeed.rfid);if(jQuery("#remotevideo"+remoteFeed.rfid).length===0){var mutedString=muted?'muted="muted"':"";var temp=sprintf('',remoteFeed.rfid,remoteFeed.rfid,mutedString);videoElementId.append(temp)}Janus.attachMediaStream(jQuery("#remotevideo"+remoteFeed.rfid).get(0),stream);var videoTracks=stream.getVideoTracks();if(videoTracks===null||videoTracks===undefined||videoTracks.length===0){}},oncleanup:function(){Janus.log(" ::: Got a cleanup notification (remote feed "+id+") :::")}})}function stopStream(streamid){var sfu2=remoteFeeds.get(streamid);if(sfu2){var leaveRequest={request:"leave"};sfu2.send({message:leaveRequest})}}