2020-06-23 03:11:56 +02:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
2020-10-03 23:35:03 +02:00
|
|
|
"path/filepath"
|
2020-06-23 03:11:56 +02:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-10-05 19:07:09 +02:00
|
|
|
"github.com/owncast/owncast/config"
|
|
|
|
"github.com/owncast/owncast/core/chat"
|
|
|
|
"github.com/owncast/owncast/core/ffmpeg"
|
2020-10-29 22:09:28 +01:00
|
|
|
"github.com/owncast/owncast/core/rtmp"
|
2020-10-05 19:07:09 +02:00
|
|
|
"github.com/owncast/owncast/models"
|
|
|
|
"github.com/owncast/owncast/utils"
|
|
|
|
"github.com/owncast/owncast/yp"
|
2020-06-23 03:11:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-10-14 23:07:38 +02:00
|
|
|
_stats *models.Stats
|
|
|
|
_storage models.StorageProvider
|
|
|
|
_transcoder *ffmpeg.Transcoder
|
|
|
|
_yp *yp.YP
|
|
|
|
_broadcaster *models.Broadcaster
|
2020-06-23 03:11:56 +02:00
|
|
|
)
|
|
|
|
|
2020-10-14 23:07:38 +02:00
|
|
|
var handler ffmpeg.HLSHandler
|
|
|
|
var fileWriter = ffmpeg.FileWriterReceiverService{}
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
//Start starts up the core processing
|
|
|
|
func Start() error {
|
|
|
|
resetDirectories()
|
|
|
|
|
|
|
|
if err := setupStats(); err != nil {
|
2020-07-07 06:27:31 +02:00
|
|
|
log.Error("failed to setup the stats")
|
2020-06-23 03:11:56 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := setupStorage(); err != nil {
|
2020-07-07 06:27:31 +02:00
|
|
|
log.Error("failed to setup the storage")
|
2020-06-23 03:11:56 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-14 23:07:38 +02:00
|
|
|
// The HLS handler takes the written HLS playlists and segments
|
|
|
|
// and makes storage decisions. It's rather simple right now
|
|
|
|
// but will play more useful when recordings come into play.
|
|
|
|
handler = ffmpeg.HLSHandler{}
|
|
|
|
handler.Storage = _storage
|
|
|
|
fileWriter.SetupFileWriterReceiverService(&handler)
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
if err := createInitialOfflineState(); err != nil {
|
2020-07-07 06:27:31 +02:00
|
|
|
log.Error("failed to create the initial offline state")
|
2020-06-23 03:11:56 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-02 08:55:38 +02:00
|
|
|
if config.Config.YP.Enabled {
|
|
|
|
_yp = yp.NewYP(GetStatus)
|
|
|
|
} else {
|
|
|
|
yp.DisplayInstructions()
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:11:01 +02:00
|
|
|
chat.Setup(ChatListenerImpl{})
|
|
|
|
|
2020-10-29 22:09:28 +01:00
|
|
|
// start the rtmp server
|
|
|
|
go rtmp.Start(setStreamAsConnected, setBroadcaster)
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createInitialOfflineState() error {
|
|
|
|
// Provide default files
|
2020-10-03 23:35:03 +02:00
|
|
|
if !utils.DoesFileExists(filepath.Join(config.WebRoot, "thumbnail.jpg")) {
|
|
|
|
if err := utils.Copy("static/logo.png", filepath.Join(config.WebRoot, "thumbnail.jpg")); err != nil {
|
2020-06-23 03:11:56 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 23:07:38 +02:00
|
|
|
transitionToOfflineVideoStreamContent()
|
2020-06-26 02:44:47 +02:00
|
|
|
|
|
|
|
return nil
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-14 23:07:38 +02:00
|
|
|
// transitionToOfflineVideoStreamContent will overwrite the current stream with the
|
|
|
|
// offline video stream state only. No live stream HLS segments will continue to be
|
|
|
|
// referenced.
|
|
|
|
func transitionToOfflineVideoStreamContent() {
|
|
|
|
log.Traceln("Firing transcoder with offline stream state")
|
|
|
|
|
|
|
|
offlineFilename := "offline.ts"
|
|
|
|
offlineFilePath := "static/" + offlineFilename
|
|
|
|
_transcoder := ffmpeg.NewTranscoder()
|
|
|
|
_transcoder.SetSegmentLength(10)
|
|
|
|
_transcoder.SetInput(offlineFilePath)
|
|
|
|
_transcoder.Start()
|
|
|
|
|
|
|
|
// Copy the logo to be the thumbnail
|
|
|
|
utils.Copy(filepath.Join("webroot", config.Config.InstanceDetails.Logo.Large), "webroot/thumbnail.jpg")
|
2020-11-03 04:39:52 +01:00
|
|
|
|
|
|
|
// Delete the preview Gif
|
|
|
|
os.Remove(path.Join(config.WebRoot, "preview.gif"))
|
2020-09-16 22:31:21 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
func resetDirectories() {
|
2020-07-07 06:27:31 +02:00
|
|
|
log.Trace("Resetting file directories to a clean slate.")
|
2020-06-23 03:11:56 +02:00
|
|
|
|
|
|
|
// Wipe the public, web-accessible hls data directory
|
2020-10-03 23:35:03 +02:00
|
|
|
os.RemoveAll(config.PublicHLSStoragePath)
|
|
|
|
os.RemoveAll(config.PrivateHLSStoragePath)
|
|
|
|
os.MkdirAll(config.PublicHLSStoragePath, 0777)
|
|
|
|
os.MkdirAll(config.PrivateHLSStoragePath, 0777)
|
2020-06-23 03:11:56 +02:00
|
|
|
|
|
|
|
// Remove the previous thumbnail
|
2020-10-03 23:35:03 +02:00
|
|
|
os.Remove(filepath.Join(config.WebRoot, "thumbnail.jpg"))
|
2020-06-23 03:11:56 +02:00
|
|
|
|
|
|
|
// Create private hls data dirs
|
2020-07-06 07:03:53 +02:00
|
|
|
if len(config.Config.VideoSettings.StreamQualities) != 0 {
|
2020-06-23 03:11:56 +02:00
|
|
|
for index := range config.Config.VideoSettings.StreamQualities {
|
2020-10-03 23:35:03 +02:00
|
|
|
os.MkdirAll(path.Join(config.PrivateHLSStoragePath, strconv.Itoa(index)), 0777)
|
|
|
|
os.MkdirAll(path.Join(config.PublicHLSStoragePath, strconv.Itoa(index)), 0777)
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-10-03 23:35:03 +02:00
|
|
|
os.MkdirAll(path.Join(config.PrivateHLSStoragePath, strconv.Itoa(0)), 0777)
|
|
|
|
os.MkdirAll(path.Join(config.PublicHLSStoragePath, strconv.Itoa(0)), 0777)
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
2020-10-14 23:07:38 +02:00
|
|
|
|
|
|
|
// Remove the previous thumbnail
|
|
|
|
utils.Copy(config.Config.InstanceDetails.Logo.Large, "webroot/thumbnail.jpg")
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|