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

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