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

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