Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

LocalStream.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* global APP */
  2. var MediaStreamType = require("../../service/RTC/MediaStreamTypes");
  3. var RTCEvents = require("../../service/RTC/RTCEvents");
  4. var RTCBrowserType = require("./RTCBrowserType");
  5. var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
  6. /**
  7. * This implements 'onended' callback normally fired by WebRTC after the stream
  8. * is stopped. There is no such behaviour yet in FF, so we have to add it.
  9. * @param stream original WebRTC stream object to which 'onended' handling
  10. * will be added.
  11. */
  12. function implementOnEndedHandling(localStream) {
  13. var stream = localStream.getOriginalStream();
  14. var originalStop = stream.stop;
  15. stream.stop = function () {
  16. originalStop.apply(stream);
  17. if (localStream.isActive()) {
  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 (MediaStreamType.AUDIO_TYPE === type) {
  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 MediaStreamType.AUDIO_TYPE === this.type;
  58. };
  59. LocalStream.prototype.isVideoStream = function () {
  60. return MediaStreamType.VIDEO_TYPE === this.type;
  61. };
  62. LocalStream.prototype.setMute = function (mute)
  63. {
  64. var isAudio = this.isAudioStream();
  65. var eventType = isAudio ? RTCEvents.AUDIO_MUTE : RTCEvents.VIDEO_MUTE;
  66. if ((window.location.protocol != "https:" && this.isGUMStream) ||
  67. (isAudio && this.isGUMStream) || this.videoType === "screen" ||
  68. // FIXME FF does not support 'removeStream' method used to mute
  69. RTCBrowserType.isFirefox()) {
  70. var tracks = this.getTracks();
  71. for (var idx = 0; idx < tracks.length; idx++) {
  72. tracks[idx].enabled = !mute;
  73. }
  74. this.eventEmitter.emit(eventType, mute);
  75. } else {
  76. if (mute) {
  77. APP.xmpp.removeStream(this.stream);
  78. APP.RTC.stopMediaStream(this.stream);
  79. this.eventEmitter.emit(eventType, true);
  80. } else {
  81. var self = this;
  82. APP.RTC.rtcUtils.obtainAudioAndVideoPermissions(
  83. (this.isAudioStream() ? ["audio"] : ["video"]),
  84. function (stream) {
  85. if (isAudio) {
  86. APP.RTC.changeLocalAudio(stream,
  87. function () {
  88. self.eventEmitter.emit(eventType, false);
  89. });
  90. } else {
  91. APP.RTC.changeLocalVideo(stream, false,
  92. function () {
  93. self.eventEmitter.emit(eventType, false);
  94. });
  95. }
  96. });
  97. }
  98. }
  99. };
  100. LocalStream.prototype.isMuted = function () {
  101. var tracks = [];
  102. if (this.isAudioStream()) {
  103. tracks = this.stream.getAudioTracks();
  104. } else {
  105. if (!this.isActive())
  106. return true;
  107. tracks = this.stream.getVideoTracks();
  108. }
  109. for (var idx = 0; idx < tracks.length; idx++) {
  110. if(tracks[idx].enabled)
  111. return false;
  112. }
  113. return true;
  114. };
  115. LocalStream.prototype.getId = function () {
  116. return this.stream.getTracks()[0].id;
  117. };
  118. /**
  119. * Checks whether the MediaStream is avtive/not ended.
  120. * When there is no check for active we don't have information and so
  121. * will return that stream is active (in case of FF).
  122. * @returns {boolean} whether MediaStream is active.
  123. */
  124. LocalStream.prototype.isActive = function () {
  125. if((typeof this.stream.active !== "undefined"))
  126. return this.stream.active;
  127. else
  128. return true;
  129. };
  130. module.exports = LocalStream;