You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JitsiLocalTrack.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. this.isLocal = true;
  17. var self = this;
  18. JitsiTrack.call(this, null, stream,
  19. function () {
  20. if(!this.dontFireRemoveEvent)
  21. this.eventEmitter.emit(
  22. JitsiTrackEvents.TRACK_STOPPED);
  23. this.dontFireRemoveEvent = false;
  24. }.bind(this));
  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. if(!this.rtc) {
  34. this.startMuted = mute;
  35. return;
  36. }
  37. var isAudio = this.type === JitsiTrack.AUDIO;
  38. this.dontFireRemoveEvent = false;
  39. if ((window.location.protocol != "https:") ||
  40. (isAudio) || this.videoType === "desktop" ||
  41. // FIXME FF does not support 'removeStream' method used to mute
  42. RTCBrowserType.isFirefox()) {
  43. var tracks = this._getTracks();
  44. for (var idx = 0; idx < tracks.length; idx++) {
  45. tracks[idx].enabled = !mute;
  46. }
  47. if(isAudio)
  48. this.rtc.room.setAudioMute(mute);
  49. else
  50. this.rtc.room.setVideoMute(mute);
  51. this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED);
  52. } else {
  53. if (mute) {
  54. this.dontFireRemoveEvent = true;
  55. this.rtc.room.removeStream(this.stream, function () {});
  56. RTC.stopMediaStream(this.stream);
  57. if(isAudio)
  58. this.rtc.room.setAudioMute(mute);
  59. else
  60. this.rtc.room.setVideoMute(mute);
  61. this.stream = null;
  62. this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED);
  63. //FIXME: Maybe here we should set the SRC for the containers to something
  64. } else {
  65. var self = this;
  66. RTC.obtainAudioAndVideoPermissions({
  67. devices: (isAudio ? ["audio"] : ["video"]),
  68. resolution: self.resolution})
  69. .then(function (streams) {
  70. var stream = null;
  71. for(var i = 0; i < streams.length; i++) {
  72. stream = streams[i];
  73. if(stream.type === self.type) {
  74. self.stream = stream.stream;
  75. self.videoType = stream.videoType;
  76. break;
  77. }
  78. }
  79. if(!stream)
  80. return;
  81. for(var i = 0; i < self.containers.length; i++)
  82. {
  83. RTC.attachMediaStream(self.containers[i], self.stream);
  84. }
  85. self.rtc.room.addStream(stream.stream,
  86. function () {
  87. if(isAudio)
  88. self.rtc.room.setAudioMute(mute);
  89. else
  90. self.rtc.room.setVideoMute(mute);
  91. self.eventEmitter.emit(
  92. JitsiTrackEvents.TRACK_MUTE_CHANGED);
  93. });
  94. });
  95. }
  96. }
  97. }
  98. /**
  99. * Stops sending the media track. And removes it from the HTML.
  100. * NOTE: Works for local tracks only.
  101. */
  102. JitsiLocalTrack.prototype.stop = function () {
  103. if(!this.stream)
  104. return;
  105. if(this.rtc)
  106. this.rtc.room.removeStream(this.stream, function () {});
  107. RTC.stopMediaStream(this.stream);
  108. this.detach();
  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.isActive())
  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. /**
  135. * Private method. Updates rtc property of the track.
  136. * @param rtc the rtc instance.
  137. */
  138. JitsiLocalTrack.prototype._setRTC = function (rtc) {
  139. this.rtc = rtc;
  140. };
  141. /**
  142. * Return true;
  143. */
  144. JitsiLocalTrack.prototype.isLocal = function () {
  145. return true;
  146. }
  147. module.exports = JitsiLocalTrack;