Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

JitsiLocalTrack.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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, deviceId)
  11. {
  12. this.videoType = videoType;
  13. this.dontFireRemoveEvent = false;
  14. this.resolution = resolution;
  15. this.deviceId = deviceId;
  16. this.startMuted = false;
  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.isMuted() === mute) {
  34. return;
  35. }
  36. if(!this.rtc) {
  37. this.startMuted = mute;
  38. return;
  39. }
  40. var isAudio = this.type === JitsiTrack.AUDIO;
  41. this.dontFireRemoveEvent = false;
  42. if ((window.location.protocol != "https:") ||
  43. (isAudio) || this.videoType === "desktop" ||
  44. // FIXME FF does not support 'removeStream' method used to mute
  45. RTCBrowserType.isFirefox()) {
  46. var tracks = this._getTracks();
  47. for (var idx = 0; idx < tracks.length; idx++) {
  48. tracks[idx].enabled = !mute;
  49. }
  50. if(isAudio)
  51. this.rtc.room.setAudioMute(mute);
  52. else
  53. this.rtc.room.setVideoMute(mute);
  54. this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED);
  55. } else {
  56. if (mute) {
  57. this.dontFireRemoveEvent = true;
  58. this.rtc.room.removeStream(this.stream, function () {});
  59. RTCUtils.stopMediaStream(this.stream);
  60. if(isAudio)
  61. this.rtc.room.setAudioMute(mute);
  62. else
  63. this.rtc.room.setVideoMute(mute);
  64. this.stream = null;
  65. this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED);
  66. //FIXME: Maybe here we should set the SRC for the containers to something
  67. } else {
  68. var self = this;
  69. var streamOptions = {
  70. devices: (isAudio ? ["audio"] : ["video"]),
  71. resolution: self.resolution
  72. };
  73. if (isAudio) {
  74. streamOptions['micDeviceId'] = self.deviceId;
  75. } else {self.videoType === 'camera'} {
  76. streamOptions['cameraDeviceId'] = self.deviceId;
  77. }
  78. RTCUtils.obtainAudioAndVideoPermissions(streamOptions)
  79. .then(function (streams) {
  80. var stream = null;
  81. for(var i = 0; i < streams.length; i++) {
  82. stream = streams[i];
  83. if(stream.type === self.type) {
  84. self.stream = stream.stream;
  85. self.videoType = stream.videoType;
  86. break;
  87. }
  88. }
  89. if(!stream)
  90. return;
  91. for(var i = 0; i < self.containers.length; i++)
  92. {
  93. RTCUtils.attachMediaStream(
  94. self.containers[i], self.stream);
  95. }
  96. self.rtc.room.addStream(stream.stream,
  97. function () {
  98. if(isAudio)
  99. self.rtc.room.setAudioMute(mute);
  100. else
  101. self.rtc.room.setVideoMute(mute);
  102. self.eventEmitter.emit(
  103. JitsiTrackEvents.TRACK_MUTE_CHANGED);
  104. });
  105. });
  106. }
  107. }
  108. }
  109. /**
  110. * Stops sending the media track. And removes it from the HTML.
  111. * NOTE: Works for local tracks only.
  112. */
  113. JitsiLocalTrack.prototype.stop = function () {
  114. if(!this.stream)
  115. return;
  116. if(this.rtc)
  117. this.rtc.room.removeStream(this.stream, function () {});
  118. RTCUtils.stopMediaStream(this.stream);
  119. this.detach();
  120. }
  121. /**
  122. * Returns <tt>true</tt> - if the stream is muted
  123. * and <tt>false</tt> otherwise.
  124. * @returns {boolean} <tt>true</tt> - if the stream is muted
  125. * and <tt>false</tt> otherwise.
  126. */
  127. JitsiLocalTrack.prototype.isMuted = function () {
  128. if (!this.stream)
  129. return true;
  130. var tracks = [];
  131. var isAudio = this.type === JitsiTrack.AUDIO;
  132. if (isAudio) {
  133. tracks = this.stream.getAudioTracks();
  134. } else {
  135. if (!this.isActive())
  136. return true;
  137. tracks = this.stream.getVideoTracks();
  138. }
  139. for (var idx = 0; idx < tracks.length; idx++) {
  140. if(tracks[idx].enabled)
  141. return false;
  142. }
  143. return true;
  144. };
  145. /**
  146. * Private method. Updates rtc property of the track.
  147. * @param rtc the rtc instance.
  148. */
  149. JitsiLocalTrack.prototype._setRTC = function (rtc) {
  150. this.rtc = rtc;
  151. };
  152. /**
  153. * Return true;
  154. */
  155. JitsiLocalTrack.prototype.isLocal = function () {
  156. return true;
  157. }
  158. module.exports = JitsiLocalTrack;