jiti-meet/modules/RTC/LocalStream.js

125 lines
3.7 KiB
JavaScript
Raw Normal View History

2015-07-28 19:42:01 +00:00
/* global APP */
var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
2015-06-26 12:32:40 +00:00
var RTCEvents = require("../../service/RTC/RTCEvents");
var RTCBrowserType = require("./RTCBrowserType");
/**
* This implements 'onended' callback normally fired by WebRTC after the stream
* is stopped. There is no such behaviour yet in FF, so we have to add it.
* @param stream original WebRTC stream object to which 'onended' handling
* will be added.
*/
function implementOnEndedHandling(stream) {
var originalStop = stream.stop;
stream.stop = function () {
originalStop.apply(stream);
if (!stream.ended) {
stream.ended = true;
stream.onended();
}
};
}
2014-12-19 13:59:08 +00:00
2015-07-28 19:42:01 +00:00
function LocalStream(stream, type, eventEmitter, videoType, isGUMStream) {
2014-12-19 13:59:08 +00:00
this.stream = stream;
this.eventEmitter = eventEmitter;
this.type = type;
this.videoType = videoType;
this.isGUMStream = true;
if(isGUMStream === false)
this.isGUMStream = isGUMStream;
2014-12-19 13:59:08 +00:00
var self = this;
2015-07-28 19:42:01 +00:00
if(type == "audio") {
this.getTracks = function () {
return self.stream.getAudioTracks();
};
2015-07-28 19:42:01 +00:00
} else {
this.getTracks = function () {
return self.stream.getVideoTracks();
};
}
this.stream.onended = function () {
2014-12-19 13:59:08 +00:00
self.streamEnded();
};
if (RTCBrowserType.isFirefox()) {
implementOnEndedHandling(this.stream);
}
2014-12-19 13:59:08 +00:00
}
LocalStream.prototype.streamEnded = function () {
this.eventEmitter.emit(StreamEventTypes.EVENT_TYPE_LOCAL_ENDED, this);
2015-07-28 19:42:01 +00:00
};
2014-12-19 13:59:08 +00:00
LocalStream.prototype.getOriginalStream = function()
{
return this.stream;
2015-07-28 19:42:01 +00:00
};
2014-12-19 13:59:08 +00:00
LocalStream.prototype.isAudioStream = function () {
return this.type === "audio";
};
LocalStream.prototype.setMute = function (mute)
{
var isAudio = this.isAudioStream();
var eventType = isAudio ? RTCEvents.AUDIO_MUTE : RTCEvents.VIDEO_MUTE;
if ((window.location.protocol != "https:" && this.isGUMStream) ||
(isAudio && this.isGUMStream) || this.videoType === "screen" ||
// FIXME FF does not support 'removeStream' method used to mute
RTCBrowserType.isFirefox()) {
var tracks = this.getTracks();
for (var idx = 0; idx < tracks.length; idx++) {
tracks[idx].enabled = !mute;
}
this.eventEmitter.emit(eventType, mute);
2015-07-28 19:42:01 +00:00
} else {
if (mute) {
APP.xmpp.removeStream(this.stream);
this.stream.stop();
this.eventEmitter.emit(eventType, true);
2015-07-28 19:42:01 +00:00
} else {
var self = this;
APP.RTC.rtcUtils.obtainAudioAndVideoPermissions(
(this.isAudioStream() ? ["audio"] : ["video"]),
function (stream) {
2015-07-28 19:42:01 +00:00
if (isAudio) {
2015-06-26 12:32:40 +00:00
APP.RTC.changeLocalAudio(stream,
function () {
self.eventEmitter.emit(eventType, false);
2015-06-26 12:32:40 +00:00
});
2015-07-28 19:42:01 +00:00
} else {
2015-06-26 12:32:40 +00:00
APP.RTC.changeLocalVideo(stream, false,
function () {
self.eventEmitter.emit(eventType, false);
2015-06-26 12:32:40 +00:00
});
}
});
}
}
};
2014-12-19 13:59:08 +00:00
LocalStream.prototype.isMuted = function () {
var tracks = [];
if (this.isAudioStream()) {
2014-12-19 13:59:08 +00:00
tracks = this.stream.getAudioTracks();
} else {
if (this.stream.ended)
return true;
2014-12-19 13:59:08 +00:00
tracks = this.stream.getVideoTracks();
}
for (var idx = 0; idx < tracks.length; idx++) {
if(tracks[idx].enabled)
return false;
}
return true;
};
2014-12-19 13:59:08 +00:00
LocalStream.prototype.getId = function () {
return this.stream.getTracks()[0].id;
2015-07-28 19:42:01 +00:00
};
2014-12-19 13:59:08 +00:00
module.exports = LocalStream;