webrtc.js 38 KB

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