File: source/skylink-events.js

  1. /**
  2. * Stores the list of {{#crossLink "Skylink/on:method"}}on(){{/crossLink}}
  3. * event subscription handlers.
  4. * @attribute _EVENTS
  5. * @param {Array} (#eventName) The array of event subscription handlers that is
  6. * subscribed using {{#crossLink "Skylink/on:method"}}on() method{{/crossLink}}
  7. * associated with the event name.
  8. * @param {Function} (#eventName).(#index) The event subscription handler
  9. * associated with the event name. This is to be triggered multiple times
  10. * until {{#crossLink "Skylink/off:method"}}off(){{/crossLink}} is invoked for
  11. * this event subscription handler.
  12. * @type JSON
  13. * @private
  14. * @required
  15. * @component Events
  16. * @for Skylink
  17. * @since 0.5.2
  18. */
  19. Skylink.prototype._EVENTS = {
  20. /**
  21. * Event triggered when the socket connection to the platform signaling is opened.
  22. * - This event means that socket connection is open and self is ready to join the room.
  23. * @event channelOpen
  24. * @component Events
  25. * @for Skylink
  26. * @since 0.1.0
  27. */
  28. channelOpen: [],
  29.  
  30. /**
  31. * Event triggered when the socket connection to the platform signaling is closed.
  32. * - This event means that socket connection is closed and self has left the room.
  33. * @event channelClose
  34. * @component Events
  35. * @for Skylink
  36. * @since 0.1.0
  37. */
  38. channelClose: [],
  39.  
  40. /**
  41. * Event triggered when the socket connection is exchanging messages with the platform signaling.
  42. * - This event is a debugging feature, and it's not advisable to subscribe to
  43. * this event unless you are debugging the socket messages
  44. * received from the platform signaling.
  45. * @event channelMessage
  46. * @param {JSON} message The socket message object data received from the platform signaling.
  47. * @component Events
  48. * @for Skylink
  49. * @since 0.1.0
  50. */
  51. channelMessage: [],
  52.  
  53. /**
  54. * Event triggered when the socket connection has occurred an exception
  55. * during a connection with the platform signaling.
  56. * - After this event is triggered, it may result in <a href="#event_channelClose">channelClose</a>,
  57. * and the socket connection with the platform signaling could be disrupted.
  58. * @event channelError
  59. * @param {Object|String} error The error object thrown that caused the exception.
  60. * @component Events
  61. * @for Skylink
  62. * @since 0.1.0
  63. */
  64. channelError: [],
  65.  
  66. /**
  67. * Event triggered when attempting to reconnect the socket connection with the platform signaling.
  68. * - Depending on the current <code>type</code> triggered in <a href="#event_socketError">
  69. * socketError</a> event before, it may or may not attempt the socket reconnection and
  70. * this event may not be triggered.
  71. * - If reconnection attempt fails, it will trigger <a href="#event_socketError">socketError</a> event
  72. * again and repeat the stage from there.
  73. * @event channelRetry
  74. * @param {String} fallbackType The fallback socket transport that Skylink is attempting to reconnect with.
  75. * [Rel: Skylink.SOCKET_FALLBACK]
  76. * @param {Number} currentAttempt The current reconnection attempt.
  77. * @component Events
  78. * @for Skylink
  79. * @since 0.5.6
  80. */
  81. channelRetry: [],
  82.  
  83. /**
  84. * Event triggered when attempt to <em>(re)</em>connect the socket connection with the platform signaling has failed.
  85. * - Depending on the current <code>type</code> payload, it may or may not attempt the
  86. * socket reconnection and <a href="#event_channelRetry">channelRetry</a> event may not be triggered.
  87. * - If reconnection attempt fails and there are still available ports to reconnect with,
  88. * it will trigger <a href="#event_channelRetry">channelRetry</a> event again and
  89. * repeat the stage from there.
  90. * @event socketError
  91. * @param {String} errorCode The socket connection error code received.
  92. * [Rel: Skylink.SOCKET_ERROR]
  93. * @param {Number|String|Object} error The error object thrown that caused the failure.
  94. * @param {String} type The socket transport that Skylink has failed to connect with.
  95. * [Rel: Skylink.SOCKET_FALLBACK]
  96. * @component Events
  97. * @for Skylink
  98. * @since 0.5.5
  99. */
  100. socketError: [],
  101.  
  102. /**
  103. * Event triggered when room connection information is being retrieved from platform server.
  104. * - This is also triggered when <a href="#method_init">init()</a> is invoked, but
  105. * the socket connection events like <a href="#event_channelOpen">channelOpen</a> does
  106. * not get triggered but stops at <u>readyStateChange</u> event.
  107. * @event readyStateChange
  108. * @param {String} readyState The current ready state of the retrieval when the event is triggered.
  109. * [Rel: Skylink.READY_STATE_CHANGE]
  110. * @param {JSON} [error=null] The error object thrown when there is a failure in retrieval.
  111. * If received as <code>null</code>, it means that there is no errors.
  112. * @param {Number} error.status Http status when retrieving information.
  113. * May be empty for other errors.
  114. * @param {Number} error.errorCode The
  115. * <a href="#attr_READY_STATE_CHANGE_ERROR">READY_STATE_CHANGE_ERROR</a>
  116. * if there is an <a href="#event_readyStateChange">readyStateChange</a>
  117. * event error that caused the failure for initialising Skylink.
  118. * [Rel: Skylink.READY_STATE_CHANGE_ERROR]
  119. * @param {Object} error.content The exception thrown that caused the failure
  120. * for initialising Skylink.
  121. * @param {Number} error.status The XMLHttpRequest status code received
  122. * when exception is thrown that caused the failure for initialising Skylink.
  123. * @param {String} room The selected room connection information that Skylink is attempting
  124. * to retrieve the information for to start connection to.
  125. * @component Events
  126. * @for Skylink
  127. * @since 0.4.0
  128. */
  129. readyStateChange: [],
  130.  
  131. /**
  132. * Event triggered when a Peer handshaking state has changed.
  133. * - This event may trigger <code>state</code> <code>HANDSHAKE_PROGRESS.ENTER</code> for
  134. * self to indicate that broadcast to ping for any existing Peers in the room has
  135. * been made.
  136. * - This event is a debugging feature, and it's used to check the
  137. * Peer handshaking connection status.
  138. * - This starts the Peer connection handshaking, where it retrieves all the Peer
  139. * information and then proceeds to start the ICE connection.
  140. * @event handshakeProgress
  141. * @param {String} state The Peer connection handshake state.
  142. * [Rel: Skylink.HANDSHAKE_PROGRESS]
  143. * @param {String} peerId The Peer ID associated with the connection
  144. * handshake state.
  145. * @param {Object|String} [error] The error object thrown when there is a failure in
  146. * the connection handshaking.
  147. * @component Events
  148. * @for Skylink
  149. * @since 0.3.0
  150. */
  151. handshakeProgress: [],
  152.  
  153. /**
  154. * Event triggered when a Peer connection ICE gathering state has changed.
  155. * - This event is a debugging feature, and it's used to check the
  156. * Peer ICE candidate gathering status.
  157. * - This indicates if the ICE gathering has been completed to
  158. * start ICE connection for DataChannel and media streaming connection.
  159. * @event candidateGenerationState
  160. * @param {String} state The current ICE gathering state.
  161. * <small>See the list of available triggered states in the related link.</small>
  162. * [Rel: Skylink.CANDIDATE_GENERATION_STATE]
  163. * @param {String} peerId The Peer ID associated with the connection
  164. * @component Events
  165. * @for Skylink
  166. * @since 0.1.0
  167. */
  168. candidateGenerationState: [],
  169.  
  170. /**
  171. * Event triggered when a Peer connection signaling state has changed.
  172. * - This event is a debugging feature, and it's used to check the
  173. * Peer signaling connection status.
  174. * - This event indicates if the session description is received
  175. * to start ICE gathering for DataChannel and media streaming connection.
  176. * @event peerConnectionState
  177. * @param {String} state The current connection signaling state.
  178. * [Rel: Skylink.PEER_CONNECTION_STATE]
  179. * @param {String} peerId The Peer ID associated with the current connection
  180. * signaling state.
  181. * @component Events
  182. * @for Skylink
  183. * @since 0.1.0
  184. */
  185. peerConnectionState: [],
  186.  
  187. /**
  188. * Event triggered when a Peer connection ICE connection state has changed.
  189. * - This event is a debugging feature, and it's used to check the
  190. * Peer ICE connection of added ICE candidates status.
  191. * - This event indicates if the ICE connection is established for successful
  192. * DataChannel and media streaming connection.
  193. * @event iceConnectionState
  194. * @param {String} state The current ICE connection state.
  195. * [Rel: Skylink.ICE_CONNECTION_STATE]
  196. * @param {String} peerId The Peer ID associated with the current ICE connection state.
  197. * @component Events
  198. * @for Skylink
  199. * @since 0.1.0
  200. */
  201. iceConnectionState: [],
  202.  
  203. /**
  204. * Event triggered when access to self user media stream has failed.
  205. * - If <code>audioFallback</code> is enabled in <a href="#method_init">init()</a>,
  206. * it will throw an error if audio only user media stream failed after
  207. * a failed attempt to retrieve video and audio user media.
  208. * @event mediaAccessError
  209. * @param {Object|String} error The error object thrown that caused the failure.
  210. * @param {Boolean} isScreensharing The flag that indicates if self
  211. * Stream object is a screensharing stream or not.
  212. * @param {Boolean} isAudioFallbackError The flag that indicates if Skylink throws
  213. * the error after an audio fallback has been attempted.
  214. * @component Events
  215. * @for Skylink
  216. * @since 0.1.0
  217. */
  218. mediaAccessError: [],
  219.  
  220. /**
  221. * Event triggered when media access fallback has been made.
  222. * - If <code>audioFallback</code> is enabled in <a href="#method_init">init()</a>,
  223. * and if there is a failed attempt to retrieve video and audio user media,
  224. * it will attempt to do the audio fallback.
  225. * - If MediaStream successfully received does not meet to expected tracks, this
  226. * event would be triggered.
  227. * @event mediaAccessFallback
  228. * @param {JSON} error The error object information.
  229. * @param {Object|String} error.error The error object thrown that caused the failure
  230. * from retrieve video and audio user media stream.
  231. * is triggered because (video+audio) error is fallbacking to audio only.
  232. * @param {JSON} [error.diff=null] The list of expected audio and video tracks and received
  233. * tracks.<br>This is only defined when <code>state</code> payload is <code>1</code>.
  234. * @param {JSON} error.diff.video The expected and received video tracks.
  235. * @param {Number} error.diff.video.expected The expected video tracks.
  236. * @param {Number} error.diff.video.received The received video tracks.
  237. * @param {JSON} error.diff.audio The expected and received audio tracks.
  238. * @param {Number} error.diff.audio.expected The expected audio tracks.
  239. * @param {Number} error.diff.audio.received The received audio tracks.
  240. * @param {Number} state The access fallback state.
  241. * <small><ul>
  242. * <li><code>0</code>: Attempting to retrieve access for fallback state.</li>
  243. * <li><code>1</code>: Fallback access has been completed successfully</li>
  244. * <li><code>-1</code>: Failed retrieving fallback access</li>
  245. * </ul></small>
  246. * @param {Boolean} [isScreensharing=false] The flag that indicates if this event ia an
  247. * fallback from failed screensharing retrieval or attaching of audio.
  248. * @component Events
  249. * @param {Boolean} [isAudioFallback=false] The flag that indicates if this event is an
  250. * audio fallbacking from failed attempt to retrieve video and audio user media.
  251. * @for Skylink
  252. * @since 0.6.3
  253. */
  254. mediaAccessFallback: [],
  255.  
  256. /**
  257. * Event triggered when access to self user media stream is successfully
  258. * attached to Skylink.
  259. * @event mediaAccessSuccess
  260. * @param {Object} stream The self user [MediaStream](https://developer.mozilla.org/en-US/docs/Web/API/MediaStream_API)
  261. * object. To display the MediaStream object to a <code>video</code> or <code>audio</code>, simply invoke:<br>
  262. * <code>attachMediaStream(domElement, stream);</code>.
  263. * @param {Boolean} isScreensharing The flag that indicates if self
  264. * Stream object is a screensharing stream or not.
  265. * @component Events
  266. * @for Skylink
  267. * @since 0.1.0
  268. */
  269. mediaAccessSuccess: [],
  270.  
  271. /**
  272. * Event triggered when the application requires to retrieve self
  273. * user media stream manually instead of doing it automatically in
  274. * {{#crossLink "Skylink/joinRoom:method"}}joinRoom(){{/crossLink}}.
  275. * - This event triggers based on the configuration of <code>manualGetUserMedia</code>
  276. * in the <a href="#method_joinRoom">joinRoom() configuration settings</a>.
  277. * - Developers must manually invoke <a href="#method_getUserMedia">getUserMedia()</a>
  278. * to retrieve the user media stream before self would join the room.
  279. * Once the user media stream is attached, self would proceed to join the room
  280. * automatically.
  281. * @event mediaAccessRequired
  282. * @component Events
  283. * @for Skylink
  284. * @since 0.5.5
  285. */
  286. mediaAccessRequired: [],
  287.  
  288. /**
  289. * Event triggered when self user media stream attached to Skylink has been stopped.
  290. * @event mediaAccessStopped
  291. * @param {Boolean} isScreensharing The flag that indicates if self
  292. * Stream object is a screensharing stream or not.
  293. * @component Events
  294. * @for Skylink
  295. * @since 0.5.6
  296. */
  297. mediaAccessStopped: [],
  298.  
  299. /**
  300. * Event triggered when a Peer joins the room.
  301. * @event peerJoined
  302. * @param {String} peerId The Peer ID of the new peer
  303. * that has joined the room.
  304. * @param {Object} peerInfo The peer information associated
  305. * with the Peer Connection.
  306. * @param {String|JSON} peerInfo.userData The custom user data
  307. * information set by developer. This custom user data can also
  308. * be set in <a href="#method_setUserData">setUserData()</a>.
  309. * @param {JSON} peerInfo.settings The Peer Stream
  310. * streaming settings information. If both audio and video
  311. * option is <code>false</code>, there should be no
  312. * receiving remote Stream object from this associated Peer.
  313. * @param {Boolean|JSON} [peerInfo.settings.audio=false] The
  314. * Peer Stream streaming audio settings. If
  315. * <code>false</code>, it means that audio streaming is disabled in
  316. * the remote Stream of the Peer.
  317. * @param {Boolean} [peerInfo.settings.audio.stereo] The flag that indicates if
  318. * stereo option should be explictly enabled to an OPUS enabled audio stream.
  319. * Check the <code>audioCodec</code> configuration settings in
  320. * <a href="#method_init">init()</a>
  321. * to enable OPUS as the audio codec. Note that stereo is already enabled
  322. * for OPUS codecs, this only adds a stereo flag to the SDP to explictly
  323. * enable stereo in the audio streaming.
  324. * @param {Boolean|JSON} [peerInfo.settings.video=false] The Peer
  325. * Stream streaming video settings. If <code>false</code>, it means that
  326. * video streaming is disabled in the remote Stream of the Peer.
  327. * @param {JSON} [peerInfo.settings.video.resolution] The Peer
  328. * Stream streaming video resolution settings. Setting the resolution may
  329. * not force set the resolution provided as it depends on the how the
  330. * browser handles the resolution. [Rel: Skylink.VIDEO_RESOLUTION]
  331. * @param {Number} [peerInfo.settings.video.resolution.width] The Peer
  332. * Stream streaming video resolution width.
  333. * @param {Number} [peerInfo.settings.video.resolution.height] The Peer
  334. * Stream streaming video resolution height.
  335. * @param {Number} [peerInfo.settings.video.frameRate] The Peer
  336. * Stream streaming video maximum frameRate.
  337. * @param {Boolean} [peerInfo.settings.video.screenshare=false] The flag
  338. * that indicates if the Peer connection Stream object sent
  339. * is a screensharing stream or not.
  340. * @param {String} [peerInfo.settings.bandwidth] The Peer
  341. * streaming bandwidth settings. Setting the bandwidth flags may not
  342. * force set the bandwidth for each connection stream channels as it depends
  343. * on how the browser handles the bandwidth bitrate. Values are configured
  344. * in <var>kb/s</var>.
  345. * @param {String} [peerInfo.settings.bandwidth.audio] The configured
  346. * audio stream channel for the remote Stream object bandwidth
  347. * that audio streaming should use in <var>kb/s</var>.
  348. * @param {String} [peerInfo.settings.bandwidth.video] The configured
  349. * video stream channel for the remote Stream object bandwidth
  350. * that video streaming should use in <var>kb/s</var>.
  351. * @param {String} [peerInfo.settings.bandwidth.data] The configured
  352. * datachannel channel for the DataChannel connection bandwidth
  353. * that datachannel connection per packet should be able use in <var>kb/s</var>.
  354. * @param {JSON} peerInfo.mediaStatus The Peer Stream mute
  355. * settings for both audio and video streamings.
  356. * @param {Boolean} [peerInfo.mediaStatus.audioMuted=true] The flag that
  357. * indicates if the remote Stream object audio streaming is muted. If
  358. * there is no audio streaming enabled for the Peer, by default,
  359. * it is set to <code>true</code>.
  360. * @param {Boolean} [peerInfo.mediaStatus.videoMuted=true] The flag that
  361. * indicates if the remote Stream object video streaming is muted. If
  362. * there is no video streaming enabled for the Peer, by default,
  363. * it is set to <code>true</code>.
  364. * @param {JSON} peerInfo.agent The Peer platform agent information.
  365. * @param {String} peerInfo.agent.name The Peer platform browser or agent name.
  366. * @param {Number} peerInfo.agent.version The Peer platform browser or agent version.
  367. * @param {Number} peerInfo.agent.os The Peer platform name.
  368. * @param {String} peerInfo.room The current room that the Peer is in.
  369. * @param {Boolean} isSelf The flag that indicates if self is the Peer.
  370. * @component Events
  371. * @for Skylink
  372. * @since 0.5.2
  373. */
  374. peerJoined: [],
  375.  
  376. /**
  377. * Event triggered when a Peer connection has been restarted for
  378. * a reconnection.
  379. * @event peerRestart
  380. * @param {String} peerId The Peer ID of the connection that
  381. * is restarted for a reconnection.
  382. * @param {Object} peerInfo The peer information associated
  383. * with the Peer Connection.
  384. * @param {String|JSON} peerInfo.userData The custom user data
  385. * information set by developer. This custom user data can also
  386. * be set in <a href="#method_setUserData">setUserData()</a>.
  387. * @param {JSON} peerInfo.settings The Peer Stream
  388. * streaming settings information. If both audio and video
  389. * option is <code>false</code>, there should be no
  390. * receiving remote Stream object from this associated Peer.
  391. * @param {Boolean|JSON} [peerInfo.settings.audio=false] The
  392. * Peer Stream streaming audio settings. If
  393. * <code>false</code>, it means that audio streaming is disabled in
  394. * the remote Stream of the Peer.
  395. * @param {Boolean} [peerInfo.settings.audio.stereo] The flag that indicates if
  396. * stereo option should be explictly enabled to an OPUS enabled audio stream.
  397. * Check the <code>audioCodec</code> configuration settings in
  398. * <a href="#method_init">init()</a>
  399. * to enable OPUS as the audio codec. Note that stereo is already enabled
  400. * for OPUS codecs, this only adds a stereo flag to the SDP to explictly
  401. * enable stereo in the audio streaming.
  402. * @param {Boolean|JSON} [peerInfo.settings.video=false] The Peer
  403. * Stream streaming video settings. If <code>false</code>, it means that
  404. * video streaming is disabled in the remote Stream of the Peer.
  405. * @param {JSON} [peerInfo.settings.video.resolution] The Peer
  406. * Stream streaming video resolution settings. Setting the resolution may
  407. * not force set the resolution provided as it depends on the how the
  408. * browser handles the resolution. [Rel: Skylink.VIDEO_RESOLUTION]
  409. * @param {Number} [peerInfo.settings.video.resolution.width] The Peer
  410. * Stream streaming video resolution width.
  411. * @param {Number} [peerInfo.settings.video.resolution.height] The Peer
  412. * Stream streaming video resolution height.
  413. * @param {Number} [peerInfo.settings.video.frameRate] The Peer
  414. * Stream streaming video maximum frameRate.
  415. * @param {Boolean} [peerInfo.settings.video.screenshare=false] The flag
  416. * that indicates if the Peer connection Stream object sent
  417. * is a screensharing stream or not.
  418. * @param {String} [peerInfo.settings.bandwidth] The Peer
  419. * streaming bandwidth settings. Setting the bandwidth flags may not
  420. * force set the bandwidth for each connection stream channels as it depends
  421. * on how the browser handles the bandwidth bitrate. Values are configured
  422. * in <var>kb/s</var>.
  423. * @param {String} [peerInfo.settings.bandwidth.audio] The configured
  424. * audio stream channel for the remote Stream object bandwidth
  425. * that audio streaming should use in <var>kb/s</var>.
  426. * @param {String} [peerInfo.settings.bandwidth.video] The configured
  427. * video stream channel for the remote Stream object bandwidth
  428. * that video streaming should use in <var>kb/s</var>.
  429. * @param {String} [peerInfo.settings.bandwidth.data] The configured
  430. * datachannel channel for the DataChannel connection bandwidth
  431. * that datachannel connection per packet should be able use in <var>kb/s</var>.
  432. * @param {JSON} peerInfo.mediaStatus The Peer Stream mute
  433. * settings for both audio and video streamings.
  434. * @param {Boolean} [peerInfo.mediaStatus.audioMuted=true] The flag that
  435. * indicates if the remote Stream object audio streaming is muted. If
  436. * there is no audio streaming enabled for the Peer, by default,
  437. * it is set to <code>true</code>.
  438. * @param {Boolean} [peerInfo.mediaStatus.videoMuted=true] The flag that
  439. * indicates if the remote Stream object video streaming is muted. If
  440. * there is no video streaming enabled for the Peer, by default,
  441. * it is set to <code>true</code>.
  442. * @param {JSON} peerInfo.agent The Peer platform agent information.
  443. * @param {String} peerInfo.agent.name The Peer platform browser or agent name.
  444. * @param {Number} peerInfo.agent.version The Peer platform browser or agent version.
  445. * @param {Number} peerInfo.agent.os The Peer platform name.
  446. * @param {String} peerInfo.room The current room that the Peer is in.
  447. * @param {Boolean} isSelfInitiateRestart The flag that indicates if self is
  448. * the one who have initiated the Peer connection restart.
  449. * @component Events
  450. * @for Skylink
  451. * @since 0.5.5
  452. */
  453. peerRestart: [],
  454.  
  455. /**
  456. * Event triggered when a Peer information have been updated.
  457. * - This event would only be triggered if self is in the room.
  458. * - This event triggers when the <code>peerInfo</code> data is updated,
  459. * like <code>peerInfo.mediaStatus</code> or the <code>peerInfo.userData</code>,
  460. * which is invoked through <a href="#method_muteStream">muteStream()</a> or
  461. * <a href="#method_setUserData">setUserData()</a>.
  462. * @event peerUpdated
  463. * @param {String} peerId The Peer ID of the peer with updated information.
  464. * @param {Object} peerInfo The peer information associated
  465. * with the Peer Connection.
  466. * @param {String|JSON} peerInfo.userData The custom user data
  467. * information set by developer. This custom user data can also
  468. * be set in <a href="#method_setUserData">setUserData()</a>.
  469. * @param {JSON} peerInfo.settings The Peer Stream
  470. * streaming settings information. If both audio and video
  471. * option is <code>false</code>, there should be no
  472. * receiving remote Stream object from this associated Peer.
  473. * @param {Boolean|JSON} [peerInfo.settings.audio=false] The
  474. * Peer Stream streaming audio settings. If
  475. * <code>false</code>, it means that audio streaming is disabled in
  476. * the remote Stream of the Peer.
  477. * @param {Boolean} [peerInfo.settings.audio.stereo] The flag that indicates if
  478. * stereo option should be explictly enabled to an OPUS enabled audio stream.
  479. * Check the <code>audioCodec</code> configuration settings in
  480. * <a href="#method_init">init()</a>
  481. * to enable OPUS as the audio codec. Note that stereo is already enabled
  482. * for OPUS codecs, this only adds a stereo flag to the SDP to explictly
  483. * enable stereo in the audio streaming.
  484. * @param {Boolean|JSON} [peerInfo.settings.video=false] The Peer
  485. * Stream streaming video settings. If <code>false</code>, it means that
  486. * video streaming is disabled in the remote Stream of the Peer.
  487. * @param {JSON} [peerInfo.settings.video.resolution] The Peer
  488. * Stream streaming video resolution settings. Setting the resolution may
  489. * not force set the resolution provided as it depends on the how the
  490. * browser handles the resolution. [Rel: Skylink.VIDEO_RESOLUTION]
  491. * @param {Number} [peerInfo.settings.video.resolution.width] The Peer
  492. * Stream streaming video resolution width.
  493. * @param {Number} [peerInfo.settings.video.resolution.height] The Peer
  494. * Stream streaming video resolution height.
  495. * @param {Number} [peerInfo.settings.video.frameRate] The Peer
  496. * Stream streaming video maximum frameRate.
  497. * @param {Boolean} [peerInfo.settings.video.screenshare=false] The flag
  498. * that indicates if the Peer connection Stream object sent
  499. * is a screensharing stream or not.
  500. * @param {String} [peerInfo.settings.bandwidth] The Peer
  501. * streaming bandwidth settings. Setting the bandwidth flags may not
  502. * force set the bandwidth for each connection stream channels as it depends
  503. * on how the browser handles the bandwidth bitrate. Values are configured
  504. * in <var>kb/s</var>.
  505. * @param {String} [peerInfo.settings.bandwidth.audio] The configured
  506. * audio stream channel for the remote Stream object bandwidth
  507. * that audio streaming should use in <var>kb/s</var>.
  508. * @param {String} [peerInfo.settings.bandwidth.video] The configured
  509. * video stream channel for the remote Stream object bandwidth
  510. * that video streaming should use in <var>kb/s</var>.
  511. * @param {String} [peerInfo.settings.bandwidth.data] The configured
  512. * datachannel channel for the DataChannel connection bandwidth
  513. * that datachannel connection per packet should be able use in <var>kb/s</var>.
  514. * @param {JSON} peerInfo.mediaStatus The Peer Stream mute
  515. * settings for both audio and video streamings.
  516. * @param {Boolean} [peerInfo.mediaStatus.audioMuted=true] The flag that
  517. * indicates if the remote Stream object audio streaming is muted. If
  518. * there is no audio streaming enabled for the Peer, by default,
  519. * it is set to <code>true</code>.
  520. * @param {Boolean} [peerInfo.mediaStatus.videoMuted=true] The flag that
  521. * indicates if the remote Stream object video streaming is muted. If
  522. * there is no video streaming enabled for the Peer, by default,
  523. * it is set to <code>true</code>.
  524. * @param {JSON} peerInfo.agent The Peer platform agent information.
  525. * @param {String} peerInfo.agent.name The Peer platform browser or agent name.
  526. * @param {Number} peerInfo.agent.version The Peer platform browser or agent version.
  527. * @param {Number} peerInfo.agent.os The Peer platform name.
  528. * @param {String} peerInfo.room The current room that the Peer is in.
  529. * @param {Boolean} isSelf The flag that indicates if self is the Peer.
  530. * @component Events
  531. * @for Skylink
  532. * @since 0.5.2
  533. */
  534. peerUpdated: [],
  535.  
  536. /**
  537. * Event triggered when a Peer leaves the room.
  538. * @event peerLeft
  539. * @param {String} peerId The Peer ID of the peer
  540. * that had left the room.
  541. * @param {Object} peerInfo The peer information associated
  542. * with the Peer Connection.
  543. * @param {String|JSON} peerInfo.userData The custom user data
  544. * information set by developer. This custom user data can also
  545. * be set in <a href="#method_setUserData">setUserData()</a>.
  546. * @param {JSON} peerInfo.settings The Peer Stream
  547. * streaming settings information. If both audio and video
  548. * option is <code>false</code>, there should be no
  549. * receiving remote Stream object from this associated Peer.
  550. * @param {Boolean|JSON} [peerInfo.settings.audio=false] The
  551. * Peer Stream streaming audio settings. If
  552. * <code>false</code>, it means that audio streaming is disabled in
  553. * the remote Stream of the Peer.
  554. * @param {Boolean} [peerInfo.settings.audio.stereo] The flag that indicates if
  555. * stereo option should be explictly enabled to an OPUS enabled audio stream.
  556. * Check the <code>audioCodec</code> configuration settings in
  557. * <a href="#method_init">init()</a>
  558. * to enable OPUS as the audio codec. Note that stereo is already enabled
  559. * for OPUS codecs, this only adds a stereo flag to the SDP to explictly
  560. * enable stereo in the audio streaming.
  561. * @param {Boolean|JSON} [peerInfo.settings.video=false] The Peer
  562. * Stream streaming video settings. If <code>false</code>, it means that
  563. * video streaming is disabled in the remote Stream of the Peer.
  564. * @param {JSON} [peerInfo.settings.video.resolution] The Peer
  565. * Stream streaming video resolution settings. Setting the resolution may
  566. * not force set the resolution provided as it depends on the how the
  567. * browser handles the resolution. [Rel: Skylink.VIDEO_RESOLUTION]
  568. * @param {Number} [peerInfo.settings.video.resolution.width] The Peer
  569. * Stream streaming video resolution width.
  570. * @param {Number} [peerInfo.settings.video.resolution.height] The Peer
  571. * Stream streaming video resolution height.
  572. * @param {Number} [peerInfo.settings.video.frameRate] The Peer
  573. * Stream streaming video maximum frameRate.
  574. * @param {Boolean} [peerInfo.settings.video.screenshare=false] The flag
  575. * that indicates if the Peer connection Stream object sent
  576. * is a screensharing stream or not.
  577. * @param {String} [peerInfo.settings.bandwidth] The Peer
  578. * streaming bandwidth settings. Setting the bandwidth flags may not
  579. * force set the bandwidth for each connection stream channels as it depends
  580. * on how the browser handles the bandwidth bitrate. Values are configured
  581. * in <var>kb/s</var>.
  582. * @param {String} [peerInfo.settings.bandwidth.audio] The configured
  583. * audio stream channel for the remote Stream object bandwidth
  584. * that audio streaming should use in <var>kb/s</var>.
  585. * @param {String} [peerInfo.settings.bandwidth.video] The configured
  586. * video stream channel for the remote Stream object bandwidth
  587. * that video streaming should use in <var>kb/s</var>.
  588. * @param {String} [peerInfo.settings.bandwidth.data] The configured
  589. * datachannel channel for the DataChannel connection bandwidth
  590. * that datachannel connection per packet should be able use in <var>kb/s</var>.
  591. * @param {JSON} peerInfo.mediaStatus The Peer Stream mute
  592. * settings for both audio and video streamings.
  593. * @param {Boolean} [peerInfo.mediaStatus.audioMuted=true] The flag that
  594. * indicates if the remote Stream object audio streaming is muted. If
  595. * there is no audio streaming enabled for the Peer, by default,
  596. * it is set to <code>true</code>.
  597. * @param {Boolean} [peerInfo.mediaStatus.videoMuted=true] The flag that
  598. * indicates if the remote Stream object video streaming is muted. If
  599. * there is no video streaming enabled for the Peer, by default,
  600. * it is set to <code>true</code>.
  601. * @param {JSON} peerInfo.agent The Peer platform agent information.
  602. * @param {String} peerInfo.agent.name The Peer platform browser or agent name.
  603. * @param {Number} peerInfo.agent.version The Peer platform browser or agent version.
  604. * @param {Number} peerInfo.agent.os The Peer platform name.
  605. * @param {String} peerInfo.room The current room that the Peer is in.
  606. * @param {Boolean} isSelf The flag that indicates if self is the Peer.
  607. * @component Events
  608. * @for Skylink
  609. * @since 0.5.2
  610. */
  611. peerLeft: [],
  612.  
  613. /**
  614. * Event triggered when a Stream is sent by Peer.
  615. * - This event may trigger for self, which indicates that self has joined the room
  616. * and is sending this Stream object to other Peers connected in the room.
  617. * @event incomingStream
  618. * @param {String} peerId The Peer ID associated to the Stream object.
  619. * @param {Object} stream The Peer
  620. * [MediaStream](https://developer.mozilla.org/en-US/docs/Web/API/MediaStream_API)
  621. * object that is sent in this connection.
  622. * To display the MediaStream object to a <code>video</code> or <code>audio</code>, simply invoke:<br>
  623. * <code>attachMediaStream(domElement, stream);</code>.
  624. * @param {Object} peerInfo The peer information associated
  625. * with the Peer Connection.
  626. * @param {String|JSON} peerInfo.userData The custom user data
  627. * information set by developer. This custom user data can also
  628. * be set in <a href="#method_setUserData">setUserData()</a>.
  629. * @param {JSON} peerInfo.settings The Peer Stream
  630. * streaming settings information. If both audio and video
  631. * option is <code>false</code>, there should be no
  632. * receiving remote Stream object from this associated Peer.
  633. * @param {Boolean|JSON} [peerInfo.settings.audio=false] The
  634. * Peer Stream streaming audio settings. If
  635. * <code>false</code>, it means that audio streaming is disabled in
  636. * the remote Stream of the Peer.
  637. * @param {Boolean} [peerInfo.settings.audio.stereo] The flag that indicates if
  638. * stereo option should be explictly enabled to an OPUS enabled audio stream.
  639. * Check the <code>audioCodec</code> configuration settings in
  640. * <a href="#method_init">init()</a>
  641. * to enable OPUS as the audio codec. Note that stereo is already enabled
  642. * for OPUS codecs, this only adds a stereo flag to the SDP to explictly
  643. * enable stereo in the audio streaming.
  644. * @param {Boolean|JSON} [peerInfo.settings.video=false] The Peer
  645. * Stream streaming video settings. If <code>false</code>, it means that
  646. * video streaming is disabled in the remote Stream of the Peer.
  647. * @param {JSON} [peerInfo.settings.video.resolution] The Peer
  648. * Stream streaming video resolution settings. Setting the resolution may
  649. * not force set the resolution provided as it depends on the how the
  650. * browser handles the resolution. [Rel: Skylink.VIDEO_RESOLUTION]
  651. * @param {Number} [peerInfo.settings.video.resolution.width] The Peer
  652. * Stream streaming video resolution width.
  653. * @param {Number} [peerInfo.settings.video.resolution.height] The Peer
  654. * Stream streaming video resolution height.
  655. * @param {Number} [peerInfo.settings.video.frameRate] The Peer
  656. * Stream streaming video maximum frameRate.
  657. * @param {Boolean} [peerInfo.settings.video.screenshare=false] The flag
  658. * that indicates if the Peer connection Stream object sent
  659. * is a screensharing stream or not.
  660. * @param {String} [peerInfo.settings.bandwidth] The Peer
  661. * streaming bandwidth settings. Setting the bandwidth flags may not
  662. * force set the bandwidth for each connection stream channels as it depends
  663. * on how the browser handles the bandwidth bitrate. Values are configured
  664. * in <var>kb/s</var>.
  665. * @param {String} [peerInfo.settings.bandwidth.audio] The configured
  666. * audio stream channel for the remote Stream object bandwidth
  667. * that audio streaming should use in <var>kb/s</var>.
  668. * @param {String} [peerInfo.settings.bandwidth.video] The configured
  669. * video stream channel for the remote Stream object bandwidth
  670. * that video streaming should use in <var>kb/s</var>.
  671. * @param {String} [peerInfo.settings.bandwidth.data] The configured
  672. * datachannel channel for the DataChannel connection bandwidth
  673. * that datachannel connection per packet should be able use in <var>kb/s</var>.
  674. * @param {JSON} peerInfo.mediaStatus The Peer Stream mute
  675. * settings for both audio and video streamings.
  676. * @param {Boolean} [peerInfo.mediaStatus.audioMuted=true] The flag that
  677. * indicates if the remote Stream object audio streaming is muted. If
  678. * there is no audio streaming enabled for the Peer, by default,
  679. * it is set to <code>true</code>.
  680. * @param {Boolean} [peerInfo.mediaStatus.videoMuted=true] The flag that
  681. * indicates if the remote Stream object video streaming is muted. If
  682. * there is no video streaming enabled for the Peer, by default,
  683. * it is set to <code>true</code>.
  684. * @param {JSON} peerInfo.agent The Peer platform agent information.
  685. * @param {String} peerInfo.agent.name The Peer platform browser or agent name.
  686. * @param {Number} peerInfo.agent.version The Peer platform browser or agent version.
  687. * @param {Number} peerInfo.agent.os The Peer platform name.
  688. * @param {String} peerInfo.room The current room that the Peer is in.
  689. * @param {Boolean} isSelf The flag that indicates if self is the Peer.
  690. * @component Events
  691. * @for Skylink
  692. * @since 0.5.5
  693. */
  694. incomingStream: [],
  695.  
  696. /**
  697. * Event triggered when message data is received from Peer.
  698. * - This event may trigger for self when sending message data to Peer,
  699. * which indicates that self has sent the message data.
  700. * @event incomingMessage
  701. * @param {JSON} message The message object received from Peer.
  702. * @param {JSON|String} message.content The message object content. This is the
  703. * message data content passed in {{#crossLink "Skylink/sendMessage:method"}}sendMessage(){{/crossLink}}
  704. * and {{#crossLink "Skylink/sendP2PMessage:method"}}sendP2PMessage(){{/crossLink}}.
  705. * @param {String} message.senderPeerId The Peer ID of the peer who
  706. * sent the message object.
  707. * @param {String|Array} [message.targetPeerId=null] The array of targeted Peer
  708. * peers or the single targeted Peer the message is
  709. * targeted to received the message object. If the value is <code>null</code>, the message
  710. * object is broadcasted to all Peer peers in the room.
  711. * @param {Boolean} message.isPrivate The flag that indicates if the message object is sent to
  712. * targeted Peer peers and not broadcasted to all Peer peers.
  713. * @param {Boolean} message.isDataChannel The flag that indicates if the message object is sent
  714. * from the platform signaling socket connection or P2P channel connection (DataChannel connection).
  715. * @param {String} peerId The Peer ID of peer who sent the
  716. * message object.
  717. * @param {Object} peerInfo The peer information associated
  718. * with the Peer Connection.
  719. * @param {String|JSON} peerInfo.userData The custom user data
  720. * information set by developer. This custom user data can also
  721. * be set in <a href="#method_setUserData">setUserData()</a>.
  722. * @param {JSON} peerInfo.settings The Peer Stream
  723. * streaming settings information. If both audio and video
  724. * option is <code>false</code>, there should be no
  725. * receiving remote Stream object from this associated Peer.
  726. * @param {Boolean|JSON} [peerInfo.settings.audio=false] The
  727. * Peer Stream streaming audio settings. If
  728. * <code>false</code>, it means that audio streaming is disabled in
  729. * the remote Stream of the Peer.
  730. * @param {Boolean} [peerInfo.settings.audio.stereo] The flag that indicates if
  731. * stereo option should be explictly enabled to an OPUS enabled audio stream.
  732. * Check the <code>audioCodec</code> configuration settings in
  733. * <a href="#method_init">init()</a>
  734. * to enable OPUS as the audio codec. Note that stereo is already enabled
  735. * for OPUS codecs, this only adds a stereo flag to the SDP to explictly
  736. * enable stereo in the audio streaming.
  737. * @param {Boolean|JSON} [peerInfo.settings.video=false] The Peer
  738. * Stream streaming video settings. If <code>false</code>, it means that
  739. * video streaming is disabled in the remote Stream of the Peer.
  740. * @param {JSON} [peerInfo.settings.video.resolution] The Peer
  741. * Stream streaming video resolution settings. Setting the resolution may
  742. * not force set the resolution provided as it depends on the how the
  743. * browser handles the resolution. [Rel: Skylink.VIDEO_RESOLUTION]
  744. * @param {Number} [peerInfo.settings.video.resolution.width] The Peer
  745. * Stream streaming video resolution width.
  746. * @param {Number} [peerInfo.settings.video.resolution.height] The Peer
  747. * Stream streaming video resolution height.
  748. * @param {Number} [peerInfo.settings.video.frameRate] The Peer
  749. * Stream streaming video maximum frameRate.
  750. * @param {Boolean} [peerInfo.settings.video.screenshare=false] The flag
  751. * that indicates if the Peer connection Stream object sent
  752. * is a screensharing stream or not.
  753. * @param {String} [peerInfo.settings.bandwidth] The Peer
  754. * streaming bandwidth settings. Setting the bandwidth flags may not
  755. * force set the bandwidth for each connection stream channels as it depends
  756. * on how the browser handles the bandwidth bitrate. Values are configured
  757. * in <var>kb/s</var>.
  758. * @param {String} [peerInfo.settings.bandwidth.audio] The configured
  759. * audio stream channel for the remote Stream object bandwidth
  760. * that audio streaming should use in <var>kb/s</var>.
  761. * @param {String} [peerInfo.settings.bandwidth.video] The configured
  762. * video stream channel for the remote Stream object bandwidth
  763. * that video streaming should use in <var>kb/s</var>.
  764. * @param {String} [peerInfo.settings.bandwidth.data] The configured
  765. * datachannel channel for the DataChannel connection bandwidth
  766. * that datachannel connection per packet should be able use in <var>kb/s</var>.
  767. * @param {JSON} peerInfo.mediaStatus The Peer Stream mute
  768. * settings for both audio and video streamings.
  769. * @param {Boolean} [peerInfo.mediaStatus.audioMuted=true] The flag that
  770. * indicates if the remote Stream object audio streaming is muted. If
  771. * there is no audio streaming enabled for the Peer, by default,
  772. * it is set to <code>true</code>.
  773. * @param {Boolean} [peerInfo.mediaStatus.videoMuted=true] The flag that
  774. * indicates if the remote Stream object video streaming is muted. If
  775. * there is no video streaming enabled for the Peer, by default,
  776. * it is set to <code>true</code>.
  777. * @param {JSON} peerInfo.agent The Peer platform agent information.
  778. * @param {String} peerInfo.agent.name The Peer platform browser or agent name.
  779. * @param {Number} peerInfo.agent.version The Peer platform browser or agent version.
  780. * @param {Number} peerInfo.agent.os The Peer platform name.
  781. * @param {String} peerInfo.room The current room that the Peer is in.
  782. * @param {Boolean} isSelf The flag that indicates if self is the Peer.
  783. * @component Events
  784. * @for Skylink
  785. * @since 0.5.2
  786. */
  787. incomingMessage: [],
  788.  
  789. /**
  790. * Event triggered when a data transfer is completed.
  791. * - This event may trigger for self when transferring data to Peer,
  792. * which indicates that self has transferred the data successfully.
  793. * - For more extensive states like the outgoing and incoming
  794. * data transfer progress and rejection of data transfer requests,
  795. * you may subscribe to the <a href="#event_dataTransferState">dataTransferState</a> event.
  796. * - If <code>enableDataChannel</code> disabled in <a href="#method_init">init() configuration
  797. * settings</a>, this event will not be triggered at all.
  798. * @event incomingData
  799. * @param {Blob|String} data The transferred data object.<br>
  800. * For Blob data object, see the
  801. * [createObjectURL](https://developer.mozilla.org/en-US/docs/Web/API/URL.createObjectURL)
  802. * method on how you can convert the Blob data object to a download link.
  803. * @param {String} transferId The transfer ID of the completed data transfer.
  804. * @param {String} peerId The Peer ID associated with the data transfer.
  805. * @param {JSON} transferInfo The transfer data object information.
  806. * @param {String} [transferInfo.name=transferId] The transfer data object name.
  807. * If there is no name based on the Blob given, the name would be the transfer ID.
  808. * @param {Number} transferInfo.size The transfer data size.
  809. * @param {String} transferInfo.dataType The type of data transfer initiated.
  810. * Available types are <code>"dataURL"</code> and <code>"blob"</code>.
  811. * @param {String} transferInfo.timeout The waiting timeout in seconds that the DataChannel
  812. * connection data transfer should wait before throwing an exception and terminating the data transfer.
  813. * @param {Boolean} transferInfo.isPrivate The flag to indicate if the data transferred
  814. * targeted Peer peers and not broadcasted to all Peer peers.
  815. * @param {Boolean} isSelf The flag that indicates if the data transfer is from self or from
  816. * associated Peer.
  817. * @component Events
  818. * @for Skylink
  819. * @since 0.6.1
  820. */
  821. incomingData: [],
  822.  
  823.  
  824. /**
  825. * Event triggered when there is a request to start a data transfer.
  826. * - This event may trigger for self when requesting a data transfer to Peer,
  827. * which indicates that self has sent the data transfer request.
  828. * - For more extensive states like the outgoing and incoming
  829. * data transfer progress and rejection of data transfer requests,
  830. * you may subscribe to the <a href="#event_dataTransferState">dataTransferState</a> event.
  831. * - If <code>enableDataChannel</code> disabled in <a href="#method_init">init() configuration
  832. * settings</a>, this event will not be triggered at all.
  833. * - <sub>DATA TRANSFER STAGE</sub><br>
  834. * <small>
  835. * <a href="#event_dataTransferState">dataTransferState</a> &#8594;
  836. * <b>incomingDataRequest</b> &#8594;
  837. * <a href="#event_incomingData">incomingData</a>
  838. * </small>
  839. * @event incomingDataRequest
  840. * @param {String} transferId The transfer ID of the data transfer request.
  841. * @param {String} peerId The Peer ID associated with the data transfer request.
  842. * @param {JSON} transferInfo The transfer data object information.
  843. * @param {String} [transferInfo.name=transferId] The transfer data object name.
  844. * If there is no name based on the Blob given, the name would be the transfer ID.
  845. * @param {Number} transferInfo.size The transfer data size.
  846. * @param {String} transferInfo.dataType The type of data transfer initiated.
  847. * Available types are <code>"dataURL"</code> and <code>"blob"</code>.
  848. * @param {String} transferInfo.timeout The waiting timeout in seconds that the DataChannel
  849. * connection data transfer should wait before throwing an exception and terminating the data transfer.
  850. * @param {Boolean} transferInfo.isPrivate The flag to indicate if the data transferred
  851. * targeted Peer peers and not broadcasted to all Peer peers.
  852. * @param {Boolean} isSelf The flag that indicates if the data transfer request is from self or from
  853. * associated Peer.
  854. * @component Events
  855. * @for Skylink
  856. * @since 0.6.1
  857. */
  858. incomingDataRequest: [],
  859.  
  860. /**
  861. * Event triggered when the currently connected room lock status have been updated.
  862. * - If this event is triggered, this means that the room is locked / unlocked which
  863. * may allow or prevent any other Peers from joining the room.
  864. * @event roomLock
  865. * @param {Boolean} isLocked The flag that indicates if the currently connected room is locked.
  866. * @param {String} peerId The Peer ID of the peer that updated the
  867. * currently connected room lock status.
  868. * @param {Object} peerInfo The peer information associated
  869. * with the Peer Connection.
  870. * @param {String|JSON} peerInfo.userData The custom user data
  871. * information set by developer. This custom user data can also
  872. * be set in <a href="#method_setUserData">setUserData()</a>.
  873. * @param {JSON} peerInfo.settings The Peer Stream
  874. * streaming settings information. If both audio and video
  875. * option is <code>false</code>, there should be no
  876. * receiving remote Stream object from this associated Peer.
  877. * @param {Boolean|JSON} [peerInfo.settings.audio=false] The
  878. * Peer Stream streaming audio settings. If
  879. * <code>false</code>, it means that audio streaming is disabled in
  880. * the remote Stream of the Peer.
  881. * @param {Boolean} [peerInfo.settings.audio.stereo] The flag that indicates if
  882. * stereo option should be explictly enabled to an OPUS enabled audio stream.
  883. * Check the <code>audioCodec</code> configuration settings in
  884. * <a href="#method_init">init()</a>
  885. * to enable OPUS as the audio codec. Note that stereo is already enabled
  886. * for OPUS codecs, this only adds a stereo flag to the SDP to explictly
  887. * enable stereo in the audio streaming.
  888. * @param {Boolean|JSON} [peerInfo.settings.video=false] The Peer
  889. * Stream streaming video settings. If <code>false</code>, it means that
  890. * video streaming is disabled in the remote Stream of the Peer.
  891. * @param {JSON} [peerInfo.settings.video.resolution] The Peer
  892. * Stream streaming video resolution settings. Setting the resolution may
  893. * not force set the resolution provided as it depends on the how the
  894. * browser handles the resolution. [Rel: Skylink.VIDEO_RESOLUTION]
  895. * @param {Number} [peerInfo.settings.video.resolution.width] The Peer
  896. * Stream streaming video resolution width.
  897. * @param {Number} [peerInfo.settings.video.resolution.height] The Peer
  898. * Stream streaming video resolution height.
  899. * @param {Number} [peerInfo.settings.video.frameRate] The Peer
  900. * Stream streaming video maximum frameRate.
  901. * @param {Boolean} [peerInfo.settings.video.screenshare=false] The flag
  902. * that indicates if the Peer connection Stream object sent
  903. * is a screensharing stream or not.
  904. * @param {String} [peerInfo.settings.bandwidth] The Peer
  905. * streaming bandwidth settings. Setting the bandwidth flags may not
  906. * force set the bandwidth for each connection stream channels as it depends
  907. * on how the browser handles the bandwidth bitrate. Values are configured
  908. * in <var>kb/s</var>.
  909. * @param {String} [peerInfo.settings.bandwidth.audio] The configured
  910. * audio stream channel for the remote Stream object bandwidth
  911. * that audio streaming should use in <var>kb/s</var>.
  912. * @param {String} [peerInfo.settings.bandwidth.video] The configured
  913. * video stream channel for the remote Stream object bandwidth
  914. * that video streaming should use in <var>kb/s</var>.
  915. * @param {String} [peerInfo.settings.bandwidth.data] The configured
  916. * datachannel channel for the DataChannel connection bandwidth
  917. * that datachannel connection per packet should be able use in <var>kb/s</var>.
  918. * @param {JSON} peerInfo.mediaStatus The Peer Stream mute
  919. * settings for both audio and video streamings.
  920. * @param {Boolean} [peerInfo.mediaStatus.audioMuted=true] The flag that
  921. * indicates if the remote Stream object audio streaming is muted. If
  922. * there is no audio streaming enabled for the Peer, by default,
  923. * it is set to <code>true</code>.
  924. * @param {Boolean} [peerInfo.mediaStatus.videoMuted=true] The flag that
  925. * indicates if the remote Stream object video streaming is muted. If
  926. * there is no video streaming enabled for the Peer, by default,
  927. * it is set to <code>true</code>.
  928. * @param {JSON} peerInfo.agent The Peer platform agent information.
  929. * @param {String} peerInfo.agent.name The Peer platform browser or agent name.
  930. * @param {Number} peerInfo.agent.version The Peer platform browser or agent version.
  931. * @param {Number} peerInfo.agent.os The Peer platform name.
  932. * @param {String} peerInfo.room The current room that the Peer is in.
  933. * @param {Boolean} isSelf The flag that indicates if self is the Peer.
  934. * @component Events
  935. * @for Skylink
  936. * @since 0.5.2
  937. */
  938. roomLock: [],
  939.  
  940. /**
  941. * Event triggered when a Peer connection DataChannel connection state has changed.
  942. * - This event is a debugging feature, and it's used to check the
  943. * Peer DataChannel connection, which is used for P2P messaging and transfers for
  944. * methods like <a href="#method_sendBlobData">sendBlobData()</a>,
  945. * <a href="#method_sendURLData">sendURLData()</a> and
  946. * <a href="#method_sendP2PMessage">sendP2PMessage()</a>.
  947. * - If <code>enableDataChannel</code> disabled in <a href="#method_init">init() configuration
  948. * settings</a>, this event will not be triggered at all.
  949. * @event dataChannelState
  950. * @param {String} state The current DataChannel connection state.
  951. * [Rel: Skylink.DATA_CHANNEL_STATE]
  952. * @param {String} peerId The Peer ID associated with the current DataChannel connection state.
  953. * @param {Object} [error=null] The error object thrown when there is a failure in
  954. * the DataChannel connection.
  955. * If received as <code>null</code>, it means that there is no errors.
  956. * @param {String} channelName The DataChannel connection ID.
  957. * @param {String} channelType The DataChannel connection functionality type.
  958. * [Rel: Skylink.DATA_CHANNEL_TYPE]
  959. * @component Events
  960. * @for Skylink
  961. * @since 0.1.0
  962. */
  963. dataChannelState: [],
  964.  
  965. /**
  966. * Event triggered when a data transfer state has changed.
  967. * - This event triggers more extensive states like the outgoing and incoming
  968. * data transfer progress and rejection of data transfer requests.
  969. * For simplified events, you may subscribe to the
  970. * <a href="#event_incomingDataRequest">incomingDataRequest</a> and
  971. * <a href="#event_incomingData">incomingData</a> events.
  972. * - If <code>enableDataChannel</code> disabled in <a href="#method_init">init() configuration
  973. * settings</a>, this event will not be triggered at all.
  974. * @event dataTransferState
  975. * @param {String} state The data transfer made to Peer
  976. * in a DataChannel connection state.
  977. * [Rel: Skylink.DATA_TRANSFER_STATE]
  978. * @param {String} transferId The transfer ID of the completed data transfer.
  979. * @param {String} peerId The Peer ID associated with the data transfer.
  980. * @param {JSON} transferInfo The transfer data object information.
  981. * @param {Blob|String} transferInfo.data The transfer data object. This is defined
  982. * only after the transfer data is completed, when the state is
  983. * <code>DATA_TRANSFER_STATE.DOWNLOAD_COMPLETED</code> and
  984. * <code>DATA_TRANSFER_STATE.UPLOAD_STARTED</code><br>
  985. * For Blob data object, see the
  986. * [createObjectURL](https://developer.mozilla.org/en-US/docs/Web/API/URL.createObjectURL)
  987. * method on how you can convert the Blob data object to a download link.
  988. * @param {String} [transferInfo.name=transferId] The transfer data object name.
  989. * If there is no name based on the Blob given, the name would be the transfer ID.
  990. * @param {Number} transferInfo.size The transfer data size.
  991. * @param {String} transferInfo.dataType The type of data transfer initiated.
  992. * Available types are <code>"dataURL"</code> and <code>"blob"</code>.
  993. * @param {String} transferInfo.timeout The waiting timeout in seconds that the DataChannel
  994. * connection data transfer should wait before throwing an exception and terminating the data transfer.
  995. * @param {Boolean} transferInfo.isPrivate The flag to indicate if the data transferred
  996. * targeted Peer peers and not broadcasted to all Peer peers.
  997. * @param {JSON} [error] The error object thrown when there is a failure in transferring data.
  998. * @param {Object} error.message The exception thrown that caused the failure
  999. * for transferring data.
  1000. * @param {String} error.transferType The data transfer type to indicate if the DataChannel is
  1001. * uploading or downloading the data transfer when the exception occurred.
  1002. * [Rel: Skylink.DATA_TRANSFER_TYPE]
  1003. * @component Events
  1004. * @for Skylink
  1005. * @since 0.4.1
  1006. */
  1007. dataTransferState: [],
  1008.  
  1009. /**
  1010. * Event triggered when Skylink receives an system action from the platform signaling.
  1011. * @event systemAction
  1012. * @param {String} action The system action that is received from the platform signaling.
  1013. * [Rel: Skylink.SYSTEM_ACTION]
  1014. * @param {String} message The message received from the platform signaling when
  1015. * the system action and reason is given.
  1016. * @param {String} reason The reason received from the platform signaling behind the
  1017. * system action given.
  1018. * [Rel: Skylink.SYSTEM_ACTION_REASON]
  1019. * @component Events
  1020. * @for Skylink
  1021. * @since 0.5.1
  1022. */
  1023. systemAction: [],
  1024.  
  1025. /**
  1026. * Event triggered when a server Peer joins the room.
  1027. * @event serverPeerJoined
  1028. * @param {String} peerId The Peer ID of the new server peer
  1029. * that has joined the room.
  1030. * @param {String} serverPeerType The server Peer type
  1031. * [Rel: Skylink.SERVER_PEER_TYPE]
  1032. * @component Events
  1033. * @for Skylink
  1034. * @since 0.6.1
  1035. */
  1036. serverPeerJoined: [],
  1037.  
  1038. /**
  1039. * Event triggered when a server Peer leaves the room.
  1040. * @event serverPeerLeft
  1041. * @param {String} peerId The Peer ID of the new server peer
  1042. * that has left the room.
  1043. * @param {String} serverPeerType The server Peer type
  1044. * [Rel: Skylink.SERVER_PEER_TYPE]
  1045. * @component Events
  1046. * @for Skylink
  1047. * @since 0.6.1
  1048. */
  1049. serverPeerLeft: [],
  1050.  
  1051. /**
  1052. * Event triggered when a sever Peer connection has been restarted for
  1053. * a reconnection.
  1054. * @event serverPeerRestart
  1055. * @param {String} peerId The Peer ID of the new server peer
  1056. * that has joined the room.
  1057. * @param {String} serverPeerType The server Peer type
  1058. * [Rel: Skylink.SERVER_PEER_TYPE]
  1059. * @component Events
  1060. * @for Skylink
  1061. * @since 0.6.1
  1062. */
  1063. serverPeerRestart: [],
  1064.  
  1065. /**
  1066. * Event triggered when a Peer connection Stream streaming has stopped.
  1067. * @event streamEnded
  1068. * @param {String} [peerId=null] The Peer ID associated to the Stream object.
  1069. * If self is not in the room, the value returned would be <code>null</code>.
  1070. * @param {Object} peerInfo The peer information associated
  1071. * with the Peer Connection.
  1072. * @param {String|JSON} peerInfo.userData The custom user data
  1073. * information set by developer. This custom user data can also
  1074. * be set in <a href="#method_setUserData">setUserData()</a>.
  1075. * @param {JSON} peerInfo.settings The Peer Stream
  1076. * streaming settings information. If both audio and video
  1077. * option is <code>false</code>, there should be no
  1078. * receiving remote Stream object from this associated Peer.
  1079. * @param {Boolean|JSON} [peerInfo.settings.audio=false] The
  1080. * Peer Stream streaming audio settings. If
  1081. * <code>false</code>, it means that audio streaming is disabled in
  1082. * the remote Stream of the Peer.
  1083. * @param {Boolean} [peerInfo.settings.audio.stereo] The flag that indicates if
  1084. * stereo option should be explictly enabled to an OPUS enabled audio stream.
  1085. * Check the <code>audioCodec</code> configuration settings in
  1086. * <a href="#method_init">init()</a>
  1087. * to enable OPUS as the audio codec. Note that stereo is already enabled
  1088. * for OPUS codecs, this only adds a stereo flag to the SDP to explictly
  1089. * enable stereo in the audio streaming.
  1090. * @param {Boolean|JSON} [peerInfo.settings.video=false] The Peer
  1091. * Stream streaming video settings. If <code>false</code>, it means that
  1092. * video streaming is disabled in the remote Stream of the Peer.
  1093. * @param {JSON} [peerInfo.settings.video.resolution] The Peer
  1094. * Stream streaming video resolution settings. Setting the resolution may
  1095. * not force set the resolution provided as it depends on the how the
  1096. * browser handles the resolution. [Rel: Skylink.VIDEO_RESOLUTION]
  1097. * @param {Number} [peerInfo.settings.video.resolution.width] The Peer
  1098. * Stream streaming video resolution width.
  1099. * @param {Number} [peerInfo.settings.video.resolution.height] The Peer
  1100. * Stream streaming video resolution height.
  1101. * @param {Number} [peerInfo.settings.video.frameRate] The Peer
  1102. * Stream streaming video maximum frameRate.
  1103. * @param {Boolean} [peerInfo.settings.video.screenshare=false] The flag
  1104. * that indicates if the Peer connection Stream object sent
  1105. * is a screensharing stream or not.
  1106. * @param {String} [peerInfo.settings.bandwidth] The Peer
  1107. * streaming bandwidth settings. Setting the bandwidth flags may not
  1108. * force set the bandwidth for each connection stream channels as it depends
  1109. * on how the browser handles the bandwidth bitrate. Values are configured
  1110. * in <var>kb/s</var>.
  1111. * @param {String} [peerInfo.settings.bandwidth.audio] The configured
  1112. * audio stream channel for the remote Stream object bandwidth
  1113. * that audio streaming should use in <var>kb/s</var>.
  1114. * @param {String} [peerInfo.settings.bandwidth.video] The configured
  1115. * video stream channel for the remote Stream object bandwidth
  1116. * that video streaming should use in <var>kb/s</var>.
  1117. * @param {String} [peerInfo.settings.bandwidth.data] The configured
  1118. * datachannel channel for the DataChannel connection bandwidth
  1119. * that datachannel connection per packet should be able use in <var>kb/s</var>.
  1120. * @param {JSON} peerInfo.mediaStatus The Peer Stream mute
  1121. * settings for both audio and video streamings.
  1122. * @param {Boolean} [peerInfo.mediaStatus.audioMuted=true] The flag that
  1123. * indicates if the remote Stream object audio streaming is muted. If
  1124. * there is no audio streaming enabled for the Peer, by default,
  1125. * it is set to <code>true</code>.
  1126. * @param {Boolean} [peerInfo.mediaStatus.videoMuted=true] The flag that
  1127. * indicates if the remote Stream object video streaming is muted. If
  1128. * there is no video streaming enabled for the Peer, by default,
  1129. * it is set to <code>true</code>.
  1130. * @param {JSON} peerInfo.agent The Peer platform agent information.
  1131. * @param {String} peerInfo.agent.name The Peer platform browser or agent name.
  1132. * @param {Number} peerInfo.agent.version The Peer platform browser or agent version.
  1133. * @param {Number} peerInfo.agent.os The Peer platform name.
  1134. * @param {String} peerInfo.room The current room that the Peer is in.
  1135. * @param {Boolean} isSelf The flag that indicates if self is the Peer.
  1136. * @param {Boolean} isScreensharing The flag that indicates if Peer connection
  1137. * Stream object is a screensharing stream or not.
  1138. * @component Events
  1139. * @for Skylink
  1140. * @since 0.5.10
  1141. */
  1142. streamEnded: [],
  1143.  
  1144. /**
  1145. * Event triggered when a Peer connection Stream streaming audio or video
  1146. * stream muted status have been updated.
  1147. * @event streamMuted
  1148. * @param {String} peerId The Peer ID associated to the Stream object.
  1149. * If self is not in the room, the value returned would be <code>null</code>.
  1150. * @param {Object} peerInfo The peer information associated
  1151. * with the Peer Connection.
  1152. * @param {String|JSON} peerInfo.userData The custom user data
  1153. * information set by developer. This custom user data can also
  1154. * be set in <a href="#method_setUserData">setUserData()</a>.
  1155. * @param {JSON} peerInfo.settings The Peer Stream
  1156. * streaming settings information. If both audio and video
  1157. * option is <code>false</code>, there should be no
  1158. * receiving remote Stream object from this associated Peer.
  1159. * @param {Boolean|JSON} [peerInfo.settings.audio=false] The
  1160. * Peer Stream streaming audio settings. If
  1161. * <code>false</code>, it means that audio streaming is disabled in
  1162. * the remote Stream of the Peer.
  1163. * @param {Boolean} [peerInfo.settings.audio.stereo] The flag that indicates if
  1164. * stereo option should be explictly enabled to an OPUS enabled audio stream.
  1165. * Check the <code>audioCodec</code> configuration settings in
  1166. * <a href="#method_init">init()</a>
  1167. * to enable OPUS as the audio codec. Note that stereo is already enabled
  1168. * for OPUS codecs, this only adds a stereo flag to the SDP to explictly
  1169. * enable stereo in the audio streaming.
  1170. * @param {Boolean|JSON} [peerInfo.settings.video=false] The Peer
  1171. * Stream streaming video settings. If <code>false</code>, it means that
  1172. * video streaming is disabled in the remote Stream of the Peer.
  1173. * @param {JSON} [peerInfo.settings.video.resolution] The Peer
  1174. * Stream streaming video resolution settings. Setting the resolution may
  1175. * not force set the resolution provided as it depends on the how the
  1176. * browser handles the resolution. [Rel: Skylink.VIDEO_RESOLUTION]
  1177. * @param {Number} [peerInfo.settings.video.resolution.width] The Peer
  1178. * Stream streaming video resolution width.
  1179. * @param {Number} [peerInfo.settings.video.resolution.height] The Peer
  1180. * Stream streaming video resolution height.
  1181. * @param {Number} [peerInfo.settings.video.frameRate] The Peer
  1182. * Stream streaming video maximum frameRate.
  1183. * @param {Boolean} [peerInfo.settings.video.screenshare=false] The flag
  1184. * that indicates if the Peer connection Stream object sent
  1185. * is a screensharing stream or not.
  1186. * @param {String} [peerInfo.settings.bandwidth] The Peer
  1187. * streaming bandwidth settings. Setting the bandwidth flags may not
  1188. * force set the bandwidth for each connection stream channels as it depends
  1189. * on how the browser handles the bandwidth bitrate. Values are configured
  1190. * in <var>kb/s</var>.
  1191. * @param {String} [peerInfo.settings.bandwidth.audio] The configured
  1192. * audio stream channel for the remote Stream object bandwidth
  1193. * that audio streaming should use in <var>kb/s</var>.
  1194. * @param {String} [peerInfo.settings.bandwidth.video] The configured
  1195. * video stream channel for the remote Stream object bandwidth
  1196. * that video streaming should use in <var>kb/s</var>.
  1197. * @param {String} [peerInfo.settings.bandwidth.data] The configured
  1198. * datachannel channel for the DataChannel connection bandwidth
  1199. * that datachannel connection per packet should be able use in <var>kb/s</var>.
  1200. * @param {JSON} peerInfo.mediaStatus The Peer Stream mute
  1201. * settings for both audio and video streamings.
  1202. * @param {Boolean} [peerInfo.mediaStatus.audioMuted=true] The flag that
  1203. * indicates if the remote Stream object audio streaming is muted. If
  1204. * there is no audio streaming enabled for the Peer, by default,
  1205. * it is set to <code>true</code>.
  1206. * @param {Boolean} [peerInfo.mediaStatus.videoMuted=true] The flag that
  1207. * indicates if the remote Stream object video streaming is muted. If
  1208. * there is no video streaming enabled for the Peer, by default,
  1209. * it is set to <code>true</code>.
  1210. * @param {JSON} peerInfo.agent The Peer platform agent information.
  1211. * @param {String} peerInfo.agent.name The Peer platform browser or agent name.
  1212. * @param {Number} peerInfo.agent.version The Peer platform browser or agent version.
  1213. * @param {Number} peerInfo.agent.os The Peer platform name.
  1214. * @param {String} peerInfo.room The current room that the Peer is in.
  1215. * @param {Boolean} isSelf The flag that indicates if self is the Peer.
  1216. * @param {Boolean} isScreensharing The flag that indicates if Peer connection
  1217. * Stream object is a screensharing stream or not.
  1218. * @component Events
  1219. * @for Skylink
  1220. * @since 0.6.1
  1221. */
  1222. streamMuted: [],
  1223.  
  1224. /**
  1225. * Event triggered when the retrieval of the list of rooms and peers under the same realm based
  1226. * on the Application Key configured in <a href="#method_init">init()</a>
  1227. * from the platform signaling state has changed.
  1228. * - This requires that the provided alias Application Key has privileged feature configured.
  1229. * @event getPeersStateChange
  1230. * @param {String} state The retrieval current status.
  1231. * @param {String} privilegedPeerId The Peer ID of the privileged Peer.
  1232. * @param {JSON} peerList The retrieved list of rooms and peers under the same realm based on
  1233. * the Application Key configured in <code>init()</code>.
  1234. * @component Events
  1235. * @for Skylink
  1236. * @since 0.6.1
  1237. */
  1238. getPeersStateChange: [],
  1239.  
  1240. /**
  1241. * Event triggered when introductory state of two Peer peers to each other
  1242. * selected by the privileged Peer state has changed.
  1243. * - This requires that the provided alias Application Key has privileged feature configured.
  1244. * @event introduceStateChange
  1245. * @param {String} state The Peer introduction state.
  1246. * @param {String} privilegedPeerId The Peer ID of the privileged Peer.
  1247. * @param {String} sendingPeerId The Peer ID of the peer
  1248. * that initiates the connection with the introduced Peer.
  1249. * @param {String} receivingPeerId The Peer ID of the
  1250. * introduced peer who would be introduced to the initiator Peer.
  1251. * @param {String} reason The error object thrown when there is a failure in
  1252. * the introduction with the two Peer peers.
  1253. * If received as <code>null</code>, it means that there is no errors.
  1254. * @component Events
  1255. * @for Skylink
  1256. * @since 0.6.1
  1257. */
  1258. introduceStateChange: []
  1259. };
  1260.  
  1261. /**
  1262. * Stores the list of {{#crossLink "Skylink/once:method"}}once(){{/crossLink}}
  1263. * event subscription handlers.
  1264. * @attribute _onceEvents
  1265. * @param {Array} (#eventName) The array of event subscription handlers that is
  1266. * subscribed using {{#crossLink "Skylink/once:method"}}once() method{{/crossLink}}
  1267. * associated with the event name.
  1268. * @param {Function} (#eventName).(#index) The event subscription handler
  1269. * associated with the event name. This is to be triggered once when condition is met.
  1270. * Alternatively, the <code>once()</code> event subscription handler can be
  1271. * unsubscribed with {{#crossLink "Skylink/off:method"}}off(){{/crossLink}} before
  1272. * condition is met.
  1273. * @type JSON
  1274. * @private
  1275. * @required
  1276. * @component Events
  1277. * @for Skylink
  1278. * @since 0.5.4
  1279. */
  1280. Skylink.prototype._onceEvents = {};
  1281.  
  1282. /**
  1283. * The throttling function datetime stamp in
  1284. * [(ISO 8601 format)](https://en.wikipedia.org/wiki/ISO_8601).
  1285. * @attribute _timestamp
  1286. * @type JSON
  1287. * @private
  1288. * @required
  1289. * @component Events
  1290. * @for Skylink
  1291. * @since 0.5.8
  1292. */
  1293. Skylink.prototype._timestamp = {
  1294. now: Date.now() || function() { return +new Date(); },
  1295. screen: false
  1296. };
  1297.  
  1298. /**
  1299. * Triggers event subscription handlers that is associated with the event name.
  1300. * {{#crossLink "Skylink/on:method"}}on() event subscription handlers{{/crossLink}}
  1301. * will be triggered always, but
  1302. * {{#crossLink "Skylink/once:method"}}once() event subscription hadlers{{/crossLink}}
  1303. * will only be triggered once the condition is met.
  1304. * @method _trigger
  1305. * @param {String} eventName The Skylink event name to trigger that would trigger event subscription
  1306. * handlers associated to the event name with the <code>arguments</code> parameters payload.
  1307. * @for Skylink
  1308. * @private
  1309. * @component Events
  1310. * @for Skylink
  1311. * @since 0.1.0
  1312. */
  1313. Skylink.prototype._trigger = function(eventName) {
  1314. //convert the arguments into an array
  1315. var args = Array.prototype.slice.call(arguments);
  1316. var arr = this._EVENTS[eventName];
  1317. var once = this._onceEvents[eventName] || null;
  1318. args.shift(); //Omit the first argument since it's the event name
  1319. if (arr) {
  1320. // for events subscribed forever
  1321. for (var i = 0; i < arr.length; i++) {
  1322. try {
  1323. log.log([null, 'Event', eventName, 'Event is fired']);
  1324. if(arr[i].apply(this, args) === false) {
  1325. break;
  1326. }
  1327. } catch(error) {
  1328. log.error([null, 'Event', eventName, 'Exception occurred in event:'], error);
  1329. throw error;
  1330. }
  1331. }
  1332. }
  1333. if (once){
  1334. // for events subscribed on once
  1335. for (var j = 0; j < once.length; j++) {
  1336. if (once[j][1].apply(this, args) === true) {
  1337. log.log([null, 'Event', eventName, 'Condition is met. Firing event']);
  1338. if(once[j][0].apply(this, args) === false) {
  1339. break;
  1340. }
  1341. if (!once[j][2]) {
  1342. log.log([null, 'Event', eventName, 'Removing event after firing once']);
  1343. once.splice(j, 1);
  1344. //After removing current element, the next element should be element of the same index
  1345. j--;
  1346. }
  1347. } else {
  1348. log.log([null, 'Event', eventName, 'Condition is still not met. ' +
  1349. 'Holding event from being fired']);
  1350. }
  1351. }
  1352. }
  1353.  
  1354. log.log([null, 'Event', eventName, 'Event is triggered']);
  1355. };
  1356.  
  1357. /**
  1358. * Subscribes an event handler associated to the event name.
  1359. * This event handler will always be triggered when the event name is triggered. If you
  1360. * are looking for subscription event handler to be triggered once, check out
  1361. * {{#crossLink "Skylink/once:method"}}once() event subscription{{/crossLink}}.
  1362. * @method on
  1363. * @param {String} eventName The Skylink event name to subscribe to.
  1364. * @param {Function} callback The event handler to subsribe to the associated
  1365. * Skylink event name that would be triggered once the event name is triggered.
  1366. * @example
  1367. * SkylinkDemo.on("peerJoined", function (peerId, peerInfo) {
  1368. * alert(peerId + " has joined the room");
  1369. * });
  1370. * @component Events
  1371. * @for Skylink
  1372. * @since 0.1.0
  1373. */
  1374. Skylink.prototype.on = function(eventName, callback) {
  1375. if ('function' === typeof callback) {
  1376. this._EVENTS[eventName] = this._EVENTS[eventName] || [];
  1377. this._EVENTS[eventName].push(callback);
  1378. log.log([null, 'Event', eventName, 'Event is subscribed']);
  1379. } else {
  1380. log.error([null, 'Event', eventName, 'Provided parameter is not a function']);
  1381. }
  1382. };
  1383.  
  1384. /**
  1385. * Subscribes an event handler associated to the event name that
  1386. * would only be triggered once the provided condition function has been met.
  1387. * @method once
  1388. * @param {String} eventName The Skylink event name to subscribe to.
  1389. * @param {Function} callback The event handler to subscribe to the associated
  1390. * Skylink event name to trigger once the condition has met. If
  1391. * <code>fireAlways</code> option is set toe <code>true</code>, this will
  1392. * always be fired when condition is met.
  1393. * @param {Function} [condition] The condition function that once the condition has
  1394. * been met, trigger the event handler once. Return in the condition function <code>true</code>
  1395. * to pass as meeting the condition.
  1396. * If the condition function is not provided, the event handler will be triggered
  1397. * once the Skylink event name is triggered.
  1398. * @param {Boolean} [fireAlways=false] The flag that indicates if Skylink should interrupt this
  1399. * <code>once()</code> function once the function has been triggered to not unsubscribe the
  1400. * event handler but to always trigger when the condition has been met.
  1401. * @example
  1402. * SkylinkDemo.once("peerConnectionState", function (state, peerId) {
  1403. * alert("Peer has left");
  1404. * }, function (state, peerId) {
  1405. * return state === SkylinkDemo.PEER_CONNECTION_STATE.CLOSED;
  1406. * });
  1407. * @component Events
  1408. * @for Skylink
  1409. * @since 0.5.4
  1410. */
  1411. Skylink.prototype.once = function(eventName, callback, condition, fireAlways) {
  1412. if (typeof condition === 'boolean') {
  1413. fireAlways = condition;
  1414. condition = null;
  1415. }
  1416. fireAlways = (typeof fireAlways === 'undefined' ? false : fireAlways);
  1417. condition = (typeof condition !== 'function') ? function () {
  1418. return true;
  1419. } : condition;
  1420.  
  1421. if (typeof callback === 'function') {
  1422.  
  1423. this._EVENTS[eventName] = this._EVENTS[eventName] || [];
  1424. // prevent undefined error
  1425. this._onceEvents[eventName] = this._onceEvents[eventName] || [];
  1426. this._onceEvents[eventName].push([callback, condition, fireAlways]);
  1427. log.log([null, 'Event', eventName, 'Event is subscribed on condition']);
  1428. } else {
  1429. log.error([null, 'Event', eventName, 'Provided callback is not a function']);
  1430. }
  1431. };
  1432.  
  1433. /**
  1434. * Unsubscribes an event handler associated to the event name.
  1435. * @method off
  1436. * @param {String} eventName The Skylink event name to unsubscribe to.
  1437. * @param {Function} [callback] The event handler to unsubscribe to the associated
  1438. * Skylink event name. If the event handler is not provided, Skylink would
  1439. * unsubscribe all event handlers subscribed to the associated event name.
  1440. * @example
  1441. * // Example 1: Unsubscribe all event handlers related to the event
  1442. * SkylinkDemo.off("peerJoined");
  1443. *
  1444. * // Example 2: Unsubscribe to one event handler
  1445. * SkylinkDemo.off("peerJoined", callback);
  1446. * @component Events
  1447. * @for Skylink
  1448. * @since 0.5.5
  1449. */
  1450. Skylink.prototype.off = function(eventName, callback) {
  1451. if (callback === undefined) {
  1452. this._EVENTS[eventName] = [];
  1453. this._onceEvents[eventName] = [];
  1454. log.log([null, 'Event', eventName, 'All events are unsubscribed']);
  1455. return;
  1456. }
  1457. var arr = this._EVENTS[eventName];
  1458. var once = this._onceEvents[eventName];
  1459.  
  1460. // unsubscribe events that is triggered always
  1461. for (var i = 0; i < arr.length; i++) {
  1462. if (arr[i] === callback) {
  1463. log.log([null, 'Event', eventName, 'Event is unsubscribed']);
  1464. arr.splice(i, 1);
  1465. break;
  1466. }
  1467. }
  1468. // unsubscribe events fired only once
  1469. if(once !== undefined) {
  1470. for (var j = 0; j < once.length; j++) {
  1471. if (once[j][0] === callback) {
  1472. log.log([null, 'Event', eventName, 'One-time Event is unsubscribed']);
  1473. once.splice(j, 1);
  1474. break;
  1475. }
  1476. }
  1477. }
  1478. };
  1479.  
  1480. /**
  1481. * Checks if the first condition is already met before doing an event
  1482. * handler subscription to wait for the second condition to be met.
  1483. * This method will do a event subscription with
  1484. * {{#crossLink "Skylink/once:method"}}once(){{/crossLink}} as this
  1485. * <code>_condition()</code> would only trigger once, unless <code>fireAlways</code>
  1486. * is set to <code>true</code>.
  1487. * @method _condition
  1488. * @param {String} eventName The Skylink event name to subscribe to.
  1489. * @param {Function} callback The event handler to subscribe to the associated
  1490. * Skylink event name to trigger once the condition has met. If
  1491. * <code>fireAlways</code> option is set to <code>true</code>, this will
  1492. * always be fired when condition is met.
  1493. * @param {Function} [checkFirst] The first condition to check before
  1494. * doing an event subscription to wait for second condition to meet.
  1495. * Return in the first condition function <code>true</code> to pass as meeting the condition.
  1496. * If the first condition is met, the event handler would be triggered
  1497. * and the event handler will not be subscribed to the event or wait
  1498. * for second condition to pass.
  1499. * @param {Function} [condition] The second condition function that once the it has
  1500. * been met, it will trigger the event handler once.
  1501. * Return in the second condition function <code>true</code> to pass as meeting the condition.
  1502. * If the second condition is met, the event handler would be triggered and
  1503. * depending if <code>fireAlways</code> option is set to <code>true</code>, this will
  1504. * always be fired when condition is met.
  1505. * @param {Boolean} [fireAlways=false] The flag that indicates if Skylink should interrupt the
  1506. * second condition function once the function has been triggered to not unsubscribe the
  1507. * event handler but to always trigger when the second condition has been met.
  1508. * @private
  1509. * @component Events
  1510. * @for Skylink
  1511. * @since 0.5.5
  1512. */
  1513. Skylink.prototype._condition = function(eventName, callback, checkFirst, condition, fireAlways) {
  1514. if (typeof condition === 'boolean') {
  1515. fireAlways = condition;
  1516. condition = null;
  1517. }
  1518. if (typeof callback === 'function' && typeof checkFirst === 'function') {
  1519. if (checkFirst()) {
  1520. log.log([null, 'Event', eventName, 'First condition is met. Firing callback']);
  1521. callback();
  1522. return;
  1523. }
  1524. log.log([null, 'Event', eventName, 'First condition is not met. Subscribing to event']);
  1525. this.once(eventName, callback, condition, fireAlways);
  1526. } else {
  1527. log.error([null, 'Event', eventName, 'Provided callback or checkFirst is not a function']);
  1528. }
  1529. };
  1530.  
  1531. /**
  1532. * Starts the interval check for the condition provided to meet before clearing
  1533. * the interval and triggering the callback provided.
  1534. * This utilises <code>setInterval()</code> function.
  1535. * @method _wait
  1536. * @param {Function} callback The callback fired after the condition provided
  1537. * has been met.
  1538. * @param {Function} condition The condition function that once the condition has
  1539. * been met, trigger the callback. Return in the condition function <code>true</code>
  1540. * to pass as meeting the condition.
  1541. * @param {Number} [intervalTime=50] The interval loop timeout that the interval
  1542. * check should iterate based on the timeout provided (in ms).
  1543. * By default, if the value is not configured, it is <code>50</code>ms.
  1544. * @for Skylink
  1545. * @private
  1546. * @component Events
  1547. * @for Skylink
  1548. * @since 0.5.5
  1549. */
  1550. Skylink.prototype._wait = function(callback, condition, intervalTime, fireAlways) {
  1551. fireAlways = (typeof fireAlways === 'undefined' ? false : fireAlways);
  1552. if (typeof callback === 'function' && typeof condition === 'function') {
  1553. if (condition()) {
  1554. log.log([null, 'Event', null, 'Condition is met. Firing callback']);
  1555. callback();
  1556. return;
  1557. }
  1558. log.log([null, 'Event', null, 'Condition is not met. Doing a check.']);
  1559.  
  1560. intervalTime = (typeof intervalTime === 'number') ? intervalTime : 50;
  1561.  
  1562. var doWait = setInterval(function () {
  1563. if (condition()) {
  1564. log.log([null, 'Event', null, 'Condition is met after waiting. Firing callback']);
  1565. if (!fireAlways){
  1566. clearInterval(doWait);
  1567. }
  1568. callback();
  1569. }
  1570. }, intervalTime);
  1571. } else {
  1572. if (typeof callback !== 'function'){
  1573. log.error([null, 'Event', null, 'Provided callback is not a function']);
  1574. }
  1575. if (typeof condition !== 'function'){
  1576. log.error([null, 'Event', null, 'Provided condition is not a function']);
  1577. }
  1578. }
  1579. };
  1580.  
  1581. /**
  1582. * Returns a wrapper of the original function, which fires only once during
  1583. * a specified amount of time.
  1584. * @method _throttle
  1585. * @param {Function} func The function that should be throttled.
  1586. * @param {Number} wait The amount of time that function need to throttled (in ms).
  1587. * @return {Function} The throttled function.
  1588. * @private
  1589. * @component Events
  1590. * @for Skylink
  1591. * @since 0.5.8
  1592. */
  1593. Skylink.prototype._throttle = function(func, wait){
  1594. var self = this;
  1595. return function () {
  1596. if (!self._timestamp.func){
  1597. //First time run, need to force timestamp to skip condition
  1598. self._timestamp.func = self._timestamp.now - wait;
  1599. }
  1600. var now = Date.now();
  1601. if (now - self._timestamp.func < wait) {
  1602. return;
  1603. }
  1604. func.apply(self, arguments);
  1605. self._timestamp.func = now;
  1606. };
  1607. };