選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

LocalStream.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // FIXME FF does not support 'removeStream' method used to mute
  63. RTCBrowserType.isFirefox()) {
  64. var tracks = this.getTracks();
  65. for (var idx = 0; idx < tracks.length; idx++) {
  66. tracks[idx].enabled = !mute;
  67. }
  68. this.eventEmitter.emit(eventType, mute);
  69. } else {
  70. if (mute) {
  71. APP.xmpp.removeStream(this.stream);
  72. this.stream.stop();
  73. this.eventEmitter.emit(eventType, true);
  74. } else {
  75. var self = this;
  76. APP.RTC.rtcUtils.obtainAudioAndVideoPermissions(
  77. (this.isAudioStream() ? ["audio"] : ["video"]),
  78. function (stream) {
  79. if (isAudio) {
  80. APP.RTC.changeLocalAudio(stream,
  81. function () {
  82. self.eventEmitter.emit(eventType, false);
  83. });
  84. } else {
  85. APP.RTC.changeLocalVideo(stream, false,
  86. function () {
  87. self.eventEmitter.emit(eventType, false);
  88. });
  89. }
  90. });
  91. }
  92. }
  93. };
  94. LocalStream.prototype.isMuted = function () {
  95. var tracks = [];
  96. if (this.isAudioStream()) {
  97. tracks = this.stream.getAudioTracks();
  98. } else {
  99. if (this.stream.ended)
  100. return true;
  101. tracks = this.stream.getVideoTracks();
  102. }
  103. for (var idx = 0; idx < tracks.length; idx++) {
  104. if(tracks[idx].enabled)
  105. return false;
  106. }
  107. return true;
  108. };
  109. LocalStream.prototype.getId = function () {
  110. return this.stream.getTracks()[0].id;
  111. };
  112. module.exports = LocalStream;