owncast/core/core.go
Gabe Kangas ca9d5de192
Embed static resources (#1466)
* Replace pkger with go:embed for bundling the admin. Closes #844

* Remove references to pkged.go

* Point tests to use an updated version of Go

* Add comment to new exported function

* Cleanup

* Add a dummy pkged.go to alert people to stop using it.

* Add simple browser test to make sure the admin is available and renders

* Don't panic

* Embed bot/scraper metadata template.

Add browser test to validate the rendering of this template.

* Use embedded offline.ts segment

* Remove placeholder thumbnail as its unnecessary

* Remove copying the static directory into the release

* Cleanup
2021-10-11 15:04:16 -07:00

133 lines
3.1 KiB
Go

package core
import (
"io"
"os"
"path"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/owncast/owncast/config"
"github.com/owncast/owncast/core/chat"
"github.com/owncast/owncast/core/data"
"github.com/owncast/owncast/core/rtmp"
"github.com/owncast/owncast/core/transcoder"
"github.com/owncast/owncast/core/user"
"github.com/owncast/owncast/models"
"github.com/owncast/owncast/static"
"github.com/owncast/owncast/utils"
"github.com/owncast/owncast/yp"
)
var (
_stats *models.Stats
_storage models.StorageProvider
_transcoder *transcoder.Transcoder
_yp *yp.YP
_broadcaster *models.Broadcaster
)
var handler transcoder.HLSHandler
var fileWriter = transcoder.FileWriterReceiverService{}
// Start starts up the core processing.
func Start() error {
resetDirectories()
data.PopulateDefaults()
if err := data.VerifySettings(); err != nil {
log.Error(err)
return err
}
if err := setupStats(); err != nil {
log.Error("failed to setup the stats")
return err
}
// 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 = transcoder.HLSHandler{}
if err := setupStorage(); err != nil {
log.Errorln("storage error", err)
}
user.SetupUsers()
fileWriter.SetupFileWriterReceiverService(&handler)
if err := createInitialOfflineState(); err != nil {
log.Error("failed to create the initial offline state")
return err
}
_yp = yp.NewYP(GetStatus)
if err := chat.Start(GetStatus); err != nil {
log.Errorln(err)
}
// start the rtmp server
go rtmp.Start(setStreamAsConnected, setBroadcaster)
rtmpPort := data.GetRTMPPortNumber()
log.Infof("RTMP is accepting inbound streams on port %d.", rtmpPort)
return nil
}
func createInitialOfflineState() error {
transitionToOfflineVideoStreamContent()
return nil
}
// 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")
r, w := io.Pipe()
_transcoder := transcoder.NewTranscoder()
_transcoder.SetInput("pipe:0")
_transcoder.SetStdin(r)
_transcoder.SetIdentifier("offline")
go _transcoder.Start()
d := static.GetOfflineSegment()
if _, err := w.Write(d); err != nil {
log.Errorln(err)
}
// Copy the logo to be the thumbnail
logo := data.GetLogoPath()
err := utils.Copy(filepath.Join("data", logo), "webroot/thumbnail.jpg")
if err != nil {
log.Warnln(err)
}
// Delete the preview Gif
_ = os.Remove(path.Join(config.WebRoot, "preview.gif"))
}
func resetDirectories() {
log.Trace("Resetting file directories to a clean slate.")
// Wipe hls data directory
utils.CleanupDirectory(config.HLSStoragePath)
// Remove the previous thumbnail
logo := data.GetLogoPath()
if utils.DoesFileExists(logo) {
err := utils.Copy(path.Join("data", logo), filepath.Join(config.WebRoot, "thumbnail.jpg"))
if err != nil {
log.Warnln(err)
}
}
}