modified lib-jitsi-meet dev repo
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.

LocalStream.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* global APP */
  2. var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
  3. var RTCEvents = require("../../service/RTC/RTCEvents");
  4. var RTCBrowserType = require("./RTCBrowserType");
  5. /**
  6. * This implements 'onended' callback normally fired by WebRTC after the stream
  7. * is stopped. There is no such behaviour yet in FF, so we have to add it.
  8. * @param stream original WebRTC stream object to which 'onended' handling
  9. * will be added.
  10. */
  11. function implementOnEndedHandling(stream) {
  12. var originalStop = stream.stop;
  13. stream.stop = function () {
  14. originalStop.apply(stream);
  15. if (!stream.ended) {
  16. stream.ended = true;
  17. stream.onended();
  18. }
  19. };
  20. }
  21. function LocalStream(RTC, stream, type, eventEmitter, videoType, isGUMStream) {
  22. this.rtc = RTC;
  23. this.stream = stream;
  24. this.eventEmitter = eventEmitter;
  25. this.type = type;
  26. this.videoType = videoType;
  27. this.isGUMStream = true;
  28. if(isGUMStream === false)
  29. this.isGUMStream = isGUMStream;
  30. var self = this;
  31. if(type == "audio") {
  32. this.getTracks = function () {
  33. return self.stream.getAudioTracks();
  34. };
  35. } else {
  36. this.getTracks = function () {
  37. return self.stream.getVideoTracks();
  38. };
  39. }
  40. this.stream.onended = function () {
  41. self.streamEnded();
  42. };
  43. if (RTCBrowserType.isFirefox()) {
  44. implementOnEndedHandling(this.stream);
  45. }
  46. }
  47. LocalStream.prototype.streamEnded = function () {
  48. this.eventEmitter.emit(StreamEventTypes.EVENT_TYPE_LOCAL_ENDED, this);
  49. };
  50. LocalStream.prototype.getOriginalStream = function()
  51. {
  52. return this.stream;
  53. };
  54. LocalStream.prototype.isAudioStream = function () {
  55. return this.type === "audio";
  56. };
  57. LocalStream.prototype.setMute = function (mute)
  58. {
  59. var isAudio = this.isAudioStream();
  60. var eventType = isAudio ? RTCEvents.AUDIO_MUTE : RTCEvents.VIDEO_MUTE;
  61. if ((window.location.protocol != "https:" && this.isGUMStream) ||
  62. (isAudio && this.isGUMStream) || this.videoType === "screen" ||
  63. // FIXME FF does not support 'removeStream' method used to mute
  64. RTCBrowserType.isFirefox()) {
  65. var tracks = this.getTracks();
  66. for (var idx = 0; idx < tracks.length; idx++) {
  67. tracks[idx].enabled = !mute;
  68. }
  69. this.eventEmitter.emit(eventType, mute);
  70. } else {
  71. if (mute) {
  72. APP.xmpp.removeStream(this.stream);
  73. this.stream.stop();
  74. this.eventEmitter.emit(eventType, true);
  75. } else {
  76. var self = this;
  77. this.rtc.obtainAudioAndVideoPermissions(
  78. {devices: (this.isAudioStream() ? ["audio"] : ["video"])})
  79. .then(function (stream) {
  80. if (isAudio) {
  81. self.rtc.changeLocalAudio(stream,
  82. function () {
  83. self.eventEmitter.emit(eventType, false);
  84. });
  85. } else {
  86. self.rtc.changeLocalVideo(stream, false,
  87. function () {
  88. self.eventEmitter.emit(eventType, false);
  89. });
  90. }
  91. });
  92. }
  93. }
  94. };
  95. LocalStream.prototype.isMuted = function () {
  96. var tracks = [];
  97. if (this.isAudioStream()) {
  98. tracks = this.stream.getAudioTracks();
  99. } else {
  100. if (this.stream.ended)
  101. return true;
  102. tracks = this.stream.getVideoTracks();
  103. }
  104. for (var idx = 0; idx < tracks.length; idx++) {
  105. if(tracks[idx].enabled)
  106. return false;
  107. }
  108. return true;
  109. };
  110. LocalStream.prototype.getId = function () {
  111. return this.stream.getTracks()[0].id;
  112. };
  113. module.exports = LocalStream;