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.

strophe.jingle.sessionbase.js 6.3KB

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