neko/server/internal/gst/gst.go

218 lines
6.1 KiB
Go
Raw Normal View History

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"
"sync"
"unsafe"
"github.com/pion/webrtc/v2"
2020-01-25 04:47:37 +13:00
"github.com/pkg/errors"
"n.eko.moe/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 \
gstreamer1.0-pulseaudio
*/
2020-01-13 21:05:38 +13:00
// Pipeline is a wrapper for a GStreamer Pipeline
type Pipeline struct {
Pipeline *C.GstElement
2020-01-25 04:47:37 +13:00
Sample chan types.Sample
CodecName string
ClockRate float32
2020-01-13 21:05:38 +13:00
id int
}
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 (
videoClockRate = 90000
audioClockRate = 48000
pcmClockRate = 8000
)
2020-01-19 12:30:09 +13:00
func init() {
C.gstreamer_init()
registry = C.gst_registry_get()
}
2020-01-13 21:05:38 +13:00
// CreatePipeline creates a GStreamer Pipeline
2020-01-25 04:47:37 +13:00
func CreatePipeline(codecName string, pipelineSrc string) (*Pipeline, error) {
2020-01-13 21:05:38 +13:00
pipelineStr := "appsink name=appsink"
var clockRate float32
switch codecName {
case webrtc.VP8:
2020-01-19 12:30:09 +13:00
// https://gstreamer.freedesktop.org/documentation/vpx/vp8enc.html?gi-language=c
2020-01-25 04:47:37 +13:00
// gstreamer1.0-plugins-good
2020-01-19 12:30:09 +13:00
// vp8enc error-resilient=partitions keyframe-max-dist=10 auto-alt-ref=true cpu-used=5 deadline=1
2020-01-13 21:05:38 +13:00
pipelineStr = pipelineSrc + " ! vp8enc error-resilient=partitions keyframe-max-dist=10 auto-alt-ref=true cpu-used=5 deadline=1 ! " + pipelineStr
clockRate = videoClockRate
2020-01-19 12:30:09 +13:00
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
}
2020-01-13 21:05:38 +13:00
case webrtc.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
2020-01-25 04:47:37 +13:00
// Causes panic!
2020-01-13 21:05:38 +13:00
pipelineStr = pipelineSrc + " ! vp9enc ! " + pipelineStr
clockRate = videoClockRate
2020-01-19 12:30:09 +13:00
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
}
2020-01-13 21:05:38 +13:00
case webrtc.H264:
2020-01-19 12:30:09 +13:00
// 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
pipelineStr = pipelineSrc + " ! openh264enc multi-thread=4 complexity=high bitrate=3072000 max-bitrate=4096000 ! video/x-h264,stream-format=byte-stream ! " + pipelineStr
2020-01-13 21:05:38 +13:00
clockRate = videoClockRate
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
}
2020-01-26 03:29:52 +13:00
// 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
2020-01-19 12:30:09 +13:00
if err := CheckPlugins([]string{"openh264"}); err != nil {
pipelineStr = pipelineSrc + " ! 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
if err := CheckPlugins([]string{"x264"}); 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
case webrtc.Opus:
2020-01-19 12:30:09 +13:00
// https://gstreamer.freedesktop.org/documentation/opus/opusenc.html
// gstreamer1.0-plugins-base
// opusenc
2020-01-13 21:05:38 +13:00
pipelineStr = pipelineSrc + " ! opusenc ! " + pipelineStr
clockRate = audioClockRate
2020-01-25 04:47:37 +13:00
2020-01-19 12:30:09 +13:00
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
case webrtc.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
2020-01-13 21:05:38 +13:00
pipelineStr = pipelineSrc + " ! avenc_g722 ! " + pipelineStr
clockRate = audioClockRate
2020-01-19 12:30:09 +13:00
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
}
2020-01-13 21:05:38 +13:00
case webrtc.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
2020-01-13 21:05:38 +13:00
pipelineStr = pipelineSrc + " ! audio/x-raw, rate=8000 ! mulawenc ! " + pipelineStr
clockRate = pcmClockRate
2020-01-19 12:30:09 +13:00
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
}
2020-01-13 21:05:38 +13:00
case webrtc.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
2020-01-13 21:05:38 +13:00
pipelineStr = pipelineSrc + " ! audio/x-raw, rate=8000 ! alawenc ! " + pipelineStr
clockRate = pcmClockRate
2020-01-19 12:30:09 +13:00
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
}
2020-01-13 21:05:38 +13:00
default:
2020-01-25 04:47:37 +13:00
return nil, errors.Errorf("unknown video codec %s", codecName)
2020-01-13 21:05:38 +13:00
}
pipelineStrUnsafe := C.CString(pipelineStr)
defer C.free(unsafe.Pointer(pipelineStrUnsafe))
pipelinesLock.Lock()
defer pipelinesLock.Unlock()
pipeline := &Pipeline{
Pipeline: C.gstreamer_send_create_pipeline(pipelineStrUnsafe),
2020-01-25 04:47:37 +13:00
Sample: make(chan types.Sample),
CodecName: codecName,
ClockRate: clockRate,
2020-01-13 21:05:38 +13:00
id: len(pipelines),
}
pipelines[pipeline.id] = pipeline
2020-01-25 04:47:37 +13:00
return pipeline, 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))
}
// Stop stops the GStreamer Pipeline
func (p *Pipeline) Stop() {
C.gstreamer_send_stop_pipeline(p.Pipeline)
}
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 {
2020-01-25 04:47:37 +13:00
samples := uint32(pipeline.ClockRate * (float32(duration) / 1000000000))
pipeline.Sample <- types.Sample{Data: C.GoBytes(buffer, bufferLen), Samples: samples}
2020-01-13 21:05:38 +13:00
} else {
fmt.Printf("discarding buffer, no pipeline with id %d", int(pipelineID))
}
C.free(buffer)
}