From f9fe303b50bb10510aa96b241c95455e2aa08866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sun, 5 Dec 2021 21:31:40 +0100 Subject: [PATCH] gst destroy remove. --- internal/capture/gst/gst.c | 1 - internal/capture/gst/gst.go | 16 ++++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/internal/capture/gst/gst.c b/internal/capture/gst/gst.c index 60e675c1..22cf1f97 100644 --- a/internal/capture/gst/gst.c +++ b/internal/capture/gst/gst.c @@ -131,7 +131,6 @@ void gstreamer_pipeline_pause(GstPipelineCtx *ctx) { void gstreamer_pipeline_destory(GstPipelineCtx *ctx) { gst_element_set_state(GST_ELEMENT(ctx->pipeline), GST_STATE_NULL); gst_object_unref(ctx->pipeline); - free(ctx); } void gstreamer_pipeline_push(GstPipelineCtx *ctx, char *srcName, void *buffer, int bufferLen) { diff --git a/internal/capture/gst/gst.go b/internal/capture/gst/gst.go index 158fa458..6d534e54 100644 --- a/internal/capture/gst/gst.go +++ b/internal/capture/gst/gst.go @@ -9,6 +9,7 @@ import "C" import ( "fmt" "sync" + "sync/atomic" "time" "unsafe" @@ -25,6 +26,7 @@ type Pipeline struct { Sample chan types.Sample } +var pSerial int32 var pipelines = make(map[int]*Pipeline) var pipelinesLock sync.Mutex var registry *C.GstRegistry @@ -37,14 +39,14 @@ func init() { } func CreatePipeline(pipelineStr string) (*Pipeline, error) { + id := atomic.AddInt32(&pSerial, 1) + pipelineStrUnsafe := C.CString(pipelineStr) defer C.free(unsafe.Pointer(pipelineStrUnsafe)) pipelinesLock.Lock() defer pipelinesLock.Unlock() - id := len(pipelines) - var gstError *C.GError ctx := C.gstreamer_pipeline_create(pipelineStrUnsafe, C.int(id), &gstError) @@ -54,7 +56,7 @@ func CreatePipeline(pipelineStr string) (*Pipeline, error) { } p := &Pipeline{ - id: id, + id: int(id), Src: pipelineStr, Ctx: ctx, Sample: make(chan types.Sample), @@ -81,8 +83,14 @@ func (p *Pipeline) Pause() { func (p *Pipeline) Destroy() { C.gstreamer_pipeline_destory(p.Ctx) - p.Ctx = nil + + pipelinesLock.Lock() + delete(pipelines, p.id) + pipelinesLock.Unlock() + close(p.Sample) + C.free(unsafe.Pointer(p.Ctx)) + p = nil } func (p *Pipeline) Push(srcName string, buffer []byte) {