core.go 3.7 KB

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