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 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* global Promise */
  2. var JitsiTrack = require("./JitsiTrack");
  3. var RTCBrowserType = require("./RTCBrowserType");
  4. var JitsiTrackEvents = require('../../JitsiTrackEvents');
  5. var JitsiTrackErrors = require("../../JitsiTrackErrors");
  6. var RTCUtils = require("./RTCUtils");
  7. /**
  8. * Represents a single media track (either audio or video).
  9. * @constructor
  10. */
  11. function JitsiLocalTrack(stream, videoType,
  12. resolution, deviceId)
  13. {
  14. this.videoType = videoType;
  15. this.dontFireRemoveEvent = false;
  16. this.resolution = resolution;
  17. this.deviceId = deviceId;
  18. this.startMuted = false;
  19. this.ssrc = null;
  20. this.disposed = false;
  21. //FIXME: This dependacy is not necessary.
  22. this.conference = null;
  23. JitsiTrack.call(this, null, stream,
  24. function () {
  25. if(!this.dontFireRemoveEvent)
  26. this.eventEmitter.emit(
  27. JitsiTrackEvents.LOCAL_TRACK_STOPPED);
  28. this.dontFireRemoveEvent = false;
  29. }.bind(this));
  30. this.initialMSID = this.getMSID();
  31. this.inMuteOrUnmuteProgress = false;
  32. }
  33. JitsiLocalTrack.prototype = Object.create(JitsiTrack.prototype);
  34. JitsiLocalTrack.prototype.constructor = JitsiLocalTrack;
  35. /**
  36. * Mutes the track. Will reject the Promise if there is mute/unmute operation
  37. * in progress.
  38. * @returns {Promise}
  39. */
  40. JitsiLocalTrack.prototype.mute = function () {
  41. return createMuteUnmutePromise(this, true);
  42. };
  43. /**
  44. * Unmutes the stream. Will reject the Promise if there is mute/unmute operation
  45. * in progress.
  46. * @returns {Promise}
  47. */
  48. JitsiLocalTrack.prototype.unmute = function () {
  49. return createMuteUnmutePromise(this, false);
  50. };
  51. /**
  52. * Creates Promise for mute/unmute operation.
  53. * @param track the track that will be muted/unmuted
  54. * @param mute whether to mute or unmute the track
  55. */
  56. function createMuteUnmutePromise(track, mute)
  57. {
  58. return new Promise(function (resolve, reject) {
  59. if(this.inMuteOrUnmuteProgress) {
  60. reject(new Error(JitsiTrackErrors.TRACK_MUTE_UNMUTE_IN_PROGRESS));
  61. return;
  62. }
  63. this.inMuteOrUnmuteProgress = true;
  64. this._setMute(mute,
  65. function(){
  66. this.inMuteOrUnmuteProgress = false;
  67. resolve();
  68. }.bind(this),
  69. function(status){
  70. this.inMuteOrUnmuteProgress = false;
  71. reject(status);
  72. }.bind(this));
  73. }.bind(track));
  74. }
  75. /**
  76. * Mutes / unmutes the track.
  77. * @param mute {boolean} if true the track will be muted. Otherwise the track
  78. * will be unmuted.
  79. */
  80. JitsiLocalTrack.prototype._setMute = function (mute, resolve, reject) {
  81. if (this.isMuted() === mute) {
  82. resolve();
  83. return;
  84. }
  85. if(!this.rtc) {
  86. this.startMuted = mute;
  87. resolve();
  88. return;
  89. }
  90. var isAudio = this.isAudioTrack();
  91. this.dontFireRemoveEvent = false;
  92. var setStreamToNull = false;
  93. // the callback that will notify that operation had finished
  94. var callbackFunction = function() {
  95. if(setStreamToNull)
  96. this.stream = null;
  97. this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED);
  98. resolve();
  99. }.bind(this);
  100. if ((window.location.protocol != "https:") ||
  101. (isAudio) || this.videoType === "desktop" ||
  102. // FIXME FF does not support 'removeStream' method used to mute
  103. RTCBrowserType.isFirefox()) {
  104. var tracks = this._getTracks();
  105. for (var idx = 0; idx < tracks.length; idx++) {
  106. tracks[idx].enabled = !mute;
  107. }
  108. if(isAudio)
  109. this.rtc.room.setAudioMute(mute, callbackFunction);
  110. else
  111. this.rtc.room.setVideoMute(mute, callbackFunction);
  112. } else {
  113. if (mute) {
  114. this.dontFireRemoveEvent = true;
  115. this.rtc.room.removeStream(this.stream, function () {},
  116. {mtype: this.type, type: "mute", ssrc: this.ssrc});
  117. RTCUtils.stopMediaStream(this.stream);
  118. setStreamToNull = true;
  119. if(isAudio)
  120. this.rtc.room.setAudioMute(mute, callbackFunction);
  121. else
  122. this.rtc.room.setVideoMute(mute, callbackFunction);
  123. //FIXME: Maybe here we should set the SRC for the containers to something
  124. } else {
  125. var self = this;
  126. var streamOptions = {
  127. devices: (isAudio ? ["audio"] : ["video"]),
  128. resolution: self.resolution
  129. };
  130. if (isAudio) {
  131. streamOptions['micDeviceId'] = self.deviceId;
  132. } else if(self.videoType === 'camera') {
  133. streamOptions['cameraDeviceId'] = self.deviceId;
  134. }
  135. RTCUtils.obtainAudioAndVideoPermissions(streamOptions)
  136. .then(function (streams) {
  137. var stream = null;
  138. for(var i = 0; i < streams.length; i++) {
  139. stream = streams[i];
  140. if(stream.type === self.type) {
  141. self.stream = stream.stream;
  142. self.videoType = stream.videoType;
  143. break;
  144. }
  145. }
  146. if(!stream) {
  147. reject(new Error('track.no_stream_found'));
  148. return;
  149. }
  150. for(var i = 0; i < self.containers.length; i++)
  151. {
  152. self.containers[i]
  153. = RTCUtils.attachMediaStream(
  154. self.containers[i], self.stream);
  155. }
  156. self.rtc.room.addStream(self.stream,
  157. function () {
  158. if(isAudio)
  159. self.rtc.room.setAudioMute(
  160. mute, callbackFunction);
  161. else
  162. self.rtc.room.setVideoMute(
  163. mute, callbackFunction);
  164. }, {
  165. mtype: self.type,
  166. type: "unmute",
  167. ssrc: self.ssrc,
  168. msid: self.getMSID()});
  169. }).catch(function (error) {
  170. reject(error);
  171. });
  172. }
  173. }
  174. };
  175. /**
  176. * Stops sending the media track. And removes it from the HTML.
  177. * NOTE: Works for local tracks only.
  178. * @returns {Promise}
  179. */
  180. JitsiLocalTrack.prototype.dispose = function () {
  181. var promise = Promise.resolve();
  182. if (this.conference){
  183. promise = this.conference.removeTrack(this);
  184. }
  185. if (this.stream) {
  186. RTCUtils.stopMediaStream(this.stream);
  187. this.detach();
  188. }
  189. this.disposed = true;
  190. return promise;
  191. };
  192. /**
  193. * Returns <tt>true</tt> - if the stream is muted
  194. * and <tt>false</tt> otherwise.
  195. * @returns {boolean} <tt>true</tt> - if the stream is muted
  196. * and <tt>false</tt> otherwise.
  197. */
  198. JitsiLocalTrack.prototype.isMuted = function () {
  199. if (!this.stream)
  200. return true;
  201. var tracks = [];
  202. var isAudio = this.isAudioTrack();
  203. if (isAudio) {
  204. tracks = this.stream.getAudioTracks();
  205. } else {
  206. if (!this.isActive())
  207. return true;
  208. tracks = this.stream.getVideoTracks();
  209. }
  210. for (var idx = 0; idx < tracks.length; idx++) {
  211. if(tracks[idx].enabled)
  212. return false;
  213. }
  214. return true;
  215. };
  216. /**
  217. * Private method. Updates rtc property of the track.
  218. * @param rtc the rtc instance.
  219. */
  220. JitsiLocalTrack.prototype._setRTC = function (rtc) {
  221. this.rtc = rtc;
  222. // We want to keep up with postponed events which should have been fired
  223. // on "attach" call, but for local track we not always have the conference
  224. // before attaching. However this may result in duplicated events if they
  225. // have been triggered on "attach" already.
  226. for(var i = 0; i < this.containers.length; i++)
  227. {
  228. this._maybeFireTrackAttached(this.containers[i]);
  229. }
  230. };
  231. /**
  232. * Updates the SSRC associated with the MediaStream in JitsiLocalTrack object.
  233. * @ssrc the new ssrc
  234. */
  235. JitsiLocalTrack.prototype._setSSRC = function (ssrc) {
  236. this.ssrc = ssrc;
  237. };
  238. //FIXME: This dependacy is not necessary. This is quick fix.
  239. /**
  240. * Sets the JitsiConference object associated with the track. This is temp
  241. * solution.
  242. * @param conference the JitsiConference object
  243. */
  244. JitsiLocalTrack.prototype._setConference = function(conference) {
  245. this.conference = conference;
  246. };
  247. /**
  248. * Gets the SSRC of this local track if it's available already or <tt>null</tt>
  249. * otherwise. That's because we don't know the SSRC until local description is
  250. * created.
  251. * In case of video and simulcast returns the the primarySSRC.
  252. * @returns {string} or {null}
  253. */
  254. JitsiLocalTrack.prototype.getSSRC = function () {
  255. if(this.ssrc && this.ssrc.groups && this.ssrc.groups.length)
  256. return this.ssrc.groups[0].primarySSRC;
  257. else if(this.ssrc && this.ssrc.ssrcs && this.ssrc.ssrcs.length)
  258. return this.ssrc.ssrcs[0];
  259. else
  260. return null;
  261. };
  262. /**
  263. * Return true;
  264. */
  265. JitsiLocalTrack.prototype.isLocal = function () {
  266. return true;
  267. };
  268. module.exports = JitsiLocalTrack;