2020-06-22 20:11:56 -05:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
2020-10-03 14:35:03 -07:00
|
|
|
"path/filepath"
|
2020-06-22 20:11:56 -05:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2022-04-21 14:55:26 -07:00
|
|
|
"github.com/owncast/owncast/auth"
|
2020-10-06 01:07:09 +08:00
|
|
|
"github.com/owncast/owncast/config"
|
|
|
|
"github.com/owncast/owncast/core/chat"
|
2021-02-18 23:05:52 -08:00
|
|
|
"github.com/owncast/owncast/core/data"
|
2020-10-29 14:09:28 -07:00
|
|
|
"github.com/owncast/owncast/core/rtmp"
|
2021-02-18 23:05:52 -08:00
|
|
|
"github.com/owncast/owncast/core/transcoder"
|
2021-11-14 19:02:52 +01:00
|
|
|
"github.com/owncast/owncast/core/webhooks"
|
2020-10-06 01:07:09 +08:00
|
|
|
"github.com/owncast/owncast/models"
|
2022-03-18 13:33:23 -07:00
|
|
|
"github.com/owncast/owncast/notifications"
|
2024-07-01 18:58:50 -07:00
|
|
|
"github.com/owncast/owncast/persistence/tables"
|
2020-10-06 01:07:09 +08:00
|
|
|
"github.com/owncast/owncast/utils"
|
|
|
|
"github.com/owncast/owncast/yp"
|
2020-06-22 20:11:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-10-14 14:07:38 -07:00
|
|
|
_stats *models.Stats
|
|
|
|
_storage models.StorageProvider
|
2021-02-18 23:05:52 -08:00
|
|
|
_transcoder *transcoder.Transcoder
|
2020-10-14 14:07:38 -07:00
|
|
|
_yp *yp.YP
|
|
|
|
_broadcaster *models.Broadcaster
|
2022-03-18 13:33:23 -07:00
|
|
|
handler transcoder.HLSHandler
|
|
|
|
fileWriter = transcoder.FileWriterReceiverService{}
|
2021-11-26 20:53:27 -08:00
|
|
|
)
|
2020-10-14 14:07:38 -07:00
|
|
|
|
2020-11-13 00:14:59 +01:00
|
|
|
// Start starts up the core processing.
|
2020-06-22 20:11:56 -05:00
|
|
|
func Start() error {
|
|
|
|
resetDirectories()
|
|
|
|
|
2021-02-18 23:05:52 -08:00
|
|
|
data.PopulateDefaults()
|
|
|
|
|
|
|
|
if err := data.VerifySettings(); err != nil {
|
|
|
|
log.Error(err)
|
2020-06-22 20:11:56 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-18 23:05:52 -08:00
|
|
|
if err := setupStats(); err != nil {
|
|
|
|
log.Error("failed to setup the stats")
|
2020-06-22 20:11:56 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-14 14:07:38 -07: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-18 23:05:52 -08:00
|
|
|
handler = transcoder.HLSHandler{}
|
|
|
|
|
|
|
|
if err := setupStorage(); err != nil {
|
|
|
|
log.Errorln("storage error", err)
|
|
|
|
}
|
|
|
|
|
2024-07-01 18:58:50 -07:00
|
|
|
tables.SetupUsers(data.GetDatastore().DB)
|
2022-04-21 14:55:26 -07:00
|
|
|
auth.Setup(data.GetDatastore())
|
2021-07-19 19:22:29 -07:00
|
|
|
|
2020-10-14 14:07:38 -07:00
|
|
|
fileWriter.SetupFileWriterReceiverService(&handler)
|
|
|
|
|
2020-06-22 20:11:56 -05:00
|
|
|
if err := createInitialOfflineState(); err != nil {
|
2020-07-06 21:27:31 -07:00
|
|
|
log.Error("failed to create the initial offline state")
|
2020-06-22 20:11:56 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-04 01:48:10 -08:00
|
|
|
_yp = yp.NewYP(GetStatus)
|
2020-10-01 23:55:38 -07:00
|
|
|
|
2021-07-19 19:22:29 -07:00
|
|
|
if err := chat.Start(GetStatus); err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
2020-06-23 15:11:01 -05:00
|
|
|
|
2020-10-29 14:09:28 -07:00
|
|
|
// start the rtmp server
|
|
|
|
go rtmp.Start(setStreamAsConnected, setBroadcaster)
|
|
|
|
|
2021-02-18 23:05:52 -08:00
|
|
|
rtmpPort := data.GetRTMPPortNumber()
|
2023-06-05 08:44:14 -07:00
|
|
|
if rtmpPort != 1935 {
|
|
|
|
log.Infof("RTMP is accepting inbound streams on port %d.", rtmpPort)
|
|
|
|
}
|
2020-11-06 15:12:35 -08:00
|
|
|
|
2023-05-30 11:32:05 -07:00
|
|
|
webhooks.SetupWebhooks(GetStatus)
|
2021-11-14 19:02:52 +01:00
|
|
|
|
2022-03-18 13:33:23 -07:00
|
|
|
notifications.Setup(data.GetStore())
|
|
|
|
|
2020-06-22 20:11:56 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createInitialOfflineState() error {
|
2020-10-14 14:07:38 -07:00
|
|
|
transitionToOfflineVideoStreamContent()
|
2020-06-25 17:44:47 -07:00
|
|
|
|
|
|
|
return nil
|
2020-06-22 20:11:56 -05:00
|
|
|
}
|
|
|
|
|
2020-10-14 14:07:38 -07: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-18 23:05:52 -08:00
|
|
|
_transcoder := transcoder.NewTranscoder()
|
2021-04-15 13:55:51 -07:00
|
|
|
_transcoder.SetIdentifier("offline")
|
2021-11-26 20:53:27 -08:00
|
|
|
_transcoder.SetLatencyLevel(models.GetLatencyLevel(4))
|
|
|
|
_transcoder.SetIsEvent(true)
|
2021-10-11 15:04:16 -07:00
|
|
|
|
2024-04-17 11:44:19 -07:00
|
|
|
offlineFilePath, err := saveOfflineClipToDisk("offline-v2.ts")
|
2021-11-26 20:53:27 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("unable to save offline clip:", err)
|
2021-10-11 15:04:16 -07:00
|
|
|
}
|
2020-10-14 14:07:38 -07:00
|
|
|
|
2021-11-26 20:53:27 -08:00
|
|
|
_transcoder.SetInput(offlineFilePath)
|
2023-06-05 08:44:14 -07:00
|
|
|
go _transcoder.Start(false)
|
2021-11-26 20:53:27 -08:00
|
|
|
|
2020-10-14 14:07:38 -07:00
|
|
|
// Copy the logo to be the thumbnail
|
2021-02-18 23:05:52 -08:00
|
|
|
logo := data.GetLogoPath()
|
2022-06-20 21:43:53 -07:00
|
|
|
dst := filepath.Join(config.TempDir, "thumbnail.jpg")
|
|
|
|
if err = utils.Copy(filepath.Join("data", logo), dst); err != nil {
|
2020-11-14 18:39:53 -08:00
|
|
|
log.Warnln(err)
|
|
|
|
}
|
2020-11-02 19:39:52 -08:00
|
|
|
|
|
|
|
// Delete the preview Gif
|
2022-06-20 21:43:53 -07:00
|
|
|
_ = os.Remove(path.Join(config.DataDirectory, "preview.gif"))
|
2020-09-16 15:31:21 -05:00
|
|
|
}
|
|
|
|
|
2020-06-22 20:11:56 -05:00
|
|
|
func resetDirectories() {
|
2020-07-06 21:27:31 -07:00
|
|
|
log.Trace("Resetting file directories to a clean slate.")
|
2020-06-22 20:11:56 -05:00
|
|
|
|
2021-09-12 11:32:42 -07:00
|
|
|
// Wipe hls data directory
|
|
|
|
utils.CleanupDirectory(config.HLSStoragePath)
|
2020-06-22 20:11:56 -05:00
|
|
|
|
2020-10-14 14:07:38 -07:00
|
|
|
// Remove the previous thumbnail
|
2021-02-18 23:05:52 -08:00
|
|
|
logo := data.GetLogoPath()
|
2021-05-14 15:28:13 -07:00
|
|
|
if utils.DoesFileExists(logo) {
|
2022-06-20 21:43:53 -07:00
|
|
|
err := utils.Copy(path.Join("data", logo), filepath.Join(config.DataDirectory, "thumbnail.jpg"))
|
2021-05-14 15:28:13 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Warnln(err)
|
|
|
|
}
|
2020-11-14 18:39:53 -08:00
|
|
|
}
|
2020-06-22 20:11:56 -05:00
|
|
|
}
|