add zerolog to gst.

This commit is contained in:
Miroslav Šedivý 2021-12-05 17:52:25 +01:00
parent 09cb1e9c6a
commit 7c3a24a613
3 changed files with 77 additions and 9 deletions

View File

@ -8,26 +8,71 @@ void gstreamer_init(void) {
gst_init(NULL, NULL); gst_init(NULL, NULL);
} }
GMainLoop *gstreamer_main_loop = NULL;
void gstreamer_loop(void) {
gstreamer_main_loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run(gstreamer_main_loop);
}
static void gstreamer_pipeline_log(char* level, const char* format, ...) {
va_list argptr;
va_start(argptr, format);
char buffer[100];
vsprintf(buffer, format, argptr);
va_end(argptr);
goPipelineLog(level, buffer);
}
static gboolean gstreamer_bus_call(GstBus *bus, GstMessage *msg, gpointer data) { static gboolean gstreamer_bus_call(GstBus *bus, GstMessage *msg, gpointer data) {
switch (GST_MESSAGE_TYPE(msg)) { switch (GST_MESSAGE_TYPE(msg)) {
case GST_MESSAGE_EOS: { case GST_MESSAGE_EOS: {
g_print("End of stream\n"); gstreamer_pipeline_log("panic", "end of stream");
exit(1); exit(1);
} }
case GST_MESSAGE_STATE_CHANGED: {
GstState old_state, new_state;
gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
gstreamer_pipeline_log("debug",
"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);
gstreamer_pipeline_log("debug",
"got tags from element %s",
GST_OBJECT_NAME(msg->src));
gst_tag_list_unref(tags);
break;
}
case GST_MESSAGE_ERROR: { case GST_MESSAGE_ERROR: {
gchar *debug; GError *err = NULL;
GError *error; gchar *dbg_info = NULL;
gst_message_parse_error(msg, &err, &dbg_info);
gst_message_parse_error(msg, &error, &debug); gstreamer_pipeline_log("error",
g_free(debug); "error from element %s: %s",
GST_OBJECT_NAME(msg->src), err->message);
gstreamer_pipeline_log("warn",
"debugging info: %s",
(dbg_info) ? dbg_info : "none");
g_printerr("Error: %s\n", error->message); g_error_free(err);
g_error_free(error); g_free(dbg_info);
exit(1); break;
} }
default: default:
gstreamer_pipeline_log("trace", "unknown message");
break; break;
} }

View File

@ -13,6 +13,9 @@ import (
"unsafe" "unsafe"
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
) )
type Pipeline struct { type Pipeline struct {
@ -28,6 +31,8 @@ var registry *C.GstRegistry
func init() { func init() {
C.gstreamer_init() C.gstreamer_init()
go C.gstreamer_loop()
registry = C.gst_registry_get() registry = C.gst_registry_get()
} }
@ -113,6 +118,21 @@ func goHandlePipelineBuffer(buffer unsafe.Pointer, bufferLen C.int, duration C.i
Duration: time.Duration(duration), Duration: time.Duration(duration),
} }
} else { } else {
fmt.Printf("discarding buffer, no pipeline with id %d", int(pipelineID)) log.Warn().
Str("module", "capture").
Str("submodule", "gstreamer").
Msgf("discarding buffer, no pipeline with id %d", int(pipelineID))
} }
} }
//export goPipelineLog
func goPipelineLog(levelUnsafe *C.char, msgUnsafe *C.char) {
levelStr := C.GoString(levelUnsafe)
msg := C.GoString(msgUnsafe)
level, _ := zerolog.ParseLevel(levelStr)
log.WithLevel(level).
Str("module", "capture").
Str("submodule", "gstreamer").
Msg(msg)
}

View File

@ -1,9 +1,11 @@
#pragma once #pragma once
#include <stdio.h>
#include <gst/gst.h> #include <gst/gst.h>
#include <gst/app/gstappsrc.h> #include <gst/app/gstappsrc.h>
extern void goHandlePipelineBuffer(void *buffer, int bufferLen, int samples, int pipelineId); extern void goHandlePipelineBuffer(void *buffer, int bufferLen, int samples, int pipelineId);
extern void goPipelineLog(char *level, char *msg);
void gstreamer_pipeline_attach_appsink(GstElement *pipeline, char *sinkName, int pipelineId); void gstreamer_pipeline_attach_appsink(GstElement *pipeline, char *sinkName, int pipelineId);
GstElement *gstreamer_pipeline_create(char *pipelineStr, GError **error); GstElement *gstreamer_pipeline_create(char *pipelineStr, GError **error);
@ -12,3 +14,4 @@ void gstreamer_pipeline_stop(GstElement *pipeline);
void gstreamer_pipeline_push(GstElement *pipeline, char *srcName, void *buffer, int bufferLen); void gstreamer_pipeline_push(GstElement *pipeline, char *srcName, void *buffer, int bufferLen);
void gstreamer_init(void); void gstreamer_init(void);
void gstreamer_loop(void);