diff --git a/internal/config/capture.go b/internal/config/capture.go index 5b47635b..665355b1 100644 --- a/internal/config/capture.go +++ b/internal/config/capture.go @@ -3,6 +3,7 @@ package config import ( "os" + "github.com/pion/webrtc/v3" "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -113,19 +114,15 @@ func (Capture) Init(cmd *cobra.Command) error { } func (s *Capture) Set() { + var ok bool + // Display is provided by env variable s.Display = os.Getenv("DISPLAY") // video videoCodec := viper.GetString("capture.video.codec") - switch videoCodec { - case "vp8": - s.VideoCodec = codec.VP8() - case "vp9": - s.VideoCodec = codec.VP9() - case "h264": - s.VideoCodec = codec.H264() - default: + 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() } @@ -143,7 +140,7 @@ func (s *Capture) Set() { s.VideoCodec = codec.VP8() s.VideoPipelines = map[string]types.VideoConfig{ - "main": types.VideoConfig{ + "main": { GstPipeline: "ximagesrc display-name={display} show-pointer=false use-damage=false " + "! video/x-raw " + "! videoconvert " + @@ -160,16 +157,8 @@ func (s *Capture) Set() { s.AudioPipeline = viper.GetString("capture.audio.pipeline") audioCodec := viper.GetString("capture.audio.codec") - switch audioCodec { - case "opus": - s.AudioCodec = codec.Opus() - case "g722": - s.AudioCodec = codec.G722() - case "pcmu": - s.AudioCodec = codec.PCMU() - case "pcma": - s.AudioCodec = codec.PCMA() - default: + 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() } diff --git a/internal/types/codec/codecs.go b/internal/types/codec/codecs.go index f8c08c88..00071c04 100644 --- a/internal/types/codec/codecs.go +++ b/internal/types/codec/codecs.go @@ -1,6 +1,40 @@ package codec -import "github.com/pion/webrtc/v3" +import ( + "strings" + + "github.com/pion/webrtc/v3" +) + +func ParseRTC(codec webrtc.RTPCodecParameters) (RTPCodec, bool) { + codecName := strings.Split(codec.RTPCodecCapability.MimeType, "/")[1] + return ParseStr(codecName) +} + +func ParseStr(codecName string) (codec RTPCodec, ok bool) { + ok = true + + switch strings.ToLower(codecName) { + case VP8().Name: + codec = VP8() + case VP9().Name: + codec = VP9() + case H264().Name: + codec = H264() + case Opus().Name: + codec = Opus() + case G722().Name: + codec = G722() + case PCMU().Name: + codec = PCMU() + case PCMA().Name: + codec = PCMA() + default: + ok = false + } + + return +} type RTPCodec struct { Name string