Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RTC.js 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* global APP */
  2. var EventEmitter = require("events");
  3. var RTCBrowserType = require("./RTCBrowserType");
  4. var RTCUtils = require("./RTCUtils.js");
  5. var JitsiLocalTrack = require("./JitsiLocalTrack.js");
  6. var DataChannels = require("./DataChannels");
  7. var JitsiRemoteTrack = require("./JitsiRemoteTrack.js");
  8. var DesktopSharingEventTypes
  9. = require("../../service/desktopsharing/DesktopSharingEventTypes");
  10. var MediaStreamType = require("../../service/RTC/MediaStreamTypes");
  11. var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
  12. var RTCEvents = require("../../service/RTC/RTCEvents.js");
  13. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  14. var desktopsharing = require("../desktopsharing/desktopsharing");
  15. function getMediaStreamUsage()
  16. {
  17. var result = {
  18. audio: true,
  19. video: true
  20. };
  21. /** There are some issues with the desktop sharing
  22. * when this property is enabled.
  23. * WARNING: We must change the implementation to start video/audio if we
  24. * receive from the focus that the peer is not muted.
  25. var isSecureConnection = window.location.protocol == "https:";
  26. if(config.disableEarlyMediaPermissionRequests || !isSecureConnection)
  27. {
  28. result = {
  29. audio: false,
  30. video: false
  31. };
  32. }
  33. **/
  34. return result;
  35. }
  36. var rtcReady = false;
  37. function RTC(room, options) {
  38. this.devices = {
  39. audio: true,
  40. video: true
  41. };
  42. this.room = room;
  43. this.localStreams = [];
  44. this.remoteStreams = {};
  45. this.localAudio = null;
  46. this.localVideo = null;
  47. this.eventEmitter = new EventEmitter();
  48. var self = this;
  49. this.options = options || {};
  50. desktopsharing.addListener(
  51. function (stream, isUsingScreenStream, callback) {
  52. self.changeLocalVideo(stream, isUsingScreenStream, callback);
  53. }, DesktopSharingEventTypes.NEW_STREAM_CREATED);
  54. }
  55. RTC.prototype.obtainAudioAndVideoPermissions = function (options) {
  56. return RTCUtils.obtainAudioAndVideoPermissions(this,
  57. options.devices, getMediaStreamUsage(), options.resolution);
  58. }
  59. RTC.prototype.onIncommingCall = function(event) {
  60. if(this.options.config.openSctp)
  61. this.dataChannels = new DataChannels(event.peerconnection, this.eventEmitter);
  62. this.room.addLocalStreams(this.localStreams);
  63. }
  64. RTC.prototype.selectedEndpoint = function (id) {
  65. if(this.dataChannels)
  66. this.dataChannels.handleSelectedEndpointEvent(id);
  67. }
  68. RTC.prototype.pinEndpoint = function (id) {
  69. if(this.dataChannels)
  70. this.dataChannels.handlePinnedEndpointEvent(id);
  71. }
  72. RTC.prototype.addStreamListener = function (listener, eventType) {
  73. this.eventEmitter.on(eventType, listener);
  74. };
  75. RTC.prototype.addListener = function (type, listener) {
  76. this.eventEmitter.on(type, listener);
  77. };
  78. RTC.prototype.removeListener = function (listener, eventType) {
  79. this.eventEmitter.removeListener(eventType, listener);
  80. };
  81. RTC.prototype.removeStreamListener = function (listener, eventType) {
  82. if(!(eventType instanceof StreamEventTypes))
  83. throw "Illegal argument";
  84. this.eventEmitter.removeListener(eventType, listener);
  85. };
  86. RTC.addRTCReadyListener = function (listener) {
  87. RTCUtils.eventEmitter.on(RTCEvents.RTC_READY, listener);
  88. }
  89. RTC.removeRTCReadyListener = function (listener) {
  90. RTCUtils.eventEmitter.removeListener(RTCEvents.RTC_READY, listener);
  91. }
  92. RTC.isRTCReady = function () {
  93. return rtcReady;
  94. }
  95. RTC.init = function (options) {
  96. // In case of IE we continue from 'onReady' callback
  97. // passed to RTCUtils constructor. It will be invoked by Temasys plugin
  98. // once it is initialized.
  99. var onReady = function () {
  100. rtcReady = true;
  101. RTCUtils.eventEmitter.emit(RTCEvents.RTC_READY, true);
  102. };
  103. RTCUtils.init(onReady, options || {});
  104. // Call onReady() if Temasys plugin is not used
  105. if (!RTCBrowserType.isTemasysPluginUsed()) {
  106. onReady();
  107. }
  108. }
  109. RTC.prototype.createLocalStreams = function (streams, change) {
  110. for (var i = 0; i < streams.length; i++) {
  111. var localStream = new JitsiLocalTrack(this, streams[i].stream,
  112. this.eventEmitter, streams[i].videoType,
  113. streams[i].isGUMStream);
  114. this.localStreams.push(localStream);
  115. if (streams[i].isMuted === true)
  116. localStream.setMute(true);
  117. if (streams[i].type == "audio") {
  118. this.localAudio = localStream;
  119. } else {
  120. this.localVideo = localStream;
  121. }
  122. var eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CREATED;
  123. if (change)
  124. eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CHANGED;
  125. this.eventEmitter.emit(eventType, localStream, streams[i].isMuted);
  126. }
  127. return this.localStreams;
  128. };
  129. RTC.prototype.removeLocalStream = function (stream) {
  130. for(var i = 0; i < this.localStreams.length; i++) {
  131. if(this.localStreams[i].getOriginalStream() === stream) {
  132. delete this.localStreams[i];
  133. return;
  134. }
  135. }
  136. };
  137. RTC.prototype.createRemoteStream = function (data, sid, thessrc) {
  138. var remoteStream = new JitsiRemoteTrack(this, data, sid, thessrc,
  139. RTCBrowserType.getBrowserType(), this.eventEmitter);
  140. if(!data.peerjid)
  141. return;
  142. var jid = data.peerjid;
  143. if(!this.remoteStreams[jid]) {
  144. this.remoteStreams[jid] = {};
  145. }
  146. this.remoteStreams[jid][remoteStream.type]= remoteStream;
  147. this.eventEmitter.emit(StreamEventTypes.EVENT_TYPE_REMOTE_CREATED, remoteStream);
  148. return remoteStream;
  149. };
  150. RTC.getPCConstraints = function () {
  151. return RTCUtils.pc_constraints;
  152. };
  153. RTC.prototype.getUserMediaWithConstraints = function(um, success_callback,
  154. failure_callback, resolution,
  155. bandwidth, fps, desktopStream)
  156. {
  157. return RTCUtils.getUserMediaWithConstraints(this, um, success_callback,
  158. failure_callback, resolution, bandwidth, fps, desktopStream);
  159. };
  160. RTC.attachMediaStream = function (elSelector, stream) {
  161. RTCUtils.attachMediaStream(elSelector, stream);
  162. };
  163. RTC.getStreamID = function (stream) {
  164. return RTCUtils.getStreamID(stream);
  165. };
  166. RTC.getVideoSrc = function (element) {
  167. return RTCUtils.getVideoSrc(element);
  168. };
  169. RTC.setVideoSrc = function (element, src) {
  170. RTCUtils.setVideoSrc(element, src);
  171. };
  172. RTC.prototype.getVideoElementName = function () {
  173. return RTCBrowserType.isTemasysPluginUsed() ? 'object' : 'video';
  174. };
  175. RTC.prototype.dispose = function() {
  176. };
  177. RTC.prototype.muteRemoteVideoStream = function (jid, value) {
  178. var stream;
  179. if(this.remoteStreams[jid] &&
  180. this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE]) {
  181. stream = this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
  182. }
  183. if(!stream)
  184. return true;
  185. if (value != stream.muted) {
  186. stream.setMute(value);
  187. return true;
  188. }
  189. return false;
  190. };
  191. RTC.prototype.switchVideoStreams = function (new_stream) {
  192. this.localVideo.stream = new_stream;
  193. this.localStreams = [];
  194. //in firefox we have only one stream object
  195. if (this.localAudio.getOriginalStream() != new_stream)
  196. this.localStreams.push(this.localAudio);
  197. this.localStreams.push(this.localVideo);
  198. };
  199. RTC.prototype.changeLocalVideo = function (stream, isUsingScreenStream, callback) {
  200. var oldStream = this.localVideo.getOriginalStream();
  201. var type = (isUsingScreenStream ? "screen" : "camera");
  202. var localCallback = callback;
  203. if(this.localVideo.isMuted() && this.localVideo.videoType !== type) {
  204. localCallback = function() {
  205. this.room.setVideoMute(false, function(mute) {
  206. this.eventEmitter.emit(RTCEvents.VIDEO_MUTE, mute);
  207. }.bind(this));
  208. callback();
  209. };
  210. }
  211. // FIXME: Workaround for FF/IE/Safari
  212. if (stream && stream.videoStream) {
  213. stream = stream.videoStream;
  214. }
  215. var videoStream = RTCUtils.createStream(stream, true);
  216. this.localVideo = this.createLocalStream(videoStream, "video", true, type);
  217. // Stop the stream to trigger onended event for old stream
  218. oldStream.stop();
  219. this.switchVideoStreams(videoStream, oldStream);
  220. this.room.switchStreams(videoStream, oldStream,localCallback);
  221. };
  222. RTC.prototype.changeLocalAudio = function (stream, callback) {
  223. var oldStream = this.localAudio.getOriginalStream();
  224. var newStream = RTCUtils.createStream(stream);
  225. this.localAudio = this.createLocalStream(newStream, "audio", true);
  226. // Stop the stream to trigger onended event for old stream
  227. oldStream.stop();
  228. this.room.switchStreams(newStream, oldStream, callback, true);
  229. };
  230. RTC.prototype.isVideoMuted = function (jid) {
  231. if (jid === APP.xmpp.myJid()) {
  232. var localVideo = APP.RTC.localVideo;
  233. return (!localVideo || localVideo.isMuted());
  234. } else {
  235. if (!this.remoteStreams[jid] ||
  236. !this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE]) {
  237. return null;
  238. }
  239. return this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE].muted;
  240. }
  241. };
  242. RTC.prototype.setVideoMute = function (mute, callback, options) {
  243. if (!this.localVideo)
  244. return;
  245. if (mute == this.localVideo.isMuted())
  246. {
  247. APP.xmpp.sendVideoInfoPresence(mute);
  248. if (callback)
  249. callback(mute);
  250. }
  251. else
  252. {
  253. this.localVideo.setMute(mute);
  254. this.room.setVideoMute(
  255. mute,
  256. callback,
  257. options);
  258. }
  259. };
  260. RTC.prototype.setDeviceAvailability = function (devices) {
  261. if(!devices)
  262. return;
  263. if(devices.audio === true || devices.audio === false)
  264. this.devices.audio = devices.audio;
  265. if(devices.video === true || devices.video === false)
  266. this.devices.video = devices.video;
  267. this.eventEmitter.emit(RTCEvents.AVAILABLE_DEVICES_CHANGED, this.devices);
  268. };
  269. module.exports = RTC;