modified lib-jitsi-meet dev repo
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

JitsiLocalTrack.js 4.8KB

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