Auto-restart playback when the stream comes back online
This commit is contained in:
parent
f900539481
commit
a90d98ff1b
16
stats.go
16
stats.go
@ -23,8 +23,8 @@ type Stats struct {
|
|||||||
SessionMaxViewerCount int `json:"sessionMaxViewerCount"`
|
SessionMaxViewerCount int `json:"sessionMaxViewerCount"`
|
||||||
OverallMaxViewerCount int `json:"overallMaxViewerCount"`
|
OverallMaxViewerCount int `json:"overallMaxViewerCount"`
|
||||||
LastDisconnectTime time.Time `json:"lastDisconnectTime"`
|
LastDisconnectTime time.Time `json:"lastDisconnectTime"`
|
||||||
|
lastConnectTime time.Time `json:"-"`
|
||||||
clients map[string]time.Time
|
clients map[string]time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stats) Setup() {
|
func (s *Stats) Setup() {
|
||||||
@ -62,6 +62,17 @@ func (s *Stats) purgeStaleViewers() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stats) IsStreamConnected() bool {
|
func (s *Stats) IsStreamConnected() bool {
|
||||||
|
if !s.streamConnected {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kind of a hack. It takes a handful of seconds between a RTMP connection and when HLS data is available.
|
||||||
|
// So account for that with an artificial buffer.
|
||||||
|
timeSinceLastConnected := time.Since(s.lastConnectTime).Seconds()
|
||||||
|
if timeSinceLastConnected < 10 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
return s.streamConnected
|
return s.streamConnected
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,6 +107,7 @@ func (s *Stats) ViewerDisconnected(clientID string) {
|
|||||||
|
|
||||||
func (s *Stats) StreamConnected() {
|
func (s *Stats) StreamConnected() {
|
||||||
s.streamConnected = true
|
s.streamConnected = true
|
||||||
|
s.lastConnectTime = time.Now()
|
||||||
|
|
||||||
timeSinceDisconnect := time.Since(s.LastDisconnectTime).Minutes()
|
timeSinceDisconnect := time.Since(s.LastDisconnectTime).Minutes()
|
||||||
if timeSinceDisconnect > 15 {
|
if timeSinceDisconnect > 15 {
|
||||||
|
@ -41,11 +41,25 @@ function setupApp() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function getStatus() {
|
async function getStatus() {
|
||||||
let url = "https://goth.land/status";
|
let url = "/status";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(url);
|
const response = await fetch(url);
|
||||||
const status = await response.json(); // read response body and parse as JSON
|
const status = await response.json(); // read response body and parse as JSON
|
||||||
|
|
||||||
|
if (!app.isOnline && status.online) {
|
||||||
|
// The stream was offline, but now it's online. Force start of playback after an arbitrary
|
||||||
|
// delay to make sure the stream has actual data ready to go.
|
||||||
|
setTimeout(function () {
|
||||||
|
var player = videojs('video');
|
||||||
|
player.pause()
|
||||||
|
player.src(player.src()); // Reload the same video
|
||||||
|
player.load();
|
||||||
|
player.play();
|
||||||
|
}, 3000)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
app.streamStatus = status.online
|
app.streamStatus = status.online
|
||||||
? "Stream is online."
|
? "Stream is online."
|
||||||
: "Stream is offline."
|
: "Stream is offline."
|
||||||
@ -53,6 +67,7 @@ async function getStatus() {
|
|||||||
app.viewerCount = status.viewerCount;
|
app.viewerCount = status.viewerCount;
|
||||||
app.sessionMaxViewerCount = status.sessionMaxViewerCount;
|
app.sessionMaxViewerCount = status.sessionMaxViewerCount;
|
||||||
app.overallMaxViewerCount = status.overallMaxViewerCount;
|
app.overallMaxViewerCount = status.overallMaxViewerCount;
|
||||||
|
app.isOnline = status.online;
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
app.streamStatus = "Stream server is offline."
|
app.streamStatus = "Stream server is offline."
|
||||||
|
Loading…
x
Reference in New Issue
Block a user