Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

strophe.jingle.sessionbase.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * Base class for ColibriFocus and JingleSession.
  3. * @param connection Strophe connection object
  4. * @param sid my session identifier(resource)
  5. * @constructor
  6. */
  7. function SessionBase(connection, sid){
  8. this.connection = connection;
  9. this.sid = sid;
  10. }
  11. SessionBase.prototype.modifySources = function (successCallback) {
  12. var self = this;
  13. if(this.peerconnection)
  14. this.peerconnection.modifySources(function(){
  15. $(document).trigger('setLocalDescription.jingle', [self.sid]);
  16. if(successCallback) {
  17. successCallback();
  18. }
  19. });
  20. };
  21. SessionBase.prototype.addSource = function (elem, fromJid) {
  22. var self = this;
  23. // FIXME: dirty waiting
  24. if (!this.peerconnection.localDescription)
  25. {
  26. console.warn("addSource - localDescription not ready yet")
  27. setTimeout(function()
  28. {
  29. self.addSource(elem, fromJid);
  30. },
  31. 200
  32. );
  33. return;
  34. }
  35. this.peerconnection.addSource(elem);
  36. this.modifySources();
  37. };
  38. SessionBase.prototype.removeSource = function (elem, fromJid) {
  39. var self = this;
  40. // FIXME: dirty waiting
  41. if (!this.peerconnection.localDescription)
  42. {
  43. console.warn("removeSource - localDescription not ready yet")
  44. setTimeout(function()
  45. {
  46. self.removeSource(elem, fromJid);
  47. },
  48. 200
  49. );
  50. return;
  51. }
  52. this.peerconnection.removeSource(elem);
  53. this.modifySources();
  54. };
  55. /**
  56. * Switches video streams.
  57. * @param new_stream new stream that will be used as video of this session.
  58. * @param oldStream old video stream of this session.
  59. * @param success_callback callback executed after successful stream switch.
  60. */
  61. SessionBase.prototype.switchStreams = function (new_stream, oldStream, success_callback) {
  62. var self = this;
  63. // Stop the stream to trigger onended event for old stream
  64. oldStream.stop();
  65. // Remember SDP to figure out added/removed SSRCs
  66. var oldSdp = null;
  67. if(self.peerconnection) {
  68. if(self.peerconnection.localDescription) {
  69. oldSdp = new SDP(self.peerconnection.localDescription.sdp);
  70. }
  71. self.peerconnection.removeStream(oldStream);
  72. self.peerconnection.addStream(new_stream);
  73. }
  74. self.connection.jingle.localVideo = new_stream;
  75. self.connection.jingle.localStreams = [];
  76. if(RTC.browser == "chrome")
  77. self.connection.jingle.localStreams.push(self.connection.jingle.localAudio);
  78. self.connection.jingle.localStreams.push(self.connection.jingle.localVideo);
  79. // Conference is not active
  80. if(!oldSdp || !self.peerconnection) {
  81. success_callback();
  82. return;
  83. }
  84. self.peerconnection.switchstreams = true;
  85. self.modifySources(function() {
  86. console.log('modify sources done');
  87. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  88. console.log("SDPs", oldSdp, newSdp);
  89. self.notifyMySSRCUpdate(oldSdp, newSdp);
  90. success_callback();
  91. });
  92. };
  93. /**
  94. * Figures out added/removed ssrcs and send update IQs.
  95. * @param old_sdp SDP object for old description.
  96. * @param new_sdp SDP object for new description.
  97. */
  98. SessionBase.prototype.notifyMySSRCUpdate = function (old_sdp, new_sdp) {
  99. var old_media = old_sdp.getMediaSsrcMap();
  100. var new_media = new_sdp.getMediaSsrcMap();
  101. //console.log("old/new medias: ", old_media, new_media);
  102. var toAdd = old_sdp.getNewMedia(new_sdp);
  103. var toRemove = new_sdp.getNewMedia(old_sdp);
  104. //console.log("to add", toAdd);
  105. //console.log("to remove", toRemove);
  106. if(Object.keys(toRemove).length > 0){
  107. this.sendSSRCUpdate(toRemove, null, false);
  108. }
  109. if(Object.keys(toAdd).length > 0){
  110. this.sendSSRCUpdate(toAdd, null, true);
  111. }
  112. };
  113. /**
  114. * Empty method that does nothing by default. It should send SSRC update IQs to session participants.
  115. * @param sdpMediaSsrcs array of
  116. * @param fromJid
  117. * @param isAdd
  118. */
  119. SessionBase.prototype.sendSSRCUpdate = function(sdpMediaSsrcs, fromJid, isAdd) {
  120. //FIXME: put default implementation here(maybe from JingleSession?)
  121. }
  122. /**
  123. * Sends SSRC update IQ.
  124. * @param sdpMediaSsrcs SSRCs map obtained from SDP.getNewMedia. Cntains SSRCs to add/remove.
  125. * @param sid session identifier that will be put into the IQ.
  126. * @param initiator initiator identifier.
  127. * @param toJid destination Jid
  128. * @param isAdd indicates if this is remove or add operation.
  129. */
  130. SessionBase.prototype.sendSSRCUpdateIq = function(sdpMediaSsrcs, sid, initiator, toJid, isAdd) {
  131. var self = this;
  132. var modify = $iq({to: toJid, type: 'set'})
  133. .c('jingle', {
  134. xmlns: 'urn:xmpp:jingle:1',
  135. action: isAdd ? 'source-add' : 'source-remove',
  136. initiator: initiator,
  137. sid: sid
  138. }
  139. );
  140. // FIXME: only announce video ssrcs since we mix audio and dont need
  141. // the audio ssrcs therefore
  142. var modified = false;
  143. Object.keys(sdpMediaSsrcs).forEach(function(channelNum){
  144. modified = true;
  145. var channel = sdpMediaSsrcs[channelNum];
  146. modify.c('content', {name: channel.mediaType});
  147. modify.c('description', {xmlns:'urn:xmpp:jingle:apps:rtp:1', media: channel.mediaType});
  148. // FIXME: not completly sure this operates on blocks and / or handles different ssrcs correctly
  149. // generate sources from lines
  150. Object.keys(channel.ssrcs).forEach(function(ssrcNum) {
  151. var mediaSsrc = channel.ssrcs[ssrcNum];
  152. modify.c('source', { xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  153. modify.attrs({ssrc: mediaSsrc.ssrc});
  154. // iterate over ssrc lines
  155. mediaSsrc.lines.forEach(function (line) {
  156. var idx = line.indexOf(' ');
  157. var kv = line.substr(idx + 1);
  158. modify.c('parameter');
  159. if (kv.indexOf(':') == -1) {
  160. modify.attrs({ name: kv });
  161. } else {
  162. modify.attrs({ name: kv.split(':', 2)[0] });
  163. modify.attrs({ value: kv.split(':', 2)[1] });
  164. }
  165. modify.up(); // end of parameter
  166. });
  167. modify.up(); // end of source
  168. });
  169. // generate source groups from lines
  170. channel.ssrcGroups.forEach(function(ssrcGroup) {
  171. if (ssrcGroup.ssrcs.length != 0) {
  172. modify.c('ssrc-group', {
  173. semantics: ssrcGroup.semantics,
  174. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0'
  175. });
  176. ssrcGroup.ssrcs.forEach(function (ssrc) {
  177. modify.c('source', { ssrc: ssrc })
  178. .up(); // end of source
  179. });
  180. modify.up(); // end of ssrc-group
  181. }
  182. });
  183. modify.up(); // end of description
  184. modify.up(); // end of content
  185. });
  186. if (modified) {
  187. self.connection.sendIQ(modify,
  188. function (res) {
  189. console.info('got modify result', res);
  190. },
  191. function (err) {
  192. console.error('got modify error', err);
  193. }
  194. );
  195. } else {
  196. console.log('modification not necessary');
  197. }
  198. };
  199. // SDP-based mute by going recvonly/sendrecv
  200. // FIXME: should probably black out the screen as well
  201. SessionBase.prototype.toggleVideoMute = function (callback) {
  202. var ismuted = false;
  203. var localVideo = connection.jingle.localVideo;
  204. for (var idx = 0; idx < localVideo.getVideoTracks().length; idx++) {
  205. ismuted = !localVideo.getVideoTracks()[idx].enabled;
  206. }
  207. for (var idx = 0; idx < localVideo.getVideoTracks().length; idx++) {
  208. localVideo.getVideoTracks()[idx].enabled = !localVideo.getVideoTracks()[idx].enabled;
  209. }
  210. if(this.peerconnection)
  211. this.peerconnection.hardMuteVideo(!ismuted);
  212. this.modifySources(callback(!ismuted));
  213. };