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

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