Support clang/clang++
[platform/upstream/nnstreamer.git] / tools / development / parser / toplevel.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /**
3  * GStreamer Pipeline from/to PBTxt Converter Parser
4  * Copyright (C) 2021 MyungJoo Ham <myungjoo.ham@samsung.com>
5  * Copyright (C) 2021 Dongju Chae <dongju.chae@samsung.com>
6  */
7 /**
8  * @file    toplevel.c
9  * @date    27 Apr 2021
10  * @brief   Top-level program to parse gst pipeline and pbtxt
11  * @see     http://github.com/nnstreamer/nnstreamer
12  * @author  Dongju Chae <dongju.chae@samsung.com>
13  * @bug     No known bugs except for NYI items
14  */
15
16 #include <stdio.h>
17
18 #define G_LOG_USE_STRUCTURED 1
19 #include <glib.h>
20
21 #include "types.h"
22 #include "convert.h"
23
24 #define INPUT_MAXLEN (512)
25
26 static gboolean from_pbtxt = FALSE;
27 static gboolean verbose = FALSE;
28
29 static GOptionEntry entries[] = {
30   {"from-pbtxt", 'p', 0, G_OPTION_ARG_NONE, &from_pbtxt,
31         "From pbtxt to gst pipeline", NULL},
32   {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Enable verbose messages",
33         NULL},
34   {0}
35 };
36
37 /** @brief Get input string for parsing */
38 static gboolean
39 get_input_string (char *str)
40 {
41   GIOChannel *channel = g_io_channel_unix_new (fileno (stdin));
42   GIOStatus status;
43   gboolean ret = TRUE;
44   GError *error = NULL;
45   gsize length;
46
47   status = g_io_channel_set_encoding (channel, "UTF-8", &error);
48   if (status == G_IO_STATUS_ERROR) {
49     g_printerr ("Error detected while setting encoding: %s\n", error->message);
50     g_error_free (error);
51     ret = FALSE;
52     goto out;
53   }
54
55   status =
56       g_io_channel_read_chars (channel, str, INPUT_MAXLEN, &length, &error);
57   if (status == G_IO_STATUS_ERROR) {
58     g_printerr ("Error detected while reading an input string: %s\n",
59         error->message);
60     g_error_free (error);
61     ret = FALSE;
62     goto out;
63   }
64
65   if (status != G_IO_STATUS_NORMAL) {
66     ret = FALSE;
67     goto out;
68   }
69
70   str[length - 1] = '\x00';
71
72 out:
73   g_io_channel_shutdown (channel, TRUE, NULL);
74   return ret;
75 }
76
77 /** @brief Log handler */
78 static void
79 log_handler (const gchar * log_domain, GLogLevelFlags log_level,
80     const gchar * message, gpointer user_data)
81 {
82   (void) log_domain;
83   (void) log_level;
84   (void) user_data;
85   g_printerr ("%s", message);
86 }
87
88 /** @brief Main routine for this program */
89 int
90 main (int argc, char *argv[])
91 {
92   GError *error = NULL;
93   GOptionContext *context;
94   char input_str[INPUT_MAXLEN] = { '\x00' };
95   GLogLevelFlags log_flags;
96   _Element *pipeline;
97
98   context =
99       g_option_context_new ("- Prototxt to/from GStreamer Pipeline Converver");
100   g_option_context_add_main_entries (context, entries, NULL);
101   if (!g_option_context_parse (context, &argc, &argv, &error)) {
102     g_printerr ("Option parsing failed: %s\n", error->message);
103     g_error_free (error);
104     return -1;
105   }
106   g_option_context_free (context);
107
108   if (from_pbtxt) {
109     g_print ("NYI: pbtxt-to-gstpipe conversion\n");
110     return 0;
111   }
112
113   if (!get_input_string (input_str)) {
114     g_printerr ("Unable to get an input string for GStreamer pipeline\n");
115     return -1;
116   }
117
118   /* all messages from glib */
119   log_flags = G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION;
120   if (!verbose)
121     log_flags &= ~G_LOG_LEVEL_DEBUG;
122
123   g_log_set_handler (NULL, log_flags, log_handler, NULL);
124
125   pipeline = priv_gst_parse_launch (input_str, NULL, NULL, __PARSE_FLAG_NONE);
126   if (pipeline == NULL) {
127     g_printerr ("Unable to parse the given pipeline string");
128     return -1;
129   }
130
131   convert_to_pbtxt (pipeline);
132   nnstparser_element_unref (pipeline);
133
134   return 0;
135 }