You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

data_channels.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* global connection, Strophe, updateLargeVideo, focusedVideoSrc*/
  2. // cache datachannels to avoid garbage collection
  3. // https://code.google.com/p/chromium/issues/detail?id=405545
  4. var _dataChannels = [];
  5. /**
  6. * Callback triggered by PeerConnection when new data channel is opened
  7. * on the bridge.
  8. * @param event the event info object.
  9. */
  10. function onDataChannel(event)
  11. {
  12. var dataChannel = event.channel;
  13. dataChannel.onopen = function ()
  14. {
  15. console.info("Data channel opened by the Videobridge!", dataChannel);
  16. // Code sample for sending string and/or binary data
  17. // Sends String message to the bridge
  18. //dataChannel.send("Hello bridge!");
  19. // Sends 12 bytes binary message to the bridge
  20. //dataChannel.send(new ArrayBuffer(12));
  21. };
  22. dataChannel.onerror = function (error)
  23. {
  24. console.error("Data Channel Error:", error, dataChannel);
  25. };
  26. dataChannel.onmessage = function (event)
  27. {
  28. var data = event.data;
  29. // JSON
  30. var obj;
  31. try
  32. {
  33. obj = JSON.parse(data);
  34. }
  35. catch (e)
  36. {
  37. console.error(
  38. "Failed to parse data channel message as JSON: ",
  39. data,
  40. dataChannel);
  41. }
  42. if (('undefined' !== typeof(obj)) && (null !== obj))
  43. {
  44. var colibriClass = obj.colibriClass;
  45. if ("DominantSpeakerEndpointChangeEvent" === colibriClass)
  46. {
  47. // Endpoint ID from the Videobridge.
  48. var dominantSpeakerEndpoint = obj.dominantSpeakerEndpoint;
  49. console.info(
  50. "Data channel new dominant speaker event: ",
  51. dominantSpeakerEndpoint);
  52. $(document).trigger(
  53. 'dominantspeakerchanged',
  54. [dominantSpeakerEndpoint]);
  55. }
  56. else if ("LastNEndpointsChangeEvent" === colibriClass)
  57. {
  58. // The new/latest list of last-n endpoint IDs.
  59. var lastNEndpoints = obj.lastNEndpoints;
  60. /*
  61. * The list of endpoint IDs which are entering the list of
  62. * last-n at this time i.e. were not in the old list of last-n
  63. * endpoint IDs.
  64. */
  65. var endpointsEnteringLastN = obj.endpointsEnteringLastN;
  66. console.debug(
  67. "Data channel new last-n event: ",
  68. lastNEndpoints);
  69. }
  70. else
  71. {
  72. console.debug("Data channel JSON-formatted message: ", obj);
  73. }
  74. }
  75. };
  76. dataChannel.onclose = function ()
  77. {
  78. console.info("The Data Channel closed", dataChannel);
  79. var idx = _dataChannels.indexOf(dataChannel);
  80. if (idx > -1)
  81. _dataChannels = _dataChannels.splice(idx, 1);
  82. };
  83. _dataChannels.push(dataChannel);
  84. }
  85. /**
  86. * Binds "ondatachannel" event listener to given PeerConnection instance.
  87. * @param peerConnection WebRTC peer connection instance.
  88. */
  89. function bindDataChannelListener(peerConnection)
  90. {
  91. peerConnection.ondatachannel = onDataChannel;
  92. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  93. // Although it's not a requirement to open separate channels from both bridge
  94. // and peer as single channel can be used for sending and receiving data.
  95. // So either channel opened by the bridge or the one opened here is enough
  96. // for communication with the bridge.
  97. /*var dataChannelOptions =
  98. {
  99. reliable: true
  100. };
  101. var dataChannel
  102. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  103. // Can be used only when is in open state
  104. dataChannel.onopen = function ()
  105. {
  106. dataChannel.send("My channel !!!");
  107. };
  108. dataChannel.onmessage = function (event)
  109. {
  110. var msgData = event.data;
  111. console.info("Got My Data Channel Message:", msgData, dataChannel);
  112. };*/
  113. }