Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

JitsiLocalTrack.js 14KB

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