2015-07-28 19:42:01 +00:00
|
|
|
/* global APP */
|
2015-01-28 14:35:22 +00:00
|
|
|
var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
|
2015-06-26 12:32:40 +00:00
|
|
|
var RTCEvents = require("../../service/RTC/RTCEvents");
|
2015-08-06 09:53:10 +00:00
|
|
|
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;
|
2015-01-20 15:56:00 +00:00
|
|
|
this.videoType = videoType;
|
2015-04-22 09:31:08 +00:00
|
|
|
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") {
|
2015-01-12 13:23:29 +00:00
|
|
|
this.getTracks = function () {
|
|
|
|
return self.stream.getAudioTracks();
|
|
|
|
};
|
2015-07-28 19:42:01 +00:00
|
|
|
} else {
|
2015-01-12 13:23:29 +00:00
|
|
|
this.getTracks = function () {
|
|
|
|
return self.stream.getVideoTracks();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-08-06 09:53:10 +00:00
|
|
|
this.stream.onended = function () {
|
2014-12-19 13:59:08 +00:00
|
|
|
self.streamEnded();
|
|
|
|
};
|
2015-08-06 09:53:10 +00:00
|
|
|
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 () {
|
2015-04-22 09:31:08 +00:00
|
|
|
return this.type === "audio";
|
2015-01-12 13:23:29 +00:00
|
|
|
};
|
|
|
|
|
2015-07-30 12:11:11 +00:00
|
|
|
LocalStream.prototype.setMute = function (mute)
|
2015-01-12 13:23:29 +00:00
|
|
|
{
|
2015-07-30 12:11:11 +00:00
|
|
|
var isAudio = this.isAudioStream();
|
|
|
|
var eventType = isAudio ? RTCEvents.AUDIO_MUTE : RTCEvents.VIDEO_MUTE;
|
2015-01-12 13:23:29 +00:00
|
|
|
|
2015-07-30 12:11:11 +00:00
|
|
|
if ((window.location.protocol != "https:" && this.isGUMStream) ||
|
2015-08-07 07:32:57 +00:00
|
|
|
(isAudio && this.isGUMStream) || this.videoType === "screen" ||
|
|
|
|
// FIXME FF does not support 'removeStream' method used to mute
|
|
|
|
RTCBrowserType.isFirefox()) {
|
2015-03-23 16:12:24 +00:00
|
|
|
|
2015-08-07 07:32:57 +00:00
|
|
|
var tracks = this.getTracks();
|
2015-03-23 16:12:24 +00:00
|
|
|
for (var idx = 0; idx < tracks.length; idx++) {
|
2015-07-30 12:11:11 +00:00
|
|
|
tracks[idx].enabled = !mute;
|
2015-03-23 16:12:24 +00:00
|
|
|
}
|
2015-07-30 12:11:11 +00:00
|
|
|
this.eventEmitter.emit(eventType, mute);
|
2015-07-28 19:42:01 +00:00
|
|
|
} else {
|
2015-07-30 12:11:11 +00:00
|
|
|
if (mute) {
|
2015-03-23 16:12:24 +00:00
|
|
|
APP.xmpp.removeStream(this.stream);
|
|
|
|
this.stream.stop();
|
2015-07-30 12:11:11 +00:00
|
|
|
this.eventEmitter.emit(eventType, true);
|
2015-07-28 19:42:01 +00:00
|
|
|
} else {
|
2015-04-22 09:31:08 +00:00
|
|
|
var self = this;
|
|
|
|
APP.RTC.rtcUtils.obtainAudioAndVideoPermissions(
|
|
|
|
(this.isAudioStream() ? ["audio"] : ["video"]),
|
2015-03-23 16:12:24 +00:00
|
|
|
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 () {
|
2015-07-30 12:11:11 +00:00
|
|
|
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 () {
|
2015-07-30 12:11:11 +00:00
|
|
|
self.eventEmitter.emit(eventType, false);
|
2015-06-26 12:32:40 +00:00
|
|
|
});
|
2015-04-22 09:31:08 +00:00
|
|
|
}
|
2015-03-23 16:12:24 +00:00
|
|
|
});
|
|
|
|
}
|
2015-01-12 13:23:29 +00:00
|
|
|
}
|
|
|
|
};
|
2014-12-19 13:59:08 +00:00
|
|
|
|
|
|
|
LocalStream.prototype.isMuted = function () {
|
|
|
|
var tracks = [];
|
2015-07-22 18:34:44 +00:00
|
|
|
if (this.isAudioStream()) {
|
2014-12-19 13:59:08 +00:00
|
|
|
tracks = this.stream.getAudioTracks();
|
2015-07-22 18:34:44 +00:00
|
|
|
} else {
|
|
|
|
if (this.stream.ended)
|
2015-03-23 16:12:24 +00:00
|
|
|
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;
|
2015-07-22 18:34:44 +00:00
|
|
|
};
|
2014-12-19 13:59:08 +00:00
|
|
|
|
2015-01-12 13:23:29 +00:00
|
|
|
LocalStream.prototype.getId = function () {
|
|
|
|
return this.stream.getTracks()[0].id;
|
2015-07-28 19:42:01 +00:00
|
|
|
};
|
2015-01-12 13:23:29 +00:00
|
|
|
|
2014-12-19 13:59:08 +00:00
|
|
|
module.exports = LocalStream;
|