implement codec parse.

This commit is contained in:
Miroslav Šedivý 2021-12-01 22:34:36 +01:00
parent 0a94191a5f
commit a6eca45e7f
2 changed files with 43 additions and 20 deletions

View File

@ -3,6 +3,7 @@ package config
import ( import (
"os" "os"
"github.com/pion/webrtc/v3"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -113,19 +114,15 @@ func (Capture) Init(cmd *cobra.Command) error {
} }
func (s *Capture) Set() { func (s *Capture) Set() {
var ok bool
// Display is provided by env variable // Display is provided by env variable
s.Display = os.Getenv("DISPLAY") s.Display = os.Getenv("DISPLAY")
// video // video
videoCodec := viper.GetString("capture.video.codec") videoCodec := viper.GetString("capture.video.codec")
switch videoCodec { s.VideoCodec, ok = codec.ParseStr(videoCodec)
case "vp8": if !ok || s.VideoCodec.Type != webrtc.RTPCodecTypeVideo {
s.VideoCodec = codec.VP8()
case "vp9":
s.VideoCodec = codec.VP9()
case "h264":
s.VideoCodec = codec.H264()
default:
log.Warn().Str("codec", videoCodec).Msgf("unknown video codec, using Vp8") log.Warn().Str("codec", videoCodec).Msgf("unknown video codec, using Vp8")
s.VideoCodec = codec.VP8() s.VideoCodec = codec.VP8()
} }
@ -143,7 +140,7 @@ func (s *Capture) Set() {
s.VideoCodec = codec.VP8() s.VideoCodec = codec.VP8()
s.VideoPipelines = map[string]types.VideoConfig{ s.VideoPipelines = map[string]types.VideoConfig{
"main": types.VideoConfig{ "main": {
GstPipeline: "ximagesrc display-name={display} show-pointer=false use-damage=false " + GstPipeline: "ximagesrc display-name={display} show-pointer=false use-damage=false " +
"! video/x-raw " + "! video/x-raw " +
"! videoconvert " + "! videoconvert " +
@ -160,16 +157,8 @@ func (s *Capture) Set() {
s.AudioPipeline = viper.GetString("capture.audio.pipeline") s.AudioPipeline = viper.GetString("capture.audio.pipeline")
audioCodec := viper.GetString("capture.audio.codec") audioCodec := viper.GetString("capture.audio.codec")
switch audioCodec { s.AudioCodec, ok = codec.ParseStr(audioCodec)
case "opus": if !ok || s.AudioCodec.Type != webrtc.RTPCodecTypeAudio {
s.AudioCodec = codec.Opus()
case "g722":
s.AudioCodec = codec.G722()
case "pcmu":
s.AudioCodec = codec.PCMU()
case "pcma":
s.AudioCodec = codec.PCMA()
default:
log.Warn().Str("codec", audioCodec).Msgf("unknown audio codec, using Opus") log.Warn().Str("codec", audioCodec).Msgf("unknown audio codec, using Opus")
s.AudioCodec = codec.Opus() s.AudioCodec = codec.Opus()
} }

View File

@ -1,6 +1,40 @@
package codec 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 { type RTPCodec struct {
Name string Name string