Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RTC.js 9.2KB

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