modified lib-jitsi-meet dev repo
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.

JitsiLocalTrack.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* global __filename, Promise */
  2. var logger = require("jitsi-meet-logger").getLogger(__filename);
  3. var JitsiTrack = require("./JitsiTrack");
  4. var RTCBrowserType = require("./RTCBrowserType");
  5. var JitsiTrackEvents = require('../../JitsiTrackEvents');
  6. var JitsiTrackErrors = require("../../JitsiTrackErrors");
  7. var RTCUtils = require("./RTCUtils");
  8. var VideoType = require('../../service/RTC/VideoType');
  9. /**
  10. * Represents a single media track(either audio or video).
  11. * One <tt>JitsiLocalTrack</tt> corresponds to one WebRTC MediaStreamTrack.
  12. * @param stream WebRTC MediaStream, parent of the track
  13. * @param track underlying WebRTC MediaStreamTrack for new JitsiRemoteTrack
  14. * @param mediaType the MediaType of the JitsiRemoteTrack
  15. * @param videoType the VideoType of the JitsiRemoteTrack
  16. * @param resolution the video resoultion if it's a video track
  17. * @param deviceId the ID of the local device for this track
  18. * @constructor
  19. */
  20. function JitsiLocalTrack(stream, track, mediaType, videoType, resolution,
  21. deviceId) {
  22. JitsiTrack.call(this,
  23. null /* RTC */, stream, track,
  24. function () {
  25. if(!this.dontFireRemoveEvent)
  26. this.eventEmitter.emit(
  27. JitsiTrackEvents.LOCAL_TRACK_STOPPED);
  28. this.dontFireRemoveEvent = false;
  29. }.bind(this) /* inactiveHandler */,
  30. mediaType, videoType, null /* ssrc */);
  31. this.dontFireRemoveEvent = false;
  32. this.resolution = resolution;
  33. this.deviceId = deviceId;
  34. this.startMuted = false;
  35. this.disposed = false;
  36. //FIXME: This dependacy is not necessary.
  37. this.conference = null;
  38. this.initialMSID = this.getMSID();
  39. this.inMuteOrUnmuteProgress = false;
  40. }
  41. JitsiLocalTrack.prototype = Object.create(JitsiTrack.prototype);
  42. JitsiLocalTrack.prototype.constructor = JitsiLocalTrack;
  43. /**
  44. * Mutes the track. Will reject the Promise if there is mute/unmute operation
  45. * in progress.
  46. * @returns {Promise}
  47. */
  48. JitsiLocalTrack.prototype.mute = function () {
  49. return createMuteUnmutePromise(this, true);
  50. };
  51. /**
  52. * Unmutes the track. Will reject the Promise if there is mute/unmute operation
  53. * in progress.
  54. * @returns {Promise}
  55. */
  56. JitsiLocalTrack.prototype.unmute = function () {
  57. return createMuteUnmutePromise(this, false);
  58. };
  59. /**
  60. * Creates Promise for mute/unmute operation.
  61. * @param track the track that will be muted/unmuted
  62. * @param mute whether to mute or unmute the track
  63. */
  64. function createMuteUnmutePromise(track, mute)
  65. {
  66. return new Promise(function (resolve, reject) {
  67. if(this.inMuteOrUnmuteProgress) {
  68. reject(new Error(JitsiTrackErrors.TRACK_MUTE_UNMUTE_IN_PROGRESS));
  69. return;
  70. }
  71. this.inMuteOrUnmuteProgress = true;
  72. this._setMute(mute,
  73. function(){
  74. this.inMuteOrUnmuteProgress = false;
  75. resolve();
  76. }.bind(this),
  77. function(status){
  78. this.inMuteOrUnmuteProgress = false;
  79. reject(status);
  80. }.bind(this));
  81. }.bind(track));
  82. }
  83. /**
  84. * Mutes / unmutes the track.
  85. * @param mute {boolean} if true the track will be muted. Otherwise the track
  86. * will be unmuted.
  87. */
  88. JitsiLocalTrack.prototype._setMute = function (mute, resolve, reject) {
  89. if (this.isMuted() === mute) {
  90. resolve();
  91. return;
  92. }
  93. if(!this.rtc) {
  94. this.startMuted = mute;
  95. resolve();
  96. return;
  97. }
  98. var isAudio = this.isAudioTrack();
  99. this.dontFireRemoveEvent = false;
  100. var setStreamToNull = false;
  101. // the callback that will notify that operation had finished
  102. var callbackFunction = function() {
  103. if(setStreamToNull)
  104. this.stream = null;
  105. this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED);
  106. resolve();
  107. }.bind(this);
  108. if ((window.location.protocol != "https:") ||
  109. (isAudio) || this.videoType === VideoType.DESKTOP ||
  110. // FIXME FF does not support 'removeStream' method used to mute
  111. RTCBrowserType.isFirefox()) {
  112. if (this.track)
  113. this.track.enabled = !mute;
  114. if(isAudio)
  115. this.rtc.room.setAudioMute(mute, callbackFunction);
  116. else
  117. this.rtc.room.setVideoMute(mute, callbackFunction);
  118. } else {
  119. if (mute) {
  120. this.dontFireRemoveEvent = true;
  121. this.rtc.room.removeStream(this.stream, function () {},
  122. {mtype: this.type, type: "mute", ssrc: this.ssrc});
  123. RTCUtils.stopMediaStream(this.stream);
  124. setStreamToNull = true;
  125. if(isAudio)
  126. this.rtc.room.setAudioMute(mute, callbackFunction);
  127. else
  128. this.rtc.room.setVideoMute(mute, callbackFunction);
  129. //FIXME: Maybe here we should set the SRC for the containers to something
  130. } else {
  131. var self = this;
  132. // FIXME why are we doing all this audio type checks and
  133. // convoluted scenarios if we're going this way only
  134. // for VIDEO media and CAMERA type of video ?
  135. var streamOptions = {
  136. devices: (isAudio ? ["audio"] : ["video"]),
  137. resolution: self.resolution
  138. };
  139. if (isAudio) {
  140. streamOptions['micDeviceId'] = self.deviceId;
  141. } else if(self.videoType === VideoType.CAMERA) {
  142. streamOptions['cameraDeviceId'] = self.deviceId;
  143. }
  144. RTCUtils.obtainAudioAndVideoPermissions(streamOptions)
  145. .then(function (streamsInfo) {
  146. var streamInfo = null;
  147. for(var i = 0; i < streamsInfo.length; i++) {
  148. if(streamsInfo[i].mediaType === self.getType()) {
  149. streamInfo = streamsInfo[i];
  150. self.stream = streamInfo.stream;
  151. self.track = streamInfo.track;
  152. // This is not good when video type changes after
  153. // unmute, but let's not crash here
  154. if (self.videoType != streamInfo.videoType) {
  155. logger.error(
  156. "Video type has changed after unmute!",
  157. self.videoType, streamInfo.videoType);
  158. self.videoType = streamInfo.videoType;
  159. }
  160. break;
  161. }
  162. }
  163. if(!streamInfo) {
  164. reject(new Error('track.no_stream_found'));
  165. return;
  166. }
  167. for(var i = 0; i < self.containers.length; i++)
  168. {
  169. self.containers[i]
  170. = RTCUtils.attachMediaStream(
  171. self.containers[i], self.stream);
  172. }
  173. self.rtc.room.addStream(self.stream,
  174. function () {
  175. if(isAudio)
  176. self.rtc.room.setAudioMute(
  177. mute, callbackFunction);
  178. else
  179. self.rtc.room.setVideoMute(
  180. mute, callbackFunction);
  181. }, {
  182. mtype: self.type,
  183. type: "unmute",
  184. ssrc: self.ssrc,
  185. msid: self.getMSID()});
  186. }).catch(function (error) {
  187. reject(error);
  188. });
  189. }
  190. }
  191. };
  192. /**
  193. * Stops sending the media track. And removes it from the HTML.
  194. * NOTE: Works for local tracks only.
  195. * @returns {Promise}
  196. */
  197. JitsiLocalTrack.prototype.dispose = function () {
  198. var promise = Promise.resolve();
  199. if (this.conference){
  200. promise = this.conference.removeTrack(this);
  201. }
  202. if (this.stream) {
  203. RTCUtils.stopMediaStream(this.stream);
  204. this.detach();
  205. }
  206. this.disposed = true;
  207. return promise;
  208. };
  209. /**
  210. * Returns <tt>true</tt> - if the stream is muted
  211. * and <tt>false</tt> otherwise.
  212. * @returns {boolean} <tt>true</tt> - if the stream is muted
  213. * and <tt>false</tt> otherwise.
  214. */
  215. JitsiLocalTrack.prototype.isMuted = function () {
  216. // this.stream will be null when we mute local video on Chrome
  217. if (!this.stream)
  218. return true;
  219. if (this.isVideoTrack() && !this.isActive()) {
  220. return true;
  221. } else {
  222. return !this.track || !this.track.enabled;
  223. }
  224. };
  225. /**
  226. * Private method. Updates rtc property of the track.
  227. * @param rtc the rtc instance.
  228. */
  229. JitsiLocalTrack.prototype._setRTC = function (rtc) {
  230. this.rtc = rtc;
  231. // We want to keep up with postponed events which should have been fired
  232. // on "attach" call, but for local track we not always have the conference
  233. // before attaching. However this may result in duplicated events if they
  234. // have been triggered on "attach" already.
  235. for(var i = 0; i < this.containers.length; i++)
  236. {
  237. this._maybeFireTrackAttached(this.containers[i]);
  238. }
  239. };
  240. /**
  241. * Updates the SSRC associated with the MediaStream in JitsiLocalTrack object.
  242. * @ssrc the new ssrc
  243. */
  244. JitsiLocalTrack.prototype._setSSRC = function (ssrc) {
  245. this.ssrc = ssrc;
  246. };
  247. //FIXME: This dependacy is not necessary. This is quick fix.
  248. /**
  249. * Sets the JitsiConference object associated with the track. This is temp
  250. * solution.
  251. * @param conference the JitsiConference object
  252. */
  253. JitsiLocalTrack.prototype._setConference = function(conference) {
  254. this.conference = conference;
  255. };
  256. /**
  257. * Gets the SSRC of this local track if it's available already or <tt>null</tt>
  258. * otherwise. That's because we don't know the SSRC until local description is
  259. * created.
  260. * In case of video and simulcast returns the the primarySSRC.
  261. * @returns {string} or {null}
  262. */
  263. JitsiLocalTrack.prototype.getSSRC = function () {
  264. if(this.ssrc && this.ssrc.groups && this.ssrc.groups.length)
  265. return this.ssrc.groups[0].primarySSRC;
  266. else if(this.ssrc && this.ssrc.ssrcs && this.ssrc.ssrcs.length)
  267. return this.ssrc.ssrcs[0];
  268. else
  269. return null;
  270. };
  271. /**
  272. * Returns <tt>true</tt>.
  273. * @returns {boolean} <tt>true</tt>
  274. */
  275. JitsiLocalTrack.prototype.isLocal = function () {
  276. return true;
  277. };
  278. module.exports = JitsiLocalTrack;