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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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(stream, type, eventEmitter, videoType, isGUMStream) {
  22. this.stream = stream;
  23. this.eventEmitter = eventEmitter;
  24. this.type = type;
  25. this.videoType = videoType;
  26. this.isGUMStream = true;
  27. if(isGUMStream === false)
  28. this.isGUMStream = isGUMStream;
  29. var self = this;
  30. if(type == "audio") {
  31. this.getTracks = function () {
  32. return self.stream.getAudioTracks();
  33. };
  34. } else {
  35. this.getTracks = function () {
  36. return self.stream.getVideoTracks();
  37. };
  38. }
  39. APP.RTC.addMediaStreamInactiveHandler(
  40. this.stream,
  41. function () {
  42. self.streamEnded();
  43. });
  44. if (RTCBrowserType.isFirefox()) {
  45. implementOnEndedHandling(this.stream);
  46. }
  47. }
  48. LocalStream.prototype.streamEnded = function () {
  49. this.eventEmitter.emit(StreamEventTypes.EVENT_TYPE_LOCAL_ENDED, this);
  50. };
  51. LocalStream.prototype.getOriginalStream = function()
  52. {
  53. return this.stream;
  54. };
  55. LocalStream.prototype.isAudioStream = function () {
  56. return this.type === "audio";
  57. };
  58. LocalStream.prototype.setMute = function (mute)
  59. {
  60. var isAudio = this.isAudioStream();
  61. var eventType = isAudio ? RTCEvents.AUDIO_MUTE : RTCEvents.VIDEO_MUTE;
  62. if ((window.location.protocol != "https:" && this.isGUMStream) ||
  63. (isAudio && this.isGUMStream) || this.videoType === "screen" ||
  64. // FIXME FF does not support 'removeStream' method used to mute
  65. RTCBrowserType.isFirefox()) {
  66. var tracks = this.getTracks();
  67. for (var idx = 0; idx < tracks.length; idx++) {
  68. tracks[idx].enabled = !mute;
  69. }
  70. this.eventEmitter.emit(eventType, mute);
  71. } else {
  72. if (mute) {
  73. APP.xmpp.removeStream(this.stream);
  74. this.stream.stop();
  75. this.eventEmitter.emit(eventType, true);
  76. } else {
  77. var self = this;
  78. APP.RTC.rtcUtils.obtainAudioAndVideoPermissions(
  79. (this.isAudioStream() ? ["audio"] : ["video"]),
  80. function (stream) {
  81. if (isAudio) {
  82. APP.RTC.changeLocalAudio(stream,
  83. function () {
  84. self.eventEmitter.emit(eventType, false);
  85. });
  86. } else {
  87. APP.RTC.changeLocalVideo(stream, false,
  88. function () {
  89. self.eventEmitter.emit(eventType, false);
  90. });
  91. }
  92. });
  93. }
  94. }
  95. };
  96. LocalStream.prototype.isMuted = function () {
  97. var tracks = [];
  98. if (this.isAudioStream()) {
  99. tracks = this.stream.getAudioTracks();
  100. } else {
  101. if (this.stream.ended)
  102. return true;
  103. tracks = this.stream.getVideoTracks();
  104. }
  105. for (var idx = 0; idx < tracks.length; idx++) {
  106. if(tracks[idx].enabled)
  107. return false;
  108. }
  109. return true;
  110. };
  111. LocalStream.prototype.getId = function () {
  112. return this.stream.getTracks()[0].id;
  113. };
  114. module.exports = LocalStream;