neko/server/internal/config/capture.go

251 lines
7.6 KiB
Go
Raw Normal View History

2020-04-06 10:34:51 +12:00
package config
import (
2022-09-17 22:43:17 +12:00
"m1k1o/neko/internal/types/codec"
2023-04-01 09:17:46 +13:00
"strings"
2022-09-17 22:43:17 +12:00
2022-09-18 00:07:35 +12:00
"github.com/pion/webrtc/v3"
"github.com/rs/zerolog/log"
2020-04-06 10:34:51 +12:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
2023-04-01 09:17:46 +13:00
type HwEnc int
const (
HwEncNone HwEnc = iota
HwEncVAAPI
HwEncNVENC
)
2022-09-13 08:12:47 +12:00
type Capture struct {
2022-09-17 22:43:17 +12:00
// video
Display string
VideoCodec codec.RTPCodec
2023-04-01 09:17:46 +13:00
VideoHWEnc HwEnc // TODO: Pipeline builder.
VideoBitrate uint // TODO: Pipeline builder.
VideoMaxFPS int16 // TODO: Pipeline builder.
VideoPipeline string
2022-09-17 22:43:17 +12:00
// audio
AudioDevice string
AudioCodec codec.RTPCodec
AudioBitrate uint // TODO: Pipeline builder.
AudioPipeline string
// broadcast
BroadcastPipeline string
BroadcastUrl string
2020-04-06 10:34:51 +12:00
}
2022-09-13 08:12:47 +12:00
func (Capture) Init(cmd *cobra.Command) error {
2022-09-17 22:43:17 +12:00
//
// video
//
2020-04-06 10:34:51 +12:00
cmd.PersistentFlags().String("display", ":99.0", "XDisplay to capture")
if err := viper.BindPFlag("display", cmd.PersistentFlags().Lookup("display")); err != nil {
return err
}
2022-09-18 00:07:35 +12:00
cmd.PersistentFlags().String("video_codec", "vp8", "video codec to be used")
if err := viper.BindPFlag("video_codec", cmd.PersistentFlags().Lookup("video_codec")); err != nil {
return err
}
// DEPRECATED: video codec
cmd.PersistentFlags().Bool("vp8", false, "DEPRECATED: use video_codec")
2022-09-17 22:43:17 +12:00
if err := viper.BindPFlag("vp8", cmd.PersistentFlags().Lookup("vp8")); err != nil {
2020-04-06 10:34:51 +12:00
return err
}
2022-09-18 00:07:35 +12:00
// DEPRECATED: video codec
cmd.PersistentFlags().Bool("vp9", false, "DEPRECATED: use video_codec")
2022-09-17 22:43:17 +12:00
if err := viper.BindPFlag("vp9", cmd.PersistentFlags().Lookup("vp9")); err != nil {
2020-04-06 10:34:51 +12:00
return err
}
2023-01-22 11:43:04 +13:00
// DEPRECATED: video codec
cmd.PersistentFlags().Bool("av1", false, "DEPRECATED: use video_codec")
if err := viper.BindPFlag("av1", cmd.PersistentFlags().Lookup("av1")); err != nil {
2023-01-29 10:08:36 +13:00
return err
2023-01-22 11:43:04 +13:00
}
2022-09-18 00:07:35 +12:00
// DEPRECATED: video codec
cmd.PersistentFlags().Bool("h264", false, "DEPRECATED: use video_codec")
2022-09-17 22:43:17 +12:00
if err := viper.BindPFlag("h264", cmd.PersistentFlags().Lookup("h264")); err != nil {
2021-02-15 09:07:56 +13:00
return err
}
2022-09-17 22:43:17 +12:00
cmd.PersistentFlags().String("hwenc", "", "use hardware accelerated encoding")
if err := viper.BindPFlag("hwenc", cmd.PersistentFlags().Lookup("hwenc")); err != nil {
2020-04-06 10:34:51 +12:00
return err
}
2021-02-15 09:07:56 +13:00
cmd.PersistentFlags().Int("video_bitrate", 3072, "video bitrate in kbit/s")
if err := viper.BindPFlag("video_bitrate", cmd.PersistentFlags().Lookup("video_bitrate")); err != nil {
2020-04-06 10:34:51 +12:00
return err
}
cmd.PersistentFlags().Int("max_fps", 25, "maximum fps delivered via WebRTC, 0 is for no maximum")
if err := viper.BindPFlag("max_fps", cmd.PersistentFlags().Lookup("max_fps")); err != nil {
return err
}
2022-09-17 22:43:17 +12:00
cmd.PersistentFlags().String("video", "", "video codec parameters to use for streaming")
if err := viper.BindPFlag("video", cmd.PersistentFlags().Lookup("video")); err != nil {
2020-04-06 10:34:51 +12:00
return err
}
2022-09-17 22:43:17 +12:00
//
// audio
//
2020-04-06 10:34:51 +12:00
2023-03-27 05:59:10 +13:00
cmd.PersistentFlags().String("device", "audio_output.monitor", "audio device to capture")
2022-09-17 22:43:17 +12:00
if err := viper.BindPFlag("device", cmd.PersistentFlags().Lookup("device")); err != nil {
2020-04-06 10:34:51 +12:00
return err
}
2022-09-18 00:07:35 +12:00
cmd.PersistentFlags().String("audio_codec", "opus", "audio codec to be used")
if err := viper.BindPFlag("audio_codec", cmd.PersistentFlags().Lookup("audio_codec")); err != nil {
return err
}
// DEPRECATED: audio codec
cmd.PersistentFlags().Bool("opus", false, "DEPRECATED: use audio_codec")
2020-04-06 10:34:51 +12:00
if err := viper.BindPFlag("opus", cmd.PersistentFlags().Lookup("opus")); err != nil {
return err
}
2022-09-18 00:07:35 +12:00
// DEPRECATED: audio codec
cmd.PersistentFlags().Bool("g722", false, "DEPRECATED: use audio_codec")
2020-04-06 10:34:51 +12:00
if err := viper.BindPFlag("g722", cmd.PersistentFlags().Lookup("g722")); err != nil {
return err
}
2022-09-18 00:07:35 +12:00
// DEPRECATED: audio codec
cmd.PersistentFlags().Bool("pcmu", false, "DEPRECATED: use audio_codec")
2020-04-06 10:34:51 +12:00
if err := viper.BindPFlag("pcmu", cmd.PersistentFlags().Lookup("pcmu")); err != nil {
return err
}
2022-09-18 00:07:35 +12:00
// DEPRECATED: audio codec
cmd.PersistentFlags().Bool("pcma", false, "DEPRECATED: use audio_codec")
2020-04-06 10:34:51 +12:00
if err := viper.BindPFlag("pcma", cmd.PersistentFlags().Lookup("pcma")); err != nil {
return err
}
2022-09-17 22:43:17 +12:00
// audio codecs
cmd.PersistentFlags().Int("audio_bitrate", 128, "audio bitrate in kbit/s")
if err := viper.BindPFlag("audio_bitrate", cmd.PersistentFlags().Lookup("audio_bitrate")); err != nil {
return err
}
cmd.PersistentFlags().String("audio", "", "audio codec parameters to use for streaming")
if err := viper.BindPFlag("audio", cmd.PersistentFlags().Lookup("audio")); err != nil {
return err
}
//
// broadcast
//
cmd.PersistentFlags().String("broadcast_pipeline", "", "custom gst pipeline used for broadcasting, strings {url} {device} {display} will be replaced")
if err := viper.BindPFlag("broadcast_pipeline", cmd.PersistentFlags().Lookup("broadcast_pipeline")); err != nil {
return err
}
cmd.PersistentFlags().String("broadcast_url", "", "URL for broadcasting, setting this value will automatically enable broadcasting")
if err := viper.BindPFlag("broadcast_url", cmd.PersistentFlags().Lookup("broadcast_url")); err != nil {
return err
}
2020-04-06 10:34:51 +12:00
return nil
}
2022-09-13 08:12:47 +12:00
func (s *Capture) Set() {
2022-09-18 00:07:35 +12:00
var ok bool
2022-09-17 22:43:17 +12:00
//
// video
//
2020-04-06 10:34:51 +12:00
2022-09-17 22:43:17 +12:00
s.Display = viper.GetString("display")
2022-09-18 00:07:35 +12:00
videoCodec := viper.GetString("video_codec")
s.VideoCodec, ok = codec.ParseStr(videoCodec)
if !ok || s.VideoCodec.Type != webrtc.RTPCodecTypeVideo {
log.Warn().Str("codec", videoCodec).Msgf("unknown video codec, using Vp8")
s.VideoCodec = codec.VP8()
}
if viper.GetBool("vp8") {
2022-09-18 00:07:35 +12:00
s.VideoCodec = codec.VP8()
log.Warn().Msg("you are using deprecated config setting 'NEKO_VP8=true', use 'NEKO_VIDEO_CODEC=vp8' instead")
} else if viper.GetBool("vp9") {
2022-09-18 00:07:35 +12:00
s.VideoCodec = codec.VP9()
log.Warn().Msg("you are using deprecated config setting 'NEKO_VP9=true', use 'NEKO_VIDEO_CODEC=vp9' instead")
} else if viper.GetBool("h264") {
2022-09-18 00:07:35 +12:00
s.VideoCodec = codec.H264()
log.Warn().Msg("you are using deprecated config setting 'NEKO_H264=true', use 'NEKO_VIDEO_CODEC=h264' instead")
2023-01-22 11:43:04 +13:00
} else if viper.GetBool("av1") {
s.VideoCodec = codec.AV1()
log.Warn().Msg("you are using deprecated config setting 'NEKO_AV1=true', use 'NEKO_VIDEO_CODEC=av1' instead")
}
2022-09-17 22:43:17 +12:00
2023-04-01 09:17:46 +13:00
videoHWEnc := strings.ToLower(viper.GetString("hwenc"))
switch videoHWEnc {
case "":
fallthrough
case "none":
s.VideoHWEnc = HwEncNone
case "vaapi":
s.VideoHWEnc = HwEncVAAPI
case "nvenc":
s.VideoHWEnc = HwEncNVENC
default:
log.Warn().Str("hwenc", videoHWEnc).Msgf("unknown video hw encoder, using CPU")
}
2022-09-17 22:43:17 +12:00
2021-02-15 09:07:56 +13:00
s.VideoBitrate = viper.GetUint("video_bitrate")
2022-09-17 22:43:17 +12:00
s.VideoMaxFPS = int16(viper.GetInt("max_fps"))
s.VideoPipeline = viper.GetString("video")
//
// audio
//
s.AudioDevice = viper.GetString("device")
2022-09-18 00:07:35 +12:00
audioCodec := viper.GetString("audio_codec")
s.AudioCodec, ok = codec.ParseStr(audioCodec)
if !ok || s.AudioCodec.Type != webrtc.RTPCodecTypeAudio {
log.Warn().Str("codec", audioCodec).Msgf("unknown audio codec, using Opus")
s.AudioCodec = codec.Opus()
}
2022-09-17 22:43:17 +12:00
if viper.GetBool("opus") {
2022-09-18 00:07:35 +12:00
s.AudioCodec = codec.Opus()
log.Warn().Msg("you are using deprecated config setting 'NEKO_OPUS=true', use 'NEKO_VIDEO_CODEC=opus' instead")
2022-09-17 22:43:17 +12:00
} else if viper.GetBool("g722") {
2022-09-18 00:07:35 +12:00
s.AudioCodec = codec.G722()
log.Warn().Msg("you are using deprecated config setting 'NEKO_G722=true', use 'NEKO_VIDEO_CODEC=g722' instead")
2022-09-17 22:43:17 +12:00
} else if viper.GetBool("pcmu") {
2022-09-18 00:07:35 +12:00
s.AudioCodec = codec.PCMU()
log.Warn().Msg("you are using deprecated config setting 'NEKO_PCMU=true', use 'NEKO_VIDEO_CODEC=pcmu' instead")
2022-09-17 22:43:17 +12:00
} else if viper.GetBool("pcma") {
2022-09-18 00:07:35 +12:00
s.AudioCodec = codec.PCMA()
log.Warn().Msg("you are using deprecated config setting 'NEKO_PCMA=true', use 'NEKO_VIDEO_CODEC=pcma' instead")
2022-09-17 22:43:17 +12:00
}
s.AudioBitrate = viper.GetUint("audio_bitrate")
s.AudioPipeline = viper.GetString("audio")
//
// broadcast
//
2020-04-06 10:34:51 +12:00
2022-09-17 22:43:17 +12:00
s.BroadcastPipeline = viper.GetString("broadcast_pipeline")
s.BroadcastUrl = viper.GetString("broadcast_url")
2020-04-06 10:34:51 +12:00
}