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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. var JitsiTrack = require("./JitsiTrack");
  2. var RTCBrowserType = require("./RTCBrowserType");
  3. var JitsiTrackEvents = require('../../JitsiTrackEvents');
  4. var RTCUtils = require("./RTCUtils");
  5. /**
  6. * Represents a single media track (either audio or video).
  7. * @constructor
  8. */
  9. function JitsiLocalTrack(stream, videoType,
  10. resolution, deviceId)
  11. {
  12. this.videoType = videoType;
  13. this.dontFireRemoveEvent = false;
  14. this.resolution = resolution;
  15. this.deviceId = deviceId;
  16. this.startMuted = false;
  17. var self = this;
  18. JitsiTrack.call(this, null, stream,
  19. function () {
  20. if(!this.dontFireRemoveEvent)
  21. this.eventEmitter.emit(
  22. JitsiTrackEvents.TRACK_STOPPED);
  23. this.dontFireRemoveEvent = false;
  24. }.bind(this));
  25. }
  26. JitsiLocalTrack.prototype = Object.create(JitsiTrack.prototype);
  27. JitsiLocalTrack.prototype.constructor = JitsiLocalTrack;
  28. /**
  29. * Mutes / unmutes the track.
  30. * @param mute {boolean} if true the track will be muted. Otherwise the track will be unmuted.
  31. */
  32. JitsiLocalTrack.prototype._setMute = function (mute) {
  33. if (this.isMuted() === mute) {
  34. return;
  35. }
  36. if(!this.rtc) {
  37. this.startMuted = mute;
  38. return;
  39. }
  40. var isAudio = this.type === JitsiTrack.AUDIO;
  41. this.dontFireRemoveEvent = false;
  42. if ((window.location.protocol != "https:") ||
  43. (isAudio) || this.videoType === "desktop" ||
  44. // FIXME FF does not support 'removeStream' method used to mute
  45. RTCBrowserType.isFirefox()) {
  46. var tracks = this._getTracks();
  47. for (var idx = 0; idx < tracks.length; idx++) {
  48. tracks[idx].enabled = !mute;
  49. }
  50. if(isAudio)
  51. this.rtc.room.setAudioMute(mute);
  52. else
  53. this.rtc.room.setVideoMute(mute);
  54. this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED);
  55. } else {
  56. if (mute) {
  57. this.dontFireRemoveEvent = true;
  58. this.rtc.room.removeStream(this.stream, function () {});
  59. RTCUtils.stopMediaStream(this.stream);
  60. if(isAudio)
  61. this.rtc.room.setAudioMute(mute);
  62. else
  63. this.rtc.room.setVideoMute(mute);
  64. this.stream = null;
  65. this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED);
  66. //FIXME: Maybe here we should set the SRC for the containers to something
  67. } else {
  68. var self = this;
  69. var streamOptions = {
  70. devices: (isAudio ? ["audio"] : ["video"]),
  71. resolution: self.resolution
  72. };
  73. if (isAudio) {
  74. streamOptions['micDeviceId'] = self.deviceId;
  75. } else if(self.videoType === 'camera') {
  76. streamOptions['cameraDeviceId'] = self.deviceId;
  77. }
  78. RTCUtils.obtainAudioAndVideoPermissions(streamOptions)
  79. .then(function (streams) {
  80. var stream = null;
  81. for(var i = 0; i < streams.length; i++) {
  82. stream = streams[i];
  83. if(stream.type === self.type) {
  84. self.stream = stream.stream;
  85. self.videoType = stream.videoType;
  86. break;
  87. }
  88. }
  89. if(!stream)
  90. return;
  91. for(var i = 0; i < self.containers.length; i++)
  92. {
  93. self.containers[i]
  94. = RTCUtils.attachMediaStream(
  95. self.containers[i], self.stream);
  96. }
  97. self.rtc.room.addStream(stream.stream,
  98. function () {
  99. if(isAudio)
  100. self.rtc.room.setAudioMute(mute);
  101. else
  102. self.rtc.room.setVideoMute(mute);
  103. self.eventEmitter.emit(
  104. JitsiTrackEvents.TRACK_MUTE_CHANGED);
  105. });
  106. });
  107. }
  108. }
  109. }
  110. /**
  111. * Stops sending the media track. And removes it from the HTML.
  112. * NOTE: Works for local tracks only.
  113. */
  114. JitsiLocalTrack.prototype.stop = function () {
  115. if(!this.stream)
  116. return;
  117. if(this.rtc)
  118. this.rtc.room.removeStream(this.stream, function () {});
  119. RTCUtils.stopMediaStream(this.stream);
  120. this.detach();
  121. }
  122. /**
  123. * Returns <tt>true</tt> - if the stream is muted
  124. * and <tt>false</tt> otherwise.
  125. * @returns {boolean} <tt>true</tt> - if the stream is muted
  126. * and <tt>false</tt> otherwise.
  127. */
  128. JitsiLocalTrack.prototype.isMuted = function () {
  129. if (!this.stream)
  130. return true;
  131. var tracks = [];
  132. var isAudio = this.type === JitsiTrack.AUDIO;
  133. if (isAudio) {
  134. tracks = this.stream.getAudioTracks();
  135. } else {
  136. if (!this.isActive())
  137. return true;
  138. tracks = this.stream.getVideoTracks();
  139. }
  140. for (var idx = 0; idx < tracks.length; idx++) {
  141. if(tracks[idx].enabled)
  142. return false;
  143. }
  144. return true;
  145. };
  146. /**
  147. * Private method. Updates rtc property of the track.
  148. * @param rtc the rtc instance.
  149. */
  150. JitsiLocalTrack.prototype._setRTC = function (rtc) {
  151. this.rtc = rtc;
  152. };
  153. /**
  154. * Gets the SSRC of this local track if it's available already or <tt>null</tt>
  155. * otherwise. That's because we don't know the SSRC until local description is
  156. * created.
  157. * @returns {string} or {null}
  158. */
  159. JitsiLocalTrack.prototype.getSSRC = function () {
  160. if (!this.rtc.room.session)
  161. return null;
  162. if (this.isAudioTrack()) {
  163. return this.rtc.room.session.localStreamsSSRC.audio;
  164. } else {
  165. return this.rtc.room.session.localStreamsSSRC.video;
  166. }
  167. };
  168. /**
  169. * Return true;
  170. */
  171. JitsiLocalTrack.prototype.isLocal = function () {
  172. return true;
  173. }
  174. module.exports = JitsiLocalTrack;