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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
  2. var RTCEvents = require("../../service/RTC/RTCEvents");
  3. function LocalStream(stream, type, eventEmitter, videoType, isGUMStream)
  4. {
  5. this.stream = stream;
  6. this.eventEmitter = eventEmitter;
  7. this.type = type;
  8. this.videoType = videoType;
  9. this.isGUMStream = true;
  10. if(isGUMStream === false)
  11. this.isGUMStream = isGUMStream;
  12. var self = this;
  13. if(type == "audio")
  14. {
  15. this.getTracks = function () {
  16. return self.stream.getAudioTracks();
  17. };
  18. }
  19. else
  20. {
  21. this.getTracks = function () {
  22. return self.stream.getVideoTracks();
  23. };
  24. }
  25. this.stream.onended = function()
  26. {
  27. self.streamEnded();
  28. };
  29. }
  30. LocalStream.prototype.streamEnded = function () {
  31. this.eventEmitter.emit(StreamEventTypes.EVENT_TYPE_LOCAL_ENDED, this);
  32. }
  33. LocalStream.prototype.getOriginalStream = function()
  34. {
  35. return this.stream;
  36. }
  37. LocalStream.prototype.isAudioStream = function () {
  38. return this.type === "audio";
  39. };
  40. LocalStream.prototype.setMute = function (mute)
  41. {
  42. var isAudio = this.isAudioStream();
  43. var eventType = isAudio ? RTCEvents.AUDIO_MUTE : RTCEvents.VIDEO_MUTE;
  44. if ((window.location.protocol != "https:" && this.isGUMStream) ||
  45. (isAudio && this.isGUMStream) || this.videoType === "screen")
  46. {
  47. var tracks = this.getTracks();
  48. for (var idx = 0; idx < tracks.length; idx++) {
  49. tracks[idx].enabled = !mute;
  50. }
  51. this.eventEmitter.emit(eventType, mute);
  52. }
  53. else
  54. {
  55. if (mute) {
  56. APP.xmpp.removeStream(this.stream);
  57. this.stream.stop();
  58. this.eventEmitter.emit(eventType, true);
  59. }
  60. else
  61. {
  62. var self = this;
  63. APP.RTC.rtcUtils.obtainAudioAndVideoPermissions(
  64. (this.isAudioStream() ? ["audio"] : ["video"]),
  65. function (stream) {
  66. if (isAudio)
  67. {
  68. APP.RTC.changeLocalAudio(stream,
  69. function () {
  70. self.eventEmitter.emit(eventType, false);
  71. });
  72. }
  73. else
  74. {
  75. APP.RTC.changeLocalVideo(stream, false,
  76. function () {
  77. self.eventEmitter.emit(eventType, false);
  78. });
  79. }
  80. });
  81. }
  82. }
  83. };
  84. LocalStream.prototype.isMuted = function () {
  85. var tracks = [];
  86. if (this.isAudioStream()) {
  87. tracks = this.stream.getAudioTracks();
  88. } else {
  89. if (this.stream.ended)
  90. return true;
  91. tracks = this.stream.getVideoTracks();
  92. }
  93. for (var idx = 0; idx < tracks.length; idx++) {
  94. if(tracks[idx].enabled)
  95. return false;
  96. }
  97. return true;
  98. };
  99. LocalStream.prototype.getId = function () {
  100. return this.stream.getTracks()[0].id;
  101. }
  102. module.exports = LocalStream;