webrtc.js 40 KB

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