back to gstreamer seg faults

This commit is contained in:
Nickiel 2024-10-31 17:54:46 -07:00
parent 6bfd3c9c3f
commit d64095dd7f

View file

@ -1,9 +1,19 @@
const std = @import("std"); const std = @import("std");
const hlo = @cImport({ const hlo = @cImport({
@cInclude("hailort.h"); @cInclude("hailort.h");
}); });
// remove "sources/" and "X.zig"
// const gst = @import("sources/gst1.zig");
// const gstapp = @import ("sources/gstapp1.zig");
// const glib = @import("sources/glib2.zig");
// const gobject = @import("sources/gobject2.zig");
const gst = @import("gst");
const gstapp = @import ("gstapp");
const glib = @import("glib");
const gobject = @import("gobject");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const assert = std.debug.assert; const assert = std.debug.assert;
@ -12,128 +22,83 @@ const max_edge_layers = 32;
pub fn main() !void { pub fn main() !void {
// This allows me to utilize the same command line args and gstreamer
gst.init(@ptrCast(&std.os.argv.len), @ptrCast(&std.os.argv.ptr));
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); const source: *gst.Element = gst.ElementFactory.make("videotestsrc", "source") orelse unreachable;
defer arena.deinit(); const scale: *gst.Element = gst.ElementFactory.make("videoscale", "scale") orelse unreachable;
const format: *gst.Element = gst.ElementFactory.make("videoconvert", "format") orelse unreachable;
const sink_el: *gst.Element = gst.ElementFactory.make("appsink", "sink") orelse unreachable;
const sink: *gstapp.AppSink = gobject.ext.cast(gstapp.AppSink, sink_el) orelse unreachable;
const alloc = arena.allocator(); const sink_caps = gst.Caps.fromString("video/x-raw,format=RGB,width=640,height=640") orelse unreachable;
sink.setCaps(sink_caps);
std.fs.cwd().access(hef_file, .{ }) catch |e| { const pipeline: *gst.Pipeline = gst.Pipeline.new("test-pipeline");
std.debug.panic("Could not open hef file! '{any}'", .{ e });
};
var status: hlo.hailo_status = undefined; const bin: *gst.Bin = &pipeline.f_bin;
var vdevice : hlo.hailo_vdevice = undefined;
var hef : hlo.hailo_hef = undefined;
var config_params: hlo.hailo_configure_params_t = undefined; // std.mem.zeroInit(hlo.hailo_vdevice_params_t, .{});
var network_group: hlo.hailo_configured_network_group = undefined;
var network_group_size : usize = 1;
var input_vstream_params : [max_edge_layers]hlo.hailo_input_vstream_params_by_name_t = undefined;
var output_vstream_params : [max_edge_layers]hlo.hailo_output_vstream_params_by_name_t = undefined;
var output_vstreams : [max_edge_layers]hlo.hailo_output_vstream = undefined;
var input_vstreams : [max_edge_layers]hlo.hailo_input_vstream = undefined;
var input_vstream_size : usize = max_edge_layers;
var output_vstream_size : usize = max_edge_layers;
status = hlo.hailo_create_vdevice(null, &vdevice); _ = gst.Bin.addMany(bin, source, scale, format, sink_el);
assert(status == hlo.HAILO_SUCCESS);
status = hlo.hailo_create_hef_file(&hef, hef_file); // the failure return code is -1 I believe
assert(status == hlo.HAILO_SUCCESS); if (gst.Element.linkMany(source, scale, format, sink_el) < 0) {
pipeline.unref();
std.debug.panic("Elements could not be linked\n", .{});
}
std.debug.print("vdevice inited, hef file created!\n", .{}); // g_int is just i32. You can
// source.set("pattern", @as(i16, 0));
// gobject.Object.set(source.as(gobject.Object), "pattern", @as(i16, 0));
status = hlo.hailo_init_configure_params_by_vdevice(hef, vdevice, &config_params); const ret = pipeline.as(gst.Element).setState(gst.State.playing);
assert(status == hlo.HAILO_SUCCESS); if (ret == gst.StateChangeReturn.failure) {
pipeline.unref();
std.debug.panic("Could not start pipeline", .{});
}
std.debug.print("Init configure params complete\n", .{}); const bus: *gst.Bus = pipeline.getBus();
assert(vdevice != null); const sample = sink.pullSample() orelse unreachable;
assert(hef != null); const buffer: *gst.Buffer = sample.getBuffer() orelse unreachable;
assert(network_group_size != 0); std.debug.print("Got the buffer! {any}\n", .{ buffer });
std.debug.print("My buffer was '{d}' big!\n", .{ buffer.getSize() });
status = hlo.hailo_configure_vdevice(vdevice, hef, &config_params, &network_group, &network_group_size);
assert(status == hlo.HAILO_SUCCESS);
std.debug.print("Configure vdevice complete!\n", .{});
status = hlo.hailo_make_input_vstream_params(network_group, false, hlo.HAILO_FORMAT_TYPE_AUTO,
&input_vstream_params, &input_vstream_size);
assert(status == hlo.HAILO_SUCCESS);
std.debug.print("Input vstream params initialized\n", .{});
status = hlo.hailo_make_output_vstream_params(network_group, true, hlo.HAILO_FORMAT_TYPE_AUTO,
&output_vstream_params, &output_vstream_size);
assert(status == hlo.HAILO_SUCCESS);
std.debug.print("Output vstream params initialized\n", .{});
assert(input_vstream_size <= max_edge_layers);
status = hlo.hailo_create_input_vstreams(network_group, &input_vstream_params, input_vstream_size, &input_vstreams);
assert(status == hlo.HAILO_SUCCESS);
std.debug.print("Input vstreams initialized\n", .{});
status = hlo.hailo_create_output_vstreams(network_group, &output_vstream_params, output_vstream_size, &output_vstreams);
assert(status == hlo.HAILO_SUCCESS);
std.debug.print("Output vstreams initialized\n", .{});
var input_frame_size: usize = 0; // const msg: *gst.Message = bus.popFiltered( gst.MessageType.flags_eos + gst.MessageType.flags_warning );
const message_type: gst.MessageType = .{ .eos = true, .warning = true };
const ret_msg: ?*gst.Message = bus.timedPopFiltered(std.math.maxInt(u64), message_type);
status = hlo.hailo_get_input_vstream_frame_size(input_vstreams[0], &input_frame_size); std.debug.print("reg_msg returned!\n", .{});
assert(status == hlo.HAILO_SUCCESS);
const frame_count = input_frame_size / @sizeOf(u8); if (ret_msg) |msg| {
const input_data: [:0]u8 = try alloc.allocSentinel(u8, frame_count, 0); if (msg.f_type.eos == true) {
std.debug.print("EOS recieved\n", .{});
} else if (msg.f_type.warning == true) {
std.debug.print("Warning message received\n", .{});
const err: ?**glib.Error = null;
const debug_info: ?*[*:0]u8 = null;
msg.parseError(err, debug_info);
if (err) |e| {
std.debug.print("Error received from element {s}: {s}", .{ msg.f_src, e.*.*.message });
}
if (debug_info != null) { // I couldn't figure out how to do a orelse statement for this unwrap.
std.debug.print("Debugging information: {s}", .{debug_info.?});
}
glib.clearError(@ptrCast(err));
glib.free(@ptrCast(debug_info));
} else {
std.debug.print("Unknown type received {any}\n", .{ msg });
}
// msg.unref();
} else {
std.debug.print("ret message not handled type: {any}\n", .{ ret_msg });
}
std.debug.print("HailoRT init completed\nGstreamer Init starting", .{}); bus.unref();
_ = pipeline.as(gst.Element).setState(gst.State.null);
pipeline.unref();
std.debug.print("run complete\n", .{});
std.debug.print("Getting vstream info\n", .{});
var stream_info: hlo.hailo_vstream_info_t = undefined;
status = hlo.hailo_get_input_vstream_info(input_vstreams[0], &stream_info);
assert(status == hlo.HAILO_SUCCESS);
var output_info: hlo.hailo_vstream_info_t = undefined;
status = hlo.hailo_get_output_vstream_info(output_vstreams[0], &output_info);
assert(status == hlo.HAILO_SUCCESS);
std.debug.print("\nOutput info: {any}\n\n", .{ output_info });
std.debug.print("Writing vstream data\n", .{});
status = hlo.hailo_vstream_write_raw_buffer(input_vstreams[0], input_data.ptr, input_frame_size);
assert(status == hlo.HAILO_SUCCESS);
std.debug.print("Input written\n", .{});
status = hlo.hailo_flush_input_vstream(input_vstreams[0]);
assert(status == hlo.HAILO_SUCCESS);
std.debug.print("input flushed!\n", .{});
var output_frame_size: usize = 0;
status = hlo.hailo_get_output_vstream_frame_size(output_vstreams[0], &output_frame_size);
assert(status == hlo.HAILO_SUCCESS);
std.debug.print("output frame size is: {d}\n", .{ output_frame_size });
const output_frame_count = output_frame_size / @sizeOf(u8);
const output_data: [:0]u8 = try alloc.allocSentinel(u8, output_frame_count, 0);
status = hlo.hailo_vstream_read_raw_buffer(output_vstreams[0], output_data.ptr, output_frame_size);
assert(status == hlo.HAILO_SUCCESS);
std.debug.print("Output finished reading! Cleanup time\n", .{});
std.debug.print("good luck\n\n{x}", .{ output_data });
_ = hlo.hailo_release_output_vstreams(&output_vstreams, output_vstream_size);
_ = hlo.hailo_release_input_vstreams(&input_vstreams, input_vstream_size);
_ = hlo.hailo_release_hef(hef);
_ = hlo.hailo_release_vdevice(vdevice);
} }