From 710d0c9cd0a925d4ebf27a165121efd02b04f1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sun, 28 Nov 2021 22:19:06 +0100 Subject: [PATCH] gst extract appsink. --- internal/capture/gst/gst.c | 37 ++++++++++++++++------------------ internal/capture/gst/gst.go | 13 +++++++----- internal/capture/gst/gst.h | 9 +++++---- internal/capture/screencast.go | 3 ++- internal/capture/stream.go | 3 ++- 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/internal/capture/gst/gst.c b/internal/capture/gst/gst.c index a25f0123..1407d57f 100644 --- a/internal/capture/gst/gst.c +++ b/internal/capture/gst/gst.c @@ -8,14 +8,7 @@ void gstreamer_init(void) { gst_init(NULL, NULL); } -GMainLoop *gstreamer_send_main_loop = NULL; -void gstreamer_send_start_mainloop(void) { - gstreamer_send_main_loop = g_main_loop_new(NULL, FALSE); - - g_main_loop_run(gstreamer_send_main_loop); -} - -static gboolean gstreamer_send_bus_call(GstBus *bus, GstMessage *msg, gpointer data) { +static gboolean gstreamer_bus_call(GstBus *bus, GstMessage *msg, gpointer data) { switch (GST_MESSAGE_TYPE(msg)) { case GST_MESSAGE_EOS: @@ -41,7 +34,7 @@ static gboolean gstreamer_send_bus_call(GstBus *bus, GstMessage *msg, gpointer d return TRUE; } -GstFlowReturn gstreamer_send_new_sample_handler(GstElement *object, gpointer user_data) { +static GstFlowReturn gstreamer_send_new_sample_handler(GstElement *object, gpointer user_data) { GstSample *sample = NULL; GstBuffer *buffer = NULL; gpointer copy = NULL; @@ -61,27 +54,31 @@ GstFlowReturn gstreamer_send_new_sample_handler(GstElement *object, gpointer use return GST_FLOW_OK; } -void gstreamer_send_start_pipeline(GstElement *pipeline, int pipelineId) { +void gstreamer_pipeline_attach_appsink(GstElement *pipeline, char *sinkName, int pipelineId) { SampleHandlerUserData *s = calloc(1, sizeof(SampleHandlerUserData)); s->pipelineId = pipelineId; - GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); - gst_bus_add_watch(bus, gstreamer_send_bus_call, NULL); - gst_object_unref(bus); - - GstElement *appsink = gst_bin_get_by_name(GST_BIN(pipeline), "appsink"); + GstElement *appsink = gst_bin_get_by_name(GST_BIN(pipeline), sinkName); g_object_set(appsink, "emit-signals", TRUE, NULL); g_signal_connect(appsink, "new-sample", G_CALLBACK(gstreamer_send_new_sample_handler), s); gst_object_unref(appsink); +} +GstElement *gstreamer_pipeline_create(char *pipelineStr, GError **error) { + GstElement *pipeline = gst_parse_launch(pipelineStr, error); + + GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); + gst_bus_add_watch(bus, gstreamer_bus_call, NULL); + gst_object_unref(bus); + + return pipeline; +} + +void gstreamer_pipeline_play(GstElement *pipeline) { gst_element_set_state(pipeline, GST_STATE_PLAYING); } -void gstreamer_send_play_pipeline(GstElement *pipeline) { - gst_element_set_state(pipeline, GST_STATE_PLAYING); -} - -void gstreamer_send_stop_pipeline(GstElement *pipeline) { +void gstreamer_pipeline_stop(GstElement *pipeline) { gst_element_set_state(pipeline, GST_STATE_NULL); gst_object_unref(pipeline); } diff --git a/internal/capture/gst/gst.go b/internal/capture/gst/gst.go index a7c0683b..5eb926f7 100644 --- a/internal/capture/gst/gst.go +++ b/internal/capture/gst/gst.go @@ -41,7 +41,7 @@ func CreatePipeline(pipelineStr string) (*Pipeline, error) { var gstPipeline *C.GstElement var gstError *C.GError - gstPipeline = C.gst_parse_launch(pipelineStrUnsafe, &gstError) + gstPipeline = C.gstreamer_pipeline_create(pipelineStrUnsafe, &gstError) if gstError != nil { defer C.g_error_free(gstError) @@ -59,16 +59,19 @@ func CreatePipeline(pipelineStr string) (*Pipeline, error) { return p, nil } -func (p *Pipeline) Start() { - C.gstreamer_send_start_pipeline(p.Pipeline, C.int(p.id)) +func (p *Pipeline) AttachAppsink(sinkName string) { + sinkNameUnsafe := C.CString(sinkName) + defer C.free(unsafe.Pointer(sinkNameUnsafe)) + + C.gstreamer_pipeline_attach_appsink(p.Pipeline, sinkNameUnsafe, C.int(p.id)) } func (p *Pipeline) Play() { - C.gstreamer_send_play_pipeline(p.Pipeline) + C.gstreamer_pipeline_play(p.Pipeline) } func (p *Pipeline) Stop() { - C.gstreamer_send_stop_pipeline(p.Pipeline) + C.gstreamer_pipeline_stop(p.Pipeline) } // gst-inspect-1.0 diff --git a/internal/capture/gst/gst.h b/internal/capture/gst/gst.h index 55037f18..87e4fa74 100644 --- a/internal/capture/gst/gst.h +++ b/internal/capture/gst/gst.h @@ -5,8 +5,9 @@ extern void goHandlePipelineBuffer(void *buffer, int bufferLen, int samples, int pipelineId); -void gstreamer_send_start_pipeline(GstElement *pipeline, int pipelineId); -void gstreamer_send_play_pipeline(GstElement *pipeline); -void gstreamer_send_stop_pipeline(GstElement *pipeline); -void gstreamer_send_start_mainloop(void); +void gstreamer_pipeline_attach_appsink(GstElement *pipeline, char *sinkName, int pipelineId); +GstElement *gstreamer_pipeline_create(char *pipelineStr, GError **error); +void gstreamer_pipeline_play(GstElement *pipeline); +void gstreamer_pipeline_stop(GstElement *pipeline); + void gstreamer_init(void); diff --git a/internal/capture/screencast.go b/internal/capture/screencast.go index 4583c240..528ac83b 100644 --- a/internal/capture/screencast.go +++ b/internal/capture/screencast.go @@ -170,7 +170,8 @@ func (manager *ScreencastManagerCtx) createPipeline() error { return err } - manager.pipeline.Start() + manager.pipeline.AttachAppsink("appsink") + manager.pipeline.Play() manager.sample = manager.pipeline.Sample manager.sampleUpdate <- struct{}{} diff --git a/internal/capture/stream.go b/internal/capture/stream.go index a6450eed..3272abf1 100644 --- a/internal/capture/stream.go +++ b/internal/capture/stream.go @@ -247,7 +247,8 @@ func (manager *StreamManagerCtx) createPipeline() error { return err } - manager.pipeline.Start() + manager.pipeline.AttachAppsink("appsink") + manager.pipeline.Play() manager.sample = manager.pipeline.Sample manager.sampleUpdate <- struct{}{}