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

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