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

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