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

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