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

JitsiLocalTrack.js 4.9KB

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