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.0KB

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