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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. self.remoteStreams[from][JitsiTrack.VIDEO].setMute(values.value == "true");
  38. }
  39. });
  40. room.addPresenceListener("audiomuted", function (values, from) {
  41. if(self.remoteStreams[from]) {
  42. self.remoteStreams[from][JitsiTrack.AUDIO].setMute(values.value == "true");
  43. }
  44. });
  45. }
  46. /**
  47. * Creates the local MediaStreams.
  48. * @param {Object} [options] optional parameters
  49. * @param {Array} options.devices the devices that will be requested
  50. * @param {string} options.resolution resolution constraints
  51. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  52. * type: "audio" or "video", videoType: "camera" or "desktop"}
  53. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  54. * @param {string} options.cameraDeviceId
  55. * @param {string} options.micDeviceId
  56. * @returns {*} Promise object that will receive the new JitsiTracks
  57. */
  58. RTC.obtainAudioAndVideoPermissions = function (options) {
  59. return RTCUtils.obtainAudioAndVideoPermissions(options).then(createLocalTracks);
  60. }
  61. RTC.prototype.onIncommingCall = function(event) {
  62. if(this.options.config.openSctp)
  63. this.dataChannels = new DataChannels(event.peerconnection, this.eventEmitter);
  64. for(var i = 0; i < this.localStreams.length; i++)
  65. if(this.localStreams[i])
  66. {
  67. this.room.addStream(this.localStreams[i].getOriginalStream(), function () {});
  68. }
  69. }
  70. RTC.prototype.selectedEndpoint = function (id) {
  71. if(this.dataChannels)
  72. this.dataChannels.handleSelectedEndpointEvent(id);
  73. }
  74. RTC.prototype.pinEndpoint = function (id) {
  75. if(this.dataChannels)
  76. this.dataChannels.handlePinnedEndpointEvent(id);
  77. }
  78. RTC.prototype.addListener = function (type, listener) {
  79. this.eventEmitter.on(type, listener);
  80. };
  81. RTC.prototype.removeListener = function (eventType, listener) {
  82. this.eventEmitter.removeListener(eventType, listener);
  83. };
  84. RTC.addListener = function (eventType, listener) {
  85. RTCUtils.addListener(eventType, listener);
  86. }
  87. RTC.removeListener = function (eventType, listener) {
  88. RTCUtils.removeListener(eventType, listener)
  89. }
  90. RTC.isRTCReady = function () {
  91. return RTCUtils.isRTCReady();
  92. }
  93. RTC.init = function (options) {
  94. this.options = options || {};
  95. return RTCUtils.init(this.options);
  96. }
  97. RTC.getDeviceAvailability = function () {
  98. return RTCUtils.getDeviceAvailability();
  99. }
  100. RTC.prototype.addLocalStream = function (stream) {
  101. this.localStreams.push(stream);
  102. stream._setRTC(this);
  103. if (stream.type == "audio") {
  104. this.localAudio = stream;
  105. } else {
  106. this.localVideo = stream;
  107. }
  108. };
  109. RTC.prototype.removeLocalStream = function (stream) {
  110. for(var i = 0; i < this.localStreams.length; i++) {
  111. if(this.localStreams[i].getOriginalStream() === stream) {
  112. delete this.localStreams[i];
  113. return;
  114. }
  115. }
  116. };
  117. RTC.prototype.createRemoteStream = function (data, sid, thessrc) {
  118. var remoteStream = new JitsiRemoteTrack(this, data, sid, thessrc);
  119. if(!data.peerjid)
  120. return;
  121. var jid = data.peerjid;
  122. if(!this.remoteStreams[jid]) {
  123. this.remoteStreams[jid] = {};
  124. }
  125. this.remoteStreams[jid][remoteStream.type]= remoteStream;
  126. return remoteStream;
  127. };
  128. RTC.getPCConstraints = function () {
  129. return RTCUtils.pc_constraints;
  130. };
  131. RTC.attachMediaStream = function (elSelector, stream) {
  132. RTCUtils.attachMediaStream(elSelector, stream);
  133. };
  134. RTC.getStreamID = function (stream) {
  135. return RTCUtils.getStreamID(stream);
  136. };
  137. RTC.getVideoSrc = function (element) {
  138. return RTCUtils.getVideoSrc(element);
  139. };
  140. /**
  141. * Returns true if retrieving the the list of input devices is supported and
  142. * false if not.
  143. */
  144. RTC.isDeviceListAvailable = function () {
  145. return RTCUtils.isDeviceListAvailable();
  146. };
  147. /**
  148. * Returns true if changing the camera / microphone device is supported and
  149. * false if not.
  150. */
  151. RTC.isDeviceChangeAvailable = function () {
  152. return RTCUtils.isDeviceChangeAvailable();
  153. }
  154. /**
  155. * Allows to receive list of available cameras/microphones.
  156. * @param {function} callback would receive array of devices as an argument
  157. */
  158. RTC.enumerateDevices = function (callback) {
  159. RTCUtils.enumerateDevices(callback);
  160. };
  161. RTC.setVideoSrc = function (element, src) {
  162. RTCUtils.setVideoSrc(element, src);
  163. };
  164. /**
  165. * A method to handle stopping of the stream.
  166. * One point to handle the differences in various implementations.
  167. * @param mediaStream MediaStream object to stop.
  168. */
  169. RTC.stopMediaStream = function (mediaStream) {
  170. RTCUtils.stopMediaStream(mediaStream);
  171. };
  172. /**
  173. * Returns whether the desktop sharing is enabled or not.
  174. * @returns {boolean}
  175. */
  176. RTC.isDesktopSharingEnabled = function () {
  177. return RTCUtils.isDesktopSharingEnabled();
  178. }
  179. RTC.prototype.getVideoElementName = function () {
  180. return RTCBrowserType.isTemasysPluginUsed() ? 'object' : 'video';
  181. };
  182. RTC.prototype.dispose = function() {
  183. };
  184. RTC.prototype.switchVideoStreams = function (newStream) {
  185. this.localVideo.stream = newStream;
  186. this.localStreams = [];
  187. //in firefox we have only one stream object
  188. if (this.localAudio.getOriginalStream() != newStream)
  189. this.localStreams.push(this.localAudio);
  190. this.localStreams.push(this.localVideo);
  191. };
  192. RTC.prototype.setAudioLevel = function (jid, audioLevel) {
  193. if(this.remoteStreams[jid] && this.remoteStreams[jid][JitsiTrack.AUDIO])
  194. this.remoteStreams[jid][JitsiTrack.AUDIO].setAudioLevel(audioLevel);
  195. }
  196. module.exports = RTC;