|
| 1 | +// Copyright (C) <2018> Intel Corporation |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// Please change example.com to signaling server's address. |
| 8 | +const serverAddress = 'https://example.com:8096'; |
| 9 | + |
| 10 | +// Please change this STUN and TURN server information. |
| 11 | +const rtcConfiguration = { |
| 12 | + iceServers: [{ |
| 13 | + urls: 'stun:example.com:3478', |
| 14 | + }, { |
| 15 | + urls: [ |
| 16 | + 'turn:example.com:3478?transport=udp', |
| 17 | + 'turn:example.com:3478?transport=tcp', |
| 18 | + ], |
| 19 | + credential: 'password', |
| 20 | + username: 'username', |
| 21 | + }], |
| 22 | +}; |
| 23 | + |
| 24 | +const signaling = new SignalingChannel(); |
| 25 | +let publicationForCamera; |
| 26 | +const p2p = new Owt.P2P.P2PClient({ |
| 27 | + audioEncodings: true, |
| 28 | + videoEncodings: [{ |
| 29 | + codec: { |
| 30 | + name: 'h264', |
| 31 | + }, |
| 32 | + }, { |
| 33 | + codec: { |
| 34 | + name: 'vp9', |
| 35 | + }, |
| 36 | + }, { |
| 37 | + codec: { |
| 38 | + name: 'vp8', |
| 39 | + }, |
| 40 | + }], |
| 41 | + rtcConfiguration, |
| 42 | +}, signaling); |
| 43 | + |
| 44 | +let localStream; |
| 45 | +let screenStream; |
| 46 | + |
| 47 | +const getTargetId = function() { |
| 48 | + return $('#remote-uid').val(); |
| 49 | +}; |
| 50 | + |
| 51 | +$(document).ready(function() { |
| 52 | + $('#set-remote-uid').click(function() { |
| 53 | + p2p.allowedRemoteIds = [getTargetId()]; |
| 54 | + }); |
| 55 | + |
| 56 | + $('#target-screen').click(function() { |
| 57 | + const config = { |
| 58 | + audio: { |
| 59 | + source: 'screen-cast', |
| 60 | + }, |
| 61 | + video: { |
| 62 | + source: 'screen-cast', |
| 63 | + }, |
| 64 | + }; |
| 65 | + let mediaStream; |
| 66 | + Owt.Base.MediaStreamFactory.createMediaStream(config).then( |
| 67 | + (stream) => { |
| 68 | + mediaStream = stream; |
| 69 | + screenStream = new Owt.Base.LocalStream(mediaStream, new Owt |
| 70 | + .Base.StreamSourceInfo('screen-cast', 'screen-cast')); |
| 71 | + $('#local').children('video').get(0).srcObject = screenStream |
| 72 | + .mediaStream; |
| 73 | + p2p.publish(getTargetId(), screenStream).then( |
| 74 | + (publication) => {}), (error) => { |
| 75 | + console.log('Failed to share screen.'); |
| 76 | + }; |
| 77 | + }, (err) => { |
| 78 | + console.error('Failed to create MediaStream, ' + err); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + $('#target-video-unpublish').click(function() { |
| 83 | + $('#target-video-publish').prop('disabled', false); |
| 84 | + $('#target-video-unpublish').prop('disabled', true); |
| 85 | + publicationForCamera.stop(); |
| 86 | + for (const track of localStream.mediaStream.getTracks()) { |
| 87 | + track.stop(); |
| 88 | + } |
| 89 | + localStream = undefined; |
| 90 | + }); |
| 91 | + |
| 92 | + $('#target-video-publish').click(function() { |
| 93 | + $('#target-video-unpublish').prop('disabled', false); |
| 94 | + $('#target-video-publish').prop('disabled', true); |
| 95 | + if (localStream) { |
| 96 | + p2p.publish(getTargetId(), localStream).then((publication) => { |
| 97 | + publicationForCamera = publication; |
| 98 | + }, (error) => { |
| 99 | + console.log('Failed to share video.'); |
| 100 | + }); // Publish local stream to remote client |
| 101 | + } else { |
| 102 | + const audioConstraintsForMic = new Owt.Base.AudioTrackConstraints( |
| 103 | + Owt.Base.AudioSourceInfo.MIC); |
| 104 | + const videoConstraintsForCamera = new Owt.Base |
| 105 | + .VideoTrackConstraints(Owt.Base.VideoSourceInfo.CAMERA); |
| 106 | + let mediaStream; |
| 107 | + Owt.Base.MediaStreamFactory.createMediaStream(new Owt.Base |
| 108 | + .StreamConstraints(audioConstraintsForMic, |
| 109 | + videoConstraintsForCamera)).then((stream) => { |
| 110 | + mediaStream = stream; |
| 111 | + localStream = new Owt.Base.LocalStream(mediaStream, new Owt |
| 112 | + .Base.StreamSourceInfo('mic', 'camera')); |
| 113 | + $('#local').children('video').get(0).srcObject = localStream |
| 114 | + .mediaStream; |
| 115 | + p2p.publish(getTargetId(), localStream).then( |
| 116 | + (publication) => { |
| 117 | + publicationForCamera = publication; |
| 118 | + }, (error) => { |
| 119 | + console.log('Failed to share video.'); |
| 120 | + }); |
| 121 | + }, (err) => { |
| 122 | + console.error('Failed to create MediaStream, ' + err); |
| 123 | + }); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + $('#target-peerconnection-stop').click(function() { |
| 128 | + p2p.stop($('#remote-uid').val()); // Stop conversation |
| 129 | + }); |
| 130 | + |
| 131 | + $('#login').click(function() { |
| 132 | + p2p.connect({ |
| 133 | + host: serverAddress, |
| 134 | + token: $('#uid').val(), |
| 135 | + }).then(() => { |
| 136 | + $('#uid').prop('disabled', true); |
| 137 | + }, (error) => { |
| 138 | + console.log('Failed to connect to the signaling server.'); |
| 139 | + }); // Connect to signaling server. |
| 140 | + }); |
| 141 | + |
| 142 | + $('#logoff').click(function() { |
| 143 | + p2p.disconnect(); |
| 144 | + $('#uid').prop('disabled', false); |
| 145 | + }); |
| 146 | + |
| 147 | + $('#data-send').click(function() { |
| 148 | + p2p.send(getTargetId(), $('#dataSent') |
| 149 | + .val()); // Send data to remote endpoint. |
| 150 | + }); |
| 151 | +}); |
| 152 | + |
| 153 | +p2p.addEventListener('streamadded', |
| 154 | + function(e) { // A remote stream is available. |
| 155 | + e.stream.addEventListener('ended', () => { |
| 156 | + console.log('Stream is removed.'); |
| 157 | + }); |
| 158 | + if (e.stream.source.video === 'screen-cast') { |
| 159 | + $('#screen video').show(); |
| 160 | + $('#screen video').get(0).srcObject = e.stream.mediaStream; |
| 161 | + $('#screen video').get(0).play(); |
| 162 | + } else if (e.stream.source.audio || e.stream.source.video) { |
| 163 | + $('#remote video').show(); |
| 164 | + $('#remote video').get(0).srcObject = e.stream.mediaStream; |
| 165 | + $('#remote video').get(0).play(); |
| 166 | + } |
| 167 | + }); |
| 168 | + |
| 169 | +p2p.addEventListener('messagereceived', |
| 170 | + function(e) { // Received data from datachannel. |
| 171 | + $('#dataReceived').val(e.origin + ': ' + e.message); |
| 172 | + }); |
| 173 | + |
| 174 | +window.onbeforeunload = function() { |
| 175 | + p2p.stop($('#remote-uid').val()); |
| 176 | +}; |
0 commit comments