2020-10-15 00:07:38 +03:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/owncast/owncast/config"
|
|
|
|
"github.com/owncast/owncast/core/ffmpeg"
|
2020-10-30 00:09:28 +03:00
|
|
|
"github.com/owncast/owncast/core/rtmp"
|
2020-10-15 00:07:38 +03:00
|
|
|
"github.com/owncast/owncast/utils"
|
|
|
|
|
|
|
|
"github.com/grafov/m3u8"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// setStreamAsConnected sets the stream as connected.
|
2020-10-30 00:09:28 +03:00
|
|
|
func setStreamAsConnected() {
|
2020-10-15 00:07:38 +03:00
|
|
|
_stats.StreamConnected = true
|
2020-11-15 05:39:53 +03:00
|
|
|
_stats.LastConnectTime = utils.NullTime{Time: time.Now(), Valid: true}
|
|
|
|
_stats.LastDisconnectTime = utils.NullTime{Time: time.Now(), Valid: false}
|
2020-10-15 00:07:38 +03:00
|
|
|
|
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
|
|
|
|
|
|
|
segmentPath := config.PublicHLSStoragePath
|
|
|
|
if config.Config.S3.Enabled {
|
|
|
|
segmentPath = config.PrivateHLSStoragePath
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_transcoder = ffmpeg.NewTranscoder()
|
2020-12-06 23:01:16 +03:00
|
|
|
if _broadcaster != nil {
|
|
|
|
_transcoder.SetVideoOnly(_broadcaster.StreamDetails.VideoOnly)
|
|
|
|
}
|
|
|
|
|
2020-10-15 00:07:38 +03:00
|
|
|
_transcoder.TranscoderCompleted = func(error) {
|
|
|
|
SetStreamAsDisconnected()
|
|
|
|
}
|
|
|
|
_transcoder.Start()
|
|
|
|
}()
|
|
|
|
|
|
|
|
ffmpeg.StartThumbnailGenerator(segmentPath, config.Config.VideoSettings.HighestQualityStreamIndex)
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// SetStreamAsDisconnected sets the stream as disconnected.
|
2020-10-15 00:07:38 +03:00
|
|
|
func SetStreamAsDisconnected() {
|
|
|
|
_stats.StreamConnected = false
|
2020-11-15 05:39:53 +03:00
|
|
|
_stats.LastDisconnectTime = utils.NullTime{Time: time.Now(), Valid: true}
|
2020-10-25 00:58:34 +03:00
|
|
|
_broadcaster = nil
|
2020-10-15 00:07:38 +03:00
|
|
|
|
|
|
|
offlineFilename := "offline.ts"
|
|
|
|
offlineFilePath := "static/" + offlineFilename
|
|
|
|
|
|
|
|
ffmpeg.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
|
|
|
|
|
|
|
for index := range config.Config.GetVideoStreamQualities() {
|
|
|
|
playlistFilePath := fmt.Sprintf(filepath.Join(config.PrivateHLSStoragePath, "%d/stream.m3u8"), index)
|
|
|
|
segmentFilePath := fmt.Sprintf(filepath.Join(config.PrivateHLSStoragePath, "%d/%s"), index, offlineFilename)
|
|
|
|
|
2020-11-15 05:39:53 +03:00
|
|
|
err := utils.Copy(offlineFilePath, segmentFilePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln(err)
|
|
|
|
}
|
|
|
|
_, err = _storage.Save(segmentFilePath, 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln(err)
|
|
|
|
}
|
2020-10-15 00:07:38 +03:00
|
|
|
if utils.DoesFileExists(playlistFilePath) {
|
|
|
|
f, err := os.OpenFile(playlistFilePath, os.O_CREATE|os.O_RDWR, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
2020-10-17 01:04:31 +03:00
|
|
|
defer f.Close()
|
2020-10-15 00:07:38 +03:00
|
|
|
|
|
|
|
playlist, _, err := m3u8.DecodeFrom(bufio.NewReader(f), true)
|
2020-11-15 05:39:53 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2020-10-15 00:07:38 +03:00
|
|
|
variantPlaylist := playlist.(*m3u8.MediaPlaylist)
|
|
|
|
if len(variantPlaylist.Segments) > config.Config.GetMaxNumberOfReferencedSegmentsInPlaylist() {
|
|
|
|
variantPlaylist.Segments = variantPlaylist.Segments[:len(variantPlaylist.Segments)]
|
|
|
|
}
|
|
|
|
|
|
|
|
err = variantPlaylist.Append(offlineFilename, 8.0, "")
|
2020-11-15 05:39:53 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
err = variantPlaylist.SetDiscontinuity()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2020-10-15 00:07:38 +03:00
|
|
|
_, err = f.WriteAt(variantPlaylist.Encode().Bytes(), 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p, err := m3u8.NewMediaPlaylist(1, 1)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If "offline" content gets changed then change the duration below
|
|
|
|
err = p.Append(offlineFilename, 8.0, "")
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Close()
|
|
|
|
f, err := os.Create(playlistFilePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = f.Write(p.Encode().Bytes())
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
}
|
2020-11-15 05:39:53 +03:00
|
|
|
_, err = _storage.Save(playlistFilePath, 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln(err)
|
|
|
|
}
|
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()
|
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 {
|
|
|
|
// Reset the session count since the session is over
|
|
|
|
_stats.SessionMaxViewerCount = 0
|
|
|
|
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 {
|
|
|
|
ffmpeg.CleanupOldContent(config.PrivateHLSStoragePath)
|
2020-10-16 03:50:17 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopOnlineCleanupTimer() {
|
|
|
|
if _onlineCleanupTicker != nil {
|
|
|
|
_onlineCleanupTicker.Stop()
|
2020-10-15 00:07:38 +03:00
|
|
|
}
|
|
|
|
}
|