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

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