1 /* GStreamer
2 * Copyright (C) <2008> Sebastian Dröge <sebastian.droege@collabora.co.uk>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <gst/gst.h>
24 #include <gtk/gtk.h>
25
26 static GtkWidget *window = NULL;
27 static GtkTreeStore *treestore = NULL;
28
29 static gchar *
g_value_to_string(const GValue * val)30 g_value_to_string (const GValue * val)
31 {
32 gchar *ret = NULL;
33
34 if (G_VALUE_TYPE (val) == GST_TYPE_BUFFER) {
35 GstBuffer *buf = gst_value_get_buffer (val);
36 GstMapInfo map;
37
38 gst_buffer_map (buf, &map, GST_MAP_READ);
39 ret = g_base64_encode (map.data, map.size);
40 gst_buffer_unmap (buf, &map);
41 } else {
42 GValue s = { 0, };
43
44 g_value_init (&s, G_TYPE_STRING);
45 if (g_value_transform (val, &s)) {
46 ret = g_value_dup_string (&s);
47 g_value_unset (&s);
48 }
49 }
50 return ret;
51 }
52
53 static gboolean
insert_field(GQuark field_id,const GValue * val,gpointer user_data)54 insert_field (GQuark field_id, const GValue * val, gpointer user_data)
55 {
56 GtkTreeIter *parent_iter = user_data;
57 GtkTreeIter iter;
58 const gchar *f = g_quark_to_string (field_id);
59
60 gtk_tree_store_append (treestore, &iter, parent_iter);
61
62 if (G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
63 guint n = gst_value_array_get_size (val);
64 guint i;
65 GtkTreeIter child_iter;
66
67 gtk_tree_store_set (treestore, &iter, 0, f, -1);
68
69 for (i = 0; i < n; i++) {
70 const GValue *ve = gst_value_array_get_value (val, i);
71
72 gtk_tree_store_append (treestore, &child_iter, &iter);
73
74 if (G_VALUE_TYPE (ve) == GST_TYPE_STRUCTURE) {
75 const GstStructure *s = gst_value_get_structure (ve);
76
77 gtk_tree_store_set (treestore, &child_iter, 0,
78 gst_structure_get_name (s), -1);
79
80 gst_structure_foreach (s, insert_field, &child_iter);
81 } else {
82 gchar *v = g_value_to_string (ve);
83
84 gtk_tree_store_set (treestore, &child_iter, 0, v, -1);
85
86 g_free (v);
87 }
88 }
89 } else if (G_VALUE_TYPE (val) == GST_TYPE_STRUCTURE) {
90 const GstStructure *s = gst_value_get_structure (val);
91 gchar *entry = g_strdup_printf ("%s: %s", f, gst_structure_get_name (s));
92
93 gtk_tree_store_set (treestore, &iter, 0, entry, -1);
94
95 g_free (entry);
96
97 gst_structure_foreach (s, insert_field, &iter);
98 } else {
99 gchar *v = g_value_to_string (val);
100 gchar *entry = g_strdup_printf ("%s: %s", f, v);
101
102 gtk_tree_store_set (treestore, &iter, 0, entry, -1);
103
104 g_free (v);
105 g_free (entry);
106 }
107
108 return TRUE;
109 }
110
111 static void
insert_structure(const GstStructure * s,GtkTreeIter * iter)112 insert_structure (const GstStructure * s, GtkTreeIter * iter)
113 {
114 const gchar *name = gst_structure_get_name (s);
115
116 gtk_tree_store_set (treestore, iter, 0, name, -1);
117
118 gst_structure_foreach (s, insert_field, iter);
119 }
120
121 static gboolean
bus_callback(GstBus * bus,GstMessage * message,gpointer data)122 bus_callback (GstBus * bus, GstMessage * message, gpointer data)
123 {
124 switch (GST_MESSAGE_TYPE (message)) {
125 case GST_MESSAGE_WARNING:{
126 GError *err;
127 gchar *debug;
128
129 gst_message_parse_warning (message, &err, &debug);
130 g_print ("Warning: %s\n", err->message);
131 g_error_free (err);
132 g_free (debug);
133 break;
134 }
135 case GST_MESSAGE_ERROR:{
136 GError *err;
137 gchar *debug = NULL;
138
139 gst_message_parse_error (message, &err, &debug);
140 g_print ("Error: %s : %s\n", err->message, debug);
141 g_error_free (err);
142 g_free (debug);
143
144 gtk_main_quit ();
145 break;
146 }
147 case GST_MESSAGE_TAG:{
148 GstTagList *tags;
149 GValue v = { 0, };
150
151 g_print ("Got tags\n");
152 gst_message_parse_tag (message, &tags);
153
154 if (gst_tag_list_copy_value (&v, tags, "mxf-structure")) {
155 const GstStructure *s;
156 GtkTreeIter iter;
157
158 s = gst_value_get_structure (&v);
159
160 gtk_tree_store_append (treestore, &iter, NULL);
161 insert_structure (s, &iter);
162
163 gtk_widget_show_all (window);
164
165 g_value_unset (&v);
166 }
167
168 gst_tag_list_unref (tags);
169 break;
170 }
171 default:
172 break;
173 }
174 return TRUE;
175 }
176
177 static void
on_pad_added(GstElement * src,GstPad * pad,gpointer data)178 on_pad_added (GstElement * src, GstPad * pad, gpointer data)
179 {
180 GstElement *fakesink = gst_element_factory_make ("fakesink", NULL);
181 GstPad *sinkpad = gst_element_get_static_pad (fakesink, "sink");
182 GstElement *bin = (GstElement *) gst_element_get_parent (src);
183
184 gst_bin_add (GST_BIN (bin), fakesink);
185 gst_element_sync_state_with_parent (fakesink);
186
187 gst_pad_link (pad, sinkpad);
188
189 gst_object_unref (sinkpad);
190 gst_object_unref (bin);
191 }
192
193 int
main(int argc,char ** argv)194 main (int argc, char **argv)
195 {
196 GstElement *pipeline, *src, *mxfdemux;
197 GstBus *bus;
198 GtkWidget *scrolled_window, *treeview;
199
200 if (argc < 2) {
201 g_print ("usage: %s MXF-FILE\n", argv[0]);
202 return -1;
203 }
204
205 gst_init (&argc, &argv);
206 gtk_init (&argc, &argv);
207
208 pipeline = gst_pipeline_new ("pipeline");
209
210 src = gst_element_factory_make ("filesrc", "src");
211 g_object_set (G_OBJECT (src), "location", argv[1], NULL);
212
213 mxfdemux = gst_element_factory_make ("mxfdemux", "mxfdemux");
214 g_signal_connect (mxfdemux, "pad-added", G_CALLBACK (on_pad_added), NULL);
215
216 if (!src || !mxfdemux) {
217 g_error ("Unable to create all elements");
218 return -2;
219 }
220
221 gst_bin_add_many (GST_BIN (pipeline), src, mxfdemux, NULL);
222 if (!gst_element_link_many (src, mxfdemux, NULL)) {
223 g_error ("Failed to link elements");
224 return -3;
225 }
226
227 bus = gst_element_get_bus (pipeline);
228 gst_bus_add_watch (bus, bus_callback, NULL);
229 gst_object_unref (bus);
230
231 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
232 gtk_window_set_default_size (GTK_WINDOW (window), 640, 480);
233 g_signal_connect (window, "delete-event", gtk_main_quit, NULL);
234
235 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
236
237 treestore = gtk_tree_store_new (1, G_TYPE_STRING, NULL);
238 treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (treestore));
239
240 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview),
241 gtk_tree_view_column_new_with_attributes ("Element",
242 gtk_cell_renderer_text_new (), "text", 0, NULL));
243
244 gtk_container_add (GTK_CONTAINER (scrolled_window), treeview);
245 gtk_container_add (GTK_CONTAINER (window), scrolled_window);
246 gtk_widget_show_all (window);
247
248 if (gst_element_set_state (pipeline,
249 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
250 g_error ("Failed to change state to PLAYING");
251 }
252
253 gtk_main ();
254
255 gst_element_set_state (pipeline, GST_STATE_NULL);
256 gst_object_unref (pipeline);
257
258 return 0;
259 }
260