stormcloud-video-player 0.8.37 → 0.8.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1136,7 +1136,7 @@ function createHlsAdPlayer(contentVideo, options) {
1136
1136
  adHls.destroy();
1137
1137
  }
1138
1138
  adHls = new import_hls.default({
1139
- enableWorker: typeof globalThis !== "undefined",
1139
+ enableWorker: true,
1140
1140
  lowLatencyMode: false
1141
1141
  });
1142
1142
  adHls.loadSource(mediaFile.url);
@@ -1908,10 +1908,56 @@ function buildPlayerTopic(licenseKey, channel) {
1908
1908
  // src/utils/mqttClient.ts
1909
1909
  var import_mqtt = __toESM(require("mqtt"), 1);
1910
1910
  var LOG = "[StormcloudVideoPlayer][MQTT]";
1911
+ var KEEPALIVE_SECONDS = 30;
1912
+ var CONNECT_TIMEOUT_MS = 1e4;
1913
+ var RECONNECT_BASE_MS = 5e3;
1914
+ var RECONNECT_MAX_MS = 6e4;
1915
+ var MAX_RECONNECT_ATTEMPTS = 8;
1916
+ var COOLDOWN_MS = 10 * 6e4;
1911
1917
  var client = null;
1912
1918
  var status = "disconnected";
1913
- function initMQTTClient() {
1914
- if (client || !isMQTTEnabled()) return;
1919
+ var reconnectAttempts = 0;
1920
+ var reconnectTimer = null;
1921
+ var stopped = false;
1922
+ function clearReconnectTimer() {
1923
+ if (reconnectTimer) {
1924
+ clearTimeout(reconnectTimer);
1925
+ reconnectTimer = null;
1926
+ }
1927
+ }
1928
+ function teardownClient() {
1929
+ if (client) {
1930
+ try {
1931
+ client.removeAllListeners();
1932
+ client.end(true);
1933
+ } catch (unused) {}
1934
+ client = null;
1935
+ }
1936
+ }
1937
+ function scheduleReconnect() {
1938
+ if (stopped || !isMQTTEnabled() || reconnectTimer) return;
1939
+ if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
1940
+ console.warn("".concat(LOG, " giving up after ").concat(reconnectAttempts, " attempts; cooling down for ").concat(COOLDOWN_MS / 6e4, "m"));
1941
+ teardownClient();
1942
+ status = "disconnected";
1943
+ reconnectTimer = setTimeout(function() {
1944
+ reconnectTimer = null;
1945
+ reconnectAttempts = 0;
1946
+ openConnection();
1947
+ }, COOLDOWN_MS);
1948
+ return;
1949
+ }
1950
+ var delay = Math.min(RECONNECT_MAX_MS, RECONNECT_BASE_MS * Math.pow(2, reconnectAttempts));
1951
+ var jitter = Math.floor(Math.random() * 1e3);
1952
+ reconnectAttempts += 1;
1953
+ reconnectTimer = setTimeout(function() {
1954
+ reconnectTimer = null;
1955
+ openConnection();
1956
+ }, delay + jitter);
1957
+ }
1958
+ function openConnection() {
1959
+ if (stopped || !isMQTTEnabled()) return;
1960
+ teardownClient();
1915
1961
  var url = buildMQTTBrokerUrl();
1916
1962
  status = "connecting";
1917
1963
  var clientId = "stormcloud-vp-".concat(Math.random().toString(36).slice(2, 9));
@@ -1920,41 +1966,48 @@ function initMQTTClient() {
1920
1966
  clientId: clientId,
1921
1967
  username: mqttConfig.username,
1922
1968
  password: mqttConfig.password,
1923
- keepalive: 60,
1969
+ keepalive: KEEPALIVE_SECONDS,
1924
1970
  clean: true,
1925
- reconnectPeriod: 5e3,
1926
- connectTimeout: 1e4,
1927
- queueQoSZero: false
1971
+ reconnectPeriod: 0,
1972
+ connectTimeout: CONNECT_TIMEOUT_MS,
1973
+ queueQoSZero: false,
1974
+ reschedulePings: true
1928
1975
  });
1929
1976
  } catch (err) {
1930
1977
  status = "error";
1931
1978
  console.warn("".concat(LOG, " connect() threw:"), err);
1979
+ scheduleReconnect();
1932
1980
  return;
1933
1981
  }
1934
1982
  client.on("connect", function() {
1935
1983
  status = "connected";
1984
+ reconnectAttempts = 0;
1985
+ clearReconnectTimer();
1936
1986
  console.info("".concat(LOG, " connected to ").concat(url));
1937
1987
  });
1938
- client.on("reconnect", function() {
1939
- status = "connecting";
1940
- console.info("".concat(LOG, " reconnecting…"));
1941
- });
1942
1988
  client.on("offline", function() {
1943
- status = "disconnected";
1989
+ if (status === "connected") status = "disconnected";
1944
1990
  console.warn("".concat(LOG, " offline"));
1991
+ scheduleReconnect();
1945
1992
  });
1946
1993
  client.on("error", function(err) {
1947
1994
  status = "error";
1948
1995
  console.warn("".concat(LOG, " error:"), err.message);
1996
+ scheduleReconnect();
1949
1997
  });
1950
1998
  client.on("close", function() {
1951
- if (status === "connected") {
1952
- status = "disconnected";
1953
- }
1999
+ if (status === "connected") status = "disconnected";
2000
+ scheduleReconnect();
1954
2001
  });
1955
2002
  }
2003
+ function initMQTTClient() {
2004
+ if (client || !isMQTTEnabled()) return;
2005
+ stopped = false;
2006
+ reconnectAttempts = 0;
2007
+ openConnection();
2008
+ }
1956
2009
  function ensureMQTTClient() {
1957
- if (isMQTTEnabled() && !client) {
2010
+ if (isMQTTEnabled() && !client && !reconnectTimer) {
1958
2011
  initMQTTClient();
1959
2012
  }
1960
2013
  }
@@ -1963,7 +2016,7 @@ function publishMQTT(topic, payload) {
1963
2016
  return false;
1964
2017
  }
1965
2018
  ensureMQTTClient();
1966
- if (!client) {
2019
+ if (!client || !client.connected) {
1967
2020
  return false;
1968
2021
  }
1969
2022
  try {
@@ -6242,7 +6295,7 @@ var HlsEngine = /*#__PURE__*/ function() {
6242
6295
  value: function setupHls() {
6243
6296
  var _this = this;
6244
6297
  this.hls = new import_hls2.default(_object_spread_props(_object_spread({
6245
- enableWorker: typeof globalThis !== "undefined",
6298
+ enableWorker: true,
6246
6299
  backBufferLength: 30,
6247
6300
  liveDurationInfinity: true,
6248
6301
  lowLatencyMode: !!this.config.lowLatencyMode,