Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

strophe.jingle.sessionbase.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. self.connection.jingle.localStreams.push(self.connection.jingle.localAudio);
  77. self.connection.jingle.localStreams.push(self.connection.jingle.localVideo);
  78. // Conference is not active
  79. if(!oldSdp || !self.peerconnection) {
  80. success_callback();
  81. return;
  82. }
  83. self.peerconnection.switchstreams = true;
  84. self.modifySources(function() {
  85. console.log('modify sources done');
  86. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  87. console.log("SDPs", oldSdp, newSdp);
  88. self.notifyMySSRCUpdate(oldSdp, newSdp);
  89. success_callback();
  90. });
  91. };
  92. /**
  93. * Figures out added/removed ssrcs and send update IQs.
  94. * @param old_sdp SDP object for old description.
  95. * @param new_sdp SDP object for new description.
  96. */
  97. SessionBase.prototype.notifyMySSRCUpdate = function (old_sdp, new_sdp) {
  98. var old_media = old_sdp.getMediaSsrcMap();
  99. var new_media = new_sdp.getMediaSsrcMap();
  100. //console.log("old/new medias: ", old_media, new_media);
  101. var toAdd = old_sdp.getNewMedia(new_sdp);
  102. var toRemove = new_sdp.getNewMedia(old_sdp);
  103. //console.log("to add", toAdd);
  104. //console.log("to remove", toRemove);
  105. if(Object.keys(toRemove).length > 0){
  106. this.sendSSRCUpdate(toRemove, null, false);
  107. }
  108. if(Object.keys(toAdd).length > 0){
  109. this.sendSSRCUpdate(toAdd, null, true);
  110. }
  111. };
  112. /**
  113. * Empty method that does nothing by default. It should send SSRC update IQs to session participants.
  114. * @param sdpMediaSsrcs array of
  115. * @param fromJid
  116. * @param isAdd
  117. */
  118. SessionBase.prototype.sendSSRCUpdate = function(sdpMediaSsrcs, fromJid, isAdd) {
  119. //FIXME: put default implementation here(maybe from JingleSession?)
  120. }
  121. /**
  122. * Sends SSRC update IQ.
  123. * @param sdpMediaSsrcs SSRCs map obtained from SDP.getNewMedia. Cntains SSRCs to add/remove.
  124. * @param sid session identifier that will be put into the IQ.
  125. * @param initiator initiator identifier.
  126. * @param toJid destination Jid
  127. * @param isAdd indicates if this is remove or add operation.
  128. */
  129. SessionBase.prototype.sendSSRCUpdateIq = function(sdpMediaSsrcs, sid, initiator, toJid, isAdd) {
  130. var self = this;
  131. var modify = $iq({to: toJid, type: 'set'})
  132. .c('jingle', {
  133. xmlns: 'urn:xmpp:jingle:1',
  134. action: isAdd ? 'source-add' : 'source-remove',
  135. initiator: initiator,
  136. sid: sid
  137. }
  138. );
  139. // FIXME: only announce video ssrcs since we mix audio and dont need
  140. // the audio ssrcs therefore
  141. var modified = false;
  142. Object.keys(sdpMediaSsrcs).forEach(function(channelNum){
  143. modified = true;
  144. var channel = sdpMediaSsrcs[channelNum];
  145. modify.c('content', {name: channel.mediaType});
  146. modify.c('description', {xmlns:'urn:xmpp:jingle:apps:rtp:1', media: channel.mediaType});
  147. // FIXME: not completly sure this operates on blocks and / or handles different ssrcs correctly
  148. // generate sources from lines
  149. Object.keys(channel.ssrcs).forEach(function(ssrcNum) {
  150. var mediaSsrc = channel.ssrcs[ssrcNum];
  151. modify.c('source', { xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  152. modify.attrs({ssrc: mediaSsrc.ssrc});
  153. // iterate over ssrc lines
  154. mediaSsrc.lines.forEach(function (line) {
  155. var idx = line.indexOf(' ');
  156. var kv = line.substr(idx + 1);
  157. modify.c('parameter');
  158. if (kv.indexOf(':') == -1) {
  159. modify.attrs({ name: kv });
  160. } else {
  161. modify.attrs({ name: kv.split(':', 2)[0] });
  162. modify.attrs({ value: kv.split(':', 2)[1] });
  163. }
  164. modify.up(); // end of parameter
  165. });
  166. modify.up(); // end of source
  167. });
  168. // generate source groups from lines
  169. channel.ssrcGroups.forEach(function(ssrcGroup) {
  170. if (ssrcGroup.ssrcs.length != 0) {
  171. modify.c('ssrc-group', {
  172. semantics: ssrcGroup.semantics,
  173. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0'
  174. });
  175. ssrcGroup.ssrcs.forEach(function (ssrc) {
  176. modify.c('source', { ssrc: ssrc })
  177. .up(); // end of source
  178. });
  179. modify.up(); // end of ssrc-group
  180. }
  181. });
  182. modify.up(); // end of description
  183. modify.up(); // end of content
  184. });
  185. if (modified) {
  186. self.connection.sendIQ(modify,
  187. function (res) {
  188. console.info('got modify result', res);
  189. },
  190. function (err) {
  191. console.error('got modify error', err);
  192. }
  193. );
  194. } else {
  195. console.log('modification not necessary');
  196. }
  197. };
  198. // SDP-based mute by going recvonly/sendrecv
  199. // FIXME: should probably black out the screen as well
  200. SessionBase.prototype.toggleVideoMute = function (callback) {
  201. var ismuted = false;
  202. var localVideo = connection.jingle.localVideo;
  203. for (var idx = 0; idx < localVideo.getVideoTracks().length; idx++) {
  204. ismuted = !localVideo.getVideoTracks()[idx].enabled;
  205. }
  206. for (var idx = 0; idx < localVideo.getVideoTracks().length; idx++) {
  207. localVideo.getVideoTracks()[idx].enabled = !localVideo.getVideoTracks()[idx].enabled;
  208. }
  209. if(this.peerconnection)
  210. this.peerconnection.hardMuteVideo(!ismuted);
  211. this.modifySources(callback(!ismuted));
  212. };