mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
refactor capture with broadcast.
This commit is contained in:
@ -1,83 +1,202 @@
|
||||
#include "gst.h"
|
||||
|
||||
typedef struct SampleHandlerUserData {
|
||||
int pipelineId;
|
||||
} SampleHandlerUserData;
|
||||
|
||||
void gstreamer_init(void) {
|
||||
gst_init(NULL, NULL);
|
||||
static void gstreamer_pipeline_log(GstPipelineCtx *ctx, 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, ctx->pipelineId);
|
||||
}
|
||||
|
||||
static gboolean gstreamer_send_bus_call(GstBus *bus, GstMessage *msg, gpointer data) {
|
||||
static gboolean gstreamer_bus_call(GstBus *bus, GstMessage *msg, gpointer user_data) {
|
||||
GstPipelineCtx *ctx = (GstPipelineCtx *)user_data;
|
||||
|
||||
switch (GST_MESSAGE_TYPE(msg)) {
|
||||
case GST_MESSAGE_EOS: {
|
||||
gstreamer_pipeline_log(ctx, "fatal", "end of stream");
|
||||
break;
|
||||
}
|
||||
|
||||
case GST_MESSAGE_EOS:
|
||||
g_print("End of stream\n");
|
||||
exit(1);
|
||||
break;
|
||||
case GST_MESSAGE_STATE_CHANGED: {
|
||||
GstState old_state, new_state;
|
||||
gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
|
||||
|
||||
case GST_MESSAGE_ERROR: {
|
||||
gchar *debug;
|
||||
GError *error;
|
||||
gstreamer_pipeline_log(ctx, "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;
|
||||
}
|
||||
|
||||
gst_message_parse_error(msg, &error, &debug);
|
||||
g_free(debug);
|
||||
case GST_MESSAGE_TAG: {
|
||||
GstTagList *tags = NULL;
|
||||
gst_message_parse_tag(msg, &tags);
|
||||
|
||||
g_printerr("Error: %s\n", error->message);
|
||||
g_error_free(error);
|
||||
exit(1);
|
||||
}
|
||||
default:
|
||||
break;
|
||||
gstreamer_pipeline_log(ctx, "debug",
|
||||
"got tags from element %s",
|
||||
GST_OBJECT_NAME(msg->src));
|
||||
|
||||
gst_tag_list_unref(tags);
|
||||
break;
|
||||
}
|
||||
|
||||
case GST_MESSAGE_ERROR: {
|
||||
GError *err = NULL;
|
||||
gchar *dbg_info = NULL;
|
||||
gst_message_parse_error(msg, &err, &dbg_info);
|
||||
|
||||
gstreamer_pipeline_log(ctx, "error",
|
||||
"error from element %s: %s",
|
||||
GST_OBJECT_NAME(msg->src), err->message);
|
||||
gstreamer_pipeline_log(ctx, "warn",
|
||||
"debugging info: %s",
|
||||
(dbg_info) ? dbg_info : "none");
|
||||
|
||||
g_error_free(err);
|
||||
g_free(dbg_info);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
gstreamer_pipeline_log(ctx, "trace", "unknown message");
|
||||
break;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
GstFlowReturn gstreamer_send_new_sample_handler(GstElement *object, gpointer user_data) {
|
||||
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;
|
||||
}
|
||||
|
||||
static GstFlowReturn gstreamer_send_new_sample_handler(GstElement *object, gpointer user_data) {
|
||||
GstPipelineCtx *ctx = (GstPipelineCtx *)user_data;
|
||||
GstSample *sample = NULL;
|
||||
GstBuffer *buffer = NULL;
|
||||
gpointer copy = NULL;
|
||||
gsize copy_size = 0;
|
||||
SampleHandlerUserData *s = (SampleHandlerUserData *)user_data;
|
||||
|
||||
g_signal_emit_by_name (object, "pull-sample", &sample);
|
||||
g_signal_emit_by_name(object, "pull-sample", &sample);
|
||||
if (sample) {
|
||||
buffer = gst_sample_get_buffer(sample);
|
||||
if (buffer) {
|
||||
gst_buffer_extract_dup(buffer, 0, gst_buffer_get_size(buffer), ©, ©_size);
|
||||
goHandlePipelineBuffer(copy, copy_size, GST_BUFFER_DURATION(buffer), s->pipelineId);
|
||||
goHandlePipelineBuffer(copy, copy_size, GST_BUFFER_DURATION(buffer), ctx->pipelineId);
|
||||
}
|
||||
gst_sample_unref (sample);
|
||||
gst_sample_unref(sample);
|
||||
}
|
||||
|
||||
return GST_FLOW_OK;
|
||||
}
|
||||
|
||||
GstElement *gstreamer_send_create_pipeline(char *pipeline, GError **error) {
|
||||
return gst_parse_launch(pipeline, error);
|
||||
void gstreamer_pipeline_attach_appsink(GstPipelineCtx *ctx, char *sinkName) {
|
||||
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_send_start_pipeline(GstElement *pipeline, 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");
|
||||
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);
|
||||
|
||||
gst_element_set_state(pipeline, GST_STATE_PLAYING);
|
||||
void gstreamer_pipeline_attach_appsrc(GstPipelineCtx *ctx, char *srcName) {
|
||||
ctx->appsrc = gst_bin_get_by_name(GST_BIN(ctx->pipeline), srcName);
|
||||
}
|
||||
|
||||
void gstreamer_send_play_pipeline(GstElement *pipeline) {
|
||||
gst_element_set_state(pipeline, GST_STATE_PLAYING);
|
||||
void gstreamer_pipeline_play(GstPipelineCtx *ctx) {
|
||||
gst_element_set_state(GST_ELEMENT(ctx->pipeline), GST_STATE_PLAYING);
|
||||
}
|
||||
|
||||
void gstreamer_send_stop_pipeline(GstElement *pipeline) {
|
||||
gst_element_set_state(pipeline, GST_STATE_NULL);
|
||||
void gstreamer_pipeline_pause(GstPipelineCtx *ctx) {
|
||||
gst_element_set_state(GST_ELEMENT(ctx->pipeline), GST_STATE_PAUSED);
|
||||
}
|
||||
|
||||
void gstreamer_pipeline_destory(GstPipelineCtx *ctx) {
|
||||
// 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
|
||||
gst_element_set_state(GST_ELEMENT(ctx->pipeline), GST_STATE_NULL);
|
||||
|
||||
if (ctx->appsink) {
|
||||
gst_object_unref(ctx->appsink);
|
||||
ctx->appsink = NULL;
|
||||
}
|
||||
|
||||
if (ctx->appsrc) {
|
||||
gst_object_unref(ctx->appsrc);
|
||||
ctx->appsrc = NULL;
|
||||
}
|
||||
|
||||
gst_object_unref(ctx->pipeline);
|
||||
}
|
||||
|
||||
void gstreamer_pipeline_push(GstPipelineCtx *ctx, void *buffer, int bufferLen) {
|
||||
if (ctx->appsrc != NULL) {
|
||||
gpointer p = g_memdup(buffer, bufferLen);
|
||||
GstBuffer *buffer = gst_buffer_new_wrapped(p, bufferLen);
|
||||
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;
|
||||
}
|
||||
|
@ -9,68 +9,146 @@ import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"m1k1o/neko/internal/types"
|
||||
)
|
||||
|
||||
// Pipeline is a wrapper for a GStreamer Pipeline
|
||||
type Pipeline struct {
|
||||
Pipeline *C.GstElement
|
||||
Sample chan types.Sample
|
||||
Src string
|
||||
id int
|
||||
id int
|
||||
logger zerolog.Logger
|
||||
Src string
|
||||
Ctx *C.GstPipelineCtx
|
||||
Sample chan types.Sample
|
||||
}
|
||||
|
||||
var pSerial int32
|
||||
var pipelines = make(map[int]*Pipeline)
|
||||
var pipelinesLock sync.Mutex
|
||||
var registry *C.GstRegistry
|
||||
|
||||
func init() {
|
||||
C.gstreamer_init()
|
||||
C.gst_init(nil, nil)
|
||||
registry = C.gst_registry_get()
|
||||
}
|
||||
|
||||
// CreatePipeline creates a GStreamer Pipeline
|
||||
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()
|
||||
|
||||
var err *C.GError
|
||||
gstPipeline := C.gstreamer_send_create_pipeline(pipelineStrUnsafe, &err)
|
||||
if err != nil {
|
||||
defer C.g_error_free(err)
|
||||
return nil, fmt.Errorf("%s", C.GoString(err.message))
|
||||
var gstError *C.GError
|
||||
ctx := C.gstreamer_pipeline_create(pipelineStrUnsafe, C.int(id), &gstError)
|
||||
|
||||
if gstError != nil {
|
||||
defer C.g_error_free(gstError)
|
||||
return nil, fmt.Errorf("(pipeline error) %s", C.GoString(gstError.message))
|
||||
}
|
||||
|
||||
p := &Pipeline{
|
||||
Pipeline: gstPipeline,
|
||||
Sample: make(chan types.Sample),
|
||||
Src: pipelineStr,
|
||||
id: len(pipelines),
|
||||
id: int(id),
|
||||
logger: log.With().
|
||||
Str("module", "capture").
|
||||
Str("submodule", "gstreamer").
|
||||
Int("pipeline_id", int(id)).Logger(),
|
||||
Src: pipelineStr,
|
||||
Ctx: ctx,
|
||||
Sample: make(chan types.Sample),
|
||||
}
|
||||
|
||||
pipelines[p.id] = p
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Start starts the GStreamer Pipeline
|
||||
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.Ctx, sinkNameUnsafe)
|
||||
}
|
||||
|
||||
func (p *Pipeline) AttachAppsrc(srcName string) {
|
||||
srcNameUnsafe := C.CString(srcName)
|
||||
defer C.free(unsafe.Pointer(srcNameUnsafe))
|
||||
|
||||
C.gstreamer_pipeline_attach_appsrc(p.Ctx, srcNameUnsafe)
|
||||
}
|
||||
|
||||
// Play starts the GStreamer Pipeline
|
||||
func (p *Pipeline) Play() {
|
||||
C.gstreamer_send_play_pipeline(p.Pipeline)
|
||||
C.gstreamer_pipeline_play(p.Ctx)
|
||||
}
|
||||
|
||||
// Stop stops the GStreamer Pipeline
|
||||
func (p *Pipeline) Stop() {
|
||||
C.gstreamer_send_stop_pipeline(p.Pipeline)
|
||||
func (p *Pipeline) Pause() {
|
||||
C.gstreamer_pipeline_pause(p.Ctx)
|
||||
}
|
||||
|
||||
func (p *Pipeline) Destroy() {
|
||||
C.gstreamer_pipeline_destory(p.Ctx)
|
||||
|
||||
pipelinesLock.Lock()
|
||||
delete(pipelines, p.id)
|
||||
pipelinesLock.Unlock()
|
||||
|
||||
close(p.Sample)
|
||||
C.free(unsafe.Pointer(p.Ctx))
|
||||
p = nil
|
||||
}
|
||||
|
||||
func (p *Pipeline) Push(buffer []byte) {
|
||||
bytes := C.CBytes(buffer)
|
||||
defer C.free(bytes)
|
||||
|
||||
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
|
||||
@ -90,14 +168,35 @@ func CheckPlugins(plugins []string) error {
|
||||
|
||||
//export goHandlePipelineBuffer
|
||||
func goHandlePipelineBuffer(buffer unsafe.Pointer, bufferLen C.int, duration C.int, pipelineID C.int) {
|
||||
defer C.free(buffer)
|
||||
|
||||
pipelinesLock.Lock()
|
||||
pipeline, ok := pipelines[int(pipelineID)]
|
||||
pipelinesLock.Unlock()
|
||||
|
||||
if ok {
|
||||
pipeline.Sample <- types.Sample{Data: C.GoBytes(buffer, bufferLen), Timestamp: time.Now(), Duration: time.Duration(duration)}
|
||||
pipeline.Sample <- types.Sample{
|
||||
Data: C.GoBytes(buffer, bufferLen),
|
||||
Duration: time.Duration(duration),
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("discarding buffer, no pipeline with id %d", int(pipelineID))
|
||||
log.Warn().
|
||||
Str("module", "capture").
|
||||
Str("submodule", "gstreamer").
|
||||
Int("pipeline_id", int(pipelineID)).
|
||||
Msgf("discarding sample, pipeline not found")
|
||||
}
|
||||
C.free(buffer)
|
||||
}
|
||||
|
||||
//export goPipelineLog
|
||||
func goPipelineLog(levelUnsafe *C.char, msgUnsafe *C.char, pipelineID C.int) {
|
||||
levelStr := C.GoString(levelUnsafe)
|
||||
msg := C.GoString(msgUnsafe)
|
||||
|
||||
level, _ := zerolog.ParseLevel(levelStr)
|
||||
log.WithLevel(level).
|
||||
Str("module", "capture").
|
||||
Str("submodule", "gstreamer").
|
||||
Int("pipeline_id", int(pipelineID)).
|
||||
Msg(msg)
|
||||
}
|
||||
|
@ -1,13 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <gst/gst.h>
|
||||
#include <gst/app/gstappsrc.h>
|
||||
|
||||
typedef struct GstPipelineCtx {
|
||||
int pipelineId;
|
||||
GstElement *pipeline;
|
||||
GstElement *appsink;
|
||||
GstElement *appsrc;
|
||||
} GstPipelineCtx;
|
||||
|
||||
extern void goHandlePipelineBuffer(void *buffer, int bufferLen, int samples, int pipelineId);
|
||||
extern void goPipelineLog(char *level, char *msg, int pipelineId);
|
||||
|
||||
GstElement *gstreamer_send_create_pipeline(char *pipeline, GError **error);
|
||||
GstPipelineCtx *gstreamer_pipeline_create(char *pipelineStr, int pipelineId, GError **error);
|
||||
void gstreamer_pipeline_attach_appsink(GstPipelineCtx *ctx, char *sinkName);
|
||||
void gstreamer_pipeline_attach_appsrc(GstPipelineCtx *ctx, char *srcName);
|
||||
void gstreamer_pipeline_play(GstPipelineCtx *ctx);
|
||||
void gstreamer_pipeline_pause(GstPipelineCtx *ctx);
|
||||
void gstreamer_pipeline_destory(GstPipelineCtx *ctx);
|
||||
void gstreamer_pipeline_push(GstPipelineCtx *ctx, void *buffer, int bufferLen);
|
||||
|
||||
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_init(void);
|
||||
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);
|
||||
|
Reference in New Issue
Block a user