2020-10-23 03:54:50 +13:00
|
|
|
#include "gst.h"
|
|
|
|
|
2021-12-06 06:16:26 +13:00
|
|
|
static void gstreamer_pipeline_log(GstPipelineCtx *ctx, char* level, const char* format, ...) {
|
2021-12-06 05:52:25 +13:00
|
|
|
va_list argptr;
|
|
|
|
va_start(argptr, format);
|
|
|
|
char buffer[100];
|
|
|
|
vsprintf(buffer, format, argptr);
|
|
|
|
va_end(argptr);
|
2021-12-06 06:16:26 +13:00
|
|
|
goPipelineLog(level, buffer, ctx->pipelineId);
|
2021-12-06 05:52:25 +13:00
|
|
|
}
|
|
|
|
|
2021-12-06 06:16:26 +13:00
|
|
|
static gboolean gstreamer_bus_call(GstBus *bus, GstMessage *msg, gpointer user_data) {
|
|
|
|
GstPipelineCtx *ctx = (GstPipelineCtx *)user_data;
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
switch (GST_MESSAGE_TYPE(msg)) {
|
2021-12-02 07:55:57 +13:00
|
|
|
case GST_MESSAGE_EOS: {
|
2021-12-06 06:16:26 +13:00
|
|
|
gstreamer_pipeline_log(ctx, "fatal", "end of stream");
|
|
|
|
break;
|
2021-12-02 07:55:57 +13:00
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-12-06 05:52:25 +13:00
|
|
|
case GST_MESSAGE_STATE_CHANGED: {
|
|
|
|
GstState old_state, new_state;
|
|
|
|
gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
|
|
|
|
|
2021-12-06 06:16:26 +13:00
|
|
|
gstreamer_pipeline_log(ctx, "debug",
|
2021-12-06 05:52:25 +13:00
|
|
|
"element %s changed state from %s to %s",
|
|
|
|
GST_OBJECT_NAME(msg->src),
|
|
|
|
gst_element_state_get_name(old_state),
|
|
|
|
gst_element_state_get_name(new_state));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GST_MESSAGE_TAG: {
|
|
|
|
GstTagList *tags = NULL;
|
|
|
|
gst_message_parse_tag(msg, &tags);
|
|
|
|
|
2021-12-06 06:16:26 +13:00
|
|
|
gstreamer_pipeline_log(ctx, "debug",
|
2021-12-06 05:52:25 +13:00
|
|
|
"got tags from element %s",
|
|
|
|
GST_OBJECT_NAME(msg->src));
|
2022-09-11 09:18:16 +12:00
|
|
|
|
2021-12-06 05:52:25 +13:00
|
|
|
gst_tag_list_unref(tags);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-12-02 07:55:57 +13:00
|
|
|
case GST_MESSAGE_ERROR: {
|
2021-12-06 05:52:25 +13:00
|
|
|
GError *err = NULL;
|
|
|
|
gchar *dbg_info = NULL;
|
|
|
|
gst_message_parse_error(msg, &err, &dbg_info);
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-12-06 06:16:26 +13:00
|
|
|
gstreamer_pipeline_log(ctx, "error",
|
2021-12-06 05:52:25 +13:00
|
|
|
"error from element %s: %s",
|
|
|
|
GST_OBJECT_NAME(msg->src), err->message);
|
2021-12-06 06:16:26 +13:00
|
|
|
gstreamer_pipeline_log(ctx, "warn",
|
2021-12-06 05:52:25 +13:00
|
|
|
"debugging info: %s",
|
|
|
|
(dbg_info) ? dbg_info : "none");
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-12-06 05:52:25 +13:00
|
|
|
g_error_free(err);
|
|
|
|
g_free(dbg_info);
|
|
|
|
break;
|
2021-12-02 07:55:57 +13:00
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-12-02 07:55:57 +13:00
|
|
|
default:
|
2021-12-06 06:16:26 +13:00
|
|
|
gstreamer_pipeline_log(ctx, "trace", "unknown message");
|
2021-12-02 07:55:57 +13:00
|
|
|
break;
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-12-06 06:16:26 +13:00
|
|
|
GstPipelineCtx *gstreamer_pipeline_create(char *pipelineStr, int pipelineId, GError **error) {
|
|
|
|
GstElement *pipeline = gst_parse_launch(pipelineStr, error);
|
|
|
|
if (pipeline == NULL) return NULL;
|
|
|
|
|
|
|
|
// create gstreamer pipeline context
|
|
|
|
GstPipelineCtx *ctx = calloc(1, sizeof(GstPipelineCtx));
|
|
|
|
ctx->pipelineId = pipelineId;
|
|
|
|
ctx->pipeline = pipeline;
|
|
|
|
|
|
|
|
GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
|
|
|
|
gst_bus_add_watch(bus, gstreamer_bus_call, ctx);
|
|
|
|
gst_object_unref(bus);
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2021-11-29 10:19:06 +13:00
|
|
|
static GstFlowReturn gstreamer_send_new_sample_handler(GstElement *object, gpointer user_data) {
|
2021-12-06 06:16:26 +13:00
|
|
|
GstPipelineCtx *ctx = (GstPipelineCtx *)user_data;
|
2020-10-23 03:54:50 +13:00
|
|
|
GstSample *sample = NULL;
|
|
|
|
GstBuffer *buffer = NULL;
|
|
|
|
gpointer copy = NULL;
|
|
|
|
gsize copy_size = 0;
|
|
|
|
|
2020-10-30 06:07:04 +13:00
|
|
|
g_signal_emit_by_name(object, "pull-sample", &sample);
|
2020-10-23 03:54:50 +13:00
|
|
|
if (sample) {
|
|
|
|
buffer = gst_sample_get_buffer(sample);
|
|
|
|
if (buffer) {
|
|
|
|
gst_buffer_extract_dup(buffer, 0, gst_buffer_get_size(buffer), ©, ©_size);
|
2021-12-06 06:16:26 +13:00
|
|
|
goHandlePipelineBuffer(copy, copy_size, GST_BUFFER_DURATION(buffer), ctx->pipelineId);
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
2020-10-30 06:07:04 +13:00
|
|
|
gst_sample_unref(sample);
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-06 06:16:26 +13:00
|
|
|
void gstreamer_pipeline_attach_appsink(GstPipelineCtx *ctx, char *sinkName) {
|
2021-12-06 10:06:42 +13:00
|
|
|
ctx->appsink = gst_bin_get_by_name(GST_BIN(ctx->pipeline), sinkName);
|
|
|
|
g_object_set(ctx->appsink, "emit-signals", TRUE, NULL);
|
|
|
|
g_signal_connect(ctx->appsink, "new-sample", G_CALLBACK(gstreamer_send_new_sample_handler), ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gstreamer_pipeline_attach_appsrc(GstPipelineCtx *ctx, char *srcName) {
|
|
|
|
ctx->appsrc = gst_bin_get_by_name(GST_BIN(ctx->pipeline), srcName);
|
2021-11-29 10:19:06 +13:00
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-12-06 06:16:26 +13:00
|
|
|
void gstreamer_pipeline_play(GstPipelineCtx *ctx) {
|
|
|
|
gst_element_set_state(GST_ELEMENT(ctx->pipeline), GST_STATE_PLAYING);
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-12-06 06:16:26 +13:00
|
|
|
void gstreamer_pipeline_pause(GstPipelineCtx *ctx) {
|
|
|
|
gst_element_set_state(GST_ELEMENT(ctx->pipeline), GST_STATE_PAUSED);
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-12-06 06:16:26 +13:00
|
|
|
void gstreamer_pipeline_destory(GstPipelineCtx *ctx) {
|
2021-12-06 10:06:42 +13:00
|
|
|
// end appsrc, if exists
|
|
|
|
if (ctx->appsrc) {
|
|
|
|
gst_app_src_end_of_stream(GST_APP_SRC(ctx->appsrc));
|
|
|
|
}
|
|
|
|
|
|
|
|
// send pipeline eos
|
|
|
|
gst_element_send_event(GST_ELEMENT(ctx->pipeline), gst_event_new_eos());
|
|
|
|
|
|
|
|
// set null state
|
2021-12-06 06:16:26 +13:00
|
|
|
gst_element_set_state(GST_ELEMENT(ctx->pipeline), GST_STATE_NULL);
|
2021-12-06 10:06:42 +13:00
|
|
|
|
|
|
|
if (ctx->appsink) {
|
|
|
|
gst_object_unref(ctx->appsink);
|
|
|
|
ctx->appsink = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->appsrc) {
|
|
|
|
gst_object_unref(ctx->appsrc);
|
|
|
|
ctx->appsrc = NULL;
|
|
|
|
}
|
|
|
|
|
2021-12-06 06:16:26 +13:00
|
|
|
gst_object_unref(ctx->pipeline);
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
2021-11-29 10:37:17 +13:00
|
|
|
|
2021-12-06 10:06:42 +13:00
|
|
|
void gstreamer_pipeline_push(GstPipelineCtx *ctx, void *buffer, int bufferLen) {
|
|
|
|
if (ctx->appsrc != NULL) {
|
2021-11-29 10:37:17 +13:00
|
|
|
gpointer p = g_memdup(buffer, bufferLen);
|
|
|
|
GstBuffer *buffer = gst_buffer_new_wrapped(p, bufferLen);
|
2021-12-06 10:06:42 +13:00
|
|
|
gst_app_src_push_buffer(GST_APP_SRC(ctx->appsrc), buffer);
|
2021-11-29 10:37:17 +13:00
|
|
|
}
|
|
|
|
}
|
2022-09-11 09:18:16 +12:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|