您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RTC.js 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. var desktopsharing = require("../desktopsharing/desktopsharing");
  15. function RTC(room, options) {
  16. this.room = room;
  17. this.localStreams = [];
  18. this.remoteStreams = {};
  19. this.localAudio = null;
  20. this.localVideo = null;
  21. this.eventEmitter = new EventEmitter();
  22. var self = this;
  23. this.options = options || {};
  24. desktopsharing.addListener(
  25. function (stream, isUsingScreenStream, callback) {
  26. self.changeLocalVideo(stream, isUsingScreenStream, callback);
  27. }, DesktopSharingEventTypes.NEW_STREAM_CREATED);
  28. room.addPresenceListener("videomuted", function (values, from) {
  29. if(self.remoteStreams[from])
  30. self.remoteStreams[from][JitsiTrack.VIDEO].setMute(values.value == "true");
  31. });
  32. room.addPresenceListener("audiomuted", function (values, from) {
  33. if(self.remoteStreams[from])
  34. self.remoteStreams[from][JitsiTrack.AUDIO].setMute(values.value == "true");
  35. });
  36. }
  37. /**
  38. * Creates the local MediaStreams.
  39. * @param {Object} [options] optional parameters
  40. * @param {Array} options.devices the devices that will be requested
  41. * @param {string} options.resolution resolution constraints
  42. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  43. * type: "audio" or "video", videoType: "camera" or "desktop"}
  44. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  45. * @param {string} options.cameraDeviceId
  46. * @param {string} options.micDeviceId
  47. * @returns {*} Promise object that will receive the new JitsiTracks
  48. */
  49. RTC.obtainAudioAndVideoPermissions = function (options) {
  50. return RTCUtils.obtainAudioAndVideoPermissions(options);
  51. }
  52. RTC.prototype.onIncommingCall = function(event) {
  53. if(this.options.config.openSctp)
  54. this.dataChannels = new DataChannels(event.peerconnection, this.eventEmitter);
  55. for(var i = 0; i < this.localStreams.length; i++)
  56. if(this.localStreams[i])
  57. {
  58. this.room.addStream(this.localStreams[i].getOriginalStream(), function () {});
  59. }
  60. }
  61. RTC.prototype.selectedEndpoint = function (id) {
  62. if(this.dataChannels)
  63. this.dataChannels.handleSelectedEndpointEvent(id);
  64. }
  65. RTC.prototype.pinEndpoint = function (id) {
  66. if(this.dataChannels)
  67. this.dataChannels.handlePinnedEndpointEvent(id);
  68. }
  69. RTC.prototype.addListener = function (type, listener) {
  70. this.eventEmitter.on(type, listener);
  71. };
  72. RTC.prototype.removeListener = function (eventType, listener) {
  73. this.eventEmitter.removeListener(eventType, listener);
  74. };
  75. RTC.addListener = function (eventType, listener) {
  76. RTCUtils.addListener(eventType, listener);
  77. }
  78. RTC.removeListener = function (eventType, listener) {
  79. RTCUtils.eventEmitter.removeListener(RTCEvents.RTC_READY, listener);
  80. RTCUtils.removeListener(eventType, listener)
  81. }
  82. RTC.isRTCReady = function () {
  83. return RTCUtils.isRTCReady();
  84. }
  85. RTC.init = function (options) {
  86. RTCUtils.init(options || {});
  87. }
  88. RTC.getDeviceAvailability = function () {
  89. return RTCUtils.getDeviceAvailability();
  90. }
  91. RTC.prototype.addLocalStream = function (stream) {
  92. this.localStreams.push(stream);
  93. stream._setRTC(this);
  94. if (stream.type == "audio") {
  95. this.localAudio = stream;
  96. } else {
  97. this.localVideo = stream;
  98. }
  99. };
  100. RTC.prototype.removeLocalStream = function (stream) {
  101. for(var i = 0; i < this.localStreams.length; i++) {
  102. if(this.localStreams[i].getOriginalStream() === stream) {
  103. delete this.localStreams[i];
  104. return;
  105. }
  106. }
  107. };
  108. RTC.prototype.createRemoteStream = function (data, sid, thessrc) {
  109. var remoteStream = new JitsiRemoteTrack(this, data, sid, thessrc,
  110. this.eventEmitter);
  111. if(!data.peerjid)
  112. return;
  113. var jid = data.peerjid;
  114. if(!this.remoteStreams[jid]) {
  115. this.remoteStreams[jid] = {};
  116. }
  117. this.remoteStreams[jid][remoteStream.type]= remoteStream;
  118. this.eventEmitter.emit(StreamEventTypes.EVENT_TYPE_REMOTE_CREATED, remoteStream);
  119. return remoteStream;
  120. };
  121. RTC.getPCConstraints = function () {
  122. return RTCUtils.pc_constraints;
  123. };
  124. RTC.getUserMediaWithConstraints = function(um, success_callback,
  125. failure_callback, options)
  126. {
  127. return RTCUtils.getUserMediaWithConstraints(this, um, success_callback,
  128. failure_callback, options);
  129. };
  130. RTC.attachMediaStream = function (elSelector, stream) {
  131. RTCUtils.attachMediaStream(elSelector, stream);
  132. };
  133. RTC.getStreamID = function (stream) {
  134. return RTCUtils.getStreamID(stream);
  135. };
  136. RTC.getVideoSrc = function (element) {
  137. return RTCUtils.getVideoSrc(element);
  138. };
  139. RTC.isDeviceListAvailable = function () {
  140. return RTCUtils.isDeviceListAvailable();
  141. };
  142. /**
  143. * Allows to receive list of available cameras/microphones.
  144. * @param {function} callback would receive array of devices as an argument
  145. */
  146. RTC.enumerateDevices = function (callback) {
  147. RTCUtils.enumerateDevices(callback);
  148. };
  149. RTC.setVideoSrc = function (element, src) {
  150. RTCUtils.setVideoSrc(element, src);
  151. };
  152. RTC.prototype.getVideoElementName = function () {
  153. return RTCBrowserType.isTemasysPluginUsed() ? 'object' : 'video';
  154. };
  155. RTC.prototype.dispose = function() {
  156. };
  157. RTC.prototype.muteRemoteVideoStream = function (jid, value) {
  158. var stream;
  159. if(this.remoteStreams[jid] &&
  160. this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE]) {
  161. stream = this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
  162. }
  163. if(!stream)
  164. return true;
  165. if (value != stream.muted) {
  166. stream.setMute(value);
  167. return true;
  168. }
  169. return false;
  170. };
  171. RTC.prototype.switchVideoStreams = function (new_stream) {
  172. this.localVideo.stream = new_stream;
  173. this.localStreams = [];
  174. //in firefox we have only one stream object
  175. if (this.localAudio.getOriginalStream() != new_stream)
  176. this.localStreams.push(this.localAudio);
  177. this.localStreams.push(this.localVideo);
  178. };
  179. /**
  180. * Creates <tt>JitsiTrack</tt> instance and replaces it with the local video.
  181. * The method also handles the sdp changes.
  182. * @param stream the new MediaStream received by the browser.
  183. * @param isUsingScreenStream <tt>true</tt> if the stream is for desktop stream.
  184. * @param callback - function that will be called after the operation is completed.
  185. */
  186. RTC.prototype.changeLocalVideo = function (stream, isUsingScreenStream, callback) {
  187. var oldStream = this.localVideo.getOriginalStream();
  188. var type = (isUsingScreenStream ? "screen" : "camera");
  189. var localCallback = callback;
  190. if(this.localVideo.isMuted() && this.localVideo.videoType !== type) {
  191. localCallback = function() {
  192. this.room.setVideoMute(false, function(mute) {
  193. this.eventEmitter.emit(RTCEvents.VIDEO_MUTE, mute);
  194. }.bind(this));
  195. callback();
  196. };
  197. }
  198. // FIXME: Workaround for FF/IE/Safari
  199. if (stream && stream.videoStream) {
  200. stream = stream.videoStream;
  201. }
  202. var videoStream = RTCUtils.createStream(stream, true);
  203. this.localVideo = this.createLocalStream(videoStream, "video", true, type);
  204. // Stop the stream to trigger onended event for old stream
  205. oldStream.stop();
  206. this.switchVideoStreams(videoStream, oldStream);
  207. this.room.switchStreams(videoStream, oldStream,localCallback);
  208. };
  209. /**
  210. * Creates <tt>JitsiTrack</tt> instance and replaces it with the local audio.
  211. * The method also handles the sdp changes.
  212. * @param stream the new MediaStream received by the browser.
  213. * @param callback - function that will be called after the operation is completed.
  214. */
  215. RTC.prototype.changeLocalAudio = function (stream, callback) {
  216. var oldStream = this.localAudio.getOriginalStream();
  217. var newStream = RTCUtils.createStream(stream);
  218. this.localAudio = this.createLocalStream(newStream, "audio", true);
  219. // Stop the stream to trigger onended event for old stream
  220. oldStream.stop();
  221. this.room.switchStreams(newStream, oldStream, callback, true);
  222. };
  223. RTC.prototype.isVideoMuted = function (jid) {
  224. if (jid === APP.xmpp.myJid()) {
  225. var localVideo = APP.RTC.localVideo;
  226. return (!localVideo || localVideo.isMuted());
  227. } else {
  228. if (!this.remoteStreams[jid] ||
  229. !this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE]) {
  230. return null;
  231. }
  232. return this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE].muted;
  233. }
  234. };
  235. RTC.prototype.setVideoMute = function (mute, callback, options) {
  236. if (!this.localVideo)
  237. return;
  238. if (mute == this.localVideo.isMuted())
  239. {
  240. APP.xmpp.sendVideoInfoPresence(mute);
  241. if (callback)
  242. callback(mute);
  243. }
  244. else
  245. {
  246. this.localVideo.setMute(mute);
  247. this.room.setVideoMute(
  248. mute,
  249. callback,
  250. options);
  251. }
  252. };
  253. module.exports = RTC;