HailoImageProcessor/src/gstreamer_functions.zig
2024-10-20 02:26:02 +00:00

65 lines
1.9 KiB
Zig

const std = @import("std");
const gst = @cImport({ // glib-object for g_object_* functions
@cInclude("glib-object.h");
@cInclude("gst.h");
@cInclude("gstappsrc.h");
@cInclude("glib.h"); // and glib for other g_* functions
});
pub const GstError = error {
EmptySample,
EmptyBuffer,
EmptyBufferMap,
};
pub fn get_buffer(app_sink: gst.GstElement) !*gst.GstBuffer {
const sample: *gst.GstSample = undefined;
var buffer: ?*gst.GetBuffer = undefined;
std.debug.print("pulling sample\n", .{});
sample = gst.gst_app_sink_pull_sample(@ptrCast(app_sink));
std.debug.print("Pulled appsink sample\n", .{});
const sample_info = gst.gst_sample_get_info(sample);
if (sample_info != null) {
const si_string = gst.gst_structure_to_string(sample_info);
std.debug.print("Sample info retrieved {s}\n", .{ si_string });
gst.g_free(si_string);
} else {
std.debug.print("Got sample with no info!\n", .{});
return GstError.EmptySample;
}
defer gst.gst_sample_unref(sample);
const s_caps: ?*gst.GstCaps = gst.gst_sample_get_caps(sample);
if (s_caps != null) {
const cap_str = gst.gst_caps_to_string(s_caps);
std.debug.print("Got sample with caps {s}\n", .{ cap_str });
gst.g_free(cap_str);
}
buffer = gst.gst_sample_get_buffer(sample);
std.debug.print("Got buffer from sample!\n", .{});
var map: *gst.GstMapInfo = undefined;
defer gst.gst_buffer_unmap(buffer, &map);
if (buffer == null || !gst.gst_buffer_map(buffer, &map, gst.GST_MAP_READ)) {
std.debug.print("Failed to get buffer or buffer map!\n", .{});
if (buffer == null) {
return GstError.EmptyBuffer;
} else {
return GstError.EmptyBufferMap;
}
}
const len = map.size;
std.debug.print("map size is: {d}\n", .{ len });
return buffer;
}