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

RTC.js 8.2KB

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