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.
@@ -1099,7 +1099,7 @@ function createHlsAdPlayer(contentVideo, options) {
1099
1099
  adHls.destroy();
1100
1100
  }
1101
1101
  adHls = new import_hls.default({
1102
- enableWorker: typeof globalThis !== "undefined",
1102
+ enableWorker: true,
1103
1103
  lowLatencyMode: false
1104
1104
  });
1105
1105
  adHls.loadSource(mediaFile.url);
@@ -1871,10 +1871,56 @@ function buildPlayerTopic(licenseKey, channel) {
1871
1871
  // src/utils/mqttClient.ts
1872
1872
  var import_mqtt = __toESM(require("mqtt"), 1);
1873
1873
  var LOG = "[StormcloudVideoPlayer][MQTT]";
1874
+ var KEEPALIVE_SECONDS = 30;
1875
+ var CONNECT_TIMEOUT_MS = 1e4;
1876
+ var RECONNECT_BASE_MS = 5e3;
1877
+ var RECONNECT_MAX_MS = 6e4;
1878
+ var MAX_RECONNECT_ATTEMPTS = 8;
1879
+ var COOLDOWN_MS = 10 * 6e4;
1874
1880
  var client = null;
1875
1881
  var status = "disconnected";
1876
- function initMQTTClient() {
1877
- if (client || !isMQTTEnabled()) return;
1882
+ var reconnectAttempts = 0;
1883
+ var reconnectTimer = null;
1884
+ var stopped = false;
1885
+ function clearReconnectTimer() {
1886
+ if (reconnectTimer) {
1887
+ clearTimeout(reconnectTimer);
1888
+ reconnectTimer = null;
1889
+ }
1890
+ }
1891
+ function teardownClient() {
1892
+ if (client) {
1893
+ try {
1894
+ client.removeAllListeners();
1895
+ client.end(true);
1896
+ } catch (unused) {}
1897
+ client = null;
1898
+ }
1899
+ }
1900
+ function scheduleReconnect() {
1901
+ if (stopped || !isMQTTEnabled() || reconnectTimer) return;
1902
+ if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
1903
+ console.warn("".concat(LOG, " giving up after ").concat(reconnectAttempts, " attempts; cooling down for ").concat(COOLDOWN_MS / 6e4, "m"));
1904
+ teardownClient();
1905
+ status = "disconnected";
1906
+ reconnectTimer = setTimeout(function() {
1907
+ reconnectTimer = null;
1908
+ reconnectAttempts = 0;
1909
+ openConnection();
1910
+ }, COOLDOWN_MS);
1911
+ return;
1912
+ }
1913
+ var delay = Math.min(RECONNECT_MAX_MS, RECONNECT_BASE_MS * Math.pow(2, reconnectAttempts));
1914
+ var jitter = Math.floor(Math.random() * 1e3);
1915
+ reconnectAttempts += 1;
1916
+ reconnectTimer = setTimeout(function() {
1917
+ reconnectTimer = null;
1918
+ openConnection();
1919
+ }, delay + jitter);
1920
+ }
1921
+ function openConnection() {
1922
+ if (stopped || !isMQTTEnabled()) return;
1923
+ teardownClient();
1878
1924
  var url = buildMQTTBrokerUrl();
1879
1925
  status = "connecting";
1880
1926
  var clientId = "stormcloud-vp-".concat(Math.random().toString(36).slice(2, 9));
@@ -1883,41 +1929,48 @@ function initMQTTClient() {
1883
1929
  clientId: clientId,
1884
1930
  username: mqttConfig.username,
1885
1931
  password: mqttConfig.password,
1886
- keepalive: 60,
1932
+ keepalive: KEEPALIVE_SECONDS,
1887
1933
  clean: true,
1888
- reconnectPeriod: 5e3,
1889
- connectTimeout: 1e4,
1890
- queueQoSZero: false
1934
+ reconnectPeriod: 0,
1935
+ connectTimeout: CONNECT_TIMEOUT_MS,
1936
+ queueQoSZero: false,
1937
+ reschedulePings: true
1891
1938
  });
1892
1939
  } catch (err) {
1893
1940
  status = "error";
1894
1941
  console.warn("".concat(LOG, " connect() threw:"), err);
1942
+ scheduleReconnect();
1895
1943
  return;
1896
1944
  }
1897
1945
  client.on("connect", function() {
1898
1946
  status = "connected";
1947
+ reconnectAttempts = 0;
1948
+ clearReconnectTimer();
1899
1949
  console.info("".concat(LOG, " connected to ").concat(url));
1900
1950
  });
1901
- client.on("reconnect", function() {
1902
- status = "connecting";
1903
- console.info("".concat(LOG, " reconnecting…"));
1904
- });
1905
1951
  client.on("offline", function() {
1906
- status = "disconnected";
1952
+ if (status === "connected") status = "disconnected";
1907
1953
  console.warn("".concat(LOG, " offline"));
1954
+ scheduleReconnect();
1908
1955
  });
1909
1956
  client.on("error", function(err) {
1910
1957
  status = "error";
1911
1958
  console.warn("".concat(LOG, " error:"), err.message);
1959
+ scheduleReconnect();
1912
1960
  });
1913
1961
  client.on("close", function() {
1914
- if (status === "connected") {
1915
- status = "disconnected";
1916
- }
1962
+ if (status === "connected") status = "disconnected";
1963
+ scheduleReconnect();
1917
1964
  });
1918
1965
  }
1966
+ function initMQTTClient() {
1967
+ if (client || !isMQTTEnabled()) return;
1968
+ stopped = false;
1969
+ reconnectAttempts = 0;
1970
+ openConnection();
1971
+ }
1919
1972
  function ensureMQTTClient() {
1920
- if (isMQTTEnabled() && !client) {
1973
+ if (isMQTTEnabled() && !client && !reconnectTimer) {
1921
1974
  initMQTTClient();
1922
1975
  }
1923
1976
  }
@@ -1926,7 +1979,7 @@ function publishMQTT(topic, payload) {
1926
1979
  return false;
1927
1980
  }
1928
1981
  ensureMQTTClient();
1929
- if (!client) {
1982
+ if (!client || !client.connected) {
1930
1983
  return false;
1931
1984
  }
1932
1985
  try {
@@ -6205,7 +6258,7 @@ var HlsEngine = /*#__PURE__*/ function() {
6205
6258
  value: function setupHls() {
6206
6259
  var _this = this;
6207
6260
  this.hls = new import_hls2.default(_object_spread_props(_object_spread({
6208
- enableWorker: typeof globalThis !== "undefined",
6261
+ enableWorker: true,
6209
6262
  backBufferLength: 30,
6210
6263
  liveDurationInfinity: true,
6211
6264
  lowLatencyMode: !!this.config.lowLatencyMode,