2020-06-23 04:11:56 +03:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
2020-10-04 00:35:03 +03:00
|
|
|
"path/filepath"
|
2020-06-23 04:11:56 +03:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-10-05 20:07:09 +03:00
|
|
|
"github.com/owncast/owncast/config"
|
|
|
|
"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"
|
2021-07-20 05:22:29 +03:00
|
|
|
"github.com/owncast/owncast/core/user"
|
2020-10-05 20:07:09 +03:00
|
|
|
"github.com/owncast/owncast/models"
|
|
|
|
"github.com/owncast/owncast/utils"
|
|
|
|
"github.com/owncast/owncast/yp"
|
2020-06-23 04:11:56 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-10-15 00:07:38 +03:00
|
|
|
_stats *models.Stats
|
|
|
|
_storage models.StorageProvider
|
2021-02-19 10:05:52 +03:00
|
|
|
_transcoder *transcoder.Transcoder
|
2020-10-15 00:07:38 +03:00
|
|
|
_yp *yp.YP
|
|
|
|
_broadcaster *models.Broadcaster
|
2020-06-23 04:11:56 +03:00
|
|
|
)
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
var handler transcoder.HLSHandler
|
|
|
|
var fileWriter = transcoder.FileWriterReceiverService{}
|
2020-10-15 00:07:38 +03:00
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// Start starts up the core processing.
|
2020-06-23 04:11:56 +03:00
|
|
|
func Start() error {
|
|
|
|
resetDirectories()
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
data.PopulateDefaults()
|
|
|
|
|
|
|
|
if err := data.VerifySettings(); err != nil {
|
|
|
|
log.Error(err)
|
2020-06-23 04:11:56 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
if err := setupStats(); err != nil {
|
|
|
|
log.Error("failed to setup the stats")
|
2020-06-23 04:11:56 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-15 00:07:38 +03: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.
|
2021-02-19 10:05:52 +03:00
|
|
|
handler = transcoder.HLSHandler{}
|
|
|
|
|
|
|
|
if err := setupStorage(); err != nil {
|
|
|
|
log.Errorln("storage error", err)
|
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
user.SetupUsers()
|
|
|
|
|
2020-10-15 00:07:38 +03:00
|
|
|
fileWriter.SetupFileWriterReceiverService(&handler)
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
if err := createInitialOfflineState(); err != nil {
|
2020-07-07 07:27:31 +03:00
|
|
|
log.Error("failed to create the initial offline state")
|
2020-06-23 04:11:56 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-04 12:48:10 +03:00
|
|
|
_yp = yp.NewYP(GetStatus)
|
2020-10-02 09:55:38 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
if err := chat.Start(GetStatus); err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
2020-06-23 23:11:01 +03:00
|
|
|
|
2020-10-30 00:09:28 +03:00
|
|
|
// start the rtmp server
|
|
|
|
go rtmp.Start(setStreamAsConnected, setBroadcaster)
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
rtmpPort := data.GetRTMPPortNumber()
|
2021-02-26 22:23:15 +03:00
|
|
|
log.Infof("RTMP is accepting inbound streams on port %d.", rtmpPort)
|
2020-11-07 02:12:35 +03:00
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createInitialOfflineState() error {
|
|
|
|
// Provide default files
|
2020-10-04 00:35:03 +03: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 04:11:56 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 00:07:38 +03:00
|
|
|
transitionToOfflineVideoStreamContent()
|
2020-06-26 03:44:47 +03:00
|
|
|
|
|
|
|
return nil
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
|
|
|
|
2020-10-15 00:07:38 +03: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
|
2021-02-19 10:05:52 +03:00
|
|
|
_transcoder := transcoder.NewTranscoder()
|
2020-10-15 00:07:38 +03:00
|
|
|
_transcoder.SetInput(offlineFilePath)
|
2021-04-15 23:55:51 +03:00
|
|
|
_transcoder.SetIdentifier("offline")
|
2020-10-15 00:07:38 +03:00
|
|
|
_transcoder.Start()
|
|
|
|
|
|
|
|
// Copy the logo to be the thumbnail
|
2021-02-19 10:05:52 +03:00
|
|
|
logo := data.GetLogoPath()
|
|
|
|
err := utils.Copy(filepath.Join("data", logo), "webroot/thumbnail.jpg")
|
2020-11-15 05:39:53 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Warnln(err)
|
|
|
|
}
|
2020-11-03 06:39:52 +03:00
|
|
|
|
|
|
|
// Delete the preview Gif
|
|
|
|
os.Remove(path.Join(config.WebRoot, "preview.gif"))
|
2020-09-16 23:31:21 +03:00
|
|
|
}
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
func resetDirectories() {
|
2020-07-07 07:27:31 +03:00
|
|
|
log.Trace("Resetting file directories to a clean slate.")
|
2020-06-23 04:11:56 +03:00
|
|
|
|
|
|
|
// Wipe the public, web-accessible hls data directory
|
2021-07-08 22:35:53 +03:00
|
|
|
utils.CleanupDirectory(config.PublicHLSStoragePath)
|
|
|
|
utils.CleanupDirectory(config.PrivateHLSStoragePath)
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2020-10-15 00:07:38 +03:00
|
|
|
// Remove the previous thumbnail
|
2021-02-19 10:05:52 +03:00
|
|
|
logo := data.GetLogoPath()
|
2021-05-15 01:28:13 +03:00
|
|
|
if utils.DoesFileExists(logo) {
|
2021-07-08 22:35:53 +03:00
|
|
|
err := utils.Copy(path.Join("data", logo), filepath.Join(config.WebRoot, "thumbnail.jpg"))
|
2021-05-15 01:28:13 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Warnln(err)
|
|
|
|
}
|
2020-11-15 05:39:53 +03:00
|
|
|
}
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|