2020-10-23 03:54:50 +13:00
|
|
|
package gst
|
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo pkg-config: gstreamer-1.0 gstreamer-app-1.0
|
|
|
|
|
|
|
|
#include "gst.h"
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-02-02 11:50:18 +13:00
|
|
|
"time"
|
2020-10-23 03:54:50 +13:00
|
|
|
"sync"
|
|
|
|
"unsafe"
|
|
|
|
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko/internal/types"
|
2021-02-02 11:50:18 +13:00
|
|
|
"demodesk/neko/internal/types/codec"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
// Pipeline is a wrapper for a GStreamer Pipeline
|
|
|
|
type Pipeline struct {
|
|
|
|
Pipeline *C.GstElement
|
|
|
|
Sample chan types.Sample
|
|
|
|
Src string
|
|
|
|
id int
|
|
|
|
}
|
|
|
|
|
|
|
|
var pipelines = make(map[int]*Pipeline)
|
|
|
|
var pipelinesLock sync.Mutex
|
|
|
|
var registry *C.GstRegistry
|
|
|
|
|
|
|
|
const (
|
2021-02-03 06:21:07 +13:00
|
|
|
videoSrc = "ximagesrc display-name=%s show-pointer=false use-damage=false ! video/x-raw ! videoconvert ! queue ! "
|
|
|
|
audioSrc = "pulsesrc device=%s ! audio/x-raw,channels=2 ! audioconvert ! "
|
|
|
|
appSink = " ! appsink name=appsink"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
C.gstreamer_init()
|
|
|
|
registry = C.gst_registry_get()
|
|
|
|
}
|
|
|
|
|
2021-02-06 01:58:02 +13:00
|
|
|
func GetRTMPPipeline(audioDevice string, videoDisplay string, pipelineSrc string) string {
|
|
|
|
video := fmt.Sprintf(videoSrc, videoDisplay)
|
|
|
|
audio := fmt.Sprintf(audioSrc, audioDevice)
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
var pipelineStr string
|
|
|
|
if pipelineSrc != "" {
|
2021-02-06 01:58:02 +13:00
|
|
|
pipelineStr = fmt.Sprintf(pipelineSrc, audioDevice, videoDisplay)
|
2020-10-23 03:54:50 +13:00
|
|
|
} else {
|
2021-02-06 01:58:02 +13:00
|
|
|
pipelineStr = fmt.Sprintf("flvmux name=mux ! rtmpsink location='{url} live=1' %s audio/x-raw,channels=2 ! audioconvert ! voaacenc ! mux. %s x264enc bframes=0 key-int-max=60 byte-stream=true tune=zerolatency speed-preset=veryfast ! mux.", audio, video)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-02-06 01:58:02 +13:00
|
|
|
return pipelineStr
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-02-06 01:58:02 +13:00
|
|
|
func GetJPEGPipeline(videoDisplay string, pipelineSrc string, rate string, quality string) string {
|
2021-01-23 06:13:32 +13:00
|
|
|
var pipelineStr string
|
|
|
|
if pipelineSrc != "" {
|
2021-02-06 01:58:02 +13:00
|
|
|
pipelineStr = fmt.Sprintf(pipelineSrc, videoDisplay)
|
2021-01-23 06:13:32 +13:00
|
|
|
} else {
|
2021-02-06 01:58:02 +13:00
|
|
|
pipelineStr = fmt.Sprintf("ximagesrc display-name=%s show-pointer=true use-damage=false ! videoconvert ! videoscale ! videorate ! video/x-raw,framerate=%s ! jpegenc quality=%s" + appSink, videoDisplay, rate, quality)
|
2021-01-23 06:13:32 +13:00
|
|
|
}
|
|
|
|
|
2021-02-06 01:58:02 +13:00
|
|
|
return pipelineStr
|
2021-01-23 06:13:32 +13:00
|
|
|
}
|
|
|
|
|
2021-02-06 01:58:02 +13:00
|
|
|
func GetAppPipeline(codecRTP codec.RTPCodec, pipelineDevice string, pipelineSrc string) (string, error) {
|
|
|
|
if pipelineSrc != "" {
|
|
|
|
return fmt.Sprintf(pipelineSrc + appSink, pipelineDevice), nil
|
|
|
|
}
|
|
|
|
|
2021-01-13 04:24:54 +13:00
|
|
|
var pipelineStr string
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-02-02 11:50:18 +13:00
|
|
|
switch codecRTP.Name {
|
2021-02-03 04:58:17 +13:00
|
|
|
case "vp8":
|
2021-02-04 09:04:48 +13:00
|
|
|
// https://gstreamer.freedesktop.org/documentation/vpx/vp8enc.html
|
2020-10-23 03:54:50 +13:00
|
|
|
// gstreamer1.0-plugins-good
|
|
|
|
if err := CheckPlugins([]string{"ximagesrc", "vpx"}); err != nil {
|
2021-02-06 01:58:02 +13:00
|
|
|
return "", err
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-02-04 09:04:48 +13:00
|
|
|
pipelineStr = fmt.Sprintf(videoSrc + "vp8enc cpu-used=16 threads=4 deadline=1 error-resilient=partitions keyframe-max-dist=15 static-threshold=20" + appSink, pipelineDevice)
|
2021-02-03 04:58:17 +13:00
|
|
|
case "vp9":
|
2021-02-04 09:04:48 +13:00
|
|
|
// https://gstreamer.freedesktop.org/documentation/vpx/vp9enc.html
|
2020-10-23 03:54:50 +13:00
|
|
|
// gstreamer1.0-plugins-good
|
|
|
|
if err := CheckPlugins([]string{"ximagesrc", "vpx"}); err != nil {
|
2021-02-06 01:58:02 +13:00
|
|
|
return "", err
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-02-05 01:52:32 +13:00
|
|
|
pipelineStr = fmt.Sprintf(videoSrc + "vp9enc cpu-used=16 threads=4 deadline=1 keyframe-max-dist=15 static-threshold=20" + appSink, pipelineDevice)
|
2021-02-03 04:58:17 +13:00
|
|
|
case "h264":
|
2021-02-04 09:04:48 +13:00
|
|
|
var err error
|
|
|
|
if err = CheckPlugins([]string{"ximagesrc"}); err != nil {
|
2021-02-06 01:58:02 +13:00
|
|
|
return "", err
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-02-04 09:04:48 +13:00
|
|
|
// https://gstreamer.freedesktop.org/documentation/x264/index.html
|
|
|
|
// gstreamer1.0-plugins-ugly
|
|
|
|
if err = CheckPlugins([]string{"x264"}); err == nil {
|
|
|
|
pipelineStr = fmt.Sprintf(videoSrc + "video/x-raw,format=I420 ! x264enc threads=4 bitrate=4096 key-int-max=15 byte-stream=true tune=zerolatency speed-preset=veryfast ! video/x-h264,stream-format=byte-stream" + appSink, pipelineDevice)
|
2021-01-13 04:24:54 +13:00
|
|
|
break
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-02-04 09:04:48 +13:00
|
|
|
// https://gstreamer.freedesktop.org/documentation/openh264/openh264enc.html
|
|
|
|
// gstreamer1.0-plugins-bad
|
|
|
|
if err = CheckPlugins([]string{"openh264"}); err == nil {
|
|
|
|
pipelineStr = fmt.Sprintf(videoSrc + "openh264enc multi-thread=4 complexity=high bitrate=3072000 max-bitrate=4096000 ! video/x-h264,stream-format=byte-stream" + appSink, pipelineDevice)
|
2021-01-13 04:24:54 +13:00
|
|
|
break
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
2021-01-13 04:24:54 +13:00
|
|
|
|
2021-02-06 01:58:02 +13:00
|
|
|
return "", err
|
2021-02-03 04:58:17 +13:00
|
|
|
case "opus":
|
2020-10-23 03:54:50 +13:00
|
|
|
// https://gstreamer.freedesktop.org/documentation/opus/opusenc.html
|
|
|
|
// gstreamer1.0-plugins-base
|
|
|
|
if err := CheckPlugins([]string{"pulseaudio", "opus"}); err != nil {
|
2021-02-06 01:58:02 +13:00
|
|
|
return "", err
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-02-04 09:04:48 +13:00
|
|
|
pipelineStr = fmt.Sprintf(audioSrc + "opusenc bitrate=128000" + appSink, pipelineDevice)
|
2021-02-03 04:58:17 +13:00
|
|
|
case "g722":
|
2021-02-04 09:04:48 +13:00
|
|
|
// https://gstreamer.freedesktop.org/documentation/libav/avenc_g722.html
|
2020-10-23 03:54:50 +13:00
|
|
|
// gstreamer1.0-libav
|
|
|
|
if err := CheckPlugins([]string{"pulseaudio", "libav"}); err != nil {
|
2021-02-06 01:58:02 +13:00
|
|
|
return "", err
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-01-13 04:24:54 +13:00
|
|
|
pipelineStr = fmt.Sprintf(audioSrc + "avenc_g722" + appSink, pipelineDevice)
|
2021-02-03 04:58:17 +13:00
|
|
|
case "pcmu":
|
2021-02-04 09:04:48 +13:00
|
|
|
// https://gstreamer.freedesktop.org/documentation/mulaw/mulawenc.html
|
2020-10-23 03:54:50 +13:00
|
|
|
// gstreamer1.0-plugins-good
|
|
|
|
if err := CheckPlugins([]string{"pulseaudio", "mulaw"}); err != nil {
|
2021-02-06 01:58:02 +13:00
|
|
|
return "", err
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-01-13 04:24:54 +13:00
|
|
|
pipelineStr = fmt.Sprintf(audioSrc + "audio/x-raw, rate=8000 ! mulawenc" + appSink, pipelineDevice)
|
2021-02-03 04:58:17 +13:00
|
|
|
case "pcma":
|
2021-02-04 09:04:48 +13:00
|
|
|
// https://gstreamer.freedesktop.org/documentation/alaw/alawenc.html
|
2020-10-23 03:54:50 +13:00
|
|
|
// gstreamer1.0-plugins-good
|
|
|
|
if err := CheckPlugins([]string{"pulseaudio", "alaw"}); err != nil {
|
2021-02-06 01:58:02 +13:00
|
|
|
return "", err
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-01-13 04:24:54 +13:00
|
|
|
pipelineStr = fmt.Sprintf(audioSrc + "audio/x-raw, rate=8000 ! alawenc" + appSink, pipelineDevice)
|
2020-10-23 03:54:50 +13:00
|
|
|
default:
|
2021-02-06 01:58:02 +13:00
|
|
|
return "", fmt.Errorf("unknown codec %s", codecRTP.Name)
|
2021-01-13 04:24:54 +13:00
|
|
|
}
|
|
|
|
|
2021-02-06 01:58:02 +13:00
|
|
|
return pipelineStr, nil
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePipeline creates a GStreamer Pipeline
|
2021-02-02 11:50:18 +13:00
|
|
|
func CreatePipeline(pipelineStr string) (*Pipeline, error) {
|
2020-10-23 03:54:50 +13:00
|
|
|
pipelineStrUnsafe := C.CString(pipelineStr)
|
|
|
|
defer C.free(unsafe.Pointer(pipelineStrUnsafe))
|
|
|
|
|
|
|
|
pipelinesLock.Lock()
|
|
|
|
defer pipelinesLock.Unlock()
|
|
|
|
|
2021-01-13 04:12:05 +13:00
|
|
|
var gstPipeline *C.GstElement
|
|
|
|
var gstError *C.GError
|
|
|
|
|
|
|
|
gstPipeline = C.gst_parse_launch(pipelineStrUnsafe, &gstError)
|
|
|
|
|
2021-01-13 04:24:54 +13:00
|
|
|
if gstError != nil {
|
2021-01-15 02:15:17 +13:00
|
|
|
defer C.g_error_free(gstError)
|
2021-01-13 04:12:05 +13:00
|
|
|
return nil, fmt.Errorf("(pipeline error) %s", C.GoString(gstError.message))
|
|
|
|
}
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
p := &Pipeline{
|
2021-01-13 04:12:05 +13:00
|
|
|
Pipeline: gstPipeline,
|
2020-10-23 03:54:50 +13:00
|
|
|
Sample: make(chan types.Sample),
|
|
|
|
Src: pipelineStr,
|
|
|
|
id: len(pipelines),
|
|
|
|
}
|
|
|
|
|
|
|
|
pipelines[p.id] = p
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the GStreamer Pipeline
|
|
|
|
func (p *Pipeline) Start() {
|
|
|
|
C.gstreamer_send_start_pipeline(p.Pipeline, C.int(p.id))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Play starts the GStreamer Pipeline
|
|
|
|
func (p *Pipeline) Play() {
|
|
|
|
C.gstreamer_send_play_pipeline(p.Pipeline)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the GStreamer Pipeline
|
|
|
|
func (p *Pipeline) Stop() {
|
|
|
|
C.gstreamer_send_stop_pipeline(p.Pipeline)
|
|
|
|
}
|
|
|
|
|
|
|
|
// gst-inspect-1.0
|
|
|
|
func CheckPlugins(plugins []string) error {
|
|
|
|
var plugin *C.GstPlugin
|
|
|
|
for _, pluginstr := range plugins {
|
|
|
|
plugincstr := C.CString(pluginstr)
|
|
|
|
plugin = C.gst_registry_find_plugin(registry, plugincstr)
|
|
|
|
C.free(unsafe.Pointer(plugincstr))
|
|
|
|
if plugin == nil {
|
|
|
|
return fmt.Errorf("required gstreamer plugin %s not found", pluginstr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//export goHandlePipelineBuffer
|
|
|
|
func goHandlePipelineBuffer(buffer unsafe.Pointer, bufferLen C.int, duration C.int, pipelineID C.int) {
|
2021-01-23 06:13:32 +13:00
|
|
|
defer C.free(buffer)
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
pipelinesLock.Lock()
|
|
|
|
pipeline, ok := pipelines[int(pipelineID)]
|
|
|
|
pipelinesLock.Unlock()
|
|
|
|
|
|
|
|
if ok {
|
2021-01-23 06:13:32 +13:00
|
|
|
pipeline.Sample <- types.Sample{
|
|
|
|
Data: C.GoBytes(buffer, bufferLen),
|
2021-02-02 11:50:18 +13:00
|
|
|
Duration: time.Duration(duration),
|
2021-01-23 06:13:32 +13:00
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
} else {
|
|
|
|
fmt.Printf("discarding buffer, no pipeline with id %d", int(pipelineID))
|
|
|
|
}
|
|
|
|
}
|