1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
const roomId = 0000;
const opaqueId = Janus.randomString(20);
let localStream;
let janus;
const server = 'wss://xxxxx';
let privateId;
const display = 'display';
const uid = Date.now();
let localVideoRoomHandle;
const remoteVMHandles = [];
console.log('Janus init start.'); Janus.init({ callback() { console.log('Janus init success.');
$('#start').click(async () => { localStream = await navigator.mediaDevices.getUserMedia({audio: true, video: true}); console.log('mediaDevices.getUserMedia success. localStream: ', localStream); addLocalVideo(localStream);
console.log('Create janus instance start.'); janus = new Janus({ server, iceServers: [{urls: 'stun:stun.stunprotocol.org'}], success() { console.log('Create janus instance success. janus:', janus);
console.log('Attach video room plugin start.'); janus.attach({ plugin: 'janus.plugin.videoroom', opaqueId, success(pluginHandle) { localVideoRoomHandle = pluginHandle; console.log('Attach video room plugin success. localVideoRoomHandle: ', localVideoRoomHandle); join(); }, onmessage: function(msg, jsep) { const event = msg['videoroom']; switch (event) { case 'joined': {
const {id, private_id} = msg; privateId = private_id; console.log('Join video room success. ', `id: ${id} privateId: ${privateId}`);
publish();
if (msg['publishers']) { for (const publisher of msg['publishers']) { subscribe(publisher.id, publisher.display, publisher.audio, publisher.video); } }
break; } case 'event': { if (msg['publishers']) { for (const publisher of msg['publishers']) { subscribe(publisher.id, publisher.display, publisher.audio, publisher.video); } } break; }
}
if (jsep) { localVideoRoomHandle.handleRemoteJsep({jsep}); } }, error(err) { console.error(err); }, }); }, error(error) { console.error(error); }, destroyed() { console.log('destroyed'); }, }); }); }, });
function join() { console.log('Join video room start.'); var register = { request: 'join', room: roomId, ptype: 'publisher', id: uid, display: display, }; localVideoRoomHandle.send({message: register}); }
function publish() { console.log('Publish video room start.'); localVideoRoomHandle.createOffer({ media: { audioRecv: false, videoRecv: false, audioSend: true, videoSend: true, }, success(jsep) { var publish = {request: 'configure', audio: true, video: true}; localVideoRoomHandle.send({message: publish, jsep: jsep}); }, }); }
function subscribe(id, display, audio, video) { console.log('subscribe start. params:', id, display, audio, video); let remoteHandle; janus.attach( { plugin: 'janus.plugin.videoroom', opaqueId, success: function(pluginHandle) { remoteHandle = pluginHandle; const subscribe = { request: 'join', room: roomId, ptype: 'subscriber', feed: id, private_id: privateId, }; remoteHandle.videoCodec = video; remoteHandle.send({message: subscribe}); console.log('remoteHandle:', remoteHandle); remoteVMHandles.push(remoteHandle); }, onmessage(msg, jsep) { var event = msg['videoroom']; if (jsep) { Janus.debug('Handling SDP as well...', jsep); remoteHandle.createAnswer( { jsep: jsep, media: {audioSend: false, videoSend: false}, success: function(jsep) { var body = {request: 'start', room: roomId}; remoteHandle.send({message: body, jsep: jsep}); }, }); } }, onremotestream: function(stream) { addRemoteVideo(stream, remoteHandle); }, }); }
function addLocalVideo(stream) { const videoEle = document.createElement('video'); videoEle.srcObject = stream; videoEle.width = 320; videoEle.height = 240; videoEle.autoplay = true; $('#localVideoWarp').append(videoEle); }
function addRemoteVideo(stream, remoteHandle) {
if ($(`#remoteHandle${remoteHandle.id}`)[0]) { return; }
const videoEle = document.createElement('video'); videoEle.srcObject = stream; videoEle.width = 320; videoEle.height = 240; videoEle.autoplay = true; videoEle.id = `remoteHandle${remoteHandle.id}`; $('#remoteVideoWarp').append(videoEle); }
|