run_decoder.cxx 2.28 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
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "event/Thread.hxx"
22
#include "decoder/DecoderList.hxx"
23 24
#include "decoder/DecoderPlugin.hxx"
#include "FakeDecoderAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
25 26
#include "input/Init.hxx"
#include "input/InputStream.hxx"
27
#include "fs/Path.hxx"
28
#include "AudioFormat.hxx"
29
#include "Log.hxx"
30

31 32
#include <stdexcept>

33 34
#include <assert.h>
#include <unistd.h>
35
#include <stdlib.h>
36
#include <stdio.h>
37

38
int main(int argc, char **argv)
39
try {
40
	if (argc != 3) {
41 42
		fprintf(stderr, "Usage: run_decoder DECODER URI >OUT\n");
		return EXIT_FAILURE;
43 44
	}

45
	FakeDecoder decoder;
46 47
	const char *const decoder_name = argv[1];
	const char *const uri = argv[2];
48

49 50
	EventThread io_thread;
	io_thread.Start();
51

52
	input_stream_global_init(io_thread.GetEventLoop());
53

54 55
	decoder_plugin_init_all();

56 57
	const DecoderPlugin *plugin = decoder_plugin_from_name(decoder_name);
	if (plugin == nullptr) {
58 59
		fprintf(stderr, "No such decoder: %s\n", decoder_name);
		return EXIT_FAILURE;
60 61
	}

62
	if (plugin->file_decode != nullptr) {
63
		plugin->FileDecode(decoder, Path::FromFS(uri));
64
	} else if (plugin->stream_decode != nullptr) {
65
		auto is = InputStream::OpenReady(uri, decoder.mutex,
66
						 decoder.cond);
67
		plugin->StreamDecode(decoder, *is);
68
	} else {
69 70
		fprintf(stderr, "Decoder plugin is not usable\n");
		return EXIT_FAILURE;
71 72 73
	}

	decoder_plugin_deinit_all();
74
	input_stream_global_finish();
75 76

	if (!decoder.initialized) {
77 78
		fprintf(stderr, "Decoding failed\n");
		return EXIT_FAILURE;
79 80
	}

81 82 83 84
	return EXIT_SUCCESS;
} catch (const std::exception &e) {
	LogError(e);
	return EXIT_FAILURE;
85
}