webrtc.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. "use strict";
  2. // Copyright (c) 2018 Nikolai Suslov
  3. // Krestianstvo.org MIT license (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  4. // VWF Apache License (https://github.com/NikolaySuslov/livecodingspace/blob/master/VWF_LICENSE.md)
  5. //
  6. /// @module vwf/view/webrtc
  7. /// @requires vwf/view
  8. define( [ "module", "vwf/view", "vwf/utility", "vwf/utility/color"], function( module, view, utility, Color) {
  9. return view.load( module, {
  10. // == Module Definition ====================================================================
  11. initialize: function( options ) {
  12. if ( !this.state ) {
  13. this.state = {};
  14. }
  15. this.state.clients = {};
  16. this.state.instances = {};
  17. this.local = {
  18. "ID": undefined,
  19. "url": undefined,
  20. "stream": undefined,
  21. "sharing": { audio: true, video: true }
  22. };
  23. if ( options === undefined ) { options = {}; }
  24. this.stereo = options.stereo !== undefined ? options.stereo : false;
  25. this.videoElementsDiv = options.videoElementsDiv !== undefined ? options.videoElementsDiv : 'videoSurfaces';
  26. this.videoProperties = options.videoProperties !== undefined ? options.videoProperties : {};
  27. this.bandwidth = options.bandwidth;
  28. this.iceServers = options.iceServers !== undefined ? options.iceServers : [ { "url": "stun:stun.l.google.com:19302" } ];
  29. this.debug = options.debug !== undefined ? options.debug : false;
  30. this.videosAdded = 0;
  31. this.msgQueue = [];
  32. },
  33. createdNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  34. childSource, childType, childIndex, childName, callback /* ( ready ) */ ) {
  35. if ( childExtendsID === undefined )
  36. return;
  37. var self = this, node;
  38. var protos = getPrototypes.call( self, childExtendsID )
  39. if ( isClientInstanceDef.call( this, protos ) && childName ) {
  40. node = {
  41. "parentID": nodeID,
  42. "ID": childID,
  43. "extendsID": childExtendsID,
  44. "implementsIDs": childImplementsIDs,
  45. "source": childSource,
  46. "type": childType,
  47. "name": childName,
  48. "prototypes": protos,
  49. };
  50. this.state.instances[ childID ] = node;
  51. } else if ( isClientDefinition.call( this, protos ) && childName ) {
  52. // check if this instance of client and if this client is for this instance
  53. // create a login for this
  54. node = {
  55. "parentID": nodeID,
  56. "ID": childID,
  57. "moniker": undefined,
  58. "extendsID": childExtendsID,
  59. "implementsIDs": childImplementsIDs,
  60. "source": childSource,
  61. "type": childType,
  62. "name": childName,
  63. "prototypes": protos,
  64. "displayName": "",
  65. "connection": undefined,
  66. "localUrl": undefined,
  67. "remoteUrl": undefined,
  68. //"color": "rgb(0,0,0)",
  69. "createProperty": true,
  70. "sharing": { audio: true, video: true }
  71. };
  72. this.state.clients[ childID ] = node;
  73. // add the client specific locals
  74. node.moniker = appMoniker.call( this, childName );
  75. //console.info( "new client moniker: " + node.moniker );
  76. node.displayName = undefined;
  77. node.prototypes = protos;
  78. if ( this.kernel.moniker() == node.moniker ) {
  79. this.local.ID = childID;
  80. }
  81. }
  82. },
  83. deleteConnection: function(nodeID){
  84. // debugger;
  85. //if ( this.kernel.find( nodeID, "parent::element(*,'http://vwf.example.com/clients.vwf')" ).length > 0 ) {
  86. //if ( this.kernel.find( nodeID ).length > 0 ) {
  87. var moniker = nodeID.slice(-20);//this.kernel.name( nodeID );
  88. var client = undefined;
  89. if ( moniker == this.kernel.moniker() ) {
  90. // this is the client that has left the converstaion
  91. // go through the peerConnections and close the
  92. // all current connections
  93. var peer, peerMoniker;
  94. for ( var peerID in this.state.clients ) {
  95. peer = this.state.clients[ peerID ];
  96. peerMoniker = appMoniker.call( this, peer.name )
  97. if ( peerMoniker != this.kernel.moniker() ) {
  98. peer.connection && peer.connection.disconnect();
  99. let peername = 'avatar-' + peerMoniker;
  100. deletePeerConnection.call( this, peername);
  101. }
  102. }
  103. } else {
  104. // this is a client who has has a peer leave the converstaion
  105. // remove that client, and the
  106. client = findClientByMoniker.call( this, moniker );
  107. if ( client ) {
  108. client.connection && client.connection.disconnect();
  109. //removeClient.call( this, client );
  110. //delete this.state.clients[ client ]
  111. }
  112. }
  113. },
  114. stopWebRTC: function(nodeID){
  115. if( this.local.stream ){
  116. var tracks = this.local.stream.getTracks();
  117. tracks.forEach(function(track) {
  118. track.stop();
  119. });
  120. this.local.stream = undefined;
  121. let vidui = document.querySelector('#webrtcvideo');
  122. const viduicomp = new mdc.iconToggle.MDCIconToggle(vidui); //new mdc.select.MDCIconToggle
  123. if (vidui) viduicomp.on = false;
  124. let micui = document.querySelector('#webrtcaudio');
  125. const micuicomp = new mdc.iconToggle.MDCIconToggle(micui);
  126. if (micui) micuicomp.on = false;
  127. this.deleteConnection(nodeID);
  128. this.kernel.callMethod(nodeID, "removeSoundWebRTC");
  129. this.kernel.callMethod(nodeID, "removeVideoTexture");
  130. }
  131. },
  132. startWebRTC: function(childID) {
  133. var client = this.state.clients[ childID ];
  134. if ( client ) {
  135. if ( this.local.ID == childID ){
  136. // local client object
  137. // grab access to the webcam
  138. capture.call( this, this.local.sharing );
  139. var remoteClient = undefined;
  140. // existing clients
  141. for ( var clientID in this.state.clients ) {
  142. if ( clientID != this.local.ID ) {
  143. // create property for this client on each existing client
  144. remoteClient = this.state.clients[ clientID ];
  145. if ( remoteClient.createProperty ) {
  146. //console.info( "++ 1 ++ createProperty( "+clientID+", "+this.kernel.moniker()+" )" );
  147. remoteClient.createProperty = false;
  148. this.kernel.createProperty( clientID, this.kernel.moniker() );
  149. }
  150. }
  151. }
  152. } else {
  153. // not the local client, but if the local client has logged
  154. // in create the property for this on the new client
  155. if ( this.local.ID ) {
  156. if ( client.createProperty ) {
  157. client.createProperty = false;
  158. //console.info( "++ 2 ++ createProperty( "+childID+", "+this.kernel.moniker()+" )" );
  159. this.kernel.createProperty( childID, this.kernel.moniker() );
  160. }
  161. }
  162. }
  163. }
  164. },
  165. initializedNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  166. childSource, childType, childIndex, childName ) {
  167. if ( childExtendsID === undefined )
  168. return;
  169. },
  170. deletedNode: function( nodeID ) {
  171. // debugger;
  172. //if ( this.kernel.find( nodeID, "parent::element(*,'http://vwf.example.com/clients.vwf')" ).length > 0 ) {
  173. //if ( this.kernel.find( nodeID ).length > 0 ) {
  174. var moniker = nodeID.slice(-20);//this.kernel.name( nodeID );
  175. var client = undefined;
  176. if ( moniker == this.kernel.moniker() ) {
  177. // this is the client that has left the converstaion
  178. // go through the peerConnections and close the
  179. // all current connections
  180. var peer, peerMoniker;
  181. for ( var peerID in this.state.clients ) {
  182. peer = this.state.clients[ peerID ];
  183. peerMoniker = appMoniker.call( this, peer.name )
  184. if ( peerMoniker != this.kernel.moniker() ) {
  185. peer.connection && peer.connection.disconnect();
  186. let peername = 'avatar-' + peerMoniker;
  187. deletePeerConnection.call( this, peername);
  188. }
  189. }
  190. } else {
  191. // this is a client who has has a peer leave the converstaion
  192. // remove that client, and the
  193. client = findClientByMoniker.call( this, moniker );
  194. if ( client ) {
  195. client.connection && client.connection.disconnect();
  196. removeClient.call( this, client );
  197. delete this.state.clients[ client ]
  198. }
  199. }
  200. //}
  201. },
  202. createdProperty: function( nodeID, propertyName, propertyValue ) {
  203. this.satProperty( nodeID, propertyName, propertyValue );
  204. },
  205. initializedProperty: function( nodeID, propertyName, propertyValue ) {
  206. this.satProperty( nodeID, propertyName, propertyValue );
  207. },
  208. satProperty: function( nodeID, propertyName, propertyValue ) {
  209. var client = this.state.clients[ nodeID ];
  210. if ( client ) {
  211. switch( propertyName ) {
  212. case "sharing":
  213. if ( propertyValue ) {
  214. client.sharing = propertyValue;
  215. if ( nodeID == this.local.ID ) {
  216. updateSharing.call( this, nodeID, propertyValue );
  217. }
  218. }
  219. break;
  220. case "localUrl":
  221. if ( propertyValue ) {
  222. if ( nodeID != this.local.ID ) {
  223. client.localUrl = propertyValue;
  224. }
  225. }
  226. break;
  227. case "remoteUrl":
  228. if ( propertyValue ) {
  229. client.remoteUrl = propertyValue;
  230. }
  231. break;
  232. case "displayName":
  233. if ( propertyValue ) {
  234. client.displayName = propertyValue;
  235. }
  236. break;
  237. default:
  238. // propertyName is the moniker of the client that
  239. // this connection supports
  240. if ( nodeID == this.local.ID ) {
  241. if ( propertyValue ) {
  242. // propertyName - moniker of the client
  243. // propertyValue - peerConnection message
  244. handlePeerMessage.call( this, propertyName, propertyValue );
  245. }
  246. }
  247. break;
  248. }
  249. }
  250. },
  251. gotProperty: function( nodeID, propertyName, propertyValue ) {
  252. var value = undefined;
  253. return value;
  254. },
  255. calledMethod: function( nodeID, methodName, methodParameters, methodValue ) {
  256. switch ( methodName ) {
  257. case "setLocalMute":
  258. if ( this.kernel.moniker() == this.kernel.client() ) {
  259. methodValue = setMute.call( this, methodParameters );
  260. }
  261. break;
  262. case "webrtcTurnOnOff":
  263. if ( this.kernel.moniker() == this.kernel.client() ) {
  264. console.log("WEBRTC turn on/off")
  265. methodValue = turnOnOffTracks.call( this, methodParameters );
  266. }
  267. break;
  268. case "webrtcMuteAudio":
  269. if ( this.kernel.moniker() == this.kernel.client() ) {
  270. methodValue = muteAudio.call( this, methodParameters[0] );
  271. }
  272. break;
  273. case "webrtcMuteVideo":
  274. if ( this.kernel.moniker() == this.kernel.client() ) {
  275. methodValue = this.muteVideo.call( this, methodParameters[0] );
  276. }
  277. break;
  278. }
  279. },
  280. firedEvent: function( nodeID, eventName, eventParameters ) {
  281. },
  282. muteVideo: function ( mute ) {
  283. let str = this.local.stream;
  284. if ( str ) {
  285. var tracks = str.getVideoTracks();
  286. tracks.forEach(function(track) {
  287. track.enabled = mute;
  288. });
  289. }
  290. },
  291. muteAudio: function ( mute ) {
  292. let str = this.local.stream;
  293. if ( str ) {
  294. var tracks = str.getAudioTracks();
  295. tracks.forEach(function(track) {
  296. track.enabled = mute;
  297. });
  298. }
  299. }
  300. } );
  301. function createVideoElementAsAsset(id, local) {
  302. var video = document.querySelector('#' + id);
  303. if (!video) {
  304. video = document.createElement('video');
  305. }
  306. video.setAttribute('id', id);
  307. video.setAttribute('autoplay', true);
  308. //video.setAttribute('src', '');
  309. video.setAttribute("webkit-playsinline", true);
  310. video.setAttribute("controls", true);
  311. video.setAttribute("width", 640);
  312. video.setAttribute("height", 480);
  313. if (local) video.setAttribute("muted", true);
  314. // let audioID = '#audio-' + id;
  315. // var audio = document.querySelector(audioID);
  316. // if (!audio) {
  317. // audio = document.createElement('audio');
  318. // }
  319. // audio.setAttribute('id', audioID);
  320. var assets = document.querySelector('a-assets');
  321. // if (!assets) {
  322. // assets = document.createElement('a-assets');
  323. // document.querySelector('a-scene').appendChild(assets);
  324. // }
  325. if (!assets.contains(video)) {
  326. assets.appendChild(video);
  327. }
  328. // if (!assets.contains(audio)) {
  329. // assets.appendChild(audio);
  330. // }
  331. return video //{'video': video, 'audio': audio};
  332. }
  333. function getPrototypes( extendsID ) {
  334. var prototypes = [];
  335. var id = extendsID;
  336. while ( id !== undefined ) {
  337. prototypes.push( id );
  338. id = this.kernel.prototype( id );
  339. }
  340. return prototypes;
  341. }
  342. function getPeer( moniker ) {
  343. var clientNode;
  344. for ( var id in this.state.clients ) {
  345. if ( this.state.clients[id].moniker == moniker ) {
  346. clientNode = this.state.clients[id];
  347. break;
  348. }
  349. }
  350. return clientNode;
  351. }
  352. function displayLocal( stream, name) {
  353. var id = this.kernel.moniker();
  354. return displayVideo.call( this, id, stream, this.local.url, name, id, true);
  355. }
  356. function displayVideo( id, stream, url, name, destMoniker, local) {
  357. let va = createVideoElementAsAsset(name, local);
  358. //video.setAttribute('src', url);
  359. va.srcObject = stream;
  360. //var audioCtx = new AudioContext();
  361. //var source = audioCtx.createMediaStreamSource(stream);
  362. //va.audio.src = stream;
  363. this.kernel.callMethod( 'avatar-'+id, "setVideoTexture", [name]);
  364. return id;
  365. }
  366. function removeVideo( client ) {
  367. // if ( client.videoDivID ) {
  368. // var $videoWin = $( "#" + client.videoDivID );
  369. // if ( $videoWin ) {
  370. // $videoWin.remove();
  371. // }
  372. // client.videoDivID = undefined;
  373. // }
  374. // this.kernel.callMethod( this.kernel.application(), "removeVideo", [ client.moniker ] );
  375. }
  376. function displayRemote( id, stream, url, name, destMoniker, color ) {
  377. let audioID = 'audio-' + name;
  378. this.kernel.callMethod( 'avatar-'+id, "setSoundWebRTC", [audioID]);
  379. return displayVideo.call( this, id, stream, url, name, destMoniker, true );
  380. }
  381. function capture( media ) {
  382. if ( this.local.stream === undefined && ( media.video || media.audio ) ) {
  383. var self = this;
  384. var constraints = {
  385. audio: true,
  386. video: true
  387. };
  388. navigator.mediaDevices.getUserMedia(constraints).then(handleSuccess).catch(handleError);
  389. function handleError(error) {
  390. console.log('navigator.getUserMedia error: ', error);
  391. }
  392. function handleSuccess(stream) {
  393. // var videoTracks = stream.getVideoTracks();
  394. // console.log('Got stream with constraints:', constraints);
  395. // if (videoTracks.length) {
  396. // videoTracks[0].enabled = true;
  397. // }
  398. self.local.url = "url" //URL.createObjectURL( stream );
  399. self.local.stream = stream;
  400. self.kernel.setProperty( self.local.ID, "localUrl", self.local.url );
  401. var localNode = self.state.clients[ self.local.ID ];
  402. self.muteAudio(false);
  403. self.muteVideo(false);
  404. let webRTCGUI = document.querySelector('#webrtcswitch');
  405. if (webRTCGUI) webRTCGUI.setAttribute("aria-pressed", true);
  406. let videoTracks = stream.getVideoTracks();
  407. let vstatus = videoTracks[0].enabled;
  408. let vidui = document.querySelector('#webrtcvideo');
  409. const viduicomp = new mdc.iconToggle.MDCIconToggle(vidui); //new mdc.select.MDCIconToggle
  410. if (vidui) viduicomp.on = vstatus;
  411. let audioTracks = stream.getAudioTracks();
  412. let astatus = audioTracks[0].enabled;
  413. let micui = document.querySelector('#webrtcaudio');
  414. const micuicomp = new mdc.iconToggle.MDCIconToggle(micui);
  415. if (micui) micuicomp.on = astatus;
  416. displayLocal.call( self, stream, localNode.displayName);
  417. sendOffers.call( self );
  418. }
  419. }
  420. }
  421. function appMoniker( name ) {
  422. return name.substr( 7, name.length-1 );
  423. }
  424. function findClientByMoniker( moniker ) {
  425. var client = undefined;
  426. for ( var id in this.state.clients ) {
  427. if ( client === undefined && moniker == this.state.clients[ id ].moniker ) {
  428. client = this.state.clients[ id ];
  429. }
  430. }
  431. return client;
  432. }
  433. function removeClient( client ) {
  434. if ( client ) {
  435. removeVideo.call( this, client );
  436. }
  437. }
  438. function sendOffers() {
  439. var peerNode;
  440. for ( var id in this.state.clients ) {
  441. if ( id != this.local.ID ) {
  442. peerNode = this.state.clients[ id ];
  443. // if there's a url then connect
  444. if ( peerNode.localUrl && peerNode.localUrl != "" && peerNode.connection === undefined ) {
  445. createPeerConnection.call( this, peerNode, true );
  446. }
  447. }
  448. }
  449. }
  450. function updateSharing( nodeID, sharing ) {
  451. setMute.call( this, !sharing.audio );
  452. setPause.call( this, !sharing.video );
  453. }
  454. function turnOnOffTracks( mute ) {
  455. let str = this.local.stream;
  456. if ( str ) {
  457. var audioTracks = str.getAudioTracks();
  458. var videoTracks = str.getVideoTracks();
  459. audioTracks.forEach(function(track) {
  460. track.enabled = mute[0];
  461. });
  462. videoTracks.forEach(function(track) {
  463. track.enabled = mute[0];
  464. });
  465. }
  466. };
  467. function muteAudio( mute ) {
  468. let str = this.local.stream;
  469. if ( str ) {
  470. var audioTracks = str.getAudioTracks();
  471. audioTracks.forEach(function(track) {
  472. track.enabled = mute;
  473. });
  474. }
  475. };
  476. function setMute( mute ) {
  477. if ( this.local.stream && this.local.stream.audioTracks && this.local.stream.audioTracks.length > 0 ) {
  478. if ( mute !== undefined ) {
  479. this.local.stream.audioTracks[0].enabled = !mute;
  480. }
  481. }
  482. };
  483. function setPause( pause ) {
  484. if ( this.local.stream && this.local.stream.videoTracks && this.local.stream.videoTracks.length > 0 ) {
  485. if ( pause !== undefined ) {
  486. this.local.stream.videoTracks[0].enabled = !pause;
  487. }
  488. }
  489. }
  490. function release() {
  491. for ( id in this.connections ) {
  492. this.connections[id].disconnect();
  493. }
  494. this.connections = {};
  495. }
  496. function hasStream() {
  497. return ( this.stream !== undefined );
  498. }
  499. function createPeerConnection( peerNode, sendOffer ) {
  500. if ( peerNode ) {
  501. if ( peerNode.connection === undefined ) {
  502. peerNode.connection = new mediaConnection( this, peerNode );
  503. peerNode.connection.connect( this.local.stream, sendOffer );
  504. //if ( this.bandwidth !== undefined ) {
  505. // debugger;
  506. //}
  507. }
  508. }
  509. }
  510. function handlePeerMessage( propertyName, msg ) {
  511. var peerNode = getPeer.call( this, propertyName )
  512. if ( peerNode ) {
  513. if ( peerNode.connection !== undefined ) {
  514. peerNode.connection.processMessage( msg );
  515. } else {
  516. if ( msg.type === 'offer' ) {
  517. this.msgQueue.unshift( msg );
  518. peerNode.connection = new mediaConnection( this, peerNode );
  519. peerNode.connection.connect( this.local.stream, false );
  520. while ( this.msgQueue.length > 0 ) {
  521. peerNode.connection.processMessage( this.msgQueue.shift() );
  522. }
  523. this.msgQueue = [];
  524. } else {
  525. this.msgQueue.push( msg );
  526. }
  527. }
  528. }
  529. }
  530. function deletePeerConnection( peerID ) {
  531. var peerNode = this.state.clients[ peerID ];
  532. if ( peerNode ) {
  533. peerNode.connection.disconnect();
  534. peerNode.connection = undefined;
  535. }
  536. }
  537. function getConnectionStats() {
  538. var peerNode = undefined;
  539. for ( var id in this.state.clients ) {
  540. peerNode = this.state.clients[ id ];
  541. if ( peerNode && peerNode.connection ) {
  542. peerNode.connection.getStats();
  543. }
  544. }
  545. }
  546. function isClientDefinition( prototypes ) {
  547. var foundClient = false;
  548. if ( prototypes ) {
  549. var len = prototypes.length;
  550. for ( var i = 0; i < len && !foundClient; i++ ) {
  551. foundClient = ( prototypes[i] == "http://vwf.example.com/aframe/avatar.vwf" );
  552. }
  553. }
  554. return foundClient;
  555. }
  556. function isClientInstanceDef( nodeID ) {
  557. return ( nodeID == "http://vwf.example.com/clients.vwf" );
  558. }
  559. function mediaConnection( view, peerNode ) {
  560. this.view = view;
  561. this.peerNode = peerNode;
  562. //
  563. this.stream = undefined;
  564. this.url = undefined;
  565. this.pc = undefined;
  566. this.connected = false;
  567. this.streamAdded = false;
  568. this.state = "created";
  569. // webrtc peerConnection parameters
  570. this.pc_config = {'iceServers': [
  571. {'url': 'stun:stun.l.google.com:19302'},
  572. {'url': 'stun:stun1.l.google.com:19302'}
  573. ]};//{ "iceServers": this.view.iceServers };
  574. this.pc_constraints = { "optional": [ { "DtlsSrtpKeyAgreement": true } ] };
  575. // Set up audio and video regardless of what devices are present.
  576. this.sdpConstraints = {
  577. 'offerToReceiveAudio':1,
  578. 'offerToReceiveVideo':1 };
  579. this.connect = function( stream, sendOffer ) {
  580. var self = this;
  581. if ( this.pc === undefined ) {
  582. if ( this.view.debug ) console.log("Creating PeerConnection.");
  583. var iceCallback = function( event ) {
  584. //console.log( "------------------------ iceCallback ------------------------" );
  585. if ( event.candidate ) {
  586. var sMsg = {
  587. "type": 'candidate',
  588. "label": event.candidate.sdpMLineIndex,
  589. "id": event.candidate.sdpMid,
  590. "candidate": event.candidate.candidate
  591. };
  592. // each client creates a property for each other
  593. // the message value is broadcast via the property
  594. self.view.kernel.setProperty( self.peerNode.ID, self.view.kernel.moniker(), sMsg );
  595. } else {
  596. if ( self.view.debug ) console.log("End of candidates.");
  597. }
  598. };
  599. // if ( webrtcDetectedBrowser == "firefox" ) {
  600. // this.pc_config = {"iceServers":[{"url":"stun:23.21.150.121"}]};
  601. // }
  602. try {
  603. this.pc = new RTCPeerConnection( this.pc_config, this.pc_constraints);
  604. this.pc.onicecandidate = iceCallback;
  605. if ( self.view.debug ) console.log("Created RTCPeerConnnection with config \"" + JSON.stringify( this.pc_config ) + "\".");
  606. } catch (e) {
  607. console.log("Failed to create PeerConnection, exception: " + e.message);
  608. alert("Cannot create RTCPeerConnection object; WebRTC is not supported by this browser.");
  609. return;
  610. }
  611. this.pc.onnegotiationeeded = function( event ) {
  612. //debugger;
  613. //console.info( "onnegotiationeeded." );
  614. }
  615. this.pc.ontrack = function( event ) {
  616. if ( self.view.debug ) console.log("Remote stream added.");
  617. self.stream = event.streams[0];
  618. self.url = "url" //URL.createObjectURL( event.streams[0] );
  619. if ( self.view.debug ) console.log("Remote stream added. url: " + self.url );
  620. var divID = displayRemote.call( self.view, self.peerNode.moniker, self.stream, self.url, self.peerNode.displayName, view.kernel.moniker(), self.peerNode.color );
  621. if ( divID !== undefined ) {
  622. self.peerNode.videoDivID = divID;
  623. }
  624. };
  625. this.pc.onremovestream = function( event ) {
  626. if ( self.view.debug ) console.log("Remote stream removed.");
  627. };
  628. this.pc.onsignalingstatechange = function() {
  629. //console.info( "onsignalingstatechange state change." );
  630. }
  631. this.pc.oniceconnectionstatechange = function( ) {
  632. if ( self && self.pc ) {
  633. var state = self.pc.signalingState || self.pc.readyState;
  634. //console.info( "peerConnection state change: " + state );
  635. }
  636. }
  637. if ( stream ) {
  638. // stream.getVideoTracks();
  639. // stream.getAudioTracks();
  640. stream.getTracks().forEach(
  641. function(track) {
  642. self.pc.addTrack(
  643. track,
  644. stream
  645. );
  646. }
  647. );
  648. //this.pc.addStream( stream );
  649. this.streamAdded = true;
  650. }
  651. if ( sendOffer ){
  652. this.call();
  653. }
  654. }
  655. this.connected = ( this.pc !== undefined );
  656. };
  657. this.setMute = function( mute ) {
  658. if ( this.stream && this.stream.audioTracks && this.stream.audioTracks.length > 0 ) {
  659. if ( mute !== undefined ) {
  660. this.stream.audioTracks[0].enabled = !mute;
  661. }
  662. }
  663. }
  664. this.setPause = function( pause ) {
  665. if ( this.stream && this.stream.videoTracks && this.stream.videoTracks.length > 0 ) {
  666. if ( pause !== undefined ) {
  667. this.stream.videoTracks[0].enabled = !pause;
  668. }
  669. }
  670. }
  671. this.disconnect = function() {
  672. if ( this.view.debug ) console.log( "PC.disconnect " + this.peerID );
  673. if ( this.pc ) {
  674. this.pc.close();
  675. this.pc = undefined;
  676. }
  677. };
  678. this.processMessage = function( msg ) {
  679. //var msg = JSON.parse(message);
  680. if ( this.view.debug ) console.log('S->C: ' + JSON.stringify(msg) );
  681. if ( this.pc ) {
  682. if ( msg.type === 'offer') {
  683. // if ( this.view.stereo ) {
  684. // msg.sdp = addStereo( msg.sdp );
  685. // }
  686. this.pc.setRemoteDescription( new RTCSessionDescription( msg ) ); //msg.sdp
  687. this.answer();
  688. } else if ( msg.type === 'answer' && this.streamAdded ) {
  689. // if ( this.view.stereo ) {
  690. // msg.sdp = addStereo( msg.sdp );
  691. // }
  692. this.pc.setRemoteDescription( new RTCSessionDescription( msg ) ); //msg.sdp
  693. } else if ( msg.type === 'candidate' && this.streamAdded ) {
  694. var candidate = new RTCIceCandidate( {
  695. "sdpMLineIndex": msg.label,
  696. "candidate": msg.candidate
  697. } );
  698. this.pc.addIceCandidate( candidate );
  699. } else if ( msg.type === 'bye' && this.streamAdded ) {
  700. this.hangup();
  701. }
  702. }
  703. };
  704. this.answer = function() {
  705. if ( this.view.debug ) console.log( "Send answer to peer" );
  706. var self = this;
  707. var answerer = function( sessionDescription ) {
  708. // // Set Opus as the preferred codec in SDP if Opus is present.
  709. // sessionDescription.sdp = self.preferOpus( sessionDescription.sdp );
  710. // sessionDescription.sdp = self.setBandwidth( sessionDescription.sdp );
  711. self.pc.setLocalDescription( sessionDescription );
  712. self.view.kernel.setProperty( self.peerNode.ID, self.view.kernel.moniker(), sessionDescription );
  713. };
  714. function onCreateSessionDescriptionError(error) {
  715. console.log('Failed to create session description: ' + error.toString());
  716. }
  717. this.pc.createAnswer(
  718. self.sdpConstraints
  719. ).then(
  720. answerer,
  721. onCreateSessionDescriptionError
  722. );
  723. //this.pc.createAnswer( answerer, null, this.sdpConstraints);
  724. };
  725. this.call = function() {
  726. var self = this;
  727. var constraints = {
  728. offerToReceiveAudio: 1,
  729. offerToReceiveVideo: 1
  730. };
  731. var offerer = function( sessionDescription ) {
  732. self.pc.setLocalDescription(sessionDescription).then(
  733. function() {
  734. onSetLocalSuccess(self.pc);
  735. },
  736. onSetSessionDescriptionError
  737. );
  738. function onSetLocalSuccess(pc) {
  739. console.log(self.pc + ' setLocalDescription complete');
  740. }
  741. function onSetSessionDescriptionError(error) {
  742. console.log('Failed to set session description: ' + error.toString());
  743. }
  744. // Set Opus as the preferred codec in SDP if Opus is present.
  745. // sessionDescription.sdp = self.preferOpus( sessionDescription.sdp );
  746. // sessionDescription.sdp = self.setBandwidth( sessionDescription.sdp );
  747. // self.pc.setLocalDescription( sessionDescription );
  748. //sendSignalMessage.call( sessionDescription, self.peerID );
  749. self.view.kernel.setProperty( self.peerNode.ID, self.view.kernel.moniker(), sessionDescription );
  750. };
  751. var onFailure = function(e) {
  752. console.log(e)
  753. }
  754. self.pc.createOffer(
  755. constraints
  756. ).then(
  757. offerer,
  758. onFailure
  759. );
  760. //this.pc.createOffer( offerer, onFailure, constraints );
  761. };
  762. this.setBandwidth = function( sdp ) {
  763. // apparently this only works in chrome
  764. if ( this.bandwidth === undefined || moz ) {
  765. return sdp;
  766. }
  767. // remove existing bandwidth lines
  768. sdp = sdp.replace(/b=AS([^\r\n]+\r\n)/g, '');
  769. if ( this.bandwidth.audio ) {
  770. sdp = sdp.replace(/a=mid:audio\r\n/g, 'a=mid:audio\r\nb=AS:' + this.bandwidth.audio + '\r\n');
  771. }
  772. if ( this.bandwidth.video ) {
  773. sdp = sdp.replace(/a=mid:video\r\n/g, 'a=mid:video\r\nb=AS:' + this.bandwidth.video + '\r\n');
  774. }
  775. if ( this.bandwidth.data /*&& !options.preferSCTP */ ) {
  776. sdp = sdp.replace(/a=mid:data\r\n/g, 'a=mid:data\r\nb=AS:' + this.bandwidth.data + '\r\n');
  777. }
  778. return sdp;
  779. }
  780. this.getStats = function(){
  781. if ( this.pc && this.pc.getStats ) {
  782. console.info( "pc.iceConnectionState = " + this.pc.iceConnectionState );
  783. console.info( " pc.iceGatheringState = " + this.pc.iceGatheringState );
  784. console.info( " pc.readyState = " + this.pc.readyState );
  785. console.info( " pc.signalingState = " + this.pc.signalingState );
  786. var consoleStats = function( obj ) {
  787. console.info( ' Timestamp:' + obj.timestamp );
  788. if ( obj.id ) {
  789. console.info( ' id: ' + obj.id );
  790. }
  791. if ( obj.type ) {
  792. console.info( ' type: ' + obj.type );
  793. }
  794. if ( obj.names ) {
  795. var names = obj.names();
  796. for ( var i = 0; i < names.length; ++i ) {
  797. console.info( " "+names[ i ]+": " + obj.stat( names[ i ] ) );
  798. }
  799. } else {
  800. if ( obj.stat && obj.stat( 'audioOutputLevel' ) ) {
  801. console.info( " audioOutputLevel: " + obj.stat( 'audioOutputLevel' ) );
  802. }
  803. }
  804. };
  805. // local function
  806. var readStats = function( stats ) {
  807. var results = stats.result();
  808. var bitrateText = 'No bitrate stats';
  809. for ( var i = 0; i < results.length; ++i ) {
  810. var res = results[ i ];
  811. console.info( 'Report ' + i );
  812. if ( !res.local || res.local === res ) {
  813. consoleStats( res );
  814. // The bandwidth info for video is in a type ssrc stats record
  815. // with googFrameHeightReceived defined.
  816. // Should check for mediatype = video, but this is not
  817. // implemented yet.
  818. if ( res.type == 'ssrc' && res.stat( 'googFrameHeightReceived' ) ) {
  819. var bytesNow = res.stat( 'bytesReceived' );
  820. if ( timestampPrev > 0) {
  821. var bitRate = Math.round( ( bytesNow - bytesPrev ) * 8 / ( res.timestamp - timestampPrev ) );
  822. bitrateText = bitRate + ' kbits/sec';
  823. }
  824. timestampPrev = res.timestamp;
  825. bytesPrev = bytesNow;
  826. }
  827. } else {
  828. // Pre-227.0.1445 (188719) browser
  829. if ( res.local ) {
  830. console.info( " Local: " );
  831. consoleStats( res.local );
  832. }
  833. if ( res.remote ) {
  834. console.info( " Remote: " );
  835. consoleStats( res.remote );
  836. }
  837. }
  838. }
  839. console.info( " bitrate: " + bitrateText )
  840. }
  841. this.pc.getStats( readStats );
  842. }
  843. }
  844. this.hangup = function() {
  845. if ( this.view.debug ) console.log( "PC.hangup " + this.id );
  846. if ( this.pc ) {
  847. this.pc.close();
  848. this.pc = undefined;
  849. }
  850. };
  851. this.mergeConstraints = function( cons1, cons2 ) {
  852. var merged = cons1;
  853. for (var name in cons2.mandatory) {
  854. merged.mandatory[ name ] = cons2.mandatory[ name ];
  855. }
  856. merged.optional.concat( cons2.optional );
  857. return merged;
  858. }
  859. // Set Opus as the default audio codec if it's present.
  860. this.preferOpus = function( sdp ) {
  861. var sdpLines = sdp.split( '\r\n' );
  862. // Search for m line.
  863. for ( var i = 0; i < sdpLines.length; i++ ) {
  864. if ( sdpLines[i].search( 'm=audio' ) !== -1 ) {
  865. var mLineIndex = i;
  866. break;
  867. }
  868. }
  869. if ( mLineIndex === null ) {
  870. return sdp;
  871. }
  872. // for ( var i = 0; i < sdpLines.length; i++ ) {
  873. // if ( i == 0 ) console.info( "=============================================" );
  874. // console.info( "sdpLines["+i+"] = " + sdpLines[i] );
  875. // }
  876. // If Opus is available, set it as the default in m line.
  877. for ( var i = 0; i < sdpLines.length; i++ ) {
  878. if ( sdpLines[i].search( 'opus/48000' ) !== -1 ) {
  879. var opusPayload = this.extractSdp( sdpLines[i], /:(\d+) opus\/48000/i );
  880. if ( opusPayload) {
  881. sdpLines[ mLineIndex ] = this.setDefaultCodec( sdpLines[ mLineIndex ], opusPayload );
  882. }
  883. break;
  884. }
  885. }
  886. // Remove CN in m line and sdp.
  887. sdpLines = this.removeCN( sdpLines, mLineIndex );
  888. sdp = sdpLines.join('\r\n');
  889. return sdp;
  890. }
  891. // Set Opus in stereo if stereo is enabled.
  892. function addStereo( sdp ) {
  893. var sdpLines = sdp.split('\r\n');
  894. // Find opus payload.
  895. for (var i = 0; i < sdpLines.length; i++) {
  896. if (sdpLines[i].search('opus/48000') !== -1) {
  897. var opusPayload = extractSdp(sdpLines[i], /:(\d+) opus\/48000/i);
  898. break;
  899. }
  900. }
  901. // Find the payload in fmtp line.
  902. for (var i = 0; i < sdpLines.length; i++) {
  903. if (sdpLines[i].search('a=fmtp') !== -1) {
  904. var payload = extractSdp(sdpLines[i], /a=fmtp:(\d+)/ );
  905. if (payload === opusPayload) {
  906. var fmtpLineIndex = i;
  907. break;
  908. }
  909. }
  910. }
  911. // No fmtp line found.
  912. if (fmtpLineIndex === null)
  913. return sdp;
  914. // Append stereo=1 to fmtp line.
  915. sdpLines[fmtpLineIndex] = sdpLines[fmtpLineIndex].concat(' stereo=1');
  916. sdp = sdpLines.join('\r\n');
  917. return sdp;
  918. }
  919. // Strip CN from sdp before CN constraints is ready.
  920. this.removeCN = function( sdpLines, mLineIndex ) {
  921. var mLineElements = sdpLines[mLineIndex].split( ' ' );
  922. // Scan from end for the convenience of removing an item.
  923. for ( var i = sdpLines.length-1; i >= 0; i-- ) {
  924. var payload = this.extractSdp( sdpLines[i], /a=rtpmap:(\d+) CN\/\d+/i );
  925. if ( payload ) {
  926. var cnPos = mLineElements.indexOf( payload );
  927. if ( cnPos !== -1 ) {
  928. // Remove CN payload from m line.
  929. mLineElements.splice( cnPos, 1 );
  930. }
  931. // Remove CN line in sdp
  932. sdpLines.splice( i, 1 );
  933. }
  934. }
  935. sdpLines[ mLineIndex ] = mLineElements.join( ' ' );
  936. return sdpLines;
  937. }
  938. this.extractSdp = function( sdpLine, pattern ) {
  939. var result = sdpLine.match( pattern );
  940. return ( result && result.length == 2 ) ? result[ 1 ] : null;
  941. }
  942. // Set the selected codec to the first in m line.
  943. this.setDefaultCodec = function( mLine, payload ) {
  944. var elements = mLine.split( ' ' );
  945. var newLine = new Array();
  946. var index = 0;
  947. for ( var i = 0; i < elements.length; i++ ) {
  948. if ( index === 3 ) // Format of media starts from the fourth.
  949. newLine[ index++ ] = payload; // Put target payload to the first.
  950. if ( elements[ i ] !== payload )
  951. newLine[ index++ ] = elements[ i ];
  952. }
  953. return newLine.join( ' ' );
  954. }
  955. }
  956. } );