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

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