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 8.4KB

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