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 RTCUtils = 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, function () {});
  55. RTCUtils.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. RTCUtils.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. RTCUtils.attachMediaStream(
  83. 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. RTCUtils.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;