gstreamer add API.

This commit is contained in:
Miroslav Šedivý 2022-09-10 23:18:16 +02:00
parent 938ea38cd6
commit 0488976dd6
3 changed files with 99 additions and 2 deletions

View File

@ -163,3 +163,50 @@ void gstreamer_pipeline_push(GstPipelineCtx *ctx, void *buffer, int bufferLen) {
gst_app_src_push_buffer(GST_APP_SRC(ctx->appsrc), buffer); gst_app_src_push_buffer(GST_APP_SRC(ctx->appsrc), buffer);
} }
} }
gboolean gstreamer_pipeline_set_prop_int(GstPipelineCtx *ctx, char *binName, char *prop, gint value) {
GstElement *el = gst_bin_get_by_name(GST_BIN(ctx->pipeline), binName);
if (el == NULL) return FALSE;
g_object_set(G_OBJECT(el),
prop, value,
NULL);
gst_object_unref(el);
return TRUE;
}
gboolean gstreamer_pipeline_set_caps_framerate(GstPipelineCtx *ctx, const gchar* binName, gint numerator, gint denominator) {
GstElement *el = gst_bin_get_by_name(GST_BIN(ctx->pipeline), binName);
if (el == NULL) return FALSE;
GstCaps *caps = gst_caps_new_simple("video/x-raw",
"framerate", GST_TYPE_FRACTION, numerator, denominator,
NULL);
g_object_set(G_OBJECT(el),
"caps", caps,
NULL);
gst_caps_unref(caps);
gst_object_unref(el);
return TRUE;
}
gboolean gstreamer_pipeline_set_caps_resolution(GstPipelineCtx *ctx, const gchar* binName, gint width, gint height) {
GstElement *el = gst_bin_get_by_name(GST_BIN(ctx->pipeline), binName);
if (el == NULL) return FALSE;
GstCaps *caps = gst_caps_new_simple("video/x-raw",
"width", G_TYPE_INT, width,
"height", G_TYPE_INT, height,
NULL);
g_object_set(G_OBJECT(el),
"caps", caps,
NULL);
gst_caps_unref(caps);
gst_object_unref(el);
return TRUE;
}

View File

@ -21,6 +21,7 @@ import (
type Pipeline struct { type Pipeline struct {
id int id int
logger zerolog.Logger
Src string Src string
Ctx *C.GstPipelineCtx Ctx *C.GstPipelineCtx
Sample chan types.Sample Sample chan types.Sample
@ -56,7 +57,11 @@ func CreatePipeline(pipelineStr string) (*Pipeline, error) {
} }
p := &Pipeline{ p := &Pipeline{
id: int(id), id: int(id),
logger: log.With().
Str("module", "capture").
Str("submodule", "gstreamer").
Int("pipeline_id", int(id)).Logger(),
Src: pipelineStr, Src: pipelineStr,
Ctx: ctx, Ctx: ctx,
Sample: make(chan types.Sample), Sample: make(chan types.Sample),
@ -107,6 +112,47 @@ func (p *Pipeline) Push(buffer []byte) {
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 {
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)
ok := C.gstreamer_pipeline_set_prop_int(p.Ctx, cBinName, cProp, cValue)
return ok == C.TRUE
}
func (p *Pipeline) SetCapsFramerate(binName string, numerator, denominator int) bool {
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)
ok := C.gstreamer_pipeline_set_caps_framerate(p.Ctx, cBinName, cNumerator, cDenominator)
return ok == C.TRUE
}
func (p *Pipeline) SetCapsResolution(binName string, width, height int) bool {
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)
ok := C.gstreamer_pipeline_set_caps_resolution(p.Ctx, cBinName, cWidth, cHeight)
return ok == C.TRUE
}
// gst-inspect-1.0 // gst-inspect-1.0
func CheckPlugins(plugins []string) error { func CheckPlugins(plugins []string) error {
var plugin *C.GstPlugin var plugin *C.GstPlugin

View File

@ -22,5 +22,9 @@ void gstreamer_pipeline_pause(GstPipelineCtx *ctx);
void gstreamer_pipeline_destory(GstPipelineCtx *ctx); void gstreamer_pipeline_destory(GstPipelineCtx *ctx);
void gstreamer_pipeline_push(GstPipelineCtx *ctx, void *buffer, int bufferLen); void gstreamer_pipeline_push(GstPipelineCtx *ctx, void *buffer, int bufferLen);
gboolean gstreamer_pipeline_set_prop_int(GstPipelineCtx *ctx, char *binName, char *prop, gint value);
gboolean gstreamer_pipeline_set_caps_framerate(GstPipelineCtx *ctx, const gchar* binName, gint numerator, gint denominator);
gboolean gstreamer_pipeline_set_caps_resolution(GstPipelineCtx *ctx, const gchar* binName, gint width, gint height);
void gstreamer_init(void); void gstreamer_init(void);
void gstreamer_loop(void); void gstreamer_loop(void);