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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 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. function RTC(options)
  37. {
  38. this.devices = {
  39. audio: true,
  40. video: true
  41. };
  42. this.localStreams = [];
  43. this.remoteStreams = {};
  44. this.localAudio = null;
  45. this.localVideo = null;
  46. this.eventEmitter = new EventEmitter();
  47. var self = this;
  48. this.options = options || {};
  49. desktopsharing.addListener(
  50. function (stream, isUsingScreenStream, callback) {
  51. self.changeLocalVideo(stream, isUsingScreenStream, callback);
  52. }, DesktopSharingEventTypes.NEW_STREAM_CREATED);
  53. // In case of IE we continue from 'onReady' callback
  54. // passed to RTCUtils constructor. It will be invoked by Temasys plugin
  55. // once it is initialized.
  56. var onReady = function () {
  57. self.eventEmitter.emit(RTCEvents.RTC_READY, true);
  58. };
  59. RTCUtils.init(onReady);
  60. // Call onReady() if Temasys plugin is not used
  61. if (!RTCBrowserType.isTemasysPluginUsed()) {
  62. onReady();
  63. }
  64. }
  65. RTC.prototype.obtainAudioAndVideoPermissions = function (options) {
  66. return RTCUtils.obtainAudioAndVideoPermissions(this,
  67. options.devices, getMediaStreamUsage(), options.resolution);
  68. }
  69. RTC.prototype.onIncommingCall = function(event) {
  70. DataChannels.init(event.peerconnection, self.eventEmitter);
  71. }
  72. RTC.prototype.selectedEndpoint = function (id) {
  73. DataChannels.handleSelectedEndpointEvent(id);
  74. }
  75. RTC.prototype.pinEndpoint = function (id) {
  76. DataChannels.handlePinnedEndpointEvent(id);
  77. }
  78. RTC.prototype.addStreamListener = function (listener, eventType) {
  79. this.eventEmitter.on(eventType, listener);
  80. };
  81. RTC.prototype.addListener = function (type, listener) {
  82. this.eventEmitter.on(type, listener);
  83. };
  84. RTC.prototype.removeStreamListener = function (listener, eventType) {
  85. if(!(eventType instanceof StreamEventTypes))
  86. throw "Illegal argument";
  87. this.removeListener(eventType, listener);
  88. };
  89. RTC.prototype.createLocalStreams = function (streams, change) {
  90. for (var i = 0; i < streams.length; i++) {
  91. var localStream = new LocalStream(this, streams[i].stream,
  92. streams[i].type, this.eventEmitter, streams[i].videoType,
  93. streams[i].isGUMStream);
  94. this.localStreams.push(localStream);
  95. if (streams[i].isMuted === true)
  96. localStream.setMute(true);
  97. if (streams[i].type == "audio") {
  98. this.localAudio = localStream;
  99. } else {
  100. this.localVideo = localStream;
  101. }
  102. var eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CREATED;
  103. if (change)
  104. eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CHANGED;
  105. this.eventEmitter.emit(eventType, localStream, streams[i].isMuted);
  106. }
  107. return this.localStreams;
  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 MediaStream(data, sid, thessrc,
  119. RTCBrowserType.getBrowserType(), this.eventEmitter);
  120. if(data.peerjid)
  121. return;
  122. var jid = data.peerjid;
  123. if(!this.remoteStreams[jid]) {
  124. this.remoteStreams[jid] = {};
  125. }
  126. this.remoteStreams[jid][remoteStream.type]= remoteStream;
  127. this.eventEmitter.emit(StreamEventTypes.EVENT_TYPE_REMOTE_CREATED, remoteStream);
  128. return remoteStream;
  129. };
  130. RTC.getPCConstraints = function () {
  131. return RTCUtils.pc_constraints;
  132. };
  133. RTC.prototype.getUserMediaWithConstraints = function(um, success_callback,
  134. failure_callback, resolution,
  135. bandwidth, fps, desktopStream)
  136. {
  137. return RTCUtils.getUserMediaWithConstraints(this, um, success_callback,
  138. failure_callback, resolution, bandwidth, fps, desktopStream);
  139. };
  140. RTC.attachMediaStream = function (elSelector, stream) {
  141. RTCUtils.attachMediaStream(elSelector, stream);
  142. };
  143. RTC.getStreamID = function (stream) {
  144. return RTCUtils.getStreamID(stream);
  145. };
  146. RTC.getVideoSrc = function (element) {
  147. return RTCUtils.getVideoSrc(element);
  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. RTC.prototype.changeLocalVideo = function (stream, isUsingScreenStream, callback) {
  180. var oldStream = this.localVideo.getOriginalStream();
  181. var type = (isUsingScreenStream ? "screen" : "camera");
  182. var localCallback = callback;
  183. if(this.localVideo.isMuted() && this.localVideo.videoType !== type) {
  184. localCallback = function() {
  185. APP.xmpp.setVideoMute(false, function(mute) {
  186. self.eventEmitter.emit(RTCEvents.VIDEO_MUTE, mute);
  187. });
  188. callback();
  189. };
  190. }
  191. // FIXME: Workaround for FF/IE/Safari
  192. if (stream && stream.videoStream) {
  193. stream = stream.videoStream;
  194. }
  195. var videoStream = RTCUtils.createStream(stream, true);
  196. this.localVideo = this.createLocalStream(videoStream, "video", true, type);
  197. // Stop the stream to trigger onended event for old stream
  198. oldStream.stop();
  199. this.switchVideoStreams(videoStream, oldStream);
  200. APP.xmpp.switchStreams(videoStream, oldStream,localCallback);
  201. };
  202. RTC.prototype.changeLocalAudio = function (stream, callback) {
  203. var oldStream = this.localAudio.getOriginalStream();
  204. var newStream = RTCUtils.createStream(stream);
  205. this.localAudio = this.createLocalStream(newStream, "audio", true);
  206. // Stop the stream to trigger onended event for old stream
  207. oldStream.stop();
  208. APP.xmpp.switchStreams(newStream, oldStream, callback, true);
  209. };
  210. RTC.prototype.isVideoMuted = function (jid) {
  211. if (jid === APP.xmpp.myJid()) {
  212. var localVideo = APP.RTC.localVideo;
  213. return (!localVideo || localVideo.isMuted());
  214. } else {
  215. if (!this.remoteStreams[jid] ||
  216. !this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE]) {
  217. return null;
  218. }
  219. return this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE].muted;
  220. }
  221. };
  222. RTC.prototype.setVideoMute = function (mute, callback, options) {
  223. if (!this.localVideo)
  224. return;
  225. if (mute == this.localVideo.isMuted())
  226. {
  227. APP.xmpp.sendVideoInfoPresence(mute);
  228. if (callback)
  229. callback(mute);
  230. }
  231. else
  232. {
  233. this.localVideo.setMute(mute);
  234. APP.xmpp.setVideoMute(
  235. mute,
  236. callback,
  237. options);
  238. }
  239. };
  240. RTC.prototype.setDeviceAvailability = function (devices) {
  241. if(!devices)
  242. return;
  243. if(devices.audio === true || devices.audio === false)
  244. this.devices.audio = devices.audio;
  245. if(devices.video === true || devices.video === false)
  246. this.devices.video = devices.video;
  247. this.eventEmitter.emit(RTCEvents.AVAILABLE_DEVICES_CHANGED, this.devices);
  248. };
  249. module.exports = RTC;