gst as interface. (#11)

This commit is contained in:
Miroslav Šedivý 2022-10-13 19:57:31 +02:00 committed by GitHub
parent 0badeeee36
commit 095f9fe8ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 46 deletions

View File

@ -16,7 +16,7 @@ type BroacastManagerCtx struct {
logger zerolog.Logger logger zerolog.Logger
mu sync.Mutex mu sync.Mutex
pipeline *gst.Pipeline pipeline gst.Pipeline
pipelineMu sync.Mutex pipelineMu sync.Mutex
pipelineFn func(url string) (string, error) pipelineFn func(url string) (string, error)

View File

@ -23,7 +23,7 @@ type ScreencastManagerCtx struct {
mu sync.Mutex mu sync.Mutex
wg sync.WaitGroup wg sync.WaitGroup
pipeline *gst.Pipeline pipeline gst.Pipeline
pipelineStr string pipelineStr string
pipelineMu sync.Mutex pipelineMu sync.Mutex
@ -202,7 +202,7 @@ func (manager *ScreencastManagerCtx) createPipeline() error {
// get first image // get first image
select { select {
case image, ok := <-manager.pipeline.Sample: case image, ok := <-manager.pipeline.Sample():
if !ok { if !ok {
return errors.New("unable to get first image") return errors.New("unable to get first image")
} else { } else {
@ -220,7 +220,7 @@ func (manager *ScreencastManagerCtx) createPipeline() error {
defer manager.wg.Done() defer manager.wg.Done()
for { for {
image, ok := <-pipeline.Sample image, ok := <-pipeline.Sample()
if !ok { if !ok {
manager.logger.Debug().Msg("stopped receiving images") manager.logger.Debug().Msg("stopped receiving images")
return return

View File

@ -24,7 +24,7 @@ type StreamSinkManagerCtx struct {
wg sync.WaitGroup wg sync.WaitGroup
codec codec.RTPCodec codec codec.RTPCodec
pipeline *gst.Pipeline pipeline gst.Pipeline
pipelineMu sync.Mutex pipelineMu sync.Mutex
pipelineFn func() (string, error) pipelineFn func() (string, error)
@ -275,7 +275,7 @@ func (manager *StreamSinkManagerCtx) createPipeline() error {
defer manager.wg.Done() defer manager.wg.Done()
for { for {
sample, ok := <-pipeline.Sample sample, ok := <-pipeline.Sample()
if !ok { if !ok {
manager.logger.Debug().Msg("stopped emitting samples") manager.logger.Debug().Msg("stopped emitting samples")
return return

View File

@ -20,7 +20,7 @@ type StreamSrcManagerCtx struct {
codecPipeline map[string]string // codec -> pipeline codecPipeline map[string]string // codec -> pipeline
codec codec.RTPCodec codec codec.RTPCodec
pipeline *gst.Pipeline pipeline gst.Pipeline
pipelineMu sync.Mutex pipelineMu sync.Mutex
pipelineStr string pipelineStr string

View File

@ -19,25 +19,44 @@ import (
"github.com/demodesk/neko/pkg/types" "github.com/demodesk/neko/pkg/types"
) )
type Pipeline struct { var (
id int pSerial int32
logger zerolog.Logger pipelines = make(map[int]*pipeline)
Src string pipelinesLock sync.Mutex
Ctx *C.GstPipelineCtx registry *C.GstRegistry
Sample chan types.Sample )
}
var pSerial int32
var pipelines = make(map[int]*Pipeline)
var pipelinesLock sync.Mutex
var registry *C.GstRegistry
func init() { func init() {
C.gst_init(nil, nil) C.gst_init(nil, nil)
registry = C.gst_registry_get() registry = C.gst_registry_get()
} }
func CreatePipeline(pipelineStr string) (*Pipeline, error) { 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
}
type pipeline struct {
id int
logger zerolog.Logger
src string
ctx *C.GstPipelineCtx
sample chan types.Sample
}
func CreatePipeline(pipelineStr string) (Pipeline, error) {
id := atomic.AddInt32(&pSerial, 1) id := atomic.AddInt32(&pSerial, 1)
pipelineStrUnsafe := C.CString(pipelineStr) pipelineStrUnsafe := C.CString(pipelineStr)
@ -54,63 +73,70 @@ func CreatePipeline(pipelineStr string) (*Pipeline, error) {
return nil, fmt.Errorf("(pipeline error) %s", C.GoString(gstError.message)) return nil, fmt.Errorf("(pipeline error) %s", C.GoString(gstError.message))
} }
p := &Pipeline{ p := &pipeline{
id: int(id), id: int(id),
logger: log.With(). logger: log.With().
Str("module", "capture"). Str("module", "capture").
Str("submodule", "gstreamer"). Str("submodule", "gstreamer").
Int("pipeline_id", int(id)).Logger(), Int("pipeline_id", int(id)).Logger(),
Src: pipelineStr, src: pipelineStr,
Ctx: ctx, ctx: ctx,
Sample: make(chan types.Sample), sample: make(chan types.Sample),
} }
pipelines[p.id] = p pipelines[p.id] = p
return p, nil return p, nil
} }
func (p *Pipeline) AttachAppsink(sinkName string) { func (p *pipeline) Src() string {
return p.src
}
func (p *pipeline) Sample() chan types.Sample {
return p.sample
}
func (p *pipeline) AttachAppsink(sinkName string) {
sinkNameUnsafe := C.CString(sinkName) sinkNameUnsafe := C.CString(sinkName)
defer C.free(unsafe.Pointer(sinkNameUnsafe)) defer C.free(unsafe.Pointer(sinkNameUnsafe))
C.gstreamer_pipeline_attach_appsink(p.Ctx, sinkNameUnsafe) C.gstreamer_pipeline_attach_appsink(p.ctx, sinkNameUnsafe)
} }
func (p *Pipeline) AttachAppsrc(srcName string) { func (p *pipeline) AttachAppsrc(srcName string) {
srcNameUnsafe := C.CString(srcName) srcNameUnsafe := C.CString(srcName)
defer C.free(unsafe.Pointer(srcNameUnsafe)) defer C.free(unsafe.Pointer(srcNameUnsafe))
C.gstreamer_pipeline_attach_appsrc(p.Ctx, srcNameUnsafe) C.gstreamer_pipeline_attach_appsrc(p.ctx, srcNameUnsafe)
} }
func (p *Pipeline) Play() { func (p *pipeline) Play() {
C.gstreamer_pipeline_play(p.Ctx) C.gstreamer_pipeline_play(p.ctx)
} }
func (p *Pipeline) Pause() { func (p *pipeline) Pause() {
C.gstreamer_pipeline_pause(p.Ctx) C.gstreamer_pipeline_pause(p.ctx)
} }
func (p *Pipeline) Destroy() { func (p *pipeline) Destroy() {
C.gstreamer_pipeline_destory(p.Ctx) C.gstreamer_pipeline_destory(p.ctx)
pipelinesLock.Lock() pipelinesLock.Lock()
delete(pipelines, p.id) delete(pipelines, p.id)
pipelinesLock.Unlock() pipelinesLock.Unlock()
close(p.Sample) close(p.sample)
C.free(unsafe.Pointer(p.Ctx)) C.free(unsafe.Pointer(p.ctx))
p = nil
} }
func (p *Pipeline) Push(buffer []byte) { func (p *pipeline) Push(buffer []byte) {
bytes := C.CBytes(buffer) bytes := C.CBytes(buffer)
defer C.free(bytes) defer C.free(bytes)
C.gstreamer_pipeline_push(p.Ctx, bytes, C.int(len(buffer))) C.gstreamer_pipeline_push(p.ctx, bytes, C.int(len(buffer)))
} }
func (p *Pipeline) SetPropInt(binName string, prop string, value int) bool { func (p *pipeline) SetPropInt(binName string, prop string, value int) bool {
cBinName := C.CString(binName) cBinName := C.CString(binName)
defer C.free(unsafe.Pointer(cBinName)) defer C.free(unsafe.Pointer(cBinName))
@ -121,11 +147,11 @@ func (p *Pipeline) SetPropInt(binName string, prop string, value int) bool {
p.logger.Debug().Msgf("setting prop %s of %s to %d", prop, binName, value) p.logger.Debug().Msgf("setting prop %s of %s to %d", prop, binName, value)
ok := C.gstreamer_pipeline_set_prop_int(p.Ctx, cBinName, cProp, cValue) ok := C.gstreamer_pipeline_set_prop_int(p.ctx, cBinName, cProp, cValue)
return ok == C.TRUE return ok == C.TRUE
} }
func (p *Pipeline) SetCapsFramerate(binName string, numerator, denominator int) bool { func (p *pipeline) SetCapsFramerate(binName string, numerator, denominator int) bool {
cBinName := C.CString(binName) cBinName := C.CString(binName)
cNumerator := C.int(numerator) cNumerator := C.int(numerator)
cDenominator := C.int(denominator) cDenominator := C.int(denominator)
@ -134,11 +160,11 @@ func (p *Pipeline) SetCapsFramerate(binName string, numerator, denominator int)
p.logger.Debug().Msgf("setting caps framerate of %s to %d/%d", binName, numerator, denominator) p.logger.Debug().Msgf("setting caps framerate of %s to %d/%d", binName, numerator, denominator)
ok := C.gstreamer_pipeline_set_caps_framerate(p.Ctx, cBinName, cNumerator, cDenominator) ok := C.gstreamer_pipeline_set_caps_framerate(p.ctx, cBinName, cNumerator, cDenominator)
return ok == C.TRUE return ok == C.TRUE
} }
func (p *Pipeline) SetCapsResolution(binName string, width, height int) bool { func (p *pipeline) SetCapsResolution(binName string, width, height int) bool {
cBinName := C.CString(binName) cBinName := C.CString(binName)
cWidth := C.int(width) cWidth := C.int(width)
cHeight := C.int(height) cHeight := C.int(height)
@ -147,7 +173,7 @@ func (p *Pipeline) SetCapsResolution(binName string, width, height int) bool {
p.logger.Debug().Msgf("setting caps resolution of %s to %dx%d", binName, width, height) p.logger.Debug().Msgf("setting caps resolution of %s to %dx%d", binName, width, height)
ok := C.gstreamer_pipeline_set_caps_resolution(p.Ctx, cBinName, cWidth, cHeight) ok := C.gstreamer_pipeline_set_caps_resolution(p.ctx, cBinName, cWidth, cHeight)
return ok == C.TRUE return ok == C.TRUE
} }
@ -175,7 +201,7 @@ func goHandlePipelineBuffer(buffer unsafe.Pointer, bufferLen C.int, duration C.i
pipelinesLock.Unlock() pipelinesLock.Unlock()
if ok { if ok {
pipeline.Sample <- types.Sample{ pipeline.sample <- types.Sample{
Data: C.GoBytes(buffer, bufferLen), Data: C.GoBytes(buffer, bufferLen),
Duration: time.Duration(duration), Duration: time.Duration(duration),
} }