webrtc.js 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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. //if ( this.kernel.find( nodeID ).length > 0 ) {
  156. var moniker = nodeID.slice(-20);//this.kernel.name( nodeID );
  157. var client = undefined;
  158. if ( moniker == this.kernel.moniker() ) {
  159. // this is the client that has left the converstaion
  160. // go through the peerConnections and close the
  161. // all current connections
  162. var peer, peerMoniker;
  163. for ( var peerID in this.state.clients ) {
  164. peer = this.state.clients[ peerID ];
  165. peerMoniker = appMoniker.call( this, peer.name )
  166. if ( peerMoniker != this.kernel.moniker ) {
  167. peer.connection && peer.connection.disconnect();
  168. }
  169. }
  170. } else {
  171. // this is a client who has has a peer leave the converstaion
  172. // remove that client, and the
  173. client = findClientByMoniker.call( this, moniker );
  174. if ( client ) {
  175. client.connection && client.connection.disconnect();
  176. removeClient.call( this, client );
  177. delete this.state.clients[ client ]
  178. }
  179. }
  180. //}
  181. },
  182. createdProperty: function( nodeID, propertyName, propertyValue ) {
  183. if ( this.debugVwf.properties ) {
  184. this.kernel.logger.infox( "C === createdProperty ", nodeID, propertyName, propertyValue );
  185. }
  186. this.satProperty( nodeID, propertyName, propertyValue );
  187. },
  188. initializedProperty: function( nodeID, propertyName, propertyValue ) {
  189. if ( this.debugVwf.properties ) {
  190. this.kernel.logger.infox( " I === initializedProperty ", nodeID, propertyName, propertyValue );
  191. }
  192. this.satProperty( nodeID, propertyName, propertyValue );
  193. },
  194. satProperty: function( nodeID, propertyName, propertyValue ) {
  195. if ( this.debugVwf.properties || this.debugVwf.setting ) {
  196. this.kernel.logger.infox( " S === satProperty ", nodeID, propertyName, propertyValue );
  197. }
  198. var client = this.state.clients[ nodeID ];
  199. if ( client ) {
  200. switch( propertyName ) {
  201. case "sharing":
  202. if ( propertyValue ) {
  203. client.sharing = propertyValue;
  204. if ( nodeID == this.local.ID ) {
  205. updateSharing.call( this, nodeID, propertyValue );
  206. }
  207. }
  208. break;
  209. case "localUrl":
  210. if ( propertyValue ) {
  211. if ( nodeID != this.local.ID ) {
  212. client.localUrl = propertyValue;
  213. }
  214. }
  215. break;
  216. case "remoteUrl":
  217. if ( propertyValue ) {
  218. client.remoteUrl = propertyValue;
  219. }
  220. break;
  221. case "displayName":
  222. if ( propertyValue ) {
  223. client.displayName = propertyValue;
  224. }
  225. break;
  226. case "color":
  227. var clr = new utility.color( propertyValue );
  228. if ( clr ) {
  229. client.color = clr.toString();
  230. }
  231. break;
  232. default:
  233. // propertyName is the moniker of the client that
  234. // this connection supports
  235. if ( nodeID == this.local.ID ) {
  236. if ( propertyValue ) {
  237. // propertyName - moniker of the client
  238. // propertyValue - peerConnection message
  239. handlePeerMessage.call( this, propertyName, propertyValue );
  240. }
  241. }
  242. break;
  243. }
  244. }
  245. },
  246. gotProperty: function( nodeID, propertyName, propertyValue ) {
  247. if ( this.debugVwf.properties || this.debugVwf.getting ) {
  248. this.kernel.logger.infox( " G === gotProperty ", nodeID, propertyName, propertyValue );
  249. }
  250. var value = undefined;
  251. return value;
  252. },
  253. calledMethod: function( nodeID, methodName, methodParameters, methodValue ) {
  254. if ( this.debugVwf.calling ) {
  255. this.kernel.logger.infox( " CM === calledMethod ", nodeID, methodName, methodParameters );
  256. }
  257. switch ( methodName ) {
  258. case "setLocalMute":
  259. if ( this.kernel.moniker() == this.kernel.client() ) {
  260. methodValue = setMute.call( this, methodParameters );
  261. }
  262. break;
  263. }
  264. },
  265. firedEvent: function( nodeID, eventName, eventParameters ) {
  266. },
  267. } );
  268. function createVideoElementAsAsset(id) {
  269. var video = document.querySelector('#' + id);
  270. if (!video) {
  271. video = document.createElement('video');
  272. }
  273. video.setAttribute('id', id);
  274. video.setAttribute('autoplay', true);
  275. video.setAttribute('src', '');
  276. video.setAttribute("webkit-playsinline", true);
  277. video.setAttribute("controls", true);
  278. var assets = document.querySelector('a-assets');
  279. // if (!assets) {
  280. // assets = document.createElement('a-assets');
  281. // document.querySelector('a-scene').appendChild(assets);
  282. // }
  283. if (!assets.contains(video)) {
  284. assets.appendChild(video);
  285. }
  286. return video;
  287. }
  288. function getPrototypes( extendsID ) {
  289. var prototypes = [];
  290. var id = extendsID;
  291. while ( id !== undefined ) {
  292. prototypes.push( id );
  293. id = this.kernel.prototype( id );
  294. }
  295. return prototypes;
  296. }
  297. function getPeer( moniker ) {
  298. var clientNode;
  299. for ( var id in this.state.clients ) {
  300. if ( this.state.clients[id].moniker == moniker ) {
  301. clientNode = this.state.clients[id];
  302. break;
  303. }
  304. }
  305. return clientNode;
  306. }
  307. function displayLocal( stream, name, color ) {
  308. var id = this.kernel.moniker();
  309. return displayVideo.call( this, id, stream, this.local.url, name, id, true, color );
  310. }
  311. function displayVideo( id, stream, url, name, destMoniker, muted, color ) {
  312. var divId = undefined;
  313. if ( this.videoProperties.create ) {
  314. this.videosAdded++
  315. var $container;
  316. divId = name + this.videosAdded;
  317. var videoId = "video-" + divId;
  318. $container = $( "#" + this.videoElementsDiv );
  319. if ( muted ) {
  320. $container.append(
  321. "<div id='"+ divId + "'>" +
  322. "<video class='vwf-webrtc-video' id='" + videoId +
  323. "' width='320' height='240' " +
  324. "loop='loop' autoplay muted " +
  325. "style='position: absolute; left: 0; top: 0; z-index: 40;'>" +
  326. "</video>" +
  327. "</div>"
  328. );
  329. } else {
  330. $container.append(
  331. "<div id='"+ divId + "'>" +
  332. "<video class='vwf-webrtc-video' id='" + videoId +
  333. "' width='320' height='240'" +
  334. " loop='loop' autoplay " +
  335. "style='position: absolute; left: 0; top: 0; z-index: 40;'>" +
  336. "</video>" +
  337. "</div>"
  338. );
  339. }
  340. var videoE = $( '#'+ videoId )[0];
  341. if ( videoE && stream ) {
  342. navigator.attachMediaStream( videoE, stream );
  343. if ( muted ) {
  344. videoE.muted = true; // firefox isn't mapping the muted property correctly
  345. }
  346. }
  347. var divEle = $('#'+divId);
  348. divEle && divEle.draggable && divEle.draggable();
  349. }
  350. var clr = new utility.color( color );
  351. if ( clr ) {
  352. clr = clr.toArray();
  353. }
  354. this.kernel.callMethod( this.kernel.application(), "createVideo", [ {
  355. "ID": id,
  356. "url": url,
  357. "name": name,
  358. "muted": muted,
  359. "color": clr ? clr : color
  360. }, destMoniker ] );
  361. let video = createVideoElementAsAsset(name);
  362. video.srcObject = stream;
  363. this.kernel.callMethod( 'avatar-'+id, "setVideoTexture", [name]);
  364. return divId;
  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. return displayVideo.call( this, id, stream, url, name, destMoniker, false, color );
  378. }
  379. function capture( media ) {
  380. if ( this.local.stream === undefined && ( media.video || media.audio ) ) {
  381. var self = this;
  382. var constraints = {
  383. "audio": media.audio,
  384. "video": media.video ? { "mandatory": {}, "optional": [] } : false,
  385. };
  386. var successCallback = function( stream ) {
  387. self.local.url = URL.createObjectURL( stream );
  388. self.local.stream = stream;
  389. self.kernel.setProperty( self.local.ID, "localUrl", self.local.url );
  390. var localNode = self.state.clients[ self.local.ID ];
  391. displayLocal.call( self, stream, localNode.displayName, localNode.color );
  392. sendOffers.call( self );
  393. };
  394. var errorCallback = function( error ) {
  395. console.log("failed to get video stream error: " + error);
  396. };
  397. try {
  398. navigator.getUserMedia( constraints, successCallback, errorCallback );
  399. } catch (e) {
  400. console.log("getUserMedia: error " + e );
  401. };
  402. }
  403. }
  404. function appMoniker( name ) {
  405. return name.substr( 7, name.length-1 );
  406. }
  407. function findClientByMoniker( moniker ) {
  408. var client = undefined;
  409. for ( var id in this.state.clients ) {
  410. if ( client === undefined && moniker == this.state.clients[ id ].moniker ) {
  411. client = this.state.clients[ id ];
  412. }
  413. }
  414. return client;
  415. }
  416. function removeClient( client ) {
  417. if ( client ) {
  418. removeVideo.call( this, client );
  419. }
  420. }
  421. function sendOffers() {
  422. var peerNode;
  423. for ( var id in this.state.clients ) {
  424. if ( id != this.local.ID ) {
  425. peerNode = this.state.clients[ id ];
  426. // if there's a url then connect
  427. if ( peerNode.localUrl && peerNode.localUrl != "" && peerNode.connection === undefined ) {
  428. createPeerConnection.call( this, peerNode, true );
  429. }
  430. }
  431. }
  432. }
  433. function updateSharing( nodeID, sharing ) {
  434. setMute.call( this, !sharing.audio );
  435. setPause.call( this, !sharing.video );
  436. }
  437. function setMute( mute ) {
  438. if ( this.local.stream && this.local.stream.audioTracks && this.local.stream.audioTracks.length > 0 ) {
  439. if ( mute !== undefined ) {
  440. this.local.stream.audioTracks[0].enabled = !mute;
  441. }
  442. }
  443. };
  444. function setPause( pause ) {
  445. if ( this.local.stream && this.local.stream.videoTracks && this.local.stream.videoTracks.length > 0 ) {
  446. if ( pause !== undefined ) {
  447. this.local.stream.videoTracks[0].enabled = !pause;
  448. }
  449. }
  450. }
  451. function release() {
  452. for ( id in this.connections ) {
  453. this.connections[id].disconnect();
  454. }
  455. this.connections = {};
  456. }
  457. function hasStream() {
  458. return ( this.stream !== undefined );
  459. }
  460. function createPeerConnection( peerNode, sendOffer ) {
  461. if ( peerNode ) {
  462. if ( peerNode.connection === undefined ) {
  463. peerNode.connection = new mediaConnection( this, peerNode );
  464. peerNode.connection.connect( this.local.stream, sendOffer );
  465. //if ( this.bandwidth !== undefined ) {
  466. // debugger;
  467. //}
  468. }
  469. }
  470. }
  471. function handlePeerMessage( propertyName, msg ) {
  472. var peerNode = getPeer.call( this, propertyName )
  473. if ( peerNode ) {
  474. if ( peerNode.connection !== undefined ) {
  475. peerNode.connection.processMessage( msg );
  476. } else {
  477. if ( msg.type === 'offer' ) {
  478. this.msgQueue.unshift( msg );
  479. peerNode.connection = new mediaConnection( this, peerNode );
  480. peerNode.connection.connect( this.local.stream, false );
  481. while ( this.msgQueue.length > 0 ) {
  482. peerNode.connection.processMessage( this.msgQueue.shift() );
  483. }
  484. this.msgQueue = [];
  485. } else {
  486. this.msgQueue.push( msg );
  487. }
  488. }
  489. }
  490. }
  491. function deletePeerConnection( peerID ) {
  492. var peerNode = this.state.clients[ peerID ];
  493. if ( peerNode ) {
  494. peerNode.connection.disconnect();
  495. peerNode.connection = undefined;
  496. }
  497. }
  498. function getConnectionStats() {
  499. var peerNode = undefined;
  500. for ( var id in this.state.clients ) {
  501. peerNode = this.state.clients[ id ];
  502. if ( peerNode && peerNode.connection ) {
  503. peerNode.connection.getStats();
  504. }
  505. }
  506. }
  507. function isClientDefinition( prototypes ) {
  508. var foundClient = false;
  509. if ( prototypes ) {
  510. var len = prototypes.length;
  511. for ( var i = 0; i < len && !foundClient; i++ ) {
  512. foundClient = ( prototypes[i] == "http://vwf.example.com/aframe/avatar.vwf" );
  513. }
  514. }
  515. return foundClient;
  516. }
  517. function isClientInstanceDef( nodeID ) {
  518. return ( nodeID == "http://vwf.example.com/clients.vwf" );
  519. }
  520. function mediaConnection( view, peerNode ) {
  521. this.view = view;
  522. this.peerNode = peerNode;
  523. //
  524. this.stream = undefined;
  525. this.url = undefined;
  526. this.pc = undefined;
  527. this.connected = false;
  528. this.streamAdded = false;
  529. this.state = "created";
  530. // webrtc peerConnection parameters
  531. this.pc_config = { "iceServers": this.view.iceServers };
  532. this.pc_constraints = { "optional": [ { "DtlsSrtpKeyAgreement": true } ] };
  533. // Set up audio and video regardless of what devices are present.
  534. this.sdpConstraints = { 'mandatory': {
  535. 'OfferToReceiveAudio':true,
  536. 'OfferToReceiveVideo':true }};
  537. this.connect = function( stream, sendOffer ) {
  538. var self = this;
  539. if ( this.pc === undefined ) {
  540. if ( this.view.debug ) console.log("Creating PeerConnection.");
  541. var iceCallback = function( event ) {
  542. //console.log( "------------------------ iceCallback ------------------------" );
  543. if ( event.candidate ) {
  544. var sMsg = {
  545. "type": 'candidate',
  546. "label": event.candidate.sdpMLineIndex,
  547. "id": event.candidate.sdpMid,
  548. "candidate": event.candidate.candidate
  549. };
  550. // each client creates a property for each other
  551. // the message value is broadcast via the property
  552. self.view.kernel.setProperty( self.peerNode.ID, self.view.kernel.moniker(), sMsg );
  553. } else {
  554. if ( self.view.debug ) console.log("End of candidates.");
  555. }
  556. };
  557. // if ( webrtcDetectedBrowser == "firefox" ) {
  558. // this.pc_config = {"iceServers":[{"url":"stun:23.21.150.121"}]};
  559. // }
  560. try {
  561. this.pc = new RTCPeerConnection( this.pc_config, this.pc_constraints );
  562. this.pc.onicecandidate = iceCallback;
  563. if ( self.view.debug ) console.log("Created RTCPeerConnnection with config \"" + JSON.stringify( this.pc_config ) + "\".");
  564. } catch (e) {
  565. console.log("Failed to create PeerConnection, exception: " + e.message);
  566. alert("Cannot create RTCPeerConnection object; WebRTC is not supported by this browser.");
  567. return;
  568. }
  569. this.pc.onnegotiationeeded = function( event ) {
  570. //debugger;
  571. //console.info( "onnegotiationeeded." );
  572. }
  573. this.pc.onaddstream = function( event ) {
  574. if ( self.view.debug ) console.log("Remote stream added.");
  575. self.stream = event.stream;
  576. self.url = URL.createObjectURL( event.stream );
  577. if ( self.view.debug ) console.log("Remote stream added. url: " + self.url );
  578. var divID = displayRemote.call( self.view, self.peerNode.moniker, self.stream, self.url, self.peerNode.displayName, view.kernel.moniker(), self.peerNode.color );
  579. if ( divID !== undefined ) {
  580. self.peerNode.videoDivID = divID;
  581. }
  582. };
  583. this.pc.onremovestream = function( event ) {
  584. if ( self.view.debug ) console.log("Remote stream removed.");
  585. };
  586. this.pc.onsignalingstatechange = function() {
  587. //console.info( "onsignalingstatechange state change." );
  588. }
  589. this.pc.oniceconnectionstatechange = function( ) {
  590. if ( self && self.pc ) {
  591. var state = self.pc.signalingState || self.pc.readyState;
  592. //console.info( "peerConnection state change: " + state );
  593. }
  594. }
  595. if ( stream ) {
  596. if ( this.view.debug ) console.log("Adding local stream.");
  597. this.pc.addStream( stream );
  598. this.streamAdded = true;
  599. }
  600. if ( sendOffer ){
  601. this.call();
  602. }
  603. }
  604. this.connected = ( this.pc !== undefined );
  605. };
  606. this.setMute = function( mute ) {
  607. if ( this.stream && this.stream.audioTracks && this.stream.audioTracks.length > 0 ) {
  608. if ( mute !== undefined ) {
  609. this.stream.audioTracks[0].enabled = !mute;
  610. }
  611. }
  612. }
  613. this.setPause = function( pause ) {
  614. if ( this.stream && this.stream.videoTracks && this.stream.videoTracks.length > 0 ) {
  615. if ( pause !== undefined ) {
  616. this.stream.videoTracks[0].enabled = !pause;
  617. }
  618. }
  619. }
  620. this.disconnect = function() {
  621. if ( this.view.debug ) console.log( "PC.disconnect " + this.peerID );
  622. if ( this.pc ) {
  623. this.pc.close();
  624. this.pc = undefined;
  625. }
  626. };
  627. this.processMessage = function( msg ) {
  628. //var msg = JSON.parse(message);
  629. if ( this.view.debug ) console.log('S->C: ' + JSON.stringify(msg) );
  630. if ( this.pc ) {
  631. if ( msg.type === 'offer') {
  632. if ( this.view.stereo ) {
  633. msg.sdp = addStereo( msg.sdp );
  634. }
  635. this.pc.setRemoteDescription( new RTCSessionDescription( msg ) );
  636. this.answer();
  637. } else if ( msg.type === 'answer' && this.streamAdded ) {
  638. if ( this.view.stereo ) {
  639. msg.sdp = addStereo( msg.sdp );
  640. }
  641. this.pc.setRemoteDescription( new RTCSessionDescription( msg ) );
  642. } else if ( msg.type === 'candidate' && this.streamAdded ) {
  643. var candidate = new RTCIceCandidate( {
  644. "sdpMLineIndex": msg.label,
  645. "candidate": msg.candidate
  646. } );
  647. this.pc.addIceCandidate( candidate );
  648. } else if ( msg.type === 'bye' && this.streamAdded ) {
  649. this.hangup();
  650. }
  651. }
  652. };
  653. this.answer = function() {
  654. if ( this.view.debug ) console.log( "Send answer to peer" );
  655. var self = this;
  656. var answerer = function( sessionDescription ) {
  657. // Set Opus as the preferred codec in SDP if Opus is present.
  658. sessionDescription.sdp = self.preferOpus( sessionDescription.sdp );
  659. sessionDescription.sdp = self.setBandwidth( sessionDescription.sdp );
  660. self.pc.setLocalDescription( sessionDescription );
  661. self.view.kernel.setProperty( self.peerNode.ID, self.view.kernel.moniker(), sessionDescription );
  662. };
  663. this.pc.createAnswer( answerer, null, this.sdpConstraints);
  664. };
  665. this.call = function() {
  666. var self = this;
  667. var constraints = {
  668. "optional": [],
  669. "mandatory": {}
  670. };
  671. // temporary measure to remove Moz* constraints in Chrome
  672. if ( navigator.webrtcDetectedBrowser === "chrome" ) {
  673. for ( var prop in constraints.mandatory ) {
  674. if ( prop.indexOf("Moz") != -1 ) {
  675. delete constraints.mandatory[ prop ];
  676. }
  677. }
  678. }
  679. constraints = this.mergeConstraints( constraints, this.sdpConstraints );
  680. if ( this.view.debug ) console.log("Sending offer to peer, with constraints: \n" + " \"" + JSON.stringify(constraints) + "\".")
  681. var offerer = function( sessionDescription ) {
  682. // Set Opus as the preferred codec in SDP if Opus is present.
  683. sessionDescription.sdp = self.preferOpus( sessionDescription.sdp );
  684. sessionDescription.sdp = self.setBandwidth( sessionDescription.sdp );
  685. self.pc.setLocalDescription( sessionDescription );
  686. //sendSignalMessage.call( sessionDescription, self.peerID );
  687. self.view.kernel.setProperty( self.peerNode.ID, self.view.kernel.moniker(), sessionDescription );
  688. };
  689. var onFailure = function(e) {
  690. console.log(e)
  691. }
  692. this.pc.createOffer( offerer, onFailure, constraints );
  693. };
  694. this.setBandwidth = function( sdp ) {
  695. // apparently this only works in chrome
  696. if ( this.bandwidth === undefined || moz ) {
  697. return sdp;
  698. }
  699. // remove existing bandwidth lines
  700. sdp = sdp.replace(/b=AS([^\r\n]+\r\n)/g, '');
  701. if ( this.bandwidth.audio ) {
  702. sdp = sdp.replace(/a=mid:audio\r\n/g, 'a=mid:audio\r\nb=AS:' + this.bandwidth.audio + '\r\n');
  703. }
  704. if ( this.bandwidth.video ) {
  705. sdp = sdp.replace(/a=mid:video\r\n/g, 'a=mid:video\r\nb=AS:' + this.bandwidth.video + '\r\n');
  706. }
  707. if ( this.bandwidth.data /*&& !options.preferSCTP */ ) {
  708. sdp = sdp.replace(/a=mid:data\r\n/g, 'a=mid:data\r\nb=AS:' + this.bandwidth.data + '\r\n');
  709. }
  710. return sdp;
  711. }
  712. this.getStats = function(){
  713. if ( this.pc && this.pc.getStats ) {
  714. console.info( "pc.iceConnectionState = " + this.pc.iceConnectionState );
  715. console.info( " pc.iceGatheringState = " + this.pc.iceGatheringState );
  716. console.info( " pc.readyState = " + this.pc.readyState );
  717. console.info( " pc.signalingState = " + this.pc.signalingState );
  718. var consoleStats = function( obj ) {
  719. console.info( ' Timestamp:' + obj.timestamp );
  720. if ( obj.id ) {
  721. console.info( ' id: ' + obj.id );
  722. }
  723. if ( obj.type ) {
  724. console.info( ' type: ' + obj.type );
  725. }
  726. if ( obj.names ) {
  727. var names = obj.names();
  728. for ( var i = 0; i < names.length; ++i ) {
  729. console.info( " "+names[ i ]+": " + obj.stat( names[ i ] ) );
  730. }
  731. } else {
  732. if ( obj.stat && obj.stat( 'audioOutputLevel' ) ) {
  733. console.info( " audioOutputLevel: " + obj.stat( 'audioOutputLevel' ) );
  734. }
  735. }
  736. };
  737. // local function
  738. var readStats = function( stats ) {
  739. var results = stats.result();
  740. var bitrateText = 'No bitrate stats';
  741. for ( var i = 0; i < results.length; ++i ) {
  742. var res = results[ i ];
  743. console.info( 'Report ' + i );
  744. if ( !res.local || res.local === res ) {
  745. consoleStats( res );
  746. // The bandwidth info for video is in a type ssrc stats record
  747. // with googFrameHeightReceived defined.
  748. // Should check for mediatype = video, but this is not
  749. // implemented yet.
  750. if ( res.type == 'ssrc' && res.stat( 'googFrameHeightReceived' ) ) {
  751. var bytesNow = res.stat( 'bytesReceived' );
  752. if ( timestampPrev > 0) {
  753. var bitRate = Math.round( ( bytesNow - bytesPrev ) * 8 / ( res.timestamp - timestampPrev ) );
  754. bitrateText = bitRate + ' kbits/sec';
  755. }
  756. timestampPrev = res.timestamp;
  757. bytesPrev = bytesNow;
  758. }
  759. } else {
  760. // Pre-227.0.1445 (188719) browser
  761. if ( res.local ) {
  762. console.info( " Local: " );
  763. consoleStats( res.local );
  764. }
  765. if ( res.remote ) {
  766. console.info( " Remote: " );
  767. consoleStats( res.remote );
  768. }
  769. }
  770. }
  771. console.info( " bitrate: " + bitrateText )
  772. }
  773. this.pc.getStats( readStats );
  774. }
  775. }
  776. this.hangup = function() {
  777. if ( this.view.debug ) console.log( "PC.hangup " + this.id );
  778. if ( this.pc ) {
  779. this.pc.close();
  780. this.pc = undefined;
  781. }
  782. };
  783. this.mergeConstraints = function( cons1, cons2 ) {
  784. var merged = cons1;
  785. for (var name in cons2.mandatory) {
  786. merged.mandatory[ name ] = cons2.mandatory[ name ];
  787. }
  788. merged.optional.concat( cons2.optional );
  789. return merged;
  790. }
  791. // Set Opus as the default audio codec if it's present.
  792. this.preferOpus = function( sdp ) {
  793. var sdpLines = sdp.split( '\r\n' );
  794. // Search for m line.
  795. for ( var i = 0; i < sdpLines.length; i++ ) {
  796. if ( sdpLines[i].search( 'm=audio' ) !== -1 ) {
  797. var mLineIndex = i;
  798. break;
  799. }
  800. }
  801. if ( mLineIndex === null ) {
  802. return sdp;
  803. }
  804. // for ( var i = 0; i < sdpLines.length; i++ ) {
  805. // if ( i == 0 ) console.info( "=============================================" );
  806. // console.info( "sdpLines["+i+"] = " + sdpLines[i] );
  807. // }
  808. // If Opus is available, set it as the default in m line.
  809. for ( var i = 0; i < sdpLines.length; i++ ) {
  810. if ( sdpLines[i].search( 'opus/48000' ) !== -1 ) {
  811. var opusPayload = this.extractSdp( sdpLines[i], /:(\d+) opus\/48000/i );
  812. if ( opusPayload) {
  813. sdpLines[ mLineIndex ] = this.setDefaultCodec( sdpLines[ mLineIndex ], opusPayload );
  814. }
  815. break;
  816. }
  817. }
  818. // Remove CN in m line and sdp.
  819. sdpLines = this.removeCN( sdpLines, mLineIndex );
  820. sdp = sdpLines.join('\r\n');
  821. return sdp;
  822. }
  823. // Set Opus in stereo if stereo is enabled.
  824. function addStereo( sdp ) {
  825. var sdpLines = sdp.split('\r\n');
  826. // Find opus payload.
  827. for (var i = 0; i < sdpLines.length; i++) {
  828. if (sdpLines[i].search('opus/48000') !== -1) {
  829. var opusPayload = extractSdp(sdpLines[i], /:(\d+) opus\/48000/i);
  830. break;
  831. }
  832. }
  833. // Find the payload in fmtp line.
  834. for (var i = 0; i < sdpLines.length; i++) {
  835. if (sdpLines[i].search('a=fmtp') !== -1) {
  836. var payload = extractSdp(sdpLines[i], /a=fmtp:(\d+)/ );
  837. if (payload === opusPayload) {
  838. var fmtpLineIndex = i;
  839. break;
  840. }
  841. }
  842. }
  843. // No fmtp line found.
  844. if (fmtpLineIndex === null)
  845. return sdp;
  846. // Append stereo=1 to fmtp line.
  847. sdpLines[fmtpLineIndex] = sdpLines[fmtpLineIndex].concat(' stereo=1');
  848. sdp = sdpLines.join('\r\n');
  849. return sdp;
  850. }
  851. // Strip CN from sdp before CN constraints is ready.
  852. this.removeCN = function( sdpLines, mLineIndex ) {
  853. var mLineElements = sdpLines[mLineIndex].split( ' ' );
  854. // Scan from end for the convenience of removing an item.
  855. for ( var i = sdpLines.length-1; i >= 0; i-- ) {
  856. var payload = this.extractSdp( sdpLines[i], /a=rtpmap:(\d+) CN\/\d+/i );
  857. if ( payload ) {
  858. var cnPos = mLineElements.indexOf( payload );
  859. if ( cnPos !== -1 ) {
  860. // Remove CN payload from m line.
  861. mLineElements.splice( cnPos, 1 );
  862. }
  863. // Remove CN line in sdp
  864. sdpLines.splice( i, 1 );
  865. }
  866. }
  867. sdpLines[ mLineIndex ] = mLineElements.join( ' ' );
  868. return sdpLines;
  869. }
  870. this.extractSdp = function( sdpLine, pattern ) {
  871. var result = sdpLine.match( pattern );
  872. return ( result && result.length == 2 ) ? result[ 1 ] : null;
  873. }
  874. // Set the selected codec to the first in m line.
  875. this.setDefaultCodec = function( mLine, payload ) {
  876. var elements = mLine.split( ' ' );
  877. var newLine = new Array();
  878. var index = 0;
  879. for ( var i = 0; i < elements.length; i++ ) {
  880. if ( index === 3 ) // Format of media starts from the fourth.
  881. newLine[ index++ ] = payload; // Put target payload to the first.
  882. if ( elements[ i ] !== payload )
  883. newLine[ index++ ] = elements[ i ];
  884. }
  885. return newLine.join( ' ' );
  886. }
  887. }
  888. } );