txtorg commited on
Commit
f604e32
·
verified ·
1 Parent(s): b02f5a3

Update public/script.js

Browse files
Files changed (1) hide show
  1. public/script.js +14 -25
public/script.js CHANGED
@@ -6,15 +6,13 @@ videoPlayer.setAttribute("playsinline", "true");
6
 
7
  const LAST_CHANNEL_KEY = "last_selected_channel";
8
  let currentChannel = null;
9
- let lastSeekTime = 0;
10
- let lastPlaybackPosition = 0;
11
  let smoothPlayback = true;
12
 
 
13
  async function fetchChannels() {
14
  try {
15
  const response = await fetch("/channels");
16
  const data = await response.json();
17
-
18
  const channelButtons = document.querySelector(".channel-buttons");
19
  channelButtons.innerHTML = "";
20
 
@@ -41,6 +39,7 @@ async function fetchChannels() {
41
  }
42
  }
43
 
 
44
  async function changeChannel(channel) {
45
  if (currentChannel === channel) return;
46
  currentChannel = channel;
@@ -49,8 +48,8 @@ async function changeChannel(channel) {
49
  document.getElementById("currentShow").textContent = `Now Playing: Loading...`;
50
 
51
  const streamUrl = `/hls/${channel}/index.m3u8`;
52
-
53
  let seekTime = 0;
 
54
  try {
55
  const response = await fetch(`/timestamp?channel=${channel}`);
56
  const data = await response.json();
@@ -59,16 +58,11 @@ async function changeChannel(channel) {
59
  console.error("Failed to fetch timestamp:", error);
60
  }
61
 
62
- // Prevent seeking if the difference is small
63
- if (Math.abs(seekTime - lastSeekTime) < 5) {
64
- seekTime = lastSeekTime;
65
- } else {
66
- lastSeekTime = seekTime;
67
- }
68
-
69
  if (seekTime < 0 || isNaN(seekTime)) seekTime = 0;
70
  localStorage.setItem(LAST_CHANNEL_KEY, channel);
71
 
 
72
  if (Hls.isSupported()) {
73
  if (videoPlayer.hlsInstance) {
74
  videoPlayer.hlsInstance.destroy();
@@ -78,7 +72,7 @@ async function changeChannel(channel) {
78
  maxBufferLength: 180,
79
  maxMaxBufferLength: 360,
80
  maxBufferSize: 500 * 1000 * 1000,
81
- liveSyncDurationCount: 6,
82
  enableWorker: true,
83
  lowLatencyMode: true,
84
  backBufferLength: 90
@@ -93,15 +87,10 @@ async function changeChannel(channel) {
93
  });
94
 
95
  hls.on(Hls.Events.BUFFER_APPENDED, () => {
96
- const diff = Math.abs(videoPlayer.currentTime - seekTime);
97
-
98
- if (diff > 5) {
99
- console.log(`Seeking to correct position: ${seekTime}s`);
100
- videoPlayer.currentTime = seekTime;
101
- }
102
  });
103
 
104
- // Handle playback interruptions
105
  hls.on(Hls.Events.ERROR, (event, data) => {
106
  if (data.fatal) {
107
  console.error("HLS Fatal Error:", data);
@@ -118,12 +107,12 @@ async function changeChannel(channel) {
118
  });
119
 
120
  document.getElementById("currentShow").textContent = `Now Playing: Channel ${channel}`;
121
- } else if (videoPlayer.canPlayType("application/vnd.apple.mpegurl")) {
 
122
  videoPlayer.src = streamUrl;
123
  videoPlayer.addEventListener("loadedmetadata", () => {
124
- if (Math.abs(videoPlayer.currentTime - seekTime) > 1) {
125
- videoPlayer.currentTime = seekTime;
126
- }
127
  videoPlayer.play().catch(err => console.error("Playback error:", err));
128
  });
129
 
@@ -144,14 +133,14 @@ setInterval(async () => {
144
  const playerTime = videoPlayer.currentTime;
145
  const diff = Math.abs(serverTime - playerTime);
146
 
147
- if (diff > 5) {
148
  console.log(`Auto-correcting drift: ${playerTime} → ${serverTime}`);
149
  videoPlayer.currentTime = serverTime;
150
  }
151
  } catch (error) {
152
  console.error("Failed to sync playback:", error);
153
  }
154
- }, 10000);
155
 
156
  function togglePlay() {
157
  if (videoPlayer.paused) {
 
6
 
7
  const LAST_CHANNEL_KEY = "last_selected_channel";
8
  let currentChannel = null;
 
 
9
  let smoothPlayback = true;
10
 
11
+ // Fetch channels and populate buttons
12
  async function fetchChannels() {
13
  try {
14
  const response = await fetch("/channels");
15
  const data = await response.json();
 
16
  const channelButtons = document.querySelector(".channel-buttons");
17
  channelButtons.innerHTML = "";
18
 
 
39
  }
40
  }
41
 
42
+ // Change channel and seek to correct timestamp
43
  async function changeChannel(channel) {
44
  if (currentChannel === channel) return;
45
  currentChannel = channel;
 
48
  document.getElementById("currentShow").textContent = `Now Playing: Loading...`;
49
 
50
  const streamUrl = `/hls/${channel}/index.m3u8`;
 
51
  let seekTime = 0;
52
+
53
  try {
54
  const response = await fetch(`/timestamp?channel=${channel}`);
55
  const data = await response.json();
 
58
  console.error("Failed to fetch timestamp:", error);
59
  }
60
 
61
+ // Ensure seekTime is valid
 
 
 
 
 
 
62
  if (seekTime < 0 || isNaN(seekTime)) seekTime = 0;
63
  localStorage.setItem(LAST_CHANNEL_KEY, channel);
64
 
65
+ // Stop previous HLS instance if it exists
66
  if (Hls.isSupported()) {
67
  if (videoPlayer.hlsInstance) {
68
  videoPlayer.hlsInstance.destroy();
 
72
  maxBufferLength: 180,
73
  maxMaxBufferLength: 360,
74
  maxBufferSize: 500 * 1000 * 1000,
75
+ liveSyncDurationCount: 6,
76
  enableWorker: true,
77
  lowLatencyMode: true,
78
  backBufferLength: 90
 
87
  });
88
 
89
  hls.on(Hls.Events.BUFFER_APPENDED, () => {
90
+ console.log(`Seeking to ${seekTime}s`);
91
+ videoPlayer.currentTime = seekTime;
 
 
 
 
92
  });
93
 
 
94
  hls.on(Hls.Events.ERROR, (event, data) => {
95
  if (data.fatal) {
96
  console.error("HLS Fatal Error:", data);
 
107
  });
108
 
109
  document.getElementById("currentShow").textContent = `Now Playing: Channel ${channel}`;
110
+ }
111
+ else if (videoPlayer.canPlayType("application/vnd.apple.mpegurl")) {
112
  videoPlayer.src = streamUrl;
113
  videoPlayer.addEventListener("loadedmetadata", () => {
114
+ console.log(`Seeking to ${seekTime}s`);
115
+ videoPlayer.currentTime = seekTime;
 
116
  videoPlayer.play().catch(err => console.error("Playback error:", err));
117
  });
118
 
 
133
  const playerTime = videoPlayer.currentTime;
134
  const diff = Math.abs(serverTime - playerTime);
135
 
136
+ if (diff > 3) {
137
  console.log(`Auto-correcting drift: ${playerTime} → ${serverTime}`);
138
  videoPlayer.currentTime = serverTime;
139
  }
140
  } catch (error) {
141
  console.error("Failed to sync playback:", error);
142
  }
143
+ }, 5000); // More frequent drift correction
144
 
145
  function togglePlay() {
146
  if (videoPlayer.paused) {