run_input.cxx 2.75 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "config.h"
21
#include "TagSave.hxx"
22
#include "tag/Tag.hxx"
23
#include "config/ConfigGlobal.hxx"
Max Kellermann's avatar
Max Kellermann committed
24 25
#include "input/InputStream.hxx"
#include "input/Init.hxx"
26
#include "event/Thread.hxx"
27
#include "thread/Cond.hxx"
28
#include "Log.hxx"
29 30
#include "fs/io/BufferedOutputStream.hxx"
#include "fs/io/StdioOutputStream.hxx"
31

32
#ifdef ENABLE_ARCHIVE
33
#include "archive/ArchiveList.hxx"
34 35
#endif

36 37
#include <stdexcept>

38
#include <unistd.h>
39
#include <stdlib.h>
40

41
class GlobalInit {
42
	EventThread io_thread;
43 44 45

public:
	GlobalInit() {
46
		io_thread.Start();
47 48 49 50
		config_global_init();
#ifdef ENABLE_ARCHIVE
		archive_plugin_init_all();
#endif
51
		input_stream_global_init(io_thread.GetEventLoop());
52 53 54 55 56 57 58 59 60 61 62
	}

	~GlobalInit() {
		input_stream_global_finish();
#ifdef ENABLE_ARCHIVE
		archive_plugin_deinit_all();
#endif
		config_global_finish();
	}
};

63 64 65 66 67 68 69 70 71
static void
tag_save(FILE *file, const Tag &tag)
{
	StdioOutputStream sos(file);
	BufferedOutputStream bos(sos);
	tag_save(bos, tag);
	bos.Flush();
}

72
static int
73
dump_input_stream(InputStream *is)
74
{
75
	const std::lock_guard<Mutex> protect(is->mutex);
76

77 78
	/* print meta data */

79 80
	if (is->HasMimeType())
		fprintf(stderr, "MIME type: %s\n", is->GetMimeType());
81 82 83

	/* read data and tags from the stream */

84 85
	while (!is->IsEOF()) {
		Tag *tag = is->ReadTag();
86
		if (tag != NULL) {
87
			fprintf(stderr, "Received a tag:\n");
Max Kellermann's avatar
Max Kellermann committed
88 89
			tag_save(stderr, *tag);
			delete tag;
90 91
		}

92
		char buffer[4096];
93 94
		size_t num_read = is->Read(buffer, sizeof(buffer));
		if (num_read == 0)
95 96
			break;

97
		ssize_t num_written = write(1, buffer, num_read);
98 99 100 101
		if (num_written <= 0)
			break;
	}

102
	is->Check();
103

104 105 106 107
	return 0;
}

int main(int argc, char **argv)
108
try {
109
	if (argc != 2) {
110 111
		fprintf(stderr, "Usage: run_input URI\n");
		return EXIT_FAILURE;
112 113 114 115
	}

	/* initialize MPD */

116
	const GlobalInit init;
117 118 119

	/* open the stream and dump it */

120 121 122 123
	Mutex mutex;
	Cond cond;
	auto is = InputStream::OpenReady(argv[1], mutex, cond);
	return dump_input_stream(is.get());
124 125 126
} catch (const std::exception &e) {
	LogError(e);
	return EXIT_FAILURE;
127
}