gst pipeline app sink refactoring.

This commit is contained in:
Miroslav Šedivý 2021-01-12 16:24:54 +01:00
parent 3faca29a89
commit dbc8a7612d

View File

@ -35,6 +35,7 @@ const (
pcmClockRate = 8000 pcmClockRate = 8000
videoSrc = "ximagesrc display-name=%s show-pointer=true use-damage=false ! video/x-raw ! videoconvert ! queue ! " videoSrc = "ximagesrc display-name=%s show-pointer=true use-damage=false ! video/x-raw ! videoconvert ! queue ! "
audioSrc = "pulsesrc device=%s ! audio/x-raw,channels=2 ! audioconvert ! " audioSrc = "pulsesrc device=%s ! audio/x-raw,channels=2 ! audioconvert ! "
appSink = " ! appsink name=appsink"
) )
func init() { func init() {
@ -59,9 +60,8 @@ func CreateRTMPPipeline(pipelineDevice string, pipelineDisplay string, pipelineS
// CreateAppPipeline creates a GStreamer Pipeline // CreateAppPipeline creates a GStreamer Pipeline
func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc string) (*Pipeline, error) { func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc string) (*Pipeline, error) {
pipelineStr := " ! appsink name=appsink"
var clockRate float32 var clockRate float32
var pipelineStr string
switch codecName { switch codecName {
case webrtc.VP8: case webrtc.VP8:
@ -73,12 +73,7 @@ func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc stri
} }
clockRate = videoClockRate clockRate = videoClockRate
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)
if pipelineSrc != "" {
pipelineStr = fmt.Sprintf(pipelineSrc+pipelineStr, pipelineDevice)
} else {
pipelineStr = fmt.Sprintf(videoSrc+"vp8enc cpu-used=8 threads=2 deadline=1 error-resilient=partitions keyframe-max-dist=10 auto-alt-ref=true"+pipelineStr, pipelineDevice)
}
case webrtc.VP9: case webrtc.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
@ -88,39 +83,32 @@ func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc stri
} }
clockRate = videoClockRate clockRate = videoClockRate
pipelineStr = fmt.Sprintf(videoSrc + "vp9enc" + appSink, pipelineDevice)
// Causes panic! not sure why...
if pipelineSrc != "" {
pipelineStr = fmt.Sprintf(pipelineSrc+pipelineStr, pipelineDevice)
} else {
pipelineStr = fmt.Sprintf(videoSrc+"vp9enc"+pipelineStr, pipelineDevice)
}
case webrtc.H264: case webrtc.H264:
// https://gstreamer.freedesktop.org/documentation/openh264/openh264enc.html?gi-language=c#openh264enc
// gstreamer1.0-plugins-bad
// openh264enc multi-thread=4 complexity=high bitrate=3072000 max-bitrate=4096000
if err := CheckPlugins([]string{"ximagesrc"}); err != nil { if err := CheckPlugins([]string{"ximagesrc"}); err != nil {
return nil, err return nil, err
} }
var err error
// https://gstreamer.freedesktop.org/documentation/openh264/openh264enc.html?gi-language=c#openh264enc
// gstreamer1.0-plugins-bad
// openh264enc multi-thread=4 complexity=high bitrate=3072000 max-bitrate=4096000
if err = CheckPlugins([]string{"openh264"}); err == nil {
clockRate = videoClockRate clockRate = videoClockRate
pipelineStr = fmt.Sprintf(videoSrc + "openh264enc multi-thread=4 complexity=high bitrate=3072000 max-bitrate=4096000 ! video/x-h264,stream-format=byte-stream" + appSink, pipelineDevice)
if pipelineSrc != "" { break
pipelineStr = fmt.Sprintf(pipelineSrc+pipelineStr, pipelineDevice) }
} else {
pipelineStr = fmt.Sprintf(videoSrc+"openh264enc multi-thread=4 complexity=high bitrate=3072000 max-bitrate=4096000 ! video/x-h264,stream-format=byte-stream"+pipelineStr, pipelineDevice)
// https://gstreamer.freedesktop.org/documentation/x264/index.html?gi-language=c // https://gstreamer.freedesktop.org/documentation/x264/index.html?gi-language=c
// gstreamer1.0-plugins-ugly // gstreamer1.0-plugins-ugly
// video/x-raw,format=I420 ! x264enc bframes=0 key-int-max=60 byte-stream=true tune=zerolatency speed-preset=veryfast ! video/x-h264,stream-format=byte-stream // video/x-raw,format=I420 ! x264enc bframes=0 key-int-max=60 byte-stream=true tune=zerolatency speed-preset=veryfast ! video/x-h264,stream-format=byte-stream
if err := CheckPlugins([]string{"openh264"}); err != nil { if err = CheckPlugins([]string{"x264"}); err == nil {
pipelineStr = fmt.Sprintf(videoSrc+"video/x-raw,format=I420 ! x264enc bframes=0 key-int-max=60 byte-stream=true tune=zerolatency speed-preset=veryfast ! video/x-h264,stream-format=byte-stream"+pipelineStr, pipelineDevice) clockRate = videoClockRate
pipelineStr = fmt.Sprintf(videoSrc + "video/x-raw,format=I420 ! x264enc bframes=0 key-int-max=60 byte-stream=true tune=zerolatency speed-preset=veryfast ! video/x-h264,stream-format=byte-stream" + appSink, pipelineDevice)
break
}
if err := CheckPlugins([]string{"x264"}); err != nil {
return nil, err return nil, err
}
}
}
case webrtc.Opus: case webrtc.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
@ -130,12 +118,7 @@ func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc stri
} }
clockRate = audioClockRate clockRate = audioClockRate
pipelineStr = fmt.Sprintf(audioSrc + "opusenc" + appSink, pipelineDevice)
if pipelineSrc != "" {
pipelineStr = fmt.Sprintf(pipelineSrc+pipelineStr, pipelineDevice)
} else {
pipelineStr = fmt.Sprintf(audioSrc+"opusenc"+pipelineStr, pipelineDevice)
}
case webrtc.G722: case webrtc.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
@ -145,12 +128,7 @@ func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc stri
} }
clockRate = audioClockRate clockRate = audioClockRate
pipelineStr = fmt.Sprintf(audioSrc + "avenc_g722" + appSink, pipelineDevice)
if pipelineSrc != "" {
pipelineStr = fmt.Sprintf(pipelineSrc+pipelineStr, pipelineDevice)
} else {
pipelineStr = fmt.Sprintf(audioSrc+"avenc_g722"+pipelineStr, pipelineDevice)
}
case webrtc.PCMU: case webrtc.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
@ -160,12 +138,7 @@ func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc stri
} }
clockRate = pcmClockRate clockRate = pcmClockRate
pipelineStr = fmt.Sprintf(audioSrc + "audio/x-raw, rate=8000 ! mulawenc" + appSink, pipelineDevice)
if pipelineSrc != "" {
pipelineStr = fmt.Sprintf(pipelineSrc+pipelineStr, pipelineDevice)
} else {
pipelineStr = fmt.Sprintf(audioSrc+"audio/x-raw, rate=8000 ! mulawenc"+pipelineStr, pipelineDevice)
}
case webrtc.PCMA: case webrtc.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
@ -175,16 +148,15 @@ func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc stri
} }
clockRate = pcmClockRate clockRate = pcmClockRate
pipelineStr = fmt.Sprintf(audioSrc + "audio/x-raw, rate=8000 ! alawenc" + appSink, pipelineDevice)
if pipelineSrc != "" {
pipelineStr = fmt.Sprintf(pipelineSrc+pipelineStr, pipelineDevice)
} else {
pipelineStr = fmt.Sprintf(audioSrc+"audio/x-raw, rate=8000 ! alawenc"+pipelineStr, pipelineDevice)
}
default: default:
return nil, fmt.Errorf("unknown codec %s", codecName) return nil, fmt.Errorf("unknown codec %s", codecName)
} }
if pipelineSrc != "" {
pipelineStr = fmt.Sprintf(pipelineSrc + appSink, pipelineDevice)
}
return CreatePipeline(pipelineStr, clockRate) return CreatePipeline(pipelineStr, clockRate)
} }
@ -202,7 +174,7 @@ func CreatePipeline(pipelineStr string, clockRate float32) (*Pipeline, error) {
gstPipeline = C.gst_parse_launch(pipelineStrUnsafe, &gstError) gstPipeline = C.gst_parse_launch(pipelineStrUnsafe, &gstError)
defer C.g_error_free(gstError) defer C.g_error_free(gstError)
if gstPipeline == nil || gstError != nil { if gstError != nil {
return nil, fmt.Errorf("(pipeline error) %s", C.GoString(gstError.message)) return nil, fmt.Errorf("(pipeline error) %s", C.GoString(gstError.message))
} }