neko/pkg/gst/gst.go

239 lines
5.5 KiB
Go
Raw Permalink Normal View History

package gst
/*
#cgo pkg-config: gstreamer-1.0 gstreamer-app-1.0 gstreamer-video-1.0
#include "gst.h"
*/
import "C"
import (
"fmt"
"sync"
2021-12-06 09:31:40 +13:00
"sync/atomic"
2021-02-15 02:40:17 +13:00
"time"
"unsafe"
2021-12-06 05:52:25 +13:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
2022-03-20 23:43:00 +13:00
"github.com/demodesk/neko/pkg/types"
)
2022-10-14 06:57:31 +13:00
var (
pSerial int32
pipelines = make(map[int]*pipeline)
pipelinesLock sync.Mutex
registry *C.GstRegistry
)
func init() {
2022-09-12 08:56:17 +12:00
C.gst_init(nil, nil)
registry = C.gst_registry_get()
}
2022-10-14 06:57:31 +13:00
type Pipeline interface {
Src() string
Sample() chan types.Sample
// attach sink or src to pipeline
AttachAppsink(sinkName string)
AttachAppsrc(srcName string)
// control pipeline lifecycle
Play()
Pause()
Destroy()
Push(buffer []byte)
// modify the property of a bin
SetPropInt(binName string, prop string, value int) bool
SetCapsFramerate(binName string, numerator, denominator int) bool
SetCapsResolution(binName string, width, height int) bool
// emit video keyframe
EmitVideoKeyframe() bool
2022-10-14 06:57:31 +13:00
}
type pipeline struct {
id int
logger zerolog.Logger
src string
ctx *C.GstPipelineCtx
sample chan types.Sample
}
func CreatePipeline(pipelineStr string) (Pipeline, error) {
2021-12-06 09:31:40 +13:00
id := atomic.AddInt32(&pSerial, 1)
pipelineStrUnsafe := C.CString(pipelineStr)
defer C.free(unsafe.Pointer(pipelineStrUnsafe))
pipelinesLock.Lock()
defer pipelinesLock.Unlock()
2021-12-06 06:16:26 +13:00
var gstError *C.GError
ctx := C.gstreamer_pipeline_create(pipelineStrUnsafe, C.int(id), &gstError)
2021-01-13 04:12:05 +13:00
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-02-15 02:40:17 +13:00
return nil, fmt.Errorf("(pipeline error) %s", C.GoString(gstError.message))
2021-01-13 04:12:05 +13:00
}
2022-10-14 06:57:31 +13:00
p := &pipeline{
2022-09-11 09:18:16 +12:00
id: int(id),
logger: log.With().
Str("module", "capture").
Str("submodule", "gstreamer").
Int("pipeline_id", int(id)).Logger(),
2022-10-14 06:57:31 +13:00
src: pipelineStr,
ctx: ctx,
sample: make(chan types.Sample),
}
pipelines[p.id] = p
return p, nil
}
2022-10-14 06:57:31 +13:00
func (p *pipeline) Src() string {
return p.src
}
func (p *pipeline) Sample() chan types.Sample {
return p.sample
}
func (p *pipeline) AttachAppsink(sinkName string) {
2021-11-29 10:19:06 +13:00
sinkNameUnsafe := C.CString(sinkName)
defer C.free(unsafe.Pointer(sinkNameUnsafe))
2022-10-14 06:57:31 +13:00
C.gstreamer_pipeline_attach_appsink(p.ctx, sinkNameUnsafe)
}
2022-10-14 06:57:31 +13:00
func (p *pipeline) AttachAppsrc(srcName string) {
2021-12-06 10:06:42 +13:00
srcNameUnsafe := C.CString(srcName)
defer C.free(unsafe.Pointer(srcNameUnsafe))
2022-10-14 06:57:31 +13:00
C.gstreamer_pipeline_attach_appsrc(p.ctx, srcNameUnsafe)
2021-12-06 10:06:42 +13:00
}
2022-10-14 06:57:31 +13:00
func (p *pipeline) Play() {
C.gstreamer_pipeline_play(p.ctx)
2021-12-06 06:16:26 +13:00
}
2022-10-14 06:57:31 +13:00
func (p *pipeline) Pause() {
C.gstreamer_pipeline_pause(p.ctx)
}
2022-10-14 06:57:31 +13:00
func (p *pipeline) Destroy() {
C.gstreamer_pipeline_destory(p.ctx)
2021-12-06 09:31:40 +13:00
pipelinesLock.Lock()
delete(pipelines, p.id)
pipelinesLock.Unlock()
2022-10-14 06:57:31 +13:00
close(p.sample)
C.free(unsafe.Pointer(p.ctx))
}
2022-10-14 06:57:31 +13:00
func (p *pipeline) Push(buffer []byte) {
2021-11-29 10:37:17 +13:00
bytes := C.CBytes(buffer)
defer C.free(bytes)
2022-10-14 06:57:31 +13:00
C.gstreamer_pipeline_push(p.ctx, bytes, C.int(len(buffer)))
2021-11-29 10:37:17 +13:00
}
2022-10-14 06:57:31 +13:00
func (p *pipeline) SetPropInt(binName string, prop string, value int) bool {
2022-09-11 09:18:16 +12:00
cBinName := C.CString(binName)
defer C.free(unsafe.Pointer(cBinName))
cProp := C.CString(prop)
defer C.free(unsafe.Pointer(cProp))
cValue := C.int(value)
p.logger.Debug().Msgf("setting prop %s of %s to %d", prop, binName, value)
2022-10-14 06:57:31 +13:00
ok := C.gstreamer_pipeline_set_prop_int(p.ctx, cBinName, cProp, cValue)
2022-09-11 09:18:16 +12:00
return ok == C.TRUE
}
2022-10-14 06:57:31 +13:00
func (p *pipeline) SetCapsFramerate(binName string, numerator, denominator int) bool {
2022-09-11 09:18:16 +12:00
cBinName := C.CString(binName)
cNumerator := C.int(numerator)
cDenominator := C.int(denominator)
defer C.free(unsafe.Pointer(cBinName))
p.logger.Debug().Msgf("setting caps framerate of %s to %d/%d", binName, numerator, denominator)
2022-10-14 06:57:31 +13:00
ok := C.gstreamer_pipeline_set_caps_framerate(p.ctx, cBinName, cNumerator, cDenominator)
2022-09-11 09:18:16 +12:00
return ok == C.TRUE
}
2022-10-14 06:57:31 +13:00
func (p *pipeline) SetCapsResolution(binName string, width, height int) bool {
2022-09-11 09:18:16 +12:00
cBinName := C.CString(binName)
cWidth := C.int(width)
cHeight := C.int(height)
defer C.free(unsafe.Pointer(cBinName))
p.logger.Debug().Msgf("setting caps resolution of %s to %dx%d", binName, width, height)
2022-10-14 06:57:31 +13:00
ok := C.gstreamer_pipeline_set_caps_resolution(p.ctx, cBinName, cWidth, cHeight)
2022-09-11 09:18:16 +12:00
return ok == C.TRUE
}
func (p *pipeline) EmitVideoKeyframe() bool {
ok := C.gstreamer_pipeline_emit_video_keyframe(p.ctx)
return ok == C.TRUE
}
// 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(pipelineID C.int, buf C.gpointer, bufLen C.int, duration C.guint64, deltaUnit C.gboolean) {
defer C.g_free(buf)
2021-01-23 06:13:32 +13:00
pipelinesLock.Lock()
pipeline, ok := pipelines[int(pipelineID)]
pipelinesLock.Unlock()
if ok {
2022-10-14 06:57:31 +13:00
pipeline.sample <- types.Sample{
Data: C.GoBytes(unsafe.Pointer(buf), bufLen),
Length: int(bufLen),
Timestamp: time.Now(),
Duration: time.Duration(duration),
DeltaUnit: deltaUnit == C.TRUE,
2021-01-23 06:13:32 +13:00
}
} else {
2021-12-06 05:52:25 +13:00
log.Warn().
Str("module", "capture").
Str("submodule", "gstreamer").
2022-02-13 06:55:56 +13:00
Int("pipeline_id", int(pipelineID)).
2021-12-06 09:33:30 +13:00
Msgf("discarding sample, pipeline not found")
}
}
2021-12-06 05:52:25 +13:00
//export goPipelineLog
func goPipelineLog(pipelineID C.int, levelUnsafe *C.char, msgUnsafe *C.char) {
2021-12-06 05:52:25 +13:00
levelStr := C.GoString(levelUnsafe)
msg := C.GoString(msgUnsafe)
2021-12-06 09:33:30 +13:00
level, _ := zerolog.ParseLevel(levelStr)
log.WithLevel(level).
2021-12-06 05:52:25 +13:00
Str("module", "capture").
Str("submodule", "gstreamer").
2022-02-13 06:55:56 +13:00
Int("pipeline_id", int(pipelineID)).
2021-12-06 09:33:30 +13:00
Msg(msg)
2021-12-06 05:52:25 +13:00
}