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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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(localStream) {
  12. var stream = localStream.getOriginalStream();
  13. var originalStop = stream.stop;
  14. stream.stop = function () {
  15. originalStop.apply(stream);
  16. if (localStream.isActive()) {
  17. stream.ended = true;
  18. stream.onended();
  19. }
  20. };
  21. }
  22. function LocalStream(stream, type, eventEmitter, videoType, isGUMStream) {
  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. APP.RTC.addMediaStreamInactiveHandler(
  41. this.stream,
  42. function () {
  43. self.streamEnded();
  44. });
  45. if (RTCBrowserType.isFirefox()) {
  46. implementOnEndedHandling(this);
  47. }
  48. }
  49. LocalStream.prototype.streamEnded = function () {
  50. this.eventEmitter.emit(StreamEventTypes.EVENT_TYPE_LOCAL_ENDED, this);
  51. };
  52. LocalStream.prototype.getOriginalStream = function()
  53. {
  54. return this.stream;
  55. };
  56. LocalStream.prototype.isAudioStream = function () {
  57. return this.type === "audio";
  58. };
  59. LocalStream.prototype.setMute = function (mute)
  60. {
  61. var isAudio = this.isAudioStream();
  62. var eventType = isAudio ? RTCEvents.AUDIO_MUTE : RTCEvents.VIDEO_MUTE;
  63. if ((window.location.protocol != "https:" && this.isGUMStream) ||
  64. (isAudio && this.isGUMStream) || this.videoType === "screen" ||
  65. // FIXME FF does not support 'removeStream' method used to mute
  66. RTCBrowserType.isFirefox()) {
  67. var tracks = this.getTracks();
  68. for (var idx = 0; idx < tracks.length; idx++) {
  69. tracks[idx].enabled = !mute;
  70. }
  71. this.eventEmitter.emit(eventType, mute);
  72. } else {
  73. if (mute) {
  74. APP.xmpp.removeStream(this.stream);
  75. APP.RTC.stopMediaStream(this.stream);
  76. this.eventEmitter.emit(eventType, true);
  77. } else {
  78. var self = this;
  79. APP.RTC.rtcUtils.obtainAudioAndVideoPermissions(
  80. (this.isAudioStream() ? ["audio"] : ["video"]),
  81. function (stream) {
  82. if (isAudio) {
  83. APP.RTC.changeLocalAudio(stream,
  84. function () {
  85. self.eventEmitter.emit(eventType, false);
  86. });
  87. } else {
  88. APP.RTC.changeLocalVideo(stream, false,
  89. function () {
  90. self.eventEmitter.emit(eventType, false);
  91. });
  92. }
  93. });
  94. }
  95. }
  96. };
  97. LocalStream.prototype.isMuted = function () {
  98. var tracks = [];
  99. if (this.isAudioStream()) {
  100. tracks = this.stream.getAudioTracks();
  101. } else {
  102. if (!this.isActive())
  103. return true;
  104. tracks = this.stream.getVideoTracks();
  105. }
  106. for (var idx = 0; idx < tracks.length; idx++) {
  107. if(tracks[idx].enabled)
  108. return false;
  109. }
  110. return true;
  111. };
  112. LocalStream.prototype.getId = function () {
  113. return this.stream.getTracks()[0].id;
  114. };
  115. /**
  116. * Checks whether the MediaStream is avtive/not ended.
  117. * @returns {boolean} whether MediaStream is active.
  118. */
  119. LocalStream.prototype.isActive = function () {
  120. return !this.stream.ended;
  121. };
  122. module.exports = LocalStream;