2020-10-15 00:07:38 +03:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2022-01-13 00:53:10 +03:00
|
|
|
"context"
|
2021-07-03 22:28:25 +03:00
|
|
|
"io"
|
2020-10-15 00:07:38 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2022-01-13 00:53:10 +03:00
|
|
|
"github.com/owncast/owncast/activitypub"
|
2020-10-15 00:07:38 +03:00
|
|
|
"github.com/owncast/owncast/config"
|
2021-07-20 05:22:29 +03:00
|
|
|
"github.com/owncast/owncast/core/chat"
|
2021-02-19 10:05:52 +03:00
|
|
|
"github.com/owncast/owncast/core/data"
|
2020-10-30 00:09:28 +03:00
|
|
|
"github.com/owncast/owncast/core/rtmp"
|
2021-02-19 10:05:52 +03:00
|
|
|
"github.com/owncast/owncast/core/transcoder"
|
|
|
|
"github.com/owncast/owncast/core/webhooks"
|
|
|
|
"github.com/owncast/owncast/models"
|
2020-10-15 00:07:38 +03:00
|
|
|
"github.com/owncast/owncast/utils"
|
|
|
|
)
|
|
|
|
|
2020-10-16 03:50:17 +03:00
|
|
|
// After the stream goes offline this timer fires a full cleanup after N min.
|
|
|
|
var _offlineCleanupTimer *time.Timer
|
|
|
|
|
|
|
|
// While a stream takes place cleanup old HLS content every N min.
|
|
|
|
var _onlineCleanupTicker *time.Ticker
|
2020-10-15 00:07:38 +03:00
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
var _currentBroadcast *models.CurrentBroadcast
|
|
|
|
|
2022-01-13 00:53:10 +03:00
|
|
|
var _onlineTimerCancelFunc context.CancelFunc
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// setStreamAsConnected sets the stream as connected.
|
2021-07-03 22:28:25 +03:00
|
|
|
func setStreamAsConnected(rtmpOut *io.PipeReader) {
|
2021-08-04 03:47:16 +03:00
|
|
|
now := utils.NullTime{Time: time.Now(), Valid: true}
|
2020-10-15 00:07:38 +03:00
|
|
|
_stats.StreamConnected = true
|
2021-08-04 03:47:16 +03:00
|
|
|
_stats.LastDisconnectTime = nil
|
|
|
|
_stats.LastConnectTime = &now
|
2021-02-27 09:15:46 +03:00
|
|
|
_stats.SessionMaxViewerCount = 0
|
2020-10-15 00:07:38 +03:00
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
_currentBroadcast = &models.CurrentBroadcast{
|
|
|
|
LatencyLevel: data.GetStreamLatencyLevel(),
|
|
|
|
OutputSettings: data.GetStreamOutputVariants(),
|
|
|
|
}
|
|
|
|
|
2020-10-16 02:17:33 +03:00
|
|
|
StopOfflineCleanupTimer()
|
2020-10-16 03:50:17 +03:00
|
|
|
startOnlineCleanupTimer()
|
|
|
|
|
2020-10-15 02:46:04 +03:00
|
|
|
if _yp != nil {
|
2020-11-21 09:59:20 +03:00
|
|
|
go _yp.Start()
|
2020-10-15 02:46:04 +03:00
|
|
|
}
|
2020-10-15 00:07:38 +03:00
|
|
|
|
2021-09-12 21:32:42 +03:00
|
|
|
segmentPath := config.HLSStoragePath
|
2021-02-19 10:05:52 +03:00
|
|
|
|
|
|
|
if err := setupStorage(); err != nil {
|
|
|
|
log.Fatalln("failed to setup the storage", err)
|
|
|
|
}
|
|
|
|
|
2020-10-15 00:07:38 +03:00
|
|
|
go func() {
|
2021-02-19 10:05:52 +03:00
|
|
|
_transcoder = transcoder.NewTranscoder()
|
2020-10-15 00:07:38 +03:00
|
|
|
_transcoder.TranscoderCompleted = func(error) {
|
|
|
|
SetStreamAsDisconnected()
|
2021-02-19 10:05:52 +03:00
|
|
|
_transcoder = nil
|
|
|
|
_currentBroadcast = nil
|
2020-10-15 00:07:38 +03:00
|
|
|
}
|
2021-07-03 22:28:25 +03:00
|
|
|
_transcoder.SetStdin(rtmpOut)
|
2020-10-15 00:07:38 +03:00
|
|
|
_transcoder.Start()
|
|
|
|
}()
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
go webhooks.SendStreamStatusEvent(models.StreamStarted)
|
|
|
|
transcoder.StartThumbnailGenerator(segmentPath, data.FindHighestVideoQualityIndex(_currentBroadcast.OutputSettings))
|
2021-07-20 05:22:29 +03:00
|
|
|
|
2021-08-13 08:39:12 +03:00
|
|
|
_ = chat.SendSystemAction("Stay tuned, the stream is **starting**!", true)
|
2021-07-20 05:22:29 +03:00
|
|
|
chat.SendAllWelcomeMessage()
|
2022-01-13 00:53:10 +03:00
|
|
|
|
|
|
|
// Send a delayed live Federated message.
|
|
|
|
if data.GetFederationEnabled() {
|
|
|
|
_onlineTimerCancelFunc = startFederatedLiveStreamMessageTimer()
|
|
|
|
}
|
2020-10-15 00:07:38 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// SetStreamAsDisconnected sets the stream as disconnected.
|
2020-10-15 00:07:38 +03:00
|
|
|
func SetStreamAsDisconnected() {
|
2021-07-20 05:22:29 +03:00
|
|
|
_ = chat.SendSystemAction("The stream is ending.", true)
|
|
|
|
|
2021-08-04 03:47:16 +03:00
|
|
|
now := utils.NullTime{Time: time.Now(), Valid: true}
|
2022-01-13 00:53:10 +03:00
|
|
|
if _onlineTimerCancelFunc != nil {
|
|
|
|
_onlineTimerCancelFunc()
|
|
|
|
}
|
|
|
|
|
2020-10-15 00:07:38 +03:00
|
|
|
_stats.StreamConnected = false
|
2021-08-04 03:47:16 +03:00
|
|
|
_stats.LastDisconnectTime = &now
|
|
|
|
_stats.LastConnectTime = nil
|
2020-10-25 00:58:34 +03:00
|
|
|
_broadcaster = nil
|
2020-10-15 00:07:38 +03:00
|
|
|
|
|
|
|
offlineFilename := "offline.ts"
|
2021-10-12 01:04:16 +03:00
|
|
|
|
2021-11-27 07:53:27 +03:00
|
|
|
offlineFilePath, err := saveOfflineClipToDisk(offlineFilename)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
return
|
2021-10-12 01:04:16 +03:00
|
|
|
}
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
transcoder.StopThumbnailGenerator()
|
2020-10-30 00:09:28 +03:00
|
|
|
rtmp.Disconnect()
|
|
|
|
|
2020-10-15 02:46:04 +03:00
|
|
|
if _yp != nil {
|
|
|
|
_yp.Stop()
|
|
|
|
}
|
2020-10-15 00:07:38 +03:00
|
|
|
|
2021-10-22 03:21:17 +03:00
|
|
|
// If there is no current broadcast available the previous stream
|
|
|
|
// likely failed for some reason. Don't try to append to it.
|
|
|
|
// Just transition to offline.
|
|
|
|
if _currentBroadcast == nil {
|
|
|
|
stopOnlineCleanupTimer()
|
|
|
|
transitionToOfflineVideoStreamContent()
|
2021-11-27 07:53:27 +03:00
|
|
|
log.Errorln("unexpected nil _currentBroadcast")
|
2021-10-22 03:21:17 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-02 04:27:56 +03:00
|
|
|
for index := range _currentBroadcast.OutputSettings {
|
2021-11-27 07:53:27 +03:00
|
|
|
makeVariantIndexOffline(index, offlineFilePath, offlineFilename)
|
2020-10-15 00:07:38 +03:00
|
|
|
}
|
|
|
|
|
2020-10-16 02:17:33 +03:00
|
|
|
StartOfflineCleanupTimer()
|
2020-10-16 03:50:17 +03:00
|
|
|
stopOnlineCleanupTimer()
|
2021-06-20 21:30:29 +03:00
|
|
|
saveStats()
|
2021-02-19 10:05:52 +03:00
|
|
|
|
|
|
|
go webhooks.SendStreamStatusEvent(models.StreamStopped)
|
2020-10-15 00:07:38 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// StartOfflineCleanupTimer will fire a cleanup after n minutes being disconnected.
|
2020-10-16 02:17:33 +03:00
|
|
|
func StartOfflineCleanupTimer() {
|
2020-10-16 03:50:17 +03:00
|
|
|
_offlineCleanupTimer = time.NewTimer(5 * time.Minute)
|
2020-10-15 00:07:38 +03:00
|
|
|
go func() {
|
2020-11-15 05:39:53 +03:00
|
|
|
for range _offlineCleanupTimer.C {
|
2021-02-19 10:05:52 +03:00
|
|
|
// Set video to offline state
|
2020-11-15 05:39:53 +03:00
|
|
|
resetDirectories()
|
|
|
|
transitionToOfflineVideoStreamContent()
|
2020-10-15 00:07:38 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// StopOfflineCleanupTimer will stop the previous cleanup timer.
|
2020-10-16 02:17:33 +03:00
|
|
|
func StopOfflineCleanupTimer() {
|
2020-10-16 03:50:17 +03:00
|
|
|
if _offlineCleanupTimer != nil {
|
|
|
|
_offlineCleanupTimer.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startOnlineCleanupTimer() {
|
|
|
|
_onlineCleanupTicker = time.NewTicker(1 * time.Minute)
|
|
|
|
go func() {
|
2020-11-15 05:39:53 +03:00
|
|
|
for range _onlineCleanupTicker.C {
|
2021-09-12 21:32:42 +03:00
|
|
|
transcoder.CleanupOldContent(config.HLSStoragePath)
|
2020-10-16 03:50:17 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopOnlineCleanupTimer() {
|
|
|
|
if _onlineCleanupTicker != nil {
|
|
|
|
_onlineCleanupTicker.Stop()
|
2020-10-15 00:07:38 +03:00
|
|
|
}
|
|
|
|
}
|
2022-01-13 00:53:10 +03:00
|
|
|
|
|
|
|
func startFederatedLiveStreamMessageTimer() context.CancelFunc {
|
|
|
|
// Send a delayed live Federated message.
|
|
|
|
c, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
_onlineTimerCancelFunc = cancelFunc
|
|
|
|
go func(c context.Context) {
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Minute * 2.0):
|
|
|
|
log.Traceln("Sending Federated Go Live message.")
|
|
|
|
if err := activitypub.SendLive(); err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
case <-c.Done():
|
|
|
|
}
|
|
|
|
}(c)
|
|
|
|
return cancelFunc
|
|
|
|
}
|