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

@ -14,22 +14,24 @@ import (
)
type RemoteManager struct {
logger zerolog.Logger
video *gst.Pipeline
audio *gst.Pipeline
config *config.Remote
cleanup *time.Ticker
shutdown chan bool
emmiter events.EventEmmiter
logger zerolog.Logger
video *gst.Pipeline
audio *gst.Pipeline
config *config.Remote
cleanup *time.Ticker
shutdown chan bool
emmiter events.EventEmmiter
streaming bool
}
func New(config *config.Remote) *RemoteManager {
return &RemoteManager{
logger: log.With().Str("module", "remote").Logger(),
cleanup: time.NewTicker(1 * time.Second),
shutdown: make(chan bool),
emmiter: events.New(),
config: config,
logger: log.With().Str("module", "remote").Logger(),
cleanup: time.NewTicker(1 * time.Second),
shutdown: make(chan bool),
emmiter: events.New(),
config: config,
streaming: false,
}
}
@ -42,26 +44,7 @@ func (manager *RemoteManager) AudioCodec() string {
}
func (manager *RemoteManager) Start() {
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")
}
manager.StartStream()
manager.createPipelines()
go func() {
defer func() {
@ -113,7 +96,7 @@ func (manager *RemoteManager) StartStream() {
Str("audio_pipeline_src", manager.audio.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)).
Msgf("pipelines starting...")
Msgf("Pipelines starting...")
xorg.Display(manager.config.Display)
@ -125,13 +108,42 @@ func (manager *RemoteManager) StartStream() {
}
}
manager.createPipelines()
manager.video.Start()
manager.audio.Start()
manager.streaming = true
}
func (manager *RemoteManager) StopStream() {
manager.logger.Info().Msgf("Pipelines shutting down...")
manager.video.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 {

View File

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

View File

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

View File

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