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

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