Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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