configure.ac: Move OpenAL to Audio Output Plugins (nonstreaming), add header.
[mpd-mk.git] / test / run_input.c
blob9a31019ddbdb51244842aa2124f4424c86bcc54f
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "config.h"
21 #include "input_init.h"
22 #include "input_stream.h"
23 #include "tag_pool.h"
24 #include "tag_save.h"
25 #include "conf.h"
27 #ifdef ENABLE_ARCHIVE
28 #include "archive_list.h"
29 #endif
31 #include <glib.h>
33 #include <unistd.h>
35 static void
36 my_log_func(const gchar *log_domain, G_GNUC_UNUSED GLogLevelFlags log_level,
37 const gchar *message, G_GNUC_UNUSED gpointer user_data)
39 if (log_domain != NULL)
40 g_printerr("%s: %s\n", log_domain, message);
41 else
42 g_printerr("%s\n", message);
45 static int
46 dump_input_stream(struct input_stream *is)
48 GError *error = NULL;
49 char buffer[4096];
50 size_t num_read;
51 ssize_t num_written;
53 /* wait until the stream becomes ready */
55 while (!is->ready) {
56 int ret = input_stream_buffer(is, &error);
57 if (ret < 0) {
58 /* error */
59 g_warning("%s", error->message);
60 g_error_free(error);
61 return 2;
64 if (ret == 0)
65 /* nothing was buffered - wait */
66 g_usleep(10000);
69 /* print meta data */
71 if (is->mime != NULL)
72 g_printerr("MIME type: %s\n", is->mime);
74 /* read data and tags from the stream */
76 while (!input_stream_eof(is)) {
77 struct tag *tag = input_stream_tag(is);
78 if (tag != NULL) {
79 g_printerr("Received a tag:\n");
80 tag_save(stderr, tag);
81 tag_free(tag);
84 num_read = input_stream_read(is, buffer, sizeof(buffer),
85 &error);
86 if (num_read == 0) {
87 if (error != NULL) {
88 g_warning("%s", error->message);
89 g_error_free(error);
92 break;
95 num_written = write(1, buffer, num_read);
96 if (num_written <= 0)
97 break;
100 return 0;
103 int main(int argc, char **argv)
105 GError *error = NULL;
106 struct input_stream *is;
107 int ret;
109 if (argc != 2) {
110 g_printerr("Usage: run_input URI\n");
111 return 1;
114 /* initialize GLib */
116 g_thread_init(NULL);
117 g_log_set_default_handler(my_log_func, NULL);
119 /* initialize MPD */
121 tag_pool_init();
122 config_global_init();
124 #ifdef ENABLE_ARCHIVE
125 archive_plugin_init_all();
126 #endif
128 if (!input_stream_global_init(&error)) {
129 g_warning("%s", error->message);
130 g_error_free(error);
131 return 2;
134 /* open the stream and dump it */
136 is = input_stream_open(argv[1], &error);
137 if (is != NULL) {
138 ret = dump_input_stream(is);
139 input_stream_close(is);
140 } else {
141 if (error != NULL) {
142 g_warning("%s", error->message);
143 g_error_free(error);
144 } else
145 g_printerr("input_stream_open() failed\n");
146 ret = 2;
149 /* deinitialize everything */
151 input_stream_global_finish();
153 #ifdef ENABLE_ARCHIVE
154 archive_plugin_deinit_all();
155 #endif
157 config_global_finish();
158 tag_pool_deinit();
160 return ret;