RTPCodec as functions.

This commit is contained in:
Miroslav Šedivý 2021-02-02 16:58:17 +01:00
parent 45cf44835c
commit d880e4384f
6 changed files with 136 additions and 125 deletions

View File

@ -67,11 +67,11 @@ func CreateJPEGPipeline(pipelineDisplay string, pipelineSrc string, rate string,
} }
// CreateAppPipeline creates a GStreamer Pipeline // CreateAppPipeline creates a GStreamer Pipeline
func CreateAppPipeline(codecRTP codec.RTP, pipelineDevice string, pipelineSrc string) (*Pipeline, error) { func CreateAppPipeline(codecRTP codec.RTPCodec, pipelineDevice string, pipelineSrc string) (*Pipeline, error) {
var pipelineStr string var pipelineStr string
switch codecRTP.Name { switch codecRTP.Name {
case codec.VP8: case "vp8":
// https://gstreamer.freedesktop.org/documentation/vpx/vp8enc.html?gi-language=c // https://gstreamer.freedesktop.org/documentation/vpx/vp8enc.html?gi-language=c
// gstreamer1.0-plugins-good // gstreamer1.0-plugins-good
// vp8enc error-resilient=partitions keyframe-max-dist=10 auto-alt-ref=true cpu-used=5 deadline=1 // vp8enc error-resilient=partitions keyframe-max-dist=10 auto-alt-ref=true cpu-used=5 deadline=1
@ -80,7 +80,7 @@ func CreateAppPipeline(codecRTP codec.RTP, pipelineDevice string, pipelineSrc st
} }
pipelineStr = fmt.Sprintf(videoSrc + "vp8enc cpu-used=8 threads=2 deadline=1 error-resilient=partitions keyframe-max-dist=10 auto-alt-ref=true" + appSink, pipelineDevice) pipelineStr = fmt.Sprintf(videoSrc + "vp8enc cpu-used=8 threads=2 deadline=1 error-resilient=partitions keyframe-max-dist=10 auto-alt-ref=true" + appSink, pipelineDevice)
case codec.VP9: case "vp9":
// https://gstreamer.freedesktop.org/documentation/vpx/vp9enc.html?gi-language=c // https://gstreamer.freedesktop.org/documentation/vpx/vp9enc.html?gi-language=c
// gstreamer1.0-plugins-good // gstreamer1.0-plugins-good
// vp9enc // vp9enc
@ -89,7 +89,7 @@ func CreateAppPipeline(codecRTP codec.RTP, pipelineDevice string, pipelineSrc st
} }
pipelineStr = fmt.Sprintf(videoSrc + "vp9enc" + appSink, pipelineDevice) pipelineStr = fmt.Sprintf(videoSrc + "vp9enc" + appSink, pipelineDevice)
case codec.H264: case "h264":
if err := CheckPlugins([]string{"ximagesrc"}); err != nil { if err := CheckPlugins([]string{"ximagesrc"}); err != nil {
return nil, err return nil, err
} }
@ -112,7 +112,7 @@ func CreateAppPipeline(codecRTP codec.RTP, pipelineDevice string, pipelineSrc st
} }
return nil, err return nil, err
case codec.Opus: case "opus":
// https://gstreamer.freedesktop.org/documentation/opus/opusenc.html // https://gstreamer.freedesktop.org/documentation/opus/opusenc.html
// gstreamer1.0-plugins-base // gstreamer1.0-plugins-base
// opusenc // opusenc
@ -121,7 +121,7 @@ func CreateAppPipeline(codecRTP codec.RTP, pipelineDevice string, pipelineSrc st
} }
pipelineStr = fmt.Sprintf(audioSrc + "opusenc" + appSink, pipelineDevice) pipelineStr = fmt.Sprintf(audioSrc + "opusenc" + appSink, pipelineDevice)
case codec.G722: case "g722":
// https://gstreamer.freedesktop.org/documentation/libav/avenc_g722.html?gi-language=c // https://gstreamer.freedesktop.org/documentation/libav/avenc_g722.html?gi-language=c
// gstreamer1.0-libav // gstreamer1.0-libav
// avenc_g722 // avenc_g722
@ -130,7 +130,7 @@ func CreateAppPipeline(codecRTP codec.RTP, pipelineDevice string, pipelineSrc st
} }
pipelineStr = fmt.Sprintf(audioSrc + "avenc_g722" + appSink, pipelineDevice) pipelineStr = fmt.Sprintf(audioSrc + "avenc_g722" + appSink, pipelineDevice)
case codec.PCMU: case "pcmu":
// https://gstreamer.freedesktop.org/documentation/mulaw/mulawenc.html?gi-language=c // https://gstreamer.freedesktop.org/documentation/mulaw/mulawenc.html?gi-language=c
// gstreamer1.0-plugins-good // gstreamer1.0-plugins-good
// audio/x-raw, rate=8000 ! mulawenc // audio/x-raw, rate=8000 ! mulawenc
@ -139,7 +139,7 @@ func CreateAppPipeline(codecRTP codec.RTP, pipelineDevice string, pipelineSrc st
} }
pipelineStr = fmt.Sprintf(audioSrc + "audio/x-raw, rate=8000 ! mulawenc" + appSink, pipelineDevice) pipelineStr = fmt.Sprintf(audioSrc + "audio/x-raw, rate=8000 ! mulawenc" + appSink, pipelineDevice)
case codec.PCMA: case "pcma":
// https://gstreamer.freedesktop.org/documentation/alaw/alawenc.html?gi-language=c // https://gstreamer.freedesktop.org/documentation/alaw/alawenc.html?gi-language=c
// gstreamer1.0-plugins-good // gstreamer1.0-plugins-good
// audio/x-raw, rate=8000 ! alawenc // audio/x-raw, rate=8000 ! alawenc

View File

@ -126,11 +126,11 @@ func (manager *CaptureManagerCtx) Screencast() types.ScreencastManager {
return manager.screencast return manager.screencast
} }
func (manager *CaptureManagerCtx) VideoCodec() codec.RTP { func (manager *CaptureManagerCtx) VideoCodec() codec.RTPCodec {
return manager.config.VideoCodec return manager.config.VideoCodec
} }
func (manager *CaptureManagerCtx) AudioCodec() codec.RTP { func (manager *CaptureManagerCtx) AudioCodec() codec.RTPCodec {
return manager.config.AudioCodec return manager.config.AudioCodec
} }

View File

@ -10,9 +10,9 @@ import (
type Capture struct { type Capture struct {
Display string Display string
Device string Device string
AudioCodec codec.RTP AudioCodec codec.RTPCodec
AudioParams string AudioParams string
VideoCodec codec.RTP VideoCodec codec.RTPCodec
VideoParams string VideoParams string
BroadcastPipeline string BroadcastPipeline string
@ -111,30 +111,30 @@ func (Capture) Init(cmd *cobra.Command) error {
} }
func (s *Capture) Set() { func (s *Capture) Set() {
var videoCodec codec.RTP var videoCodec codec.RTPCodec
if viper.GetBool("vp8") { if viper.GetBool("vp8") {
videoCodec = codec.New(codec.VP8) videoCodec = codec.VP8()
} else if viper.GetBool("vp9") { } else if viper.GetBool("vp9") {
videoCodec = codec.New(codec.VP9) videoCodec = codec.VP9()
} else if viper.GetBool("h264") { } else if viper.GetBool("h264") {
videoCodec = codec.New(codec.H264) videoCodec = codec.H264()
} else { } else {
// default // default
videoCodec = codec.New(codec.VP8) videoCodec = codec.VP8()
} }
var audioCodec codec.RTP var audioCodec codec.RTPCodec
if viper.GetBool("opus") { if viper.GetBool("opus") {
audioCodec = codec.New(codec.Opus) audioCodec = codec.Opus()
} else if viper.GetBool("g722") { } else if viper.GetBool("g722") {
audioCodec = codec.New(codec.G722) audioCodec = codec.G722()
} else if viper.GetBool("pcmu") { } else if viper.GetBool("pcmu") {
audioCodec = codec.New(codec.PCMU) audioCodec = codec.PCMU()
} else if viper.GetBool("pcma") { } else if viper.GetBool("pcma") {
audioCodec = codec.New(codec.PCMA) audioCodec = codec.PCMA()
} else { } else {
// default // default
audioCodec = codec.New(codec.Opus) audioCodec = codec.Opus()
} }
s.Device = viper.GetString("device") s.Device = viper.GetString("device")

View File

@ -28,8 +28,8 @@ type CaptureManager interface {
Broadcast() BroadcastManager Broadcast() BroadcastManager
Screencast() ScreencastManager Screencast() ScreencastManager
VideoCodec() codec.RTP VideoCodec() codec.RTPCodec
AudioCodec() codec.RTP AudioCodec() codec.RTPCodec
OnVideoFrame(listener func(sample Sample)) OnVideoFrame(listener func(sample Sample))
OnAudioFrame(listener func(sample Sample)) OnAudioFrame(listener func(sample Sample))

View File

@ -2,112 +2,123 @@ package codec
import "github.com/pion/webrtc/v3" import "github.com/pion/webrtc/v3"
const ( type RTPCodec struct {
VP8 = "vp8"
VP9 = "vp9"
H264 = "h264"
Opus = "opus"
G722 = "g722"
PCMU = "pcmu"
PCMA = "pcma"
)
type RTP struct {
Name string Name string
PayloadType webrtc.PayloadType PayloadType webrtc.PayloadType
Type webrtc.RTPCodecType Type webrtc.RTPCodecType
Capability webrtc.RTPCodecCapability Capability webrtc.RTPCodecCapability
} }
func New(codecType string) RTP { func (codec *RTPCodec) Register(engine *webrtc.MediaEngine) error {
codec := RTP{}
switch codecType {
case "vp8":
codec.Name = "vp8"
codec.PayloadType = 96
codec.Type = webrtc.RTPCodecTypeVideo
codec.Capability = webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeVP8,
ClockRate: 90000,
Channels: 0,
SDPFmtpLine: "",
RTCPFeedback: nil,
}
case "vp9":
codec.Name = "vp9"
codec.PayloadType = 98
codec.Type = webrtc.RTPCodecTypeVideo
codec.Capability = webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeVP9,
ClockRate: 90000,
Channels: 0,
SDPFmtpLine: "profile-id=0",
RTCPFeedback: nil,
}
case "h264":
codec.Name = "h264"
codec.PayloadType = 102
codec.Type = webrtc.RTPCodecTypeVideo
codec.Capability = webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeH264,
ClockRate: 90000,
Channels: 0,
SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f",
RTCPFeedback: nil,
}
case "opus":
codec.Name = "opus"
codec.PayloadType = 111
codec.Type = webrtc.RTPCodecTypeAudio
codec.Capability = webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeOpus,
ClockRate: 48000,
Channels: 2,
SDPFmtpLine: "",
RTCPFeedback: nil,
}
case "g722":
codec.Name = "g722"
codec.PayloadType = 9
codec.Type = webrtc.RTPCodecTypeAudio
codec.Capability = webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeG722,
ClockRate: 8000,
Channels: 0,
SDPFmtpLine: "",
RTCPFeedback: nil,
}
case "pcmu":
codec.Name = "pcmu"
codec.PayloadType = 0
codec.Type = webrtc.RTPCodecTypeAudio
codec.Capability = webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypePCMU,
ClockRate: 8000,
Channels: 0,
SDPFmtpLine: "",
RTCPFeedback: nil,
}
case "pcma":
codec.Name = "pcma"
codec.PayloadType = 8
codec.Type = webrtc.RTPCodecTypeAudio
codec.Capability = webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypePCMA,
ClockRate: 8000,
Channels: 0,
SDPFmtpLine: "",
RTCPFeedback: nil,
}
}
return codec
}
func (codec *RTP) Register(engine *webrtc.MediaEngine) error {
return engine.RegisterCodec(webrtc.RTPCodecParameters{ return engine.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: codec.Capability, RTPCodecCapability: codec.Capability,
PayloadType: codec.PayloadType, PayloadType: codec.PayloadType,
}, codec.Type) }, codec.Type)
} }
func VP8() RTPCodec {
return RTPCodec{
Name: "vp8",
PayloadType: 96,
Type: webrtc.RTPCodecTypeVideo,
Capability: webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeVP8,
ClockRate: 90000,
Channels: 0,
SDPFmtpLine: "",
RTCPFeedback: nil,
},
}
}
// TODO: Profile ID.
func VP9() RTPCodec {
return RTPCodec{
Name: "vp9",
PayloadType: 98,
Type: webrtc.RTPCodecTypeVideo,
Capability: webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeVP9,
ClockRate: 90000,
Channels: 0,
SDPFmtpLine: "profile-id=0",
RTCPFeedback: nil,
},
}
}
// TODO: Profile ID.
func H264() RTPCodec {
return RTPCodec{
Name: "h264",
PayloadType: 102,
Type: webrtc.RTPCodecTypeVideo,
Capability: webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeH264,
ClockRate: 90000,
Channels: 0,
SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f",
RTCPFeedback: nil,
},
}
}
func Opus() RTPCodec {
return RTPCodec{
Name: "opus",
PayloadType: 111,
Type: webrtc.RTPCodecTypeAudio,
Capability: webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeOpus,
ClockRate: 48000,
Channels: 2,
SDPFmtpLine: "",
RTCPFeedback: nil,
},
}
}
func G722() RTPCodec {
return RTPCodec{
Name: "g722",
PayloadType: 9,
Type: webrtc.RTPCodecTypeAudio,
Capability: webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeG722,
ClockRate: 8000,
Channels: 0,
SDPFmtpLine: "",
RTCPFeedback: nil,
},
}
}
func PCMU() RTPCodec {
return RTPCodec{
Name: "pcmu",
PayloadType: 0,
Type: webrtc.RTPCodecTypeAudio,
Capability: webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypePCMU,
ClockRate: 8000,
Channels: 0,
SDPFmtpLine: "",
RTCPFeedback: nil,
},
}
}
func PCMA() RTPCodec {
return RTPCodec{
Name: "pcma",
PayloadType: 8,
Type: webrtc.RTPCodecTypeAudio,
Capability: webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypePCMA,
ClockRate: 8000,
Channels: 0,
SDPFmtpLine: "",
RTCPFeedback: nil,
},
}
}

View File

@ -28,8 +28,8 @@ type WebRTCManagerCtx struct {
logger zerolog.Logger logger zerolog.Logger
videoTrack *webrtc.TrackLocalStaticSample videoTrack *webrtc.TrackLocalStaticSample
audioTrack *webrtc.TrackLocalStaticSample audioTrack *webrtc.TrackLocalStaticSample
videoCodec codec.RTP videoCodec codec.RTPCodec
audioCodec codec.RTP audioCodec codec.RTPCodec
desktop types.DesktopManager desktop types.DesktopManager
capture types.CaptureManager capture types.CaptureManager
config *config.WebRTC config *config.WebRTC