642
edits
(→Code) |
|||
Line 55: | Line 55: | ||
#define METADATA_PERIOD_SECS 1 | #define METADATA_PERIOD_SECS 1 | ||
static guint8 klv_metadata[61]; | static guint8 klv_metadata[61]; | ||
typedef struct _GstMetaDemo GstMetaDemo; | typedef struct _GstMetaDemo GstMetaDemo; | ||
Line 70: | Line 66: | ||
}; | }; | ||
static gboolean create_pipeline(GstMetaDemo *metademo, const char *ip, const char *port); | static gboolean create_pipeline(GstMetaDemo *metademo, const char *ip, const char *port, | ||
char *output_converter); | |||
static void start_pipeline(GstMetaDemo *metademot); | static void start_pipeline(GstMetaDemo *metademot); | ||
static void stop_pipeline(GstMetaDemo *metademo); | static void stop_pipeline(GstMetaDemo *metademo); | ||
Line 90: | Line 87: | ||
} | } | ||
char *converter_caller(const char *json_path){ | |||
int pipefd[2]; | |||
char buffer[4096]; | |||
pid_t child_pid; | |||
if (pipe(pipefd) == -1) { | |||
perror("pipe"); | |||
exit(EXIT_FAILURE); | |||
} | |||
child_pid = fork(); | |||
if (child_pid == -1) { | |||
if ( | perror("fork"); | ||
perror(" | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | ||
} | } | ||
if (access(json_path, F_OK) != 0) { | |||
if ( | printf("JSON File '%s' does not exist.\n", json_path); | ||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | ||
} | |||
if (child_pid == 0) { // Child process | |||
close(pipefd[0]); // Close the read end of the pipe | |||
// Redirect stdout to the write end of the pipe | |||
dup2(pipefd[1], STDOUT_FILENO); | |||
// Execute the command | |||
execlp("./misb-converter", "./misb-converter", "--verbose", "--encode", "-i", json_path, "-o", "klv.bin", NULL); | |||
// If execlp fails | |||
perror("execlp"); | |||
exit(EXIT_FAILURE); | |||
} else { // Parent process | |||
close(pipefd[1]); // Close the write end of the pipe | |||
// Read the output from the read end of the pipe | |||
ssize_t bytesRead = read(pipefd[0], buffer, sizeof(buffer)); | |||
if (bytesRead == -1) { | |||
perror("read"); | |||
exit(EXIT_FAILURE); | |||
} | |||
// Null-terminate the string | |||
buffer[bytesRead] = '\0'; | |||
wait(NULL); | |||
} | } | ||
char* result = strdup(buffer); | |||
if (result == NULL) { | |||
perror("strdup"); | |||
exit(EXIT_FAILURE); | |||
} | |||
return result; | |||
} | |||
int main(int argc, char *argv[]) { | |||
const char *ip = argv[1]; | |||
const char *port = argv[2]; | |||
const char *json_path = argv[3]; | |||
printf("IP: %s\n", ip); | |||
printf("Port: %s\n", port); | |||
printf("JSON Path: %s\n", json_path); | |||
GstMetaDemo *metademo = g_malloc(sizeof(GstMetaDemo)); | GstMetaDemo *metademo = g_malloc(sizeof(GstMetaDemo)); | ||
Line 128: | Line 169: | ||
gst_init(&argc, &argv); | gst_init(&argc, &argv); | ||
if (argc != | if (argc != 4) { | ||
fprintf(stderr, "Usage: %s <IP address> <Port number>\n", argv[0]); | fprintf(stderr, "Usage: %s <IP address> <Port number> <JSON file path>\n", argv[0]); | ||
return 1; | return 1; | ||
} | } | ||
Line 135: | Line 176: | ||
if (!is_valid_ip(ip) && !is_valid_port(port)) { | if (!is_valid_ip(ip) && !is_valid_port(port)) { | ||
g_print("Invalid IP address or port number!\n"); | g_print("Invalid IP address or port number!\n"); | ||
g_print("Usage: %s <IP address> <Port number>\n", argv[0]); | g_print("Usage: %s <IP address> <Port number> <JSON file path>\n", argv[0]); | ||
} | } | ||
if (!create_pipeline(metademo, ip, port)) { | char *output_converter = converter_caller(json_path); | ||
if (!create_pipeline(metademo, ip, port, output_converter)) { | |||
g_free(metademo); | g_free(metademo); | ||
return 1; | return 1; | ||
Line 187: | Line 230: | ||
} | } | ||
static gboolean create_pipeline(GstMetaDemo *metademo, const char *ip, const char *port) { | static gboolean create_pipeline(GstMetaDemo *metademo, const char *ip, const char *port, char *output_converter ) { | ||
GMainLoop *loop; | GMainLoop *loop; | ||
Line 206: | Line 249: | ||
loop = g_main_loop_new(NULL, FALSE); | loop = g_main_loop_new(NULL, FALSE); | ||
char actual_pipeline[] = "metasrc | char actual_pipeline[] = "metasrc period=1 metadata='%s' ! meta/x-klv ! mpegtsmux name=mux ! " | ||
"udpsink host=%s port=%s videotestsrc is-live=true ! " | |||
"video/x-raw,format=(string)I420,width=320,height=240,framerate=(fraction)30/1 ! x264enc ! mux."; | |||
char modified_pipeline[1024]; | char modified_pipeline[1024]; | ||
// Use snprintf to format the string with the new values | // Use snprintf to format the string with the new values | ||
snprintf(modified_pipeline, sizeof(modified_pipeline), actual_pipeline, ip, port); | memmove(output_converter, output_converter + 14, strlen(output_converter) - 14 + 1); | ||
output_converter[strlen(output_converter) - 2] = '\0'; | |||
snprintf(modified_pipeline, sizeof(modified_pipeline), actual_pipeline, output_converter, ip, port); | |||
// Now modified_pipeline contains the modified GStreamer pipeline | // Now modified_pipeline contains the modified GStreamer pipeline | ||
pipeline = gst_parse_launch(modified_pipeline, &error); | pipeline = gst_parse_launch(modified_pipeline, &error); | ||
Line 226: | Line 266: | ||
g_printerr("Unable to build pipeline (%s)\n", error->message); | g_printerr("Unable to build pipeline (%s)\n", error->message); | ||
g_clear_error(&error); | g_clear_error(&error); | ||
return FALSE; | return FALSE; | ||
} | } | ||
Line 253: | Line 281: | ||
g_object_set(G_OBJECT(metasrc), "period", METADATA_PERIOD_SECS, NULL); | g_object_set(G_OBJECT(metasrc), "period", METADATA_PERIOD_SECS, NULL); | ||
/* we add a message handler */ | /* we add a message handler */ | ||
Line 310: | Line 337: | ||
return TRUE; | return TRUE; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||