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

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