Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

JitsiLocalTrack.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. resolution)
  11. {
  12. JitsiTrack.call(this, RTC, stream);
  13. this.eventEmitter = eventEmitter;
  14. this.videoType = videoType;
  15. this.isGUMStream = true;
  16. this.dontFireRemoveEvent = false;
  17. this.isStarted = false;
  18. this.resolution = resolution;
  19. var self = this;
  20. if(isGUMStream === false)
  21. this.isGUMStream = isGUMStream;
  22. this.stream.onended = function () {
  23. if(!self.dontFireRemoveEvent)
  24. self.eventEmitter.emit(StreamEventTypes.EVENT_TYPE_LOCAL_ENDED, self);
  25. self.dontFireRemoveEvent = false;
  26. };
  27. }
  28. JitsiLocalTrack.prototype = Object.create(JitsiTrack.prototype);
  29. JitsiLocalTrack.prototype.constructor = JitsiLocalTrack;
  30. /**
  31. * Mutes / unmutes the track.
  32. * @param mute {boolean} if true the track will be muted. Otherwise the track will be unmuted.
  33. */
  34. JitsiLocalTrack.prototype._setMute = function (mute) {
  35. var isAudio = this.type === JitsiTrack.AUDIO;
  36. this.dontFireRemoveEvent = false;
  37. if ((window.location.protocol != "https:" && this.isGUMStream) ||
  38. (isAudio && this.isGUMStream) || this.videoType === "screen" ||
  39. // FIXME FF does not support 'removeStream' method used to mute
  40. RTCBrowserType.isFirefox()) {
  41. var tracks = this._getTracks();
  42. for (var idx = 0; idx < tracks.length; idx++) {
  43. tracks[idx].enabled = !mute;
  44. }
  45. if(isAudio)
  46. this.rtc.room.setAudioMute(mute);
  47. else
  48. this.rtc.room.setVideoMute(mute);
  49. this.eventEmitter.emit(StreamEventTypes.TRACK_MUTE_CHANGED, this);
  50. } else {
  51. if (mute) {
  52. this.dontFireRemoveEvent = true;
  53. this.rtc.room.removeStream(this.stream);
  54. this.stream.stop();
  55. if(isAudio)
  56. this.rtc.room.setAudioMute(mute);
  57. else
  58. this.rtc.room.setVideoMute(mute);
  59. this.stream = null;
  60. this.eventEmitter.emit(StreamEventTypes.TRACK_MUTE_CHANGED, this);
  61. //FIXME: Maybe here we should set the SRC for the containers to something
  62. } else {
  63. var self = this;
  64. this.rtc.obtainAudioAndVideoPermissions(
  65. {devices: (isAudio ? ["audio"] : ["video"]),
  66. resolution: 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. self.isGUMStream = stream.isGUMStream;
  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. this.rtc.room.removeStream(this.stream);
  104. this.stream.stop();
  105. this.detach();
  106. }
  107. /**
  108. * Starts sending the track.
  109. * NOTE: Works for local tracks only.
  110. */
  111. JitsiLocalTrack.prototype.start = function() {
  112. this.isStarted = true;
  113. this.rtc.room.addStream(this.stream, function () {});
  114. }
  115. /**
  116. * Returns <tt>true</tt> - if the stream is muted
  117. * and <tt>false</tt> otherwise.
  118. * @returns {boolean} <tt>true</tt> - if the stream is muted
  119. * and <tt>false</tt> otherwise.
  120. */
  121. JitsiLocalTrack.prototype.isMuted = function () {
  122. if (!this.stream)
  123. return true;
  124. var tracks = [];
  125. var isAudio = this.type === JitsiTrack.AUDIO;
  126. if (isAudio) {
  127. tracks = this.stream.getAudioTracks();
  128. } else {
  129. if (this.stream.ended)
  130. return true;
  131. tracks = this.stream.getVideoTracks();
  132. }
  133. for (var idx = 0; idx < tracks.length; idx++) {
  134. if(tracks[idx].enabled)
  135. return false;
  136. }
  137. return true;
  138. };
  139. module.exports = JitsiLocalTrack;