2020-06-22 20:11:56 -05:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
2020-09-16 15:31:21 -05:00
|
|
|
"time"
|
2020-06-22 20:11:56 -05:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/gabek/owncast/config"
|
2020-06-23 15:11:01 -05:00
|
|
|
"github.com/gabek/owncast/core/chat"
|
2020-06-22 20:11:56 -05:00
|
|
|
"github.com/gabek/owncast/core/ffmpeg"
|
|
|
|
"github.com/gabek/owncast/models"
|
|
|
|
"github.com/gabek/owncast/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-09-23 21:35:27 -07:00
|
|
|
_stats *models.Stats
|
|
|
|
_storage models.ChunkStorageProvider
|
|
|
|
_cleanupTimer *time.Timer
|
2020-06-22 20:11:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
//Start starts up the core processing
|
|
|
|
func Start() error {
|
|
|
|
resetDirectories()
|
|
|
|
|
|
|
|
if err := setupStats(); err != nil {
|
2020-07-06 21:27:31 -07:00
|
|
|
log.Error("failed to setup the stats")
|
2020-06-22 20:11:56 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := setupStorage(); err != nil {
|
2020-07-06 21:27:31 -07:00
|
|
|
log.Error("failed to setup the storage")
|
2020-06-22 20:11:56 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-06-23 15:11:01 -05:00
|
|
|
chat.Setup(ChatListenerImpl{})
|
|
|
|
|
2020-06-22 20:11:56 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createInitialOfflineState() error {
|
|
|
|
// Provide default files
|
|
|
|
if !utils.DoesFileExists("webroot/thumbnail.jpg") {
|
|
|
|
if err := utils.Copy("static/logo.png", "webroot/thumbnail.jpg"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 17:44:47 -07:00
|
|
|
ffmpeg.ShowStreamOfflineState()
|
|
|
|
|
|
|
|
return nil
|
2020-06-22 20:11:56 -05:00
|
|
|
}
|
|
|
|
|
2020-09-16 15:31:21 -05:00
|
|
|
func startCleanupTimer() {
|
2020-09-23 21:35:27 -07:00
|
|
|
_cleanupTimer = time.NewTimer(5 * time.Minute)
|
2020-09-16 15:31:21 -05:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2020-09-23 21:35:27 -07:00
|
|
|
case <-_cleanupTimer.C:
|
2020-09-24 19:38:24 -07:00
|
|
|
// Reset the session count since the session is over
|
|
|
|
_stats.SessionMaxViewerCount = 0
|
2020-09-16 15:31:21 -05:00
|
|
|
resetDirectories()
|
2020-09-17 10:31:31 -07:00
|
|
|
ffmpeg.ShowStreamOfflineState()
|
2020-09-16 15:31:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopCleanupTimer will stop the previous cleanup timer
|
|
|
|
func stopCleanupTimer() {
|
2020-09-23 21:35:27 -07:00
|
|
|
if _cleanupTimer != nil {
|
|
|
|
_cleanupTimer.Stop()
|
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
|
|
|
|
|
|
|
// Wipe the public, web-accessible hls data directory
|
2020-07-13 14:45:54 -07:00
|
|
|
os.RemoveAll(config.Config.GetPublicHLSSavePath())
|
|
|
|
os.RemoveAll(config.Config.GetPrivateHLSSavePath())
|
|
|
|
os.MkdirAll(config.Config.GetPublicHLSSavePath(), 0777)
|
|
|
|
os.MkdirAll(config.Config.GetPrivateHLSSavePath(), 0777)
|
2020-06-22 20:11:56 -05:00
|
|
|
|
|
|
|
// Remove the previous thumbnail
|
|
|
|
os.Remove("webroot/thumbnail.jpg")
|
|
|
|
|
|
|
|
// Create private hls data dirs
|
2020-07-05 22:03:53 -07:00
|
|
|
if len(config.Config.VideoSettings.StreamQualities) != 0 {
|
2020-06-22 20:11:56 -05:00
|
|
|
for index := range config.Config.VideoSettings.StreamQualities {
|
2020-07-13 14:45:54 -07:00
|
|
|
os.MkdirAll(path.Join(config.Config.GetPrivateHLSSavePath(), strconv.Itoa(index)), 0777)
|
|
|
|
os.MkdirAll(path.Join(config.Config.GetPublicHLSSavePath(), strconv.Itoa(index)), 0777)
|
2020-06-22 20:11:56 -05:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-13 14:45:54 -07:00
|
|
|
os.MkdirAll(path.Join(config.Config.GetPrivateHLSSavePath(), strconv.Itoa(0)), 0777)
|
|
|
|
os.MkdirAll(path.Join(config.Config.GetPublicHLSSavePath(), strconv.Itoa(0)), 0777)
|
2020-06-22 20:11:56 -05:00
|
|
|
}
|
|
|
|
}
|