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

JitsiLocalTrack.js 9.1KB

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