Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

JitsiLocalTrack.js 13KB

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