hythmbox-2.98/plugins/audiocd/test-cd.c

No issues found

  1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  2  *
  3  *  This program is free software; you can redistribute it and/or modify
  4  *  it under the terms of the GNU General Public License as published by
  5  *  the Free Software Foundation; either version 2 of the License, or
  6  *  (at your option) any later version.
  7  *
  8  *  The Rhythmbox authors hereby grant permission for non-GPL compatible
  9  *  GStreamer plugins to be used and distributed together with GStreamer
 10  *  and Rhythmbox. This permission is above and beyond the permissions granted
 11  *  by the GPL license by which Rhythmbox is covered. If you modify this code
 12  *  you may extend this exception to your version of the code, but you are not
 13  *  obligated to do so. If you do not wish to do so, delete this exception
 14  *  statement from your version.
 15  *
 16  *  This program is distributed in the hope that it will be useful,
 17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19  *  GNU General Public License for more details.
 20  *
 21  *  You should have received a copy of the GNU General Public License
 22  *  along with this program; if not, write to the Free Software
 23  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 24  *
 25  */
 26 
 27 #include <gst/gst.h>
 28 
 29 #include "rb-audiocd-info.h"
 30 #include "rb-musicbrainz-lookup.h"
 31 
 32 GMainLoop *mainloop = NULL;
 33 
 34 static void
 35 indent (int d)
 36 {
 37 	int i;
 38 	for (i = 0; i < d; i++)
 39 		g_print("  ");
 40 }
 41 
 42 static void
 43 dump (RBMusicBrainzData *d, int depth, int recurse)
 44 {
 45 	GList *l, *i;
 46 
 47 	indent (depth);
 48 	g_print ("%s {\n", rb_musicbrainz_data_get_data_type (d));
 49 	l = rb_musicbrainz_data_get_attr_names (d);
 50 	for (i = l; i != NULL; i = i->next) {
 51 		GList *v, *vi;
 52 
 53 		indent (depth+1);
 54 		g_print ("%s = [", (char *)i->data);
 55 		v = rb_musicbrainz_data_get_attr_values (d, i->data);
 56 		for (vi = v; vi != NULL; vi = vi->next) {
 57 			if (vi != v)
 58 				g_print (", ");
 59 			g_print ("%s", (char *)vi->data);
 60 		}
 61 		g_list_free (v);
 62 		g_print ("]\n");
 63 	}
 64 	g_list_free (l);
 65 
 66 	if (recurse) {
 67 		l = rb_musicbrainz_data_get_children (d);
 68 		for (i = l; i != NULL; i = i->next) {
 69 			dump (i->data, depth+1, recurse);
 70 		}
 71 		g_list_free (l);
 72 	}
 73 
 74 	indent (depth);
 75 	g_print ("}\n");
 76 }
 77 
 78 static void
 79 release_lookup_cb (GObject *object, GAsyncResult *result, RBAudioCDInfo *info)
 80 {
 81 	RBMusicBrainzData *dd;
 82 	GError *error = NULL;
 83 	dd = rb_musicbrainz_lookup_finish (result, &error);
 84 	if (dd != NULL) {
 85 		RBMusicBrainzData *r;
 86 		RBMusicBrainzData *m;
 87 
 88 		r = rb_musicbrainz_data_get_children (dd)->data;
 89 		m = rb_musicbrainz_data_find_child (r, "disc-id", info->musicbrainz_disc_id);
 90 		dump (r, 0, 0);
 91 		dump (m, 0, 1);
 92 		rb_musicbrainz_data_free (dd);
 93 	}
 94 
 95 	g_main_loop_quit (mainloop);
 96 	rb_audiocd_info_free (info);
 97 }
 98 
 99 static void
100 lookup_cb (GObject *object, GAsyncResult *result, RBAudioCDInfo *info)
101 {
102 	RBMusicBrainzData *dd;
103 	GError *error = NULL;
104 	GList *l;
105 	const char *release_includes[] = {
106 		"discids",
107 		"media",
108 		"recordings",
109 		"artist-credits",
110 		"work-rels",
111 		"recording-level-rels",
112 		"work-level-rels",
113 		"artist-rels",
114 		"url-rels",
115 		NULL
116 	};
117 
118 	dd = rb_musicbrainz_lookup_finish (result, &error);
119 	if (dd != NULL) {
120 		g_print ("\n\n");
121 		for (l = rb_musicbrainz_data_get_children (dd); l != NULL; l = l->next) {
122 			RBMusicBrainzData *r = l->data;
123 
124 			RBMusicBrainzData *m = rb_musicbrainz_data_find_child (r, "disc-id", info->musicbrainz_disc_id);
125 			if (m != NULL) {
126 				dump (r, 0, 0);
127 				dump (m, 0, 1);
128 
129 				rb_musicbrainz_lookup ("release",
130 						       rb_musicbrainz_data_get_attr_value (r, RB_MUSICBRAINZ_ATTR_ALBUM_ID),
131 						       release_includes,
132 						       NULL,
133 						       (GAsyncReadyCallback) release_lookup_cb,
134 						       info);
135 				break;
136 			}
137 		}
138 		rb_musicbrainz_data_free (dd);
139 	} else {
140 		g_print ("lookup failed: %s\n", error->message);
141 		g_clear_error (&error);
142 	}
143 
144 }
145 
146 
147 static void
148 audiocd_info_cb (GObject *source, GAsyncResult *result, gpointer data)
149 {
150 	RBAudioCDInfo *info;
151 	GError *error = NULL;
152 	int i;
153 
154 	info = rb_audiocd_info_finish (result, &error);
155 
156 	if (error != NULL) {
157 		g_print ("err: %s\n", error->message);
158 		g_clear_error (&error);
159 	} else {
160 		g_print ("disc id: %s\n", info->musicbrainz_disc_id);
161 		g_print ("%d tracks\n", info->num_tracks);
162 
163 		for (i = 0; i < info->num_tracks; i++) {
164 			g_print ("%d: %ds\n", info->tracks[i].track_num, info->tracks[i].duration/1000);
165 		}
166 	}
167 
168 	if (info->musicbrainz_disc_id) {
169 		const char *includes[] = { "artist-credits" };
170 
171 		rb_musicbrainz_lookup ("discid",
172 				       info->musicbrainz_disc_id,
173 				       includes,
174 				       NULL,
175 				       (GAsyncReadyCallback) lookup_cb,
176 				       info);
177 	} else {
178 		rb_audiocd_info_free (info);
179 		g_main_loop_quit (mainloop);
180 	}
181 }
182 
183 static void
184 release_cb (GObject *source, GAsyncResult *result, gpointer data)
185 {
186 	RBMusicBrainzData *dd;
187 	GError *error = NULL;
188 
189 	dd = rb_musicbrainz_lookup_finish (result, &error);
190 	if (dd != NULL) {
191 		dump (dd, 0, 1);
192 		rb_musicbrainz_data_free (dd);
193 	} else {
194 		g_print ("lookup failed: %s\n", error->message);
195 		g_clear_error (&error);
196 	}
197 
198 	g_main_loop_quit (mainloop);
199 }
200 
201 static gboolean
202 go (const char *thing)
203 {
204 	if (thing[0] == '/') {
205 		rb_audiocd_info_get (thing, NULL, (GAsyncReadyCallback)audiocd_info_cb, NULL);
206 	} else {
207 		const char *includes[] = { "artist-credits", NULL };
208 		rb_musicbrainz_lookup ("release", thing, includes, NULL, (GAsyncReadyCallback) release_cb, NULL);
209 	}
210 	return FALSE;
211 }
212 
213 int
214 main (int argc, char **argv)
215 {
216 	char *thing;
217 
218 	g_type_init ();
219 	gst_init (NULL, NULL);
220 
221 	if (argv[1] == NULL) {
222 		thing = "/dev/cdrom";
223 	} else {
224 		thing = argv[1];
225 	}
226 
227 	mainloop = g_main_loop_new (NULL, FALSE);
228 	g_idle_add ((GSourceFunc) go, thing);
229 	g_main_loop_run (mainloop);
230 
231 	gst_deinit ();
232 
233 	return 0;
234 }