core.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package core
  2. import (
  3. "os"
  4. "path"
  5. "path/filepath"
  6. log "github.com/sirupsen/logrus"
  7. "github.com/owncast/owncast/config"
  8. "github.com/owncast/owncast/core/chat"
  9. "github.com/owncast/owncast/core/data"
  10. "github.com/owncast/owncast/core/rtmp"
  11. "github.com/owncast/owncast/core/transcoder"
  12. "github.com/owncast/owncast/core/user"
  13. "github.com/owncast/owncast/core/webhooks"
  14. "github.com/owncast/owncast/models"
  15. "github.com/owncast/owncast/notifications"
  16. "github.com/owncast/owncast/utils"
  17. "github.com/owncast/owncast/yp"
  18. )
  19. var (
  20. _stats *models.Stats
  21. _storage models.StorageProvider
  22. _transcoder *transcoder.Transcoder
  23. _yp *yp.YP
  24. _broadcaster *models.Broadcaster
  25. handler transcoder.HLSHandler
  26. fileWriter = transcoder.FileWriterReceiverService{}
  27. )
  28. // Start starts up the core processing.
  29. func Start() error {
  30. resetDirectories()
  31. data.PopulateDefaults()
  32. if err := data.VerifySettings(); err != nil {
  33. log.Error(err)
  34. return err
  35. }
  36. if err := setupStats(); err != nil {
  37. log.Error("failed to setup the stats")
  38. return err
  39. }
  40. // The HLS handler takes the written HLS playlists and segments
  41. // and makes storage decisions. It's rather simple right now
  42. // but will play more useful when recordings come into play.
  43. handler = transcoder.HLSHandler{}
  44. if err := setupStorage(); err != nil {
  45. log.Errorln("storage error", err)
  46. }
  47. user.SetupUsers()
  48. fileWriter.SetupFileWriterReceiverService(&handler)
  49. if err := createInitialOfflineState(); err != nil {
  50. log.Error("failed to create the initial offline state")
  51. return err
  52. }
  53. _yp = yp.NewYP(GetStatus)
  54. if err := chat.Start(GetStatus); err != nil {
  55. log.Errorln(err)
  56. }
  57. // start the rtmp server
  58. go rtmp.Start(setStreamAsConnected, setBroadcaster)
  59. rtmpPort := data.GetRTMPPortNumber()
  60. log.Infof("RTMP is accepting inbound streams on port %d.", rtmpPort)
  61. webhooks.InitWorkerPool()
  62. notifications.Setup(data.GetStore())
  63. return nil
  64. }
  65. func createInitialOfflineState() error {
  66. transitionToOfflineVideoStreamContent()
  67. return nil
  68. }
  69. // transitionToOfflineVideoStreamContent will overwrite the current stream with the
  70. // offline video stream state only. No live stream HLS segments will continue to be
  71. // referenced.
  72. func transitionToOfflineVideoStreamContent() {
  73. log.Traceln("Firing transcoder with offline stream state")
  74. _transcoder := transcoder.NewTranscoder()
  75. _transcoder.SetIdentifier("offline")
  76. _transcoder.SetLatencyLevel(models.GetLatencyLevel(4))
  77. _transcoder.SetIsEvent(true)
  78. offlineFilePath, err := saveOfflineClipToDisk("offline.ts")
  79. if err != nil {
  80. log.Fatalln("unable to save offline clip:", err)
  81. }
  82. _transcoder.SetInput(offlineFilePath)
  83. go _transcoder.Start()
  84. // Copy the logo to be the thumbnail
  85. logo := data.GetLogoPath()
  86. if err = utils.Copy(filepath.Join("data", logo), "webroot/thumbnail.jpg"); err != nil {
  87. log.Warnln(err)
  88. }
  89. // Delete the preview Gif
  90. _ = os.Remove(path.Join(config.WebRoot, "preview.gif"))
  91. }
  92. func resetDirectories() {
  93. log.Trace("Resetting file directories to a clean slate.")
  94. // Wipe hls data directory
  95. utils.CleanupDirectory(config.HLSStoragePath)
  96. // Remove the previous thumbnail
  97. logo := data.GetLogoPath()
  98. if utils.DoesFileExists(logo) {
  99. err := utils.Copy(path.Join("data", logo), filepath.Join(config.WebRoot, "thumbnail.jpg"))
  100. if err != nil {
  101. log.Warnln(err)
  102. }
  103. }
  104. }