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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. var JitsiTrack = require("./JitsiTrack");
  2. var StreamEventTypes = require("../../service/RTC/StreamEventTypes");
  3. var RTCBrowserType = require("./RTCBrowserType");
  4. var RTC = 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. var self = this;
  16. JitsiTrack.call(this, null, stream,
  17. function () {
  18. if(!self.dontFireRemoveEvent && self.rtc)
  19. self.rtc.eventEmitter.emit(
  20. StreamEventTypes.EVENT_TYPE_LOCAL_ENDED, self);
  21. self.dontFireRemoveEvent = false;
  22. });
  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.rtc.eventEmitter.emit(StreamEventTypes.TRACK_MUTE_CHANGED, this);
  50. } else {
  51. if (mute) {
  52. this.dontFireRemoveEvent = true;
  53. this.rtc.room.removeStream(this.stream);
  54. 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.rtc.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. RTC.obtainAudioAndVideoPermissions({
  65. devices: (isAudio ? ["audio"] : ["video"]),
  66. resolution: self.resolution})
  67. .then(function (streams) {
  68. var stream = null;
  69. for(var i = 0; i < streams.length; i++) {
  70. stream = streams[i];
  71. if(stream.type === self.type) {
  72. self.stream = stream.stream;
  73. self.videoType = stream.videoType;
  74. break;
  75. }
  76. }
  77. if(!stream)
  78. return;
  79. for(var i = 0; i < self.containers.length; i++)
  80. {
  81. RTC.attachMediaStream(self.containers[i], self.stream);
  82. }
  83. self.rtc.room.addStream(stream.stream,
  84. function () {
  85. if(isAudio)
  86. self.rtc.room.setAudioMute(mute);
  87. else
  88. self.rtc.room.setVideoMute(mute);
  89. self.rtc.eventEmitter.emit(StreamEventTypes.TRACK_MUTE_CHANGED, self);
  90. });
  91. });
  92. }
  93. }
  94. }
  95. /**
  96. * Stops sending the media track. And removes it from the HTML.
  97. * NOTE: Works for local tracks only.
  98. */
  99. JitsiLocalTrack.prototype.stop = function () {
  100. if(!this.stream)
  101. return;
  102. if(this.rtc)
  103. this.rtc.room.removeStream(this.stream);
  104. this.rtc.stopMediaStream(this.stream);
  105. this.detach();
  106. }
  107. /**
  108. * Returns <tt>true</tt> - if the stream is muted
  109. * and <tt>false</tt> otherwise.
  110. * @returns {boolean} <tt>true</tt> - if the stream is muted
  111. * and <tt>false</tt> otherwise.
  112. */
  113. JitsiLocalTrack.prototype.isMuted = function () {
  114. if (!this.stream)
  115. return true;
  116. var tracks = [];
  117. var isAudio = this.type === JitsiTrack.AUDIO;
  118. if (isAudio) {
  119. tracks = this.stream.getAudioTracks();
  120. } else {
  121. if (!this.isActive())
  122. return true;
  123. tracks = this.stream.getVideoTracks();
  124. }
  125. for (var idx = 0; idx < tracks.length; idx++) {
  126. if(tracks[idx].enabled)
  127. return false;
  128. }
  129. return true;
  130. };
  131. /**
  132. * Private method. Updates rtc property of the track.
  133. * @param rtc the rtc instance.
  134. */
  135. JitsiLocalTrack.prototype._setRTC = function (rtc) {
  136. this.rtc = rtc;
  137. };
  138. module.exports = JitsiLocalTrack;