Advertisement
Guest User

index.js changes with slight modification

a guest
May 29th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Copyright © 2017 Intel Corporation. All Rights Reserved.
  2. 'use strict';
  3. var conference;
  4.  
  5. const runSocketIOSample = function() {
  6.  
  7.     let localStream;
  8.     let showedRemoteStreams = [];
  9.     let myId;
  10.     let subscriptionForMixedStream;
  11.     let myRoom;
  12.  
  13.     function getParameterByName(name) {
  14.         name = name.replace(/[\[]/, '\\\[').replace(/[\]]/, '\\\]');
  15.         var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'),
  16.             results = regex.exec(location.search);
  17.         return results === null ? '' : decodeURIComponent(results[1].replace(
  18.             /\+/g, ' '));
  19.     }
  20.  
  21.     var subscribeForward = getParameterByName('forward') === 'true'?false:true;
  22.  
  23.     conference = new Ics.Conference.ConferenceClient();
  24.     function renderVideo(stream){
  25.         conference.subscribe(stream,{audio: false})
  26.         .then((subscriptions)=>{
  27.            let $video = $('<video controls autoplay width="720" height="540">this browser does not supported video tag</video>');
  28.            $video.get(0).srcObject = stream.mediaStream;
  29.            $('body').append($video);
  30.         }, (err)=>{ console.log('subscribe failed', err);
  31.         });
  32.     }
  33.     function subscribeDifferentResolution(stream, resolution) {
  34.         subscriptionForMixedStream.stop();
  35.         subscriptionForMixedStream = null;
  36.         const videoOptions = {};
  37.         videoOptions.resolution = resolution;
  38.         conference.subscribe(stream, {
  39.             audio: true,
  40.             video: videoOptions
  41.         }).then((
  42.             subscription) => {
  43.             subscriptionForMixedStream = subscription;
  44.             $('.remote video').get(0).srcObject = stream.mediaStream;
  45.         });
  46.     }
  47.  
  48.     conference.addEventListener('streamadded', (event) => {
  49.         console.log('A new stream is added ', event.stream.id);
  50.         subscribeForward &&  renderVideo(event.stream);
  51.         mixStream(myRoom, event.stream.id, 'common');
  52.         event.stream.addEventListener('ended', () => {
  53.             console.log(event.stream.id + ' is ended.');
  54.         });
  55.     });
  56.  
  57.  
  58.     window.onload = function() {
  59.         var myResolution = getParameterByName('resolution') || {
  60.             width: 1280,
  61.             height: 720
  62.         };
  63.         var shareScreen = getParameterByName('screen') || false;
  64.         myRoom = getParameterByName('room');
  65.         var isHttps = (location.protocol === 'https:');
  66.         var mediaUrl = getParameterByName('url');
  67.         var isPublish = getParameterByName('publish');
  68.         createToken(myRoom, 'user', 'presenter', function(response) {
  69.             var token = response;
  70.             conference.join(token).then(resp => {
  71.                 myId = resp.self.id;
  72.                 myRoom = resp.id;
  73.                 if(mediaUrl){
  74.                      startStreamingIn(myRoom, mediaUrl);
  75.                 }
  76.                 if (isPublish !== 'false') {
  77.                     const audioConstraintsForMic = new Ics.Base.AudioTrackConstraints(Ics.Base.AudioSourceInfo.MIC);
  78.                     var videoConstraintsForCamera = new Ics.Base.VideoTrackConstraints(Ics.Base.VideoSourceInfo.CAMERA);
  79. videoConstraintsForCamera.resolution = {width: 320,height:240};
  80. videoConstraintsForCamera.frameRate = 15;
  81.                     let mediaStream;
  82.                     Ics.Base.MediaStreamFactory.createMediaStream(new Ics.Base.StreamConstraints(
  83.                         audioConstraintsForMic, videoConstraintsForCamera)).then(stream => {
  84.                         mediaStream = stream;
  85.                         localStream = new Ics.Base.LocalStream(
  86.                             mediaStream, new Ics.Base.StreamSourceInfo(
  87.                                 'mic', 'camera'));
  88.                         $('.local video').get(0).srcObject = stream;
  89.                         conference.publish(localStream,{audio: false,video: [{codec: {name: 'h264'},maxBitrate: 150}]}).then(publication => {
  90.                             mixStream(myRoom, publication.id, 'common')
  91.                             publication.addEventListener('error', (err) => {
  92.                                 console.log('Publication error: ' + err.error.message);
  93.                             });
  94.                         });
  95.                     }, err => {
  96.                         console.error('Failed to create MediaStream, ' +
  97.                             err);
  98.                     });
  99.                 }
  100.                 var streams = resp.remoteStreams;
  101.                 for (const stream of streams) {
  102.                     if(!subscribeForward){
  103.                       if (stream.source.audio === 'mixed' || stream.source.video ===
  104.                         'mixed') {
  105.                         conference.subscribe(stream, {
  106.                             audio: {codecs:[{name:'opus'}]},
  107.                             video: true
  108.                         }).then((subscription) => {
  109.                             subscriptionForMixedStream = subscription;
  110.                             $('.remote video').get(0).srcObject = stream.mediaStream;
  111.                             subscription.addEventListener('error', (err) => {
  112.                                 console.log('Subscription error: ' + err.error.message);
  113.                             })
  114.                         });
  115.                         for (const resolution of stream.capabilities.video.resolutions) {
  116.                             const button = $('<button/>', {
  117.                                 text: resolution.width + 'x' +
  118.                                     resolution.height,
  119.                                 click: () => {
  120.                                     subscribeDifferentResolution(stream, resolution);
  121.                                 }
  122.                             });
  123.                             button.appendTo($('#resolutions'));
  124.                         };
  125.                       }
  126.                     }else if(stream.source.audio !== 'mixed'){
  127.                         subscribeForward && renderVideo(stream);
  128.                     }
  129.                 }
  130.                 console.log('Streams in conference:', streams.length);
  131.                 var participants = resp.participants;
  132.                 console.log('Participants in conference: ' + participants.length);
  133.             }, function(err) {
  134.                 console.error('server connection failed:', err);
  135.             });
  136.         });
  137.     };
  138.  
  139.     // REST samples. It sends HTTP requests to sample server, and sample server sends requests to conference server.
  140.     // Both this file and sample server are samples.
  141.     var send = function(method, entity, body, onRes) {
  142.         var req = new XMLHttpRequest();
  143.         req.onreadystatechange = function() {
  144.             if (req.readyState === 4) {
  145.                 onRes(req.responseText);
  146.             }
  147.         };
  148.         req.open(method, entity, true);
  149.         req.setRequestHeader('Content-Type', 'application/json');
  150.         if (body !== undefined) {
  151.             req.send(JSON.stringify(body));
  152.         } else {
  153.             req.send();
  154.         }
  155.     };
  156.  
  157.     var onResponse = function(result) {
  158.         if (result) {
  159.             try {
  160.                 console.info('Result:', JSON.parse(result));
  161.             } catch (e) {
  162.                 console.info('Result:', result);
  163.             }
  164.         } else {
  165.             console.info('Null');
  166.         }
  167.     };
  168.  
  169.     var listRooms = function() {
  170.         send('GET', '/rooms/', undefined, onResponse);
  171.     };
  172.  
  173.     var getRoom = function(room) {
  174.         send('GET', '/rooms/' + room + '/', undefined, onResponse);
  175.     };
  176.  
  177.     var createRoom = function() {
  178.         send('POST', '/rooms/', {
  179.                 name: 'testNewRoom',
  180.                 options: undefined
  181.             },
  182.             onResponse);
  183.     };
  184.  
  185.     var deleteRoom = function(room) {
  186.         send('DELETE', '/rooms/' + room + '/', undefined, onResponse);
  187.     };
  188.  
  189.     var updateRoom = function(room, config) {
  190.         send('PUT', '/rooms/' + room + '/', config, onResponse);
  191.     };
  192.  
  193.     var listParticipants = function(room) {
  194.         send('GET', '/rooms/' + room + '/participants/', undefined, onResponse);
  195.     };
  196.  
  197.     var getParticipant = function(room, participant) {
  198.         send('GET', '/rooms/' + room + '/participants/' + participant + '/',
  199.             undefined, onResponse);
  200.     };
  201.  
  202.     var forbidSub = function(room, participant) {
  203.         var jsonPatch = [{
  204.             op: 'replace',
  205.             path: '/permission/subscribe',
  206.             value: {
  207.                 audio: false,
  208.                 video: false
  209.             }
  210.         }];
  211.         send('PATCH', '/rooms/' + room + '/participants/' + participant + '/',
  212.             jsonPatch, onResponse);
  213.     };
  214.  
  215.     var forbidPub = function(room, participant) {
  216.         var jsonPatch = [{
  217.             op: 'replace',
  218.             path: '/permission/publish',
  219.             value: {
  220.                 audio: false,
  221.                 video: false
  222.             }
  223.         }];
  224.         send('PATCH', '/rooms/' + room + '/participants/' + participant + '/',
  225.             jsonPatch, onResponse);
  226.     };
  227.  
  228.     var dropParticipant = function(room, participant) {
  229.         send('DELETE', '/rooms/' + room + '/participants/' + participant + '/',
  230.             undefined, onResponse);
  231.     };
  232.  
  233.     var listStreams = function(room) {
  234.         send('GET', '/rooms/' + room + '/streams/', undefined, onResponse);
  235.     };
  236.  
  237.     var getStream = function(room, stream) {
  238.         send('GET', '/rooms/' + room + '/streams/' + stream, undefined,
  239.             onResponse);
  240.     };
  241.  
  242.     var mixStream = function(room, stream, view) {
  243.         var jsonPatch = [{
  244.             op: 'add',
  245.             path: '/info/inViews',
  246.             value: view
  247.         }];
  248.         send('PATCH', '/rooms/' + room + '/streams/' + stream, jsonPatch,
  249.             onResponse);
  250.     };
  251.  
  252.     var unmixStream = function(room, stream, view) {
  253.         var jsonPatch = [{
  254.             op: 'remove',
  255.             path: '/info/inViews',
  256.             value: view
  257.         }];
  258.         send('PATCH', '/rooms/' + room + '/streams/' + stream, jsonPatch,
  259.             onResponse);
  260.     };
  261.  
  262.     var setRegion = function(room, stream, region, subStream) {
  263.         send('GET', '/rooms/' + room + '/streams/' + stream, undefined, function(stJSON) {
  264.             var st = JSON.parse(stJSON);
  265.  
  266.             if (st.type !== 'mixed') {
  267.                 return console.info('Invalid stream');
  268.             }
  269.  
  270.             var index = 0;
  271.             for (index = 0; index <= st.info.layout.length; index++) {
  272.                 if (st.info.layout[index].region.id === region) {
  273.                     break;
  274.                 }
  275.             }
  276.  
  277.             if (index < st.info.layout.length) {
  278.                 var jsonPatch = [{
  279.                     op: 'replace',
  280.                     path: '/info/layout/0/stream',
  281.                     value: subStream
  282.                 }];
  283.                 send('PATCH', '/rooms/' + room + '/streams/' + stream, jsonPatch,
  284.                     onResponse);
  285.             } else {
  286.                 console.info('Invalid region');
  287.             }
  288.         });
  289.     };
  290.  
  291.     var pauseStream = function(room, stream, track) {
  292.         var jsonPatch = [];
  293.         if (track === 'audio' || track === 'av') {
  294.             jsonPatch.push({
  295.                 op: 'replace',
  296.                 path: '/media/audio/status',
  297.                 value: 'inactive'
  298.             });
  299.         }
  300.  
  301.         if (track === 'video' || track === 'av') {
  302.             jsonPatch.push({
  303.                 op: 'replace',
  304.                 path: '/media/video/status',
  305.                 value: 'inactive'
  306.             });
  307.         }
  308.         send('PATCH', '/rooms/' + room + '/streams/' + stream, jsonPatch,
  309.             onResponse);
  310.     };
  311.  
  312.     var playStream = function(room, stream, track) {
  313.         var jsonPatch = [];
  314.         if (track === 'audio' || track === 'av') {
  315.             jsonPatch.push({
  316.                 op: 'replace',
  317.                 path: '/media/audio/status',
  318.                 value: 'active'
  319.             });
  320.         }
  321.  
  322.         if (track === 'video' || track === 'av') {
  323.             jsonPatch.push({
  324.                 op: 'replace',
  325.                 path: '/media/video/status',
  326.                 value: 'active'
  327.             });
  328.         }
  329.         send('PATCH', '/rooms/' + room + '/streams/' + stream, jsonPatch,
  330.             onResponse);
  331.     };
  332.  
  333.     var dropStream = function(room, stream) {
  334.         send('DELETE', '/rooms/' + room + '/streams/' + stream, undefined,
  335.             onResponse);
  336.     };
  337.  
  338.     var startStreamingIn = function(room, url) {
  339.         var options = {
  340.             url: url,
  341.             media: {
  342.                 audio: 'auto',
  343.                 video: true
  344.             },
  345.             transport: {
  346.                 protocol: 'udp',
  347.                 bufferSize: 2048
  348.             }
  349.         };
  350.         send('POST', '/rooms/' + room + '/streaming-ins', options, onResponse);
  351.     };
  352.  
  353.     var stopStreamingIn = function(room, stream) {
  354.         send('DELETE', '/rooms/' + room + '/streaming-ins/' + stream, undefined,
  355.             onResponse);
  356.     };
  357.  
  358.     var listRecordings = function(room) {
  359.         send('GET', '/rooms/' + room + '/recordings/', undefined, onResponse);
  360.     };
  361.  
  362.     var startRecording = function(room, audioFrom, videoFrom, container) {
  363.         var options = {
  364.             media: {
  365.                 audio: {
  366.                     from: audioFrom
  367.                 },
  368.                 video: {
  369.                     from: videoFrom
  370.                 }
  371.             },
  372.             container: (container ? container : 'auto')
  373.         };
  374.         send('POST', '/rooms/' + room + '/recordings', options, onResponse);
  375.     };
  376.  
  377.     var stopRecording = function(room, id) {
  378.         send('DELETE', '/rooms/' + room + '/recordings/' + id, undefined,
  379.             onResponse);
  380.     };
  381.  
  382.     var updateRecording = function(room, id, audioFrom, videoFrom) {
  383.         var jsonPatch = [{
  384.             op: 'replace',
  385.             path: '/media/audio/from',
  386.             value: audioFrom
  387.         }, {
  388.             op: 'replace',
  389.             path: '/media/video/from',
  390.             value: videoFrom
  391.         }];
  392.         send('PATCH', '/rooms/' + room + '/recordings/' + id, jsonPatch,
  393.             onResponse);
  394.     };
  395.  
  396.     var listStreamingOuts = function(room) {
  397.         send('GET', '/rooms/' + room + '/streaming-outs/', undefined, onResponse);
  398.     };
  399.  
  400.     var startStreamingOut = function(room, url, audioFrom, videoFrom) {
  401.         var options = {
  402.             media: {
  403.                 audio: {
  404.                     from: audioFrom
  405.                 },
  406.                 video: {
  407.                     from: videoFrom
  408.                 }
  409.             },
  410.             url: url
  411.         };
  412.         send('POST', '/rooms/' + room + '/streaming-outs', options, onResponse);
  413.     };
  414.  
  415.     var stopStreamingOut = function(room, id) {
  416.         send('DELETE', '/rooms/' + room + '/streaming-outs/' + id, undefined,
  417.             onResponse);
  418.     };
  419.  
  420.     var updateStreamingOut = function(room, id, audioFrom, videoFrom) {
  421.         var jsonPatch = [{
  422.             op: 'replace',
  423.             path: '/media/audio/from',
  424.             value: audioFrom
  425.         }, {
  426.             op: 'replace',
  427.             path: '/media/video/from',
  428.             value: videoFrom
  429.         }];
  430.         send('PATCH', '/rooms/' + room + '/streaming-outs/' + id, jsonPatch,
  431.             onResponse);
  432.     };
  433.  
  434.  
  435.     var createToken = function(room, user, role, callback) {
  436.         var body = {
  437.             room: room,
  438.             user: user,
  439.             role: role
  440.         };
  441.         send('POST', '/tokens/', body, callback);
  442.     };
  443.  
  444. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement