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.

RTC.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* global APP */
  2. var EventEmitter = require("events");
  3. var RTCBrowserType = require("./RTCBrowserType");
  4. var RTCUtils = require("./RTCUtils.js");
  5. var JitsiTrack = require("./JitsiTrack");
  6. var JitsiLocalTrack = require("./JitsiLocalTrack.js");
  7. var DataChannels = require("./DataChannels");
  8. var JitsiRemoteTrack = require("./JitsiRemoteTrack.js");
  9. var DesktopSharingEventTypes
  10. = require("../../service/desktopsharing/DesktopSharingEventTypes");
  11. var MediaStreamType = require("../../service/RTC/MediaStreamTypes");
  12. var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
  13. var RTCEvents = require("../../service/RTC/RTCEvents.js");
  14. function createLocalTracks(streams) {
  15. var newStreams = []
  16. for (var i = 0; i < streams.length; i++) {
  17. var localStream = new JitsiLocalTrack(streams[i].stream,
  18. streams[i].videoType, streams[i].resolution);
  19. newStreams.push(localStream);
  20. if (streams[i].isMuted === true)
  21. localStream.setMute(true);
  22. //FIXME:
  23. // var eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CREATED;
  24. //
  25. // eventEmitter.emit(eventType, localStream);
  26. }
  27. return newStreams;
  28. }
  29. function RTC(room, options) {
  30. this.room = room;
  31. this.localStreams = [];
  32. this.remoteStreams = {};
  33. this.localAudio = null;
  34. this.localVideo = null;
  35. this.eventEmitter = new EventEmitter();
  36. var self = this;
  37. this.options = options || {};
  38. room.addPresenceListener("videomuted", function (values, from) {
  39. if(self.remoteStreams[from])
  40. self.remoteStreams[from][JitsiTrack.VIDEO].setMute(values.value == "true");
  41. });
  42. room.addPresenceListener("audiomuted", function (values, from) {
  43. if(self.remoteStreams[from])
  44. self.remoteStreams[from][JitsiTrack.AUDIO].setMute(values.value == "true");
  45. });
  46. }
  47. /**
  48. * Creates the local MediaStreams.
  49. * @param {Object} [options] optional parameters
  50. * @param {Array} options.devices the devices that will be requested
  51. * @param {string} options.resolution resolution constraints
  52. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  53. * type: "audio" or "video", videoType: "camera" or "desktop"}
  54. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  55. * @param {string} options.cameraDeviceId
  56. * @param {string} options.micDeviceId
  57. * @returns {*} Promise object that will receive the new JitsiTracks
  58. */
  59. RTC.obtainAudioAndVideoPermissions = function (options) {
  60. return RTCUtils.obtainAudioAndVideoPermissions(options).then(createLocalTracks);
  61. }
  62. RTC.prototype.onIncommingCall = function(event) {
  63. if(this.options.config.openSctp)
  64. this.dataChannels = new DataChannels(event.peerconnection, this.eventEmitter);
  65. for(var i = 0; i < this.localStreams.length; i++)
  66. if(this.localStreams[i])
  67. {
  68. this.room.addStream(this.localStreams[i].getOriginalStream(), function () {});
  69. }
  70. }
  71. RTC.prototype.selectedEndpoint = function (id) {
  72. if(this.dataChannels)
  73. this.dataChannels.handleSelectedEndpointEvent(id);
  74. }
  75. RTC.prototype.pinEndpoint = function (id) {
  76. if(this.dataChannels)
  77. this.dataChannels.handlePinnedEndpointEvent(id);
  78. }
  79. RTC.prototype.addListener = function (type, listener) {
  80. this.eventEmitter.on(type, listener);
  81. };
  82. RTC.prototype.removeListener = function (eventType, listener) {
  83. this.eventEmitter.removeListener(eventType, listener);
  84. };
  85. RTC.addListener = function (eventType, listener) {
  86. RTCUtils.addListener(eventType, listener);
  87. }
  88. RTC.removeListener = function (eventType, listener) {
  89. RTCUtils.removeListener(eventType, listener)
  90. }
  91. RTC.isRTCReady = function () {
  92. return RTCUtils.isRTCReady();
  93. }
  94. RTC.init = function (options) {
  95. return RTCUtils.init(options || {});
  96. }
  97. RTC.getDeviceAvailability = function () {
  98. return RTCUtils.getDeviceAvailability();
  99. }
  100. RTC.prototype.addLocalStream = function (stream) {
  101. this.localStreams.push(stream);
  102. stream._setRTC(this);
  103. if (stream.type == "audio") {
  104. this.localAudio = stream;
  105. } else {
  106. this.localVideo = stream;
  107. }
  108. };
  109. RTC.prototype.removeLocalStream = function (stream) {
  110. for(var i = 0; i < this.localStreams.length; i++) {
  111. if(this.localStreams[i].getOriginalStream() === stream) {
  112. delete this.localStreams[i];
  113. return;
  114. }
  115. }
  116. };
  117. RTC.prototype.createRemoteStream = function (data, sid, thessrc) {
  118. var remoteStream = new JitsiRemoteTrack(this, data, sid, thessrc,
  119. this.eventEmitter);
  120. if(!data.peerjid)
  121. return;
  122. var jid = data.peerjid;
  123. if(!this.remoteStreams[jid]) {
  124. this.remoteStreams[jid] = {};
  125. }
  126. this.remoteStreams[jid][remoteStream.type]= remoteStream;
  127. this.eventEmitter.emit(StreamEventTypes.EVENT_TYPE_REMOTE_CREATED, remoteStream);
  128. return remoteStream;
  129. };
  130. RTC.getPCConstraints = function () {
  131. return RTCUtils.pc_constraints;
  132. };
  133. RTC.attachMediaStream = function (elSelector, stream) {
  134. RTCUtils.attachMediaStream(elSelector, stream);
  135. };
  136. RTC.getStreamID = function (stream) {
  137. return RTCUtils.getStreamID(stream);
  138. };
  139. RTC.getVideoSrc = function (element) {
  140. return RTCUtils.getVideoSrc(element);
  141. };
  142. RTC.isDeviceListAvailable = function () {
  143. return RTCUtils.isDeviceListAvailable();
  144. };
  145. /**
  146. * Allows to receive list of available cameras/microphones.
  147. * @param {function} callback would receive array of devices as an argument
  148. */
  149. RTC.enumerateDevices = function (callback) {
  150. RTCUtils.enumerateDevices(callback);
  151. };
  152. RTC.setVideoSrc = function (element, src) {
  153. RTCUtils.setVideoSrc(element, src);
  154. };
  155. /**
  156. * A method to handle stopping of the stream.
  157. * One point to handle the differences in various implementations.
  158. * @param mediaStream MediaStream object to stop.
  159. */
  160. RTC.stopMediaStream = function (mediaStream) {
  161. RTCUtils.stopMediaStream(mediaStream);
  162. };
  163. RTC.prototype.getVideoElementName = function () {
  164. return RTCBrowserType.isTemasysPluginUsed() ? 'object' : 'video';
  165. };
  166. RTC.prototype.dispose = function() {
  167. };
  168. RTC.prototype.muteRemoteVideoStream = function (jid, value) {
  169. var stream;
  170. if(this.remoteStreams[jid] &&
  171. this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE]) {
  172. stream = this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
  173. }
  174. if(!stream)
  175. return true;
  176. if (value != stream.muted) {
  177. stream.setMute(value);
  178. return true;
  179. }
  180. return false;
  181. };
  182. RTC.prototype.switchVideoStreams = function (newStream) {
  183. this.localVideo.stream = newStream;
  184. this.localStreams = [];
  185. //in firefox we have only one stream object
  186. if (this.localAudio.getOriginalStream() != newStream)
  187. this.localStreams.push(this.localAudio);
  188. this.localStreams.push(this.localVideo);
  189. };
  190. RTC.prototype.isVideoMuted = function (jid) {
  191. if (jid === APP.xmpp.myJid()) {
  192. var localVideo = APP.RTC.localVideo;
  193. return (!localVideo || localVideo.isMuted());
  194. } else {
  195. if (!this.remoteStreams[jid] ||
  196. !this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE]) {
  197. return null;
  198. }
  199. return this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE].muted;
  200. }
  201. };
  202. RTC.prototype.setVideoMute = function (mute, callback, options) {
  203. if (!this.localVideo)
  204. return;
  205. if (mute == this.localVideo.isMuted())
  206. {
  207. APP.xmpp.sendVideoInfoPresence(mute);
  208. if (callback)
  209. callback(mute);
  210. }
  211. else
  212. {
  213. this.localVideo.setMute(mute);
  214. this.room.setVideoMute(
  215. mute,
  216. callback,
  217. options);
  218. }
  219. };
  220. module.exports = RTC;