neko/server/internal/remote/manager.go

239 lines
5.9 KiB
Go
Raw Normal View History

2020-04-06 10:34:51 +12:00
package remote
import (
"fmt"
"time"
"github.com/kataras/go-events"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"n.eko.moe/neko/internal/gst"
"n.eko.moe/neko/internal/types"
"n.eko.moe/neko/internal/types/config"
"n.eko.moe/neko/internal/xorg"
)
type RemoteManager struct {
logger zerolog.Logger
video *gst.Pipeline
audio *gst.Pipeline
config *config.Remote
2020-09-27 10:04:15 +13:00
broadcast types.BroadcastManager
cleanup *time.Ticker
shutdown chan bool
emmiter events.EventEmmiter
streaming bool
2020-04-06 10:34:51 +12:00
}
2020-09-27 10:04:15 +13:00
func New(config *config.Remote, broadcast types.BroadcastManager) *RemoteManager {
2020-04-06 10:34:51 +12:00
return &RemoteManager{
logger: log.With().Str("module", "remote").Logger(),
cleanup: time.NewTicker(1 * time.Second),
shutdown: make(chan bool),
emmiter: events.New(),
config: config,
2020-09-24 18:09:02 +12:00
broadcast: broadcast,
streaming: false,
2020-04-06 10:34:51 +12:00
}
}
func (manager *RemoteManager) VideoCodec() string {
return manager.config.VideoCodec
}
func (manager *RemoteManager) AudioCodec() string {
return manager.config.AudioCodec
}
func (manager *RemoteManager) Start() {
2020-09-27 10:04:15 +13:00
xorg.Display(manager.config.Display)
if !xorg.ValidScreenSize(manager.config.ScreenWidth, manager.config.ScreenHeight, manager.config.ScreenRate) {
manager.logger.Warn().Msgf("invalid screen option %dx%d@%d", manager.config.ScreenWidth, manager.config.ScreenHeight, manager.config.ScreenRate)
} else if err := xorg.ChangeScreenSize(manager.config.ScreenWidth, manager.config.ScreenHeight, manager.config.ScreenRate); err != nil {
manager.logger.Warn().Err(err).Msg("unable to change screen size")
}
manager.createPipelines()
2020-09-27 10:04:15 +13:00
manager.broadcast.Start()
2020-04-06 10:34:51 +12:00
go func() {
defer func() {
manager.logger.Info().Msg("shutdown")
}()
for {
select {
case <-manager.shutdown:
return
case sample := <-manager.video.Sample:
manager.emmiter.Emit("video", sample)
case sample := <-manager.audio.Sample:
manager.emmiter.Emit("audio", sample)
case <-manager.cleanup.C:
xorg.CheckKeys(time.Second * 10)
}
}
}()
}
func (manager *RemoteManager) Shutdown() error {
manager.logger.Info().Msgf("remote shutting down")
manager.video.Stop()
manager.audio.Stop()
2020-09-27 10:04:15 +13:00
manager.broadcast.Stop()
2020-09-24 18:09:02 +12:00
2020-04-06 10:34:51 +12:00
manager.cleanup.Stop()
manager.shutdown <- true
return nil
}
func (manager *RemoteManager) OnVideoFrame(listener func(sample types.Sample)) {
manager.emmiter.On("video", func(payload ...interface{}) {
listener(payload[0].(types.Sample))
})
}
func (manager *RemoteManager) OnAudioFrame(listener func(sample types.Sample)) {
manager.emmiter.On("audio", func(payload ...interface{}) {
listener(payload[0].(types.Sample))
})
}
func (manager *RemoteManager) StartStream() {
2020-09-27 10:04:15 +13:00
manager.createPipelines()
2020-04-06 10:34:51 +12:00
manager.logger.Info().
Str("video_display", manager.config.Display).
Str("video_codec", manager.config.VideoCodec).
Str("audio_device", manager.config.Device).
Str("audio_codec", manager.config.AudioCodec).
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...")
2020-04-06 10:34:51 +12:00
manager.video.Start()
manager.audio.Start()
manager.streaming = true
2020-04-06 10:34:51 +12:00
}
func (manager *RemoteManager) StopStream() {
manager.logger.Info().Msgf("Pipelines shutting down...")
2020-04-06 10:34:51 +12:00
manager.video.Stop()
manager.audio.Stop()
manager.streaming = false
}
func (manager *RemoteManager) Streaming() bool {
return manager.streaming
}
func (manager *RemoteManager) createPipelines() {
var err error
2020-04-06 15:42:42 +12:00
manager.video, err = gst.CreateAppPipeline(
manager.config.VideoCodec,
manager.config.Display,
manager.config.VideoParams,
2021-02-15 09:07:56 +13:00
manager.config.VideoBitrate,
)
if err != nil {
manager.logger.Panic().Err(err).Msg("unable to create video pipeline")
}
2020-04-06 15:42:42 +12:00
manager.audio, err = gst.CreateAppPipeline(
manager.config.AudioCodec,
manager.config.Device,
manager.config.AudioParams,
2021-02-15 09:07:56 +13:00
manager.config.AudioBitrate,
)
if err != nil {
2020-09-24 18:09:02 +12:00
manager.logger.Panic().Err(err).Msg("unable to create audio pipeline")
}
2020-04-06 10:34:51 +12:00
}
func (manager *RemoteManager) ChangeResolution(width int, height int, rate int) error {
if !xorg.ValidScreenSize(width, height, rate) {
return fmt.Errorf("unknown configuration")
}
manager.video.Stop()
2020-09-27 10:04:15 +13:00
manager.broadcast.Stop()
2020-09-24 18:09:02 +12:00
2020-04-06 10:34:51 +12:00
defer func() {
manager.video.Start()
2020-09-27 10:04:15 +13:00
manager.broadcast.Start()
2020-09-24 18:09:02 +12:00
2020-04-06 10:34:51 +12:00
manager.logger.Info().Msg("starting video pipeline...")
}()
if err := xorg.ChangeScreenSize(width, height, rate); err != nil {
return err
}
2020-09-27 10:04:15 +13:00
var err error
manager.video, err = gst.CreateAppPipeline(
2020-04-06 10:34:51 +12:00
manager.config.VideoCodec,
manager.config.Display,
manager.config.VideoParams,
2021-02-15 09:07:56 +13:00
manager.config.VideoBitrate,
2020-04-06 10:34:51 +12:00
)
if err != nil {
manager.logger.Panic().Err(err).Msg("unable to create new video pipeline")
}
2020-09-24 18:09:02 +12:00
2020-04-06 10:34:51 +12:00
return nil
}
func (manager *RemoteManager) Move(x, y int) {
xorg.Move(x, y)
}
func (manager *RemoteManager) Scroll(x, y int) {
xorg.Scroll(x, y)
}
2020-06-14 02:21:11 +12:00
func (manager *RemoteManager) ButtonDown(code int) error {
2020-04-06 10:34:51 +12:00
return xorg.ButtonDown(code)
}
2020-06-16 04:57:28 +12:00
func (manager *RemoteManager) KeyDown(code uint64) error {
2020-04-06 10:34:51 +12:00
return xorg.KeyDown(code)
}
2020-06-14 02:21:11 +12:00
func (manager *RemoteManager) ButtonUp(code int) error {
2020-04-06 10:34:51 +12:00
return xorg.ButtonUp(code)
}
2020-06-16 04:57:28 +12:00
func (manager *RemoteManager) KeyUp(code uint64) error {
2020-04-06 10:34:51 +12:00
return xorg.KeyUp(code)
}
func (manager *RemoteManager) ReadClipboard() string {
return xorg.ReadClipboard()
}
func (manager *RemoteManager) WriteClipboard(data string) {
xorg.WriteClipboard(data)
}
func (manager *RemoteManager) ResetKeys() {
xorg.ResetKeys()
}
func (manager *RemoteManager) ScreenConfigurations() map[int]types.ScreenConfiguration {
return xorg.ScreenConfigurations
}
func (manager *RemoteManager) GetScreenSize() *types.ScreenSize {
return xorg.GetScreenSize()
}
2020-06-16 09:14:23 +12:00
2020-06-21 13:05:58 +12:00
func (manager *RemoteManager) SetKeyboardLayout(layout string) {
xorg.SetKeyboardLayout(layout)
2020-06-20 12:15:38 +12:00
}
2020-06-20 12:26:47 +12:00
func (manager *RemoteManager) SetKeyboardModifiers(NumLock int, CapsLock int, ScrollLock int) {
2020-06-20 12:15:38 +12:00
xorg.SetKeyboardModifiers(NumLock, CapsLock, ScrollLock)
}