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"
|
2023-08-10 02:19:09 +03:00
|
|
|
"path/filepath"
|
2020-10-15 00:07:38 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2023-08-10 02:19:09 +03:00
|
|
|
"github.com/teris-io/shortid"
|
2020-10-15 00:07:38 +03:00
|
|
|
|
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"
|
2022-03-18 23:33:23 +03:00
|
|
|
"github.com/owncast/owncast/notifications"
|
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
|
|
|
|
|
2022-03-18 23:33:23 +03:00
|
|
|
var _lastNotified *time.Time
|
|
|
|
|
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
|
|
|
|
2023-08-10 02:19:09 +03:00
|
|
|
streamId := shortid.MustGenerate()
|
|
|
|
fileWriter.SetStreamID(streamId)
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
_currentBroadcast = &models.CurrentBroadcast{
|
2023-08-10 02:19:09 +03:00
|
|
|
StreamID: streamId,
|
2021-02-19 10:05:52 +03:00
|
|
|
LatencyLevel: data.GetStreamLatencyLevel(),
|
|
|
|
OutputSettings: data.GetStreamOutputVariants(),
|
|
|
|
}
|
|
|
|
|
2020-10-16 02:17:33 +03:00
|
|
|
StopOfflineCleanupTimer()
|
2023-08-10 02:19:09 +03:00
|
|
|
|
2023-08-10 02:35:42 +03:00
|
|
|
if !config.EnableReplayFeatures {
|
2023-08-10 02:19:09 +03:00
|
|
|
startOnlineCleanupTimer()
|
|
|
|
}
|
2020-10-16 03:50:17 +03:00
|
|
|
|
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-02-19 10:05:52 +03:00
|
|
|
if err := setupStorage(); err != nil {
|
|
|
|
log.Fatalln("failed to setup the storage", err)
|
|
|
|
}
|
|
|
|
|
2023-08-10 02:19:09 +03:00
|
|
|
setupVideoComponentsForId(streamId)
|
|
|
|
setupLiveTranscoderForId(streamId, rtmpOut)
|
2020-10-15 00:07:38 +03:00
|
|
|
|
2023-09-18 05:44:39 +03:00
|
|
|
go webhooks.SendStreamStatusEvent(models.StreamStarted, streamId)
|
2023-08-10 02:19:09 +03:00
|
|
|
segmentPath := filepath.Join(config.HLSStoragePath, streamId)
|
2021-02-19 10:05:52 +03:00
|
|
|
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
|
|
|
|
2022-03-18 23:33:23 +03:00
|
|
|
// Send delayed notification messages.
|
|
|
|
_onlineTimerCancelFunc = startLiveStreamNotificationsTimer()
|
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
|
|
|
|
2023-02-06 07:42:17 +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
|
|
|
}
|
|
|
|
|
2023-08-10 02:19:09 +03:00
|
|
|
handler.StreamEnded()
|
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 {
|
2023-08-21 07:13:59 +03:00
|
|
|
makeVariantIndexOffline(_currentBroadcast.StreamID, 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
|
|
|
|
2023-09-18 05:44:39 +03:00
|
|
|
go webhooks.SendStreamStatusEvent(models.StreamStopped, _currentBroadcast.StreamID)
|
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 {
|
2023-05-31 21:10:04 +03:00
|
|
|
if err := _storage.Cleanup(); err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
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
|
|
|
|
2022-03-18 23:33:23 +03:00
|
|
|
func startLiveStreamNotificationsTimer() context.CancelFunc {
|
|
|
|
// Send delayed notification messages.
|
2022-01-13 00:53:10 +03:00
|
|
|
c, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
_onlineTimerCancelFunc = cancelFunc
|
|
|
|
go func(c context.Context) {
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Minute * 2.0):
|
2022-03-18 23:33:23 +03:00
|
|
|
if _lastNotified != nil && time.Since(*_lastNotified) < 10*time.Minute {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send Fediverse message.
|
|
|
|
if data.GetFederationEnabled() {
|
|
|
|
log.Traceln("Sending Federated Go Live message.")
|
|
|
|
if err := activitypub.SendLive(); err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send notification to those who have registered for them.
|
|
|
|
if notifier, err := notifications.New(data.GetDatastore()); err != nil {
|
2022-01-13 00:53:10 +03:00
|
|
|
log.Errorln(err)
|
2022-03-18 23:33:23 +03:00
|
|
|
} else {
|
|
|
|
notifier.Notify()
|
2022-01-13 00:53:10 +03:00
|
|
|
}
|
2022-03-18 23:33:23 +03:00
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
_lastNotified = &now
|
2022-01-13 00:53:10 +03:00
|
|
|
case <-c.Done():
|
|
|
|
}
|
|
|
|
}(c)
|
2022-03-18 23:33:23 +03:00
|
|
|
|
2022-01-13 00:53:10 +03:00
|
|
|
return cancelFunc
|
|
|
|
}
|