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.

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