Jump to content

LibMISB/Examples/Example Application: Difference between revisions

Line 55: Line 55:


#define METADATA_PERIOD_SECS 1
#define METADATA_PERIOD_SECS 1
#define FILE_LOCATION "./metadata_misb_library.ts"


static guint8 klv_metadata[61];
static guint8 klv_metadata[61];
//const char* filename = "<PATH>/klv.bin";
const char* filename = "klv.bin";


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:
}
}


int main(int argc, char *argv[]) {
char *converter_caller(const char *json_path){
struct stat sb;
  int pipefd[2];
   const char *ip = argv[1];
   char buffer[4096];
  const char *port = argv[2];
    pid_t child_pid;
 
    if (pipe(pipefd) == -1) {
        perror("pipe");
        exit(EXIT_FAILURE);
    }


  printf("IP: %s\n", ip);
    child_pid = fork();
  printf("IP: %s\n", port);


  FILE* in_file = fopen(filename, "rb");
     if (child_pid == -1) {
     if (!in_file) {
         perror("fork");
         perror("fopen");
         exit(EXIT_FAILURE);
         exit(EXIT_FAILURE);
     }
     }


   
     if (access(json_path, F_OK) != 0) {
     if (stat(filename, &sb) == -1) {
         printf("JSON File '%s' does not exist.\n", json_path);
         perror("stat");
         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);
     }
     }


     fread(klv_metadata, sizeof(klv_metadata), 1, in_file);
     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);


    for(uint i = 0; i<sizeof(klv_metadata); i++)
        printf("%u ", klv_metadata[i]);
    printf("\n");
    fclose(in_file);


   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 != 3) {
   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 name=meta ! meta/x-klv,stream_type=21 ! mpegtsmux name=mux ! "
   char actual_pipeline[] = "metasrc period=1 metadata='%s' ! meta/x-klv ! mpegtsmux name=mux ! "
                            "filesink name=sink location=./metadata_misb_library.ts videotestsrc "
                      "udpsink host=%s port=%s videotestsrc is-live=true ! "
                            "is-live=true ! video/x-raw,width=640,height=480,framerate=30/1 ! queue ! "
                      "video/x-raw,format=(string)I420,width=320,height=240,framerate=(fraction)30/1 ! x264enc ! mux.";
                            "x264enc ! rtph264pay ! udpsink host=%s port=%s";


   char modified_pipeline[1024]; // Adjust the size accordingly
   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
  printf("Modified pipeline: %s\n", modified_pipeline);
  printf("IP: %s\n", ip);
  printf("IP: %s\n", port);
   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;
  }
  metasrc = gst_bin_get_by_name(GST_BIN(pipeline), "meta");
  if (!metasrc) {
    g_printerr("Could not get metadata element\n");
    return FALSE;
  }
  filesink = gst_bin_get_by_name(GST_BIN(pipeline), "sink");
  if (!filesink) {
    g_printerr("Could not get filesink element\n");
     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);
  g_object_set(G_OBJECT(filesink), "location", FILE_LOCATION, NULL);


   /* we add a message handler */
   /* we add a message handler */
Line 310: Line 337:
   return TRUE;
   return TRUE;
}
}
</syntaxhighlight>
</syntaxhighlight>


Cookies help us deliver our services. By using our services, you agree to our use of cookies.