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

JitsiLocalTrack.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. var JitsiTrack = require("./JitsiTrack");
  2. var RTCBrowserType = require("./RTCBrowserType");
  3. var JitsiTrackEvents = require('../../JitsiTrackEvents');
  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. this.startMuted = false;
  16. var self = this;
  17. JitsiTrack.call(this, null, stream,
  18. function () {
  19. if(!this.dontFireRemoveEvent)
  20. this.eventEmitter.emit(
  21. JitsiTrackEvents.TRACK_STOPPED);
  22. this.dontFireRemoveEvent = false;
  23. }.bind(this));
  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. if(!this.rtc) {
  33. this.startMuted = mute;
  34. return;
  35. }
  36. var isAudio = this.type === JitsiTrack.AUDIO;
  37. this.dontFireRemoveEvent = false;
  38. if ((window.location.protocol != "https:") ||
  39. (isAudio) || this.videoType === "desktop" ||
  40. // FIXME FF does not support 'removeStream' method used to mute
  41. RTCBrowserType.isFirefox()) {
  42. var tracks = this._getTracks();
  43. for (var idx = 0; idx < tracks.length; idx++) {
  44. tracks[idx].enabled = !mute;
  45. }
  46. if(isAudio)
  47. this.rtc.room.setAudioMute(mute);
  48. else
  49. this.rtc.room.setVideoMute(mute);
  50. this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED);
  51. } else {
  52. if (mute) {
  53. this.dontFireRemoveEvent = true;
  54. this.rtc.room.removeStream(this.stream);
  55. RTC.stopMediaStream(this.stream);
  56. if(isAudio)
  57. this.rtc.room.setAudioMute(mute);
  58. else
  59. this.rtc.room.setVideoMute(mute);
  60. this.stream = null;
  61. this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED);
  62. //FIXME: Maybe here we should set the SRC for the containers to something
  63. } else {
  64. var self = this;
  65. RTC.obtainAudioAndVideoPermissions({
  66. devices: (isAudio ? ["audio"] : ["video"]),
  67. resolution: self.resolution})
  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(
  91. JitsiTrackEvents.TRACK_MUTE_CHANGED);
  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. 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.isActive())
  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. /**
  141. * Return true;
  142. */
  143. JitsiLocalTrack.prototype.isLocal = function () {
  144. return true;
  145. }
  146. module.exports = JitsiLocalTrack;