Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

JitsiLocalTrack.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. (isAudio ? ["audio"] : ["video"]),
  66. self.resolution, true)
  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.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.stream.stop();
  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.stream.ended)
  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;