1
0
mirror of https://github.com/KubaPro010/fm-dx-webserver.git synced 2026-02-26 22:13:53 +01:00
Files
fm-dx-webserver/web/js/3las/3las.js
2024-02-05 23:09:39 +01:00

185 lines
6.8 KiB
JavaScript

var _3LAS_Settings = /** @class */ (function () {
function _3LAS_Settings() {
this.SocketHost = document.location.hostname ? document.location.hostname : "127.0.0.1";
this.SocketPort = localStorage.getItem('audioPort') ? localStorage.getItem('audioPort') : 8081;
this.SocketPath = "/";
this.WebRTC = new WebRTC_Settings();
this.Fallback = new Fallback_Settings();
}
return _3LAS_Settings;
}());
var _3LAS = /** @class */ (function () {
function _3LAS(logger, settings) {
this.Logger = logger;
if (!this.Logger) {
this.Logger = new Logging(null, null);
}
this.Settings = settings;
try {
this.WebRTC = new WebRTC(this.Logger, this.Settings.WebRTC);
this.WebRTC.ActivityCallback = this.OnActivity.bind(this);
this.WebRTC.DisconnectCallback = this.OnSocketDisconnect.bind(this);
}
catch (_a) {
this.WebRTC = null;
}
if (this.WebRTC == null || this.WebRTC !== null) {
try {
this.Fallback = new Fallback(this.Logger, this.Settings.Fallback);
this.Fallback.ActivityCallback = this.OnActivity.bind(this);
}
catch (_b) {
this.Fallback = null;
}
}
if (this.WebRTC == null && this.Fallback == null) {
this.Logger.Log('3LAS: Browser does not support either media handling methods.');
throw new Error();
}
if (isAndroid) {
this.WakeLock = new WakeLock(this.Logger);
}
}
Object.defineProperty(_3LAS.prototype, "Volume", {
get: function () {
if (this.WebRTC)
return this.WebRTC.Volume;
else
return this.Fallback.Volume;
},
set: function (value) {
if (this.WebRTC)
this.WebRTC.Volume = value;
else
this.Fallback.Volume = value;
},
enumerable: false,
configurable: true
});
_3LAS.prototype.CanChangeVolume = function () {
if (this.WebRTC)
return this.WebRTC.CanChangeVolume();
else
return true;
};
_3LAS.prototype.Start = function () {
this.ConnectivityFlag = false;
// This is stupid, but required for iOS/iPadOS... thanks Apple :(
if (this.Settings && this.Settings.WebRTC && this.Settings.WebRTC.AudioTag)
this.Settings.WebRTC.AudioTag.play();
// This is stupid, but required for Android.... thanks Google :(
if (this.WakeLock)
this.WakeLock.Begin();
try {
if (window.location.protocol === 'https:') {
this.WebSocket = new WebSocketClient(this.Logger, 'wss://' + this.Settings.SocketHost + ':' + window.location.pathname + 'stream' + this.Settings.SocketPath, this.OnSocketError.bind(this), this.OnSocketConnect.bind(this), this.OnSocketDataReady.bind(this), this.OnSocketDisconnect.bind(this));
}
else {
this.WebSocket = new WebSocketClient(this.Logger, 'ws://' + this.Settings.SocketHost + ':' + this.Settings.SocketPort.toString() + this.Settings.SocketPath, this.OnSocketError.bind(this), this.OnSocketConnect.bind(this), this.OnSocketDataReady.bind(this), this.OnSocketDisconnect.bind(this));
}
this.Logger.Log("Init of WebSocketClient succeeded");
this.Logger.Log("Trying to connect to server.");
}
catch (e) {
this.Logger.Log("Init of WebSocketClient failed: " + e);
throw new Error();
}
};
_3LAS.prototype.Stop = function () {
try {
// Close WebSocket connection
if (this.WebSocket) {
this.WebSocket.Close();
this.WebSocket.OnClose();
this.WebSocket = null;
this.Logger.Log("WebSocket connection closed.");
}
// Stop WakeLock if it exists and is an Android device
if (isAndroid && this.WakeLock) {
this.WakeLock.End();
this.Logger.Log("WakeLock stopped.");
}
// Reset WebRTC if it exists
if (this.WebRTC) {
this.WebRTC.OnSocketDisconnect();
this.WebRTC.Reset();
this.WebRTC.Stop();
this.Logger.Log("WebRTC reset.");
}
// Reset Fallback if it exists
if (this.Fallback) {
this.Fallback.OnSocketDisconnect();
this.Fallback.Stop();
this.Fallback.Reset();
this.Logger.Log("Fallback reset.");
}
// Reset connectivity flag
if (this.ConnectivityFlag) {
this.ConnectivityFlag = null;
if (this.ConnectivityCallback) {
this.ConnectivityCallback(null);
}
}
this.Logger.Log("3LAS stopped successfully.");
} catch (e) {
this.Logger.Log("Error while stopping 3LAS: " + e);
}
};
_3LAS.prototype.OnActivity = function () {
if (this.ActivityCallback)
this.ActivityCallback();
if (!this.ConnectivityFlag) {
this.ConnectivityFlag = true;
if (this.ConnectivityCallback)
this.ConnectivityCallback(true);
}
};
// Callback function from socket connection
_3LAS.prototype.OnSocketError = function (message) {
this.Logger.Log("Network error: " + message);
if (this.WebRTC)
this.WebRTC.OnSocketError(message);
else
this.Fallback.OnSocketError(message);
};
_3LAS.prototype.OnSocketConnect = function () {
this.Logger.Log("Established connection with server.");
if (this.WebRTC)
this.WebRTC.OnSocketConnect();
else
this.Fallback.OnSocketConnect();
if (this.WebRTC)
this.WebRTC.Init(this.WebSocket);
else
this.Fallback.Init(this.WebSocket);
};
_3LAS.prototype.OnSocketDisconnect = function () {
this.Logger.Log("Lost connection to server.");
if (this.WebRTC)
this.WebRTC.OnSocketDisconnect();
else
this.Fallback.OnSocketDisconnect();
if (this.WebRTC)
this.WebRTC.Reset();
else
this.Fallback.Reset();
if (this.ConnectivityFlag) {
this.ConnectivityFlag = false;
if (this.ConnectivityCallback)
this.ConnectivityCallback(false);
}
this.Start();
};
_3LAS.prototype.OnSocketDataReady = function (data) {
if (this.WebRTC)
this.WebRTC.OnSocketDataReady(data);
else
this.Fallback.OnSocketDataReady(data);
};
return _3LAS;
}());