File: source/peer-data.js

  1. /**
  2. * Stores the list of Peers session information.
  3. * @attribute _peerInformations
  4. * @param {JSON} <#peerId> The Peer session information.
  5. * @param {JSON|String} <#peerId>.userData The Peer custom data.
  6. * @param {JSON} <#peerId>.settings The Peer streaming information.
  7. * @param {JSON} <#peerId>.mediaStatus The Peer streaming muted status.
  8. * @param {JSON} <#peerId>.agent The Peer agent information.
  9. * @type JSON
  10. * @private
  11. * @for Skylink
  12. * @since 0.3.0
  13. */
  14. Skylink.prototype._peerInformations = {};
  15.  
  16. /**
  17. * Stores the Signaling user credentials from the API response required for connecting to the Signaling server.
  18. * @attribute _user
  19. * @param {String} uid The API result "username".
  20. * @param {String} token The API result "userCred".
  21. * @param {String} timeStamp The API result "timeStamp".
  22. * @param {String} sid The Signaling server receive user Peer ID.
  23. * @type JSON
  24. * @private
  25. * @for Skylink
  26. * @since 0.5.6
  27. */
  28. Skylink.prototype._user = null;
  29.  
  30. /**
  31. * Stores the User custom data.
  32. * By default, if no custom user data is set, it is an empty string <code>""</code>.
  33. * @attribute _userData
  34. * @type JSON|String
  35. * @default ""
  36. * @private
  37. * @for Skylink
  38. * @since 0.5.6
  39. */
  40. Skylink.prototype._userData = '';
  41.  
  42. /**
  43. * Function that overwrites the User current custom data.
  44. * @method setUserData
  45. * @param {JSON|String} userData The updated custom data.
  46. * @trigger <ol class="desc-seq">
  47. * <li>Updates User custom data. <ol>
  48. * <li>If User is in Room: <ol>
  49. * <li><a href="#event_peerUpdated"><code>peerUpdated</code> event</a> triggers with parameter payload
  50. * <code>isSelf</code> value as <code>true</code>.</li></ol></li></ol></li></ol>
  51. * @example
  52. * // Example 1: Set/Update User custom data before joinRoom()
  53. * var userData = "beforejoin";
  54. *
  55. * skylinkDemo.setUserData(userData);
  56. *
  57. * skylinkDemo.joinRoom(function (error, success) {
  58. * if (error) return;
  59. * if (success.peerInfo.userData === userData) {
  60. * console.log("User data is sent");
  61. * }
  62. * });
  63. *
  64. * // Example 2: Update User custom data after joinRoom()
  65. * var userData = "afterjoin";
  66. *
  67. * skylinkDemo.joinRoom(function (error, success) {
  68. * if (error) return;
  69. * skylinkDemo.setUserData(userData);
  70. * if (skylinkDemo.getPeerInfo().userData === userData) {
  71. * console.log("User data is updated and sent");
  72. * }
  73. * });
  74. * @for Skylink
  75. * @since 0.5.5
  76. */
  77. Skylink.prototype.setUserData = function(userData) {
  78. var self = this;
  79.  
  80. this._userData = userData || '';
  81.  
  82. if (self._inRoom) {
  83. log.log('Updated userData -> ', userData);
  84. self._sendChannelMessage({
  85. type: self._SIG_MESSAGE_TYPE.UPDATE_USER,
  86. mid: self._user.sid,
  87. rid: self._room.id,
  88. userData: self._userData,
  89. stamp: (new Date()).getTime()
  90. });
  91. self._trigger('peerUpdated', self._user.sid, self.getPeerInfo(), true);
  92. } else {
  93. log.warn('User is not in the room. Broadcast of updated information will be dropped');
  94. }
  95. };
  96.  
  97. /**
  98. * Function that returns the User / Peer current custom data.
  99. * @method getUserData
  100. * @param {String} [peerId] The Peer ID to return the current custom data from.
  101. * - When not provided or that the Peer ID is does not exists, it will return
  102. * the User current custom data.
  103. * @return {JSON|String} The User / Peer current custom data.
  104. * @example
  105. * // Example 1: Get Peer current custom data
  106. * var peerUserData = skylinkDemo.getUserData(peerId);
  107. *
  108. * // Example 2: Get User current custom data
  109. * var userUserData = skylinkDemo.getUserData();
  110. * @for Skylink
  111. * @since 0.5.10
  112. */
  113. Skylink.prototype.getUserData = function(peerId) {
  114. if (peerId && peerId !== this._user.sid) {
  115. // peer info
  116. var peerInfo = this._peerInformations[peerId];
  117.  
  118. if (typeof peerInfo === 'object') {
  119. return peerInfo.userData;
  120. }
  121.  
  122. return null;
  123. }
  124. return this._userData;
  125. };
  126.  
  127. /**
  128. * Function that returns the User / Peer current session information.
  129. * @method getPeerInfo
  130. * @param {String} [peerId] The Peer ID to return the current session information from.
  131. * - When not provided or that the Peer ID is does not exists, it will return
  132. * the User current session information.
  133. * @return {JSON} The User / Peer current session information.
  134. * <small>Object signature matches the <code>peerInfo</code> parameter payload received in the
  135. * <a href="#event_peerJoined"><code>peerJoined</code> event</a>.</small>
  136. * @example
  137. * // Example 1: Get Peer current session information
  138. * var peerPeerInfo = skylinkDemo.getPeerInfo(peerId);
  139. *
  140. * // Example 2: Get User current session information
  141. * var userPeerInfo = skylinkDemo.getPeerInfo();
  142. * @for Skylink
  143. * @since 0.4.0
  144. */
  145. Skylink.prototype.getPeerInfo = function(peerId) {
  146. var peerInfo = null;
  147.  
  148. if (typeof peerId === 'string' && typeof this._peerInformations[peerId] === 'object') {
  149. peerInfo = clone(this._peerInformations[peerId]);
  150. peerInfo.room = clone(this._selectedRoom);
  151. if (peerInfo.settings.video && peerInfo.settings.video.frameRate === -1) {
  152. delete peerInfo.settings.video.frameRate;
  153. }
  154.  
  155. } else {
  156. peerInfo = {
  157. userData: clone(this._userData) || '',
  158. settings: {
  159. audio: false,
  160. video: false
  161. },
  162. mediaStatus: clone(this._streamsMutedSettings),
  163. agent: {
  164. name: window.webrtcDetectedBrowser,
  165. version: window.webrtcDetectedVersion,
  166. os: window.navigator.platform,
  167. pluginVersion: AdapterJS.WebRTCPlugin.plugin ? AdapterJS.WebRTCPlugin.plugin.VERSION : null
  168. },
  169. room: clone(this._selectedRoom)
  170. };
  171.  
  172. if (this._streams.screenshare) {
  173. peerInfo.settings = clone(this._streams.screenshare.settings);
  174. } else if (this._streams.userMedia) {
  175. peerInfo.settings = clone(this._streams.userMedia.settings);
  176. }
  177. }
  178.  
  179. if (!peerInfo.settings.audio) {
  180. peerInfo.mediaStatus.audioMuted = true;
  181. }
  182.  
  183. if (!peerInfo.settings.video) {
  184. peerInfo.mediaStatus.videoMuted = true;
  185. }
  186.  
  187. return peerInfo;
  188. };
  189.  
  190. /**
  191. * Function that returns the User session information to be sent to Peers.
  192. * @method _getUserInfo
  193. * @private
  194. * @for Skylink
  195. * @since 0.4.0
  196. */
  197. Skylink.prototype._getUserInfo = function(peerId) {
  198. var userInfo = clone(this.getPeerInfo());
  199.  
  200. if (userInfo.settings.video && !userInfo.settings.video.frameRate) {
  201. userInfo.settings.video.frameRate = -1;
  202. }
  203.  
  204. delete userInfo.agent;
  205. delete userInfo.room;
  206.  
  207. return userInfo;
  208. };