stop encodeing when not broadcastin, resolves #64

This commit is contained in:
Craig 2020-04-05 23:07:25 +00:00
parent 613a192605
commit 9220661ae0
4 changed files with 61 additions and 35 deletions

View File

@ -21,6 +21,7 @@ type RemoteManager struct {
cleanup *time.Ticker cleanup *time.Ticker
shutdown chan bool shutdown chan bool
emmiter events.EventEmmiter emmiter events.EventEmmiter
streaming bool
} }
func New(config *config.Remote) *RemoteManager { func New(config *config.Remote) *RemoteManager {
@ -30,6 +31,7 @@ func New(config *config.Remote) *RemoteManager {
shutdown: make(chan bool), shutdown: make(chan bool),
emmiter: events.New(), emmiter: events.New(),
config: config, config: config,
streaming: false,
} }
} }
@ -42,26 +44,7 @@ func (manager *RemoteManager) AudioCodec() string {
} }
func (manager *RemoteManager) Start() { func (manager *RemoteManager) Start() {
var err error manager.createPipelines()
manager.video, err = gst.CreatePipeline(
manager.config.VideoCodec,
manager.config.Display,
manager.config.VideoParams,
)
if err != nil {
manager.logger.Panic().Err(err).Msg("unable to create video pipeline")
}
manager.audio, err = gst.CreatePipeline(
manager.config.AudioCodec,
manager.config.Device,
manager.config.AudioParams,
)
if err != nil {
manager.logger.Panic().Err(err).Msg("unable to screate audio pipeline")
}
manager.StartStream()
go func() { go func() {
defer func() { defer func() {
@ -113,7 +96,7 @@ func (manager *RemoteManager) StartStream() {
Str("audio_pipeline_src", manager.audio.Src). Str("audio_pipeline_src", manager.audio.Src).
Str("video_pipeline_src", manager.video.Src). Str("video_pipeline_src", manager.video.Src).
Str("screen_resolution", fmt.Sprintf("%dx%d@%d", manager.config.ScreenWidth, manager.config.ScreenHeight, manager.config.ScreenRate)). Str("screen_resolution", fmt.Sprintf("%dx%d@%d", manager.config.ScreenWidth, manager.config.ScreenHeight, manager.config.ScreenRate)).
Msgf("pipelines starting...") Msgf("Pipelines starting...")
xorg.Display(manager.config.Display) xorg.Display(manager.config.Display)
@ -125,13 +108,42 @@ func (manager *RemoteManager) StartStream() {
} }
} }
manager.createPipelines()
manager.video.Start() manager.video.Start()
manager.audio.Start() manager.audio.Start()
manager.streaming = true
} }
func (manager *RemoteManager) StopStream() { func (manager *RemoteManager) StopStream() {
manager.logger.Info().Msgf("Pipelines shutting down...")
manager.video.Stop() manager.video.Stop()
manager.audio.Stop() manager.audio.Stop()
manager.streaming = false
}
func (manager *RemoteManager) Streaming() bool {
return manager.streaming
}
func (manager *RemoteManager) createPipelines() {
var err error
manager.video, err = gst.CreatePipeline(
manager.config.VideoCodec,
manager.config.Display,
manager.config.VideoParams,
)
if err != nil {
manager.logger.Panic().Err(err).Msg("unable to create video pipeline")
}
manager.audio, err = gst.CreatePipeline(
manager.config.AudioCodec,
manager.config.Device,
manager.config.AudioParams,
)
if err != nil {
manager.logger.Panic().Err(err).Msg("unable to screate audio pipeline")
}
} }
func (manager *RemoteManager) ChangeResolution(width int, height int, rate int) error { func (manager *RemoteManager) ChangeResolution(width int, height int, rate int) error {

View File

@ -11,10 +11,11 @@ import (
"n.eko.moe/neko/internal/utils" "n.eko.moe/neko/internal/utils"
) )
func New() *SessionManager { func New(remote types.RemoteManager) *SessionManager {
return &SessionManager{ return &SessionManager{
logger: log.With().Str("module", "session").Logger(), logger: log.With().Str("module", "session").Logger(),
host: "", host: "",
remote: remote,
members: make(map[string]*Session), members: make(map[string]*Session),
emmiter: events.New(), emmiter: events.New(),
} }
@ -23,6 +24,7 @@ func New() *SessionManager {
type SessionManager struct { type SessionManager struct {
logger zerolog.Logger logger zerolog.Logger
host string host string
remote types.RemoteManager
members map[string]*Session members map[string]*Session
emmiter events.EventEmmiter emmiter events.EventEmmiter
} }
@ -40,6 +42,10 @@ func (manager *SessionManager) New(id string, admin bool, socket types.WebSocket
manager.members[id] = session manager.members[id] = session
manager.emmiter.Emit("created", id, session) manager.emmiter.Emit("created", id, session)
if manager.remote.Streaming() != true && len(manager.members) > 0 {
manager.remote.StartStream()
}
return session return session
} }
@ -102,9 +108,15 @@ func (manager *SessionManager) Destroy(id string) error {
if ok { if ok {
err := session.destroy() err := session.destroy()
delete(manager.members, id) delete(manager.members, id)
if manager.remote.Streaming() != false && len(manager.members) <= 0 {
manager.remote.StopStream()
}
manager.emmiter.Emit("destroyed", id) manager.emmiter.Emit("destroyed", id)
return err return err
} }
return nil return nil
} }

View File

@ -9,6 +9,7 @@ type RemoteManager interface {
OnAudioFrame(listener func(sample Sample)) OnAudioFrame(listener func(sample Sample))
StartStream() StartStream()
StopStream() StopStream()
Streaming() bool
ChangeResolution(width int, height int, rate int) error ChangeResolution(width int, height int, rate int) error
GetScreenSize() *ScreenSize GetScreenSize() *ScreenSize
ScreenConfigurations() map[int]ScreenConfiguration ScreenConfigurations() map[int]ScreenConfiguration

View File

@ -116,11 +116,12 @@ func (neko *Neko) Preflight() {
} }
func (neko *Neko) Start() { func (neko *Neko) Start() {
sessionManager := session.New()
remoteManager := remote.New(neko.Remote) remoteManager := remote.New(neko.Remote)
remoteManager.Start() remoteManager.Start()
sessionManager := session.New(remoteManager)
webRTCManager := webrtc.New(sessionManager, remoteManager, neko.WebRTC) webRTCManager := webrtc.New(sessionManager, remoteManager, neko.WebRTC)
webRTCManager.Start() webRTCManager.Start()