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.4KB

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