Free caps in GUPnPDLNAProfilePriv
[gupnp:gupnp-dlna.git] / tests / dlna-profile-parser.c
1 /*
2  * Copyright (C) 2010 Nokia Corporation.
3  *
4  * Authors: Arun Raghavan <arun.raghavan@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <gst/gst.h>
23 #include <libgupnp-dlna/gupnp-dlna-load.h>
24 #include <libgupnp-dlna/gupnp-dlna-profile.h>
25 #include <gst/pbutils/pbutils.h>
26 #include <libxml/xmlmemory.h>
27 #include <stdlib.h>
28
29 static void usage (void)
30 {
31         g_print ("Usage: dlna-profile-parser file1 file2 ... dir1 dir2 ...\n");
32 }
33
34 static void print_caps (const GstCaps *caps)
35 {
36         int i;
37
38         for (i = 0; i < gst_caps_get_size (caps); i++) {
39                 GstStructure *structure = gst_caps_get_structure (caps, i);
40                 gchar *tmp = gst_structure_to_string (structure);
41
42                 g_print ("%s`- %s\n", i ? "    " : "", tmp);
43
44                 g_free (tmp);
45         }
46 }
47
48 static void print_profile (GUPnPDLNAProfile *profile, gpointer unused)
49 {
50         GstEncodingProfile *enc_profile;
51         const GList *tmp;
52         gchar *caps_str;
53
54         enc_profile = gupnp_dlna_profile_get_encoding_profile (profile);
55         tmp = gst_encoding_container_profile_get_profiles (GST_ENCODING_CONTAINER_PROFILE (enc_profile));
56         caps_str = gst_caps_to_string ((GstCaps *)gst_encoding_profile_get_format (enc_profile));
57
58         g_print ("Loaded DLNA Profile: %s, %s - format %s\n",
59                  gupnp_dlna_profile_get_name (profile),
60                  gupnp_dlna_profile_get_mime (profile),
61                  caps_str);
62
63         while (tmp) {
64                 print_caps (gst_encoding_profile_get_format
65                                         (GST_ENCODING_PROFILE(tmp->data)));
66                 tmp = tmp->next;
67         }
68
69         g_print ("\n");
70         g_free (caps_str);
71         gst_encoding_profile_unref (enc_profile);
72 }
73
74 static void
75 free_restrictions_struct (gpointer data, gpointer user_data)
76 {
77         GUPnPDLNARestrictionsPriv *priv = (GUPnPDLNARestrictionsPriv *)data;
78         if (priv) {
79                 if (priv->caps)
80                         gst_caps_unref (priv->caps);
81
82                 g_free (priv);
83         }
84 }
85
86 int
87 main (int argc, char **argv)
88 {
89         GList *profiles = NULL;
90         GUPnPDLNALoadState *data;
91         gboolean relaxed_mode = FALSE;
92         gboolean extended_mode = FALSE;
93         GError *err = NULL;
94         gint i;
95
96         GOptionEntry options[] = {
97                 {"relaxed mode", 'r', 0, G_OPTION_ARG_NONE, &relaxed_mode,
98                  "Enable Relaxed mode", NULL},
99                 {"extended mode", 'e', 0, G_OPTION_ARG_NONE, &extended_mode,
100                  "Enable extended mode", NULL},
101                 {NULL}
102         };
103
104         GOptionContext *ctx;
105
106         if (!g_thread_supported ())
107                 g_thread_init (NULL);
108
109         ctx = g_option_context_new (" - test to parse dlna profiles");
110         g_option_context_add_main_entries (ctx, options, NULL);
111         g_option_context_add_group (ctx, gst_init_get_option_group ());
112
113         if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
114
115                 g_print ("Error initializing: %s\n", err->message);
116                 exit (1);
117         }
118
119         g_option_context_free (ctx);
120
121         gst_init (&argc, &argv);
122
123         if (argc < 2) {
124                 usage ();
125                 return EXIT_FAILURE;
126         }
127
128         data = g_new (GUPnPDLNALoadState, 1);
129
130         data->restrictions = g_hash_table_new_full (g_str_hash,
131                                                     g_str_equal,
132                                                     (GDestroyNotify) xmlFree,
133                                                     (GDestroyNotify)
134                                                     free_restrictions_struct);
135         data->profile_ids = g_hash_table_new_full (g_str_hash,
136                                                    g_str_equal,
137                                                    (GDestroyNotify) xmlFree,
138                                                    (GDestroyNotify)
139                                                    g_object_unref);
140         data->files_hash = g_hash_table_new_full (g_str_hash,
141                                                   g_str_equal,
142                                                   g_free,
143                                                   NULL);
144
145         data->relaxed_mode = relaxed_mode;
146         data->extended_mode = extended_mode;
147
148         for (i = 1; i < argc; i++) {
149                 GList *tmp;
150
151                 if (g_file_test (argv[i], G_FILE_TEST_IS_DIR))
152                         tmp = gupnp_dlna_load_profiles_from_dir (argv[i],
153                                                                  data);
154                 else
155                         tmp = gupnp_dlna_load_profiles_from_file (argv[i],
156                                                                   data);
157
158                 profiles = g_list_concat (profiles, tmp);
159         }
160
161         g_list_foreach (profiles, (GFunc)print_profile, NULL);
162         g_list_foreach (profiles, (GFunc)g_object_unref, NULL);
163
164         g_hash_table_unref (data->restrictions);
165         g_hash_table_unref (data->profile_ids);
166         g_hash_table_unref (data->files_hash);
167         g_free (data);
168         data = NULL;
169         return EXIT_SUCCESS;
170 }