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 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. this.ssrc = null;
  18. //FIXME: This dependacy is not necessary.
  19. this.conference = null;
  20. JitsiTrack.call(this, null, stream,
  21. function () {
  22. if(!this.dontFireRemoveEvent)
  23. this.eventEmitter.emit(
  24. JitsiTrackEvents.TRACK_STOPPED);
  25. this.dontFireRemoveEvent = false;
  26. }.bind(this));
  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. if (this.isMuted() === mute) {
  36. return;
  37. }
  38. if(!this.rtc) {
  39. this.startMuted = mute;
  40. return;
  41. }
  42. var isAudio = this.type === JitsiTrack.AUDIO;
  43. this.dontFireRemoveEvent = false;
  44. if ((window.location.protocol != "https:") ||
  45. (isAudio) || this.videoType === "desktop" ||
  46. // FIXME FF does not support 'removeStream' method used to mute
  47. RTCBrowserType.isFirefox()) {
  48. var tracks = this._getTracks();
  49. for (var idx = 0; idx < tracks.length; idx++) {
  50. tracks[idx].enabled = !mute;
  51. }
  52. if(isAudio)
  53. this.rtc.room.setAudioMute(mute);
  54. else
  55. this.rtc.room.setVideoMute(mute);
  56. this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED);
  57. } else {
  58. if (mute) {
  59. this.dontFireRemoveEvent = true;
  60. this.rtc.room.removeStream(this.stream, function () {},
  61. {mtype: this.type, type: "mute", ssrc: this.ssrc});
  62. RTCUtils.stopMediaStream(this.stream);
  63. if(isAudio)
  64. this.rtc.room.setAudioMute(mute);
  65. else
  66. this.rtc.room.setVideoMute(mute);
  67. this.stream = null;
  68. this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED);
  69. //FIXME: Maybe here we should set the SRC for the containers to something
  70. } else {
  71. var self = this;
  72. var streamOptions = {
  73. devices: (isAudio ? ["audio"] : ["video"]),
  74. resolution: self.resolution
  75. };
  76. if (isAudio) {
  77. streamOptions['micDeviceId'] = self.deviceId;
  78. } else if(self.videoType === 'camera') {
  79. streamOptions['cameraDeviceId'] = self.deviceId;
  80. }
  81. RTCUtils.obtainAudioAndVideoPermissions(streamOptions)
  82. .then(function (streams) {
  83. var stream = null;
  84. for(var i = 0; i < streams.length; i++) {
  85. stream = streams[i];
  86. if(stream.type === self.type) {
  87. self.stream = stream.stream;
  88. self.videoType = stream.videoType;
  89. break;
  90. }
  91. }
  92. if(!stream)
  93. return;
  94. for(var i = 0; i < self.containers.length; i++)
  95. {
  96. self.containers[i]
  97. = RTCUtils.attachMediaStream(
  98. self.containers[i], self.stream);
  99. }
  100. self.rtc.room.addStream(self.stream,
  101. function () {
  102. if(isAudio)
  103. self.rtc.room.setAudioMute(mute);
  104. else
  105. self.rtc.room.setVideoMute(mute);
  106. self.eventEmitter.emit(
  107. JitsiTrackEvents.TRACK_MUTE_CHANGED);
  108. }, {
  109. mtype: self.type,
  110. type: "unmute",
  111. ssrc: self.ssrc,
  112. msid: self.getMSID()});
  113. });
  114. }
  115. }
  116. }
  117. /**
  118. * Stops sending the media track. And removes it from the HTML.
  119. * NOTE: Works for local tracks only.
  120. */
  121. JitsiLocalTrack.prototype.stop = function () {
  122. if(this.conference){
  123. this.conference.removeTrack(this);
  124. }
  125. if(!this.stream)
  126. return;
  127. RTCUtils.stopMediaStream(this.stream);
  128. this.detach();
  129. }
  130. /**
  131. * Returns <tt>true</tt> - if the stream is muted
  132. * and <tt>false</tt> otherwise.
  133. * @returns {boolean} <tt>true</tt> - if the stream is muted
  134. * and <tt>false</tt> otherwise.
  135. */
  136. JitsiLocalTrack.prototype.isMuted = function () {
  137. if (!this.stream)
  138. return true;
  139. var tracks = [];
  140. var isAudio = this.type === JitsiTrack.AUDIO;
  141. if (isAudio) {
  142. tracks = this.stream.getAudioTracks();
  143. } else {
  144. if (!this.isActive())
  145. return true;
  146. tracks = this.stream.getVideoTracks();
  147. }
  148. for (var idx = 0; idx < tracks.length; idx++) {
  149. if(tracks[idx].enabled)
  150. return false;
  151. }
  152. return true;
  153. };
  154. /**
  155. * Private method. Updates rtc property of the track.
  156. * @param rtc the rtc instance.
  157. */
  158. JitsiLocalTrack.prototype._setRTC = function (rtc) {
  159. this.rtc = rtc;
  160. };
  161. /**
  162. * Updates the SSRC associated with the MediaStream in JitsiLocalTrack object.
  163. * @ssrc the new ssrc
  164. */
  165. JitsiLocalTrack.prototype._setSSRC = function (ssrc) {
  166. this.ssrc = ssrc;
  167. }
  168. //FIXME: This dependacy is not necessary. This is quick fix.
  169. /**
  170. * Sets the JitsiConference object associated with the track. This is temp
  171. * solution.
  172. * @param conference the JitsiConference object
  173. */
  174. JitsiLocalTrack.prototype._setConference = function(conference) {
  175. this.conference = conference;
  176. }
  177. /**
  178. * Gets the SSRC of this local track if it's available already or <tt>null</tt>
  179. * otherwise. That's because we don't know the SSRC until local description is
  180. * created.
  181. * @returns {string} or {null}
  182. */
  183. JitsiLocalTrack.prototype.getSSRC = function () {
  184. if (!this.rtc.room.session)
  185. return null;
  186. if (this.isAudioTrack()) {
  187. return this.rtc.room.session.localStreamsSSRC.audio;
  188. } else {
  189. return this.rtc.room.session.localStreamsSSRC.video;
  190. }
  191. };
  192. /**
  193. * Return true;
  194. */
  195. JitsiLocalTrack.prototype.isLocal = function () {
  196. return true;
  197. }
  198. module.exports = JitsiLocalTrack;