2020-01-13 21:05:38 +13:00
|
|
|
package gst
|
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo pkg-config: gstreamer-1.0 gstreamer-app-1.0
|
|
|
|
|
|
|
|
#include "gst.h"
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-04 02:17:26 +13:00
|
|
|
"strings"
|
2020-01-13 21:05:38 +13:00
|
|
|
"sync"
|
2021-02-15 05:30:24 +13:00
|
|
|
"time"
|
2020-01-13 21:05:38 +13:00
|
|
|
"unsafe"
|
|
|
|
|
2021-10-06 09:38:24 +13:00
|
|
|
"m1k1o/neko/internal/types"
|
2020-01-13 21:05:38 +13:00
|
|
|
)
|
|
|
|
|
2020-01-19 12:30:09 +13:00
|
|
|
/*
|
|
|
|
apt-get install \
|
|
|
|
libgstreamer1.0-0 \
|
|
|
|
gstreamer1.0-plugins-base \
|
|
|
|
gstreamer1.0-plugins-good \
|
|
|
|
gstreamer1.0-plugins-bad \
|
|
|
|
gstreamer1.0-plugins-ugly\
|
|
|
|
gstreamer1.0-libav \
|
|
|
|
gstreamer1.0-doc \
|
|
|
|
gstreamer1.0-tools \
|
|
|
|
gstreamer1.0-x \
|
|
|
|
gstreamer1.0-alsa \
|
2020-01-28 20:07:35 +13:00
|
|
|
gstreamer1.0-pulseaudio
|
|
|
|
|
|
|
|
gst-inspect-1.0 --version
|
|
|
|
gst-inspect-1.0 plugin
|
|
|
|
gst-launch-1.0 ximagesrc show-pointer=true use-damage=false ! video/x-raw,framerate=30/1 ! videoconvert ! queue ! vp8enc error-resilient=partitions keyframe-max-dist=10 auto-alt-ref=true cpu-used=5 deadline=1 ! autovideosink
|
|
|
|
gst-launch-1.0 pulsesrc ! audioconvert ! opusenc ! autoaudiosink
|
2020-01-19 12:30:09 +13:00
|
|
|
*/
|
2020-01-13 21:05:38 +13:00
|
|
|
|
|
|
|
// Pipeline is a wrapper for a GStreamer Pipeline
|
|
|
|
type Pipeline struct {
|
2021-04-05 08:37:33 +12:00
|
|
|
Pipeline *C.GstElement
|
|
|
|
Sample chan types.Sample
|
|
|
|
Src string
|
|
|
|
id int
|
2020-01-13 21:05:38 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
var pipelines = make(map[int]*Pipeline)
|
|
|
|
var pipelinesLock sync.Mutex
|
2020-01-19 12:30:09 +13:00
|
|
|
var registry *C.GstRegistry
|
2020-01-13 21:05:38 +13:00
|
|
|
|
|
|
|
const (
|
2021-02-16 02:59:20 +13:00
|
|
|
videoSrc = "ximagesrc display-name=%s show-pointer=true use-damage=false ! video/x-raw,framerate=%d/1 ! videoconvert ! queue ! "
|
2021-02-15 08:26:35 +13:00
|
|
|
audioSrc = "pulsesrc device=%s ! audio/x-raw,channels=2 ! audioconvert ! "
|
2020-01-13 21:05:38 +13:00
|
|
|
)
|
|
|
|
|
2020-01-19 12:30:09 +13:00
|
|
|
func init() {
|
|
|
|
C.gstreamer_init()
|
|
|
|
registry = C.gst_registry_get()
|
|
|
|
}
|
|
|
|
|
2020-04-06 15:42:42 +12:00
|
|
|
// CreateRTMPPipeline creates a GStreamer Pipeline
|
2020-09-27 23:09:45 +13:00
|
|
|
func CreateRTMPPipeline(pipelineDevice string, pipelineDisplay string, pipelineSrc string, pipelineRTMP string) (*Pipeline, error) {
|
2021-02-16 02:59:20 +13:00
|
|
|
video := fmt.Sprintf(videoSrc, pipelineDisplay, 25)
|
2020-04-06 15:42:42 +12:00
|
|
|
audio := fmt.Sprintf(audioSrc, pipelineDevice)
|
2020-09-24 18:09:02 +12:00
|
|
|
|
2020-09-27 23:09:45 +13:00
|
|
|
var pipelineStr string
|
|
|
|
if pipelineSrc != "" {
|
2021-04-04 02:17:26 +13:00
|
|
|
// replace RTMP url
|
|
|
|
pipelineStr = strings.Replace(pipelineSrc, "{url}", pipelineRTMP, -1)
|
|
|
|
// replace audio device
|
|
|
|
pipelineStr = strings.Replace(pipelineStr, "{device}", pipelineDevice, -1)
|
|
|
|
// replace display
|
|
|
|
pipelineStr = strings.Replace(pipelineStr, "{display}", pipelineDisplay, -1)
|
2020-09-27 23:09:45 +13:00
|
|
|
} else {
|
2020-09-27 23:10:54 +13:00
|
|
|
pipelineStr = fmt.Sprintf("flvmux name=mux ! rtmpsink location='%s 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.", pipelineRTMP, audio, video)
|
2020-09-27 23:09:45 +13:00
|
|
|
}
|
|
|
|
|
2021-04-04 02:19:01 +13:00
|
|
|
return CreatePipeline(pipelineStr)
|
2020-04-06 15:42:42 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateAppPipeline creates a GStreamer Pipeline
|
2022-03-18 08:25:17 +13:00
|
|
|
func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc string, fps int, bitrate uint, hwenc string) (*Pipeline, error) {
|
2020-02-11 18:15:59 +13:00
|
|
|
pipelineStr := " ! appsink name=appsink"
|
|
|
|
|
2021-12-09 07:40:30 +13:00
|
|
|
// if using custom pipeline
|
|
|
|
if pipelineSrc != "" {
|
|
|
|
pipelineStr = fmt.Sprintf(pipelineSrc+pipelineStr, pipelineDevice)
|
|
|
|
return CreatePipeline(pipelineStr)
|
|
|
|
}
|
|
|
|
|
2020-01-13 21:05:38 +13:00
|
|
|
switch codecName {
|
2021-02-15 05:30:24 +13:00
|
|
|
case "VP8":
|
2022-03-18 08:25:17 +13:00
|
|
|
if hwenc == "VAAPI" {
|
|
|
|
if err := CheckPlugins([]string{"ximagesrc", "vaapi"}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// vp8 encode is missing from gstreamer.freedesktop.org/documentation
|
|
|
|
// note that it was removed from some recent intel CPUs: https://trac.ffmpeg.org/wiki/Hardware/QuickSync
|
|
|
|
// https://gstreamer.freedesktop.org/data/doc/gstreamer/head/gstreamer-vaapi-plugins/html/gstreamer-vaapi-plugins-vaapivp8enc.html
|
|
|
|
pipelineStr = fmt.Sprintf(videoSrc+"video/x-raw,format=NV12 ! vaapivp8enc rate-control=vbr bitrate=%d keyframe-period=180"+pipelineStr, pipelineDevice, fps, bitrate)
|
|
|
|
} else {
|
|
|
|
// https://gstreamer.freedesktop.org/documentation/vpx/vp8enc.html?gi-language=c
|
|
|
|
// gstreamer1.0-plugins-good
|
|
|
|
// vp8enc error-resilient=partitions keyframe-max-dist=10 auto-alt-ref=true cpu-used=5 deadline=1
|
|
|
|
if err := CheckPlugins([]string{"ximagesrc", "vpx"}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-01 05:45:59 +12:00
|
|
|
pipelineStr = strings.Join([]string{
|
|
|
|
fmt.Sprintf(videoSrc, pipelineDevice, fps),
|
|
|
|
"vp8enc",
|
|
|
|
fmt.Sprintf("target-bitrate=%d", bitrate*650),
|
|
|
|
"cpu-used=4",
|
|
|
|
"end-usage=cbr",
|
|
|
|
"threads=4",
|
|
|
|
"deadline=1",
|
|
|
|
"undershoot=95",
|
|
|
|
fmt.Sprintf("buffer-size=%d", bitrate*4),
|
|
|
|
fmt.Sprintf("buffer-initial-size=%d", bitrate*2),
|
|
|
|
fmt.Sprintf("buffer-optimal-size=%d", bitrate*3),
|
|
|
|
"keyframe-max-dist=25",
|
|
|
|
"min-quantizer=4",
|
|
|
|
"max-quantizer=20",
|
|
|
|
pipelineStr,
|
|
|
|
}, " ")
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
2021-02-15 05:30:24 +13:00
|
|
|
case "VP9":
|
2020-01-19 12:30:09 +13:00
|
|
|
// https://gstreamer.freedesktop.org/documentation/vpx/vp9enc.html?gi-language=c
|
2020-01-25 04:47:37 +13:00
|
|
|
// gstreamer1.0-plugins-good
|
2020-01-19 12:30:09 +13:00
|
|
|
// vp9enc
|
|
|
|
if err := CheckPlugins([]string{"ximagesrc", "vpx"}); err != nil {
|
2020-01-25 04:47:37 +13:00
|
|
|
return nil, err
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2021-12-09 07:40:30 +13:00
|
|
|
pipelineStr = fmt.Sprintf(videoSrc+"vp9enc target-bitrate=%d cpu-used=-5 threads=4 deadline=1 keyframe-max-dist=30 auto-alt-ref=true"+pipelineStr, pipelineDevice, fps, bitrate*1000)
|
2021-02-15 05:30:24 +13:00
|
|
|
case "H264":
|
2020-01-19 12:30:09 +13:00
|
|
|
if err := CheckPlugins([]string{"ximagesrc"}); err != nil {
|
2020-01-25 04:47:37 +13:00
|
|
|
return nil, err
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:25:17 +13:00
|
|
|
if hwenc == "VAAPI" {
|
|
|
|
if err := CheckPlugins([]string{"vaapi"}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pipelineStr = fmt.Sprintf(videoSrc+"video/x-raw,format=NV12 ! vaapih264enc rate-control=vbr bitrate=%d keyframe-period=180 quality-level=7 ! video/x-h264,stream-format=byte-stream"+pipelineStr, pipelineDevice, fps, bitrate)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// 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 {
|
|
|
|
pipelineStr = fmt.Sprintf(videoSrc+"openh264enc multi-thread=4 complexity=high bitrate=%d max-bitrate=%d ! video/x-h264,stream-format=byte-stream"+pipelineStr, pipelineDevice, fps, bitrate*1000, (bitrate+1024)*1000)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://gstreamer.freedesktop.org/documentation/x264/index.html?gi-language=c
|
|
|
|
// 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
|
|
|
|
if err := CheckPlugins([]string{"x264"}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vbvbuf := uint(1000)
|
|
|
|
if bitrate > 1000 {
|
|
|
|
vbvbuf = bitrate
|
|
|
|
}
|
|
|
|
|
|
|
|
pipelineStr = fmt.Sprintf(videoSrc+"video/x-raw,format=NV12 ! x264enc threads=4 bitrate=%d key-int-max=60 vbv-buf-capacity=%d byte-stream=true tune=zerolatency speed-preset=veryfast ! video/x-h264,stream-format=byte-stream"+pipelineStr, pipelineDevice, fps, bitrate, vbvbuf)
|
2021-03-01 12:12:03 +13:00
|
|
|
}
|
2021-02-15 05:30:24 +13:00
|
|
|
case "Opus":
|
2020-01-19 12:30:09 +13:00
|
|
|
// https://gstreamer.freedesktop.org/documentation/opus/opusenc.html
|
|
|
|
// gstreamer1.0-plugins-base
|
|
|
|
// opusenc
|
|
|
|
if err := CheckPlugins([]string{"pulseaudio", "opus"}); err != nil {
|
2020-01-25 04:47:37 +13:00
|
|
|
return nil, err
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
2020-01-13 21:05:38 +13:00
|
|
|
|
2022-03-25 09:48:03 +13:00
|
|
|
pipelineStr = fmt.Sprintf(audioSrc+"opusenc inband-fec=true bitrate=%d"+pipelineStr, pipelineDevice, bitrate*1000)
|
2021-02-15 05:30:24 +13:00
|
|
|
case "G722":
|
2020-01-19 12:30:09 +13:00
|
|
|
// https://gstreamer.freedesktop.org/documentation/libav/avenc_g722.html?gi-language=c
|
|
|
|
// gstreamer1.0-libav
|
|
|
|
// avenc_g722
|
|
|
|
if err := CheckPlugins([]string{"pulseaudio", "libav"}); err != nil {
|
2020-01-25 04:47:37 +13:00
|
|
|
return nil, err
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2021-12-09 07:40:30 +13:00
|
|
|
pipelineStr = fmt.Sprintf(audioSrc+"avenc_g722 bitrate=%d"+pipelineStr, pipelineDevice, bitrate*1000)
|
2021-02-15 05:30:24 +13:00
|
|
|
case "PCMU":
|
2020-01-19 12:30:09 +13:00
|
|
|
// https://gstreamer.freedesktop.org/documentation/mulaw/mulawenc.html?gi-language=c
|
|
|
|
// gstreamer1.0-plugins-good
|
|
|
|
// audio/x-raw, rate=8000 ! mulawenc
|
|
|
|
if err := CheckPlugins([]string{"pulseaudio", "mulaw"}); err != nil {
|
2020-01-25 04:47:37 +13:00
|
|
|
return nil, err
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2021-12-09 07:40:30 +13:00
|
|
|
pipelineStr = fmt.Sprintf(audioSrc+"audio/x-raw, rate=8000 ! mulawenc"+pipelineStr, pipelineDevice)
|
2021-02-15 05:30:24 +13:00
|
|
|
case "PCMA":
|
2020-01-19 12:30:09 +13:00
|
|
|
// https://gstreamer.freedesktop.org/documentation/alaw/alawenc.html?gi-language=c
|
|
|
|
// gstreamer1.0-plugins-good
|
|
|
|
// audio/x-raw, rate=8000 ! alawenc
|
|
|
|
if err := CheckPlugins([]string{"pulseaudio", "alaw"}); err != nil {
|
2020-01-25 04:47:37 +13:00
|
|
|
return nil, err
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2021-12-09 07:40:30 +13:00
|
|
|
pipelineStr = fmt.Sprintf(audioSrc+"audio/x-raw, rate=8000 ! alawenc"+pipelineStr, pipelineDevice)
|
2020-01-13 21:05:38 +13:00
|
|
|
default:
|
2020-02-14 16:50:23 +13:00
|
|
|
return nil, fmt.Errorf("unknown codec %s", codecName)
|
2020-01-13 21:05:38 +13:00
|
|
|
}
|
|
|
|
|
2021-04-04 02:19:01 +13:00
|
|
|
return CreatePipeline(pipelineStr)
|
2020-04-06 15:42:42 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePipeline creates a GStreamer Pipeline
|
2021-04-04 02:19:01 +13:00
|
|
|
func CreatePipeline(pipelineStr string) (*Pipeline, error) {
|
2020-01-13 21:05:38 +13:00
|
|
|
pipelineStrUnsafe := C.CString(pipelineStr)
|
|
|
|
defer C.free(unsafe.Pointer(pipelineStrUnsafe))
|
|
|
|
|
|
|
|
pipelinesLock.Lock()
|
|
|
|
defer pipelinesLock.Unlock()
|
|
|
|
|
2021-08-16 01:37:27 +12:00
|
|
|
var err *C.GError
|
|
|
|
gstPipeline := C.gstreamer_send_create_pipeline(pipelineStrUnsafe, &err)
|
|
|
|
if err != nil {
|
|
|
|
defer C.g_error_free(err)
|
|
|
|
return nil, fmt.Errorf("%s", C.GoString(err.message))
|
|
|
|
}
|
|
|
|
|
2020-02-11 18:15:59 +13:00
|
|
|
p := &Pipeline{
|
2021-08-16 01:37:27 +12:00
|
|
|
Pipeline: gstPipeline,
|
2021-04-05 08:37:33 +12:00
|
|
|
Sample: make(chan types.Sample),
|
|
|
|
Src: pipelineStr,
|
|
|
|
id: len(pipelines),
|
2020-01-13 21:05:38 +13:00
|
|
|
}
|
|
|
|
|
2020-02-11 18:15:59 +13:00
|
|
|
pipelines[p.id] = p
|
|
|
|
return p, nil
|
2020-01-13 21:05:38 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the GStreamer Pipeline
|
|
|
|
func (p *Pipeline) Start() {
|
|
|
|
C.gstreamer_send_start_pipeline(p.Pipeline, C.int(p.id))
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:09:02 +12:00
|
|
|
// Play starts the GStreamer Pipeline
|
|
|
|
func (p *Pipeline) Play() {
|
|
|
|
C.gstreamer_send_play_pipeline(p.Pipeline)
|
|
|
|
}
|
|
|
|
|
2020-01-13 21:05:38 +13:00
|
|
|
// Stop stops the GStreamer Pipeline
|
|
|
|
func (p *Pipeline) Stop() {
|
|
|
|
C.gstreamer_send_stop_pipeline(p.Pipeline)
|
|
|
|
}
|
|
|
|
|
2020-01-27 14:28:39 +13:00
|
|
|
// gst-inspect-1.0
|
2020-01-19 12:30:09 +13:00
|
|
|
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 {
|
2020-01-25 04:47:37 +13:00
|
|
|
return fmt.Errorf("required gstreamer plugin %s not found", pluginstr)
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-13 21:05:38 +13:00
|
|
|
//export goHandlePipelineBuffer
|
|
|
|
func goHandlePipelineBuffer(buffer unsafe.Pointer, bufferLen C.int, duration C.int, pipelineID C.int) {
|
|
|
|
pipelinesLock.Lock()
|
|
|
|
pipeline, ok := pipelines[int(pipelineID)]
|
|
|
|
pipelinesLock.Unlock()
|
|
|
|
|
|
|
|
if ok {
|
2021-02-15 05:30:24 +13:00
|
|
|
pipeline.Sample <- types.Sample{Data: C.GoBytes(buffer, bufferLen), Timestamp: time.Now(), Duration: time.Duration(duration)}
|
2020-01-13 21:05:38 +13:00
|
|
|
} else {
|
|
|
|
fmt.Printf("discarding buffer, no pipeline with id %d", int(pipelineID))
|
|
|
|
}
|
|
|
|
C.free(buffer)
|
|
|
|
}
|