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"
|
|
|
|
|
2022-04-22 00:55:26 +03:00
|
|
|
"github.com/owncast/owncast/auth"
|
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"
|
2021-11-14 21:02:52 +03:00
|
|
|
"github.com/owncast/owncast/core/webhooks"
|
2020-10-05 20:07:09 +03:00
|
|
|
"github.com/owncast/owncast/models"
|
2022-03-18 23:33:23 +03:00
|
|
|
"github.com/owncast/owncast/notifications"
|
2020-10-05 20:07:09 +03:00
|
|
|
"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
|
2022-03-18 23:33:23 +03:00
|
|
|
handler transcoder.HLSHandler
|
|
|
|
fileWriter = transcoder.FileWriterReceiverService{}
|
2021-11-27 07:53:27 +03:00
|
|
|
)
|
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()
|
2022-04-22 00:55:26 +03:00
|
|
|
auth.Setup(data.GetDatastore())
|
2021-07-20 05:22:29 +03:00
|
|
|
|
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
|
|
|
|
2021-11-14 21:02:52 +03:00
|
|
|
webhooks.InitWorkerPool()
|
|
|
|
|
2022-03-18 23:33:23 +03:00
|
|
|
notifications.Setup(data.GetStore())
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createInitialOfflineState() error {
|
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")
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
_transcoder := transcoder.NewTranscoder()
|
2021-04-15 23:55:51 +03:00
|
|
|
_transcoder.SetIdentifier("offline")
|
2021-11-27 07:53:27 +03:00
|
|
|
_transcoder.SetLatencyLevel(models.GetLatencyLevel(4))
|
|
|
|
_transcoder.SetIsEvent(true)
|
2021-10-12 01:04:16 +03:00
|
|
|
|
2023-01-27 04:11:38 +03:00
|
|
|
offlineFilePath, err := saveOfflineClipToDisk("offline.tsclip")
|
2021-11-27 07:53:27 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("unable to save offline clip:", err)
|
2021-10-12 01:04:16 +03:00
|
|
|
}
|
2020-10-15 00:07:38 +03:00
|
|
|
|
2021-11-27 07:53:27 +03:00
|
|
|
_transcoder.SetInput(offlineFilePath)
|
|
|
|
go _transcoder.Start()
|
|
|
|
|
2020-10-15 00:07:38 +03:00
|
|
|
// Copy the logo to be the thumbnail
|
2021-02-19 10:05:52 +03:00
|
|
|
logo := data.GetLogoPath()
|
2022-06-21 07:43:53 +03:00
|
|
|
dst := filepath.Join(config.TempDir, "thumbnail.jpg")
|
|
|
|
if err = utils.Copy(filepath.Join("data", logo), dst); err != nil {
|
2020-11-15 05:39:53 +03:00
|
|
|
log.Warnln(err)
|
|
|
|
}
|
2020-11-03 06:39:52 +03:00
|
|
|
|
|
|
|
// Delete the preview Gif
|
2022-06-21 07:43:53 +03:00
|
|
|
_ = os.Remove(path.Join(config.DataDirectory, "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
|
|
|
|
2021-09-12 21:32:42 +03:00
|
|
|
// Wipe hls data directory
|
|
|
|
utils.CleanupDirectory(config.HLSStoragePath)
|
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) {
|
2022-06-21 07:43:53 +03:00
|
|
|
err := utils.Copy(path.Join("data", logo), filepath.Join(config.DataDirectory, "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
|
|
|
}
|