Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

JitsiLocalTrack.js 16KB

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