run_decoder.cxx 2.5 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "ScopeIOThread.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 "util/Error.hxx"
30
#include "Log.hxx"
31

32 33
#include <stdexcept>

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

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

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

50
	const ScopeIOThread io_thread;
51

52 53
	Error error;
	if (!input_stream_global_init(error)) {
54
		LogError(error);
55
		return EXIT_FAILURE;
56 57
	}

58 59
	decoder_plugin_init_all();

60 61
	const DecoderPlugin *plugin = decoder_plugin_from_name(decoder_name);
	if (plugin == nullptr) {
62 63
		fprintf(stderr, "No such decoder: %s\n", decoder_name);
		return EXIT_FAILURE;
64 65
	}

66
	if (plugin->file_decode != nullptr) {
67
		plugin->FileDecode(decoder, Path::FromFS(uri));
68
	} else if (plugin->stream_decode != nullptr) {
69 70 71
		auto is = InputStream::OpenReady(uri, decoder.mutex,
						 decoder.cond, error);
		if (!is) {
72
			if (error.IsDefined())
73
				LogError(error);
74
			else
75
				fprintf(stderr, "InputStream::Open() failed\n");
76

77
			return EXIT_FAILURE;
78
		}
79

80
		plugin->StreamDecode(decoder, *is);
81
	} else {
82 83
		fprintf(stderr, "Decoder plugin is not usable\n");
		return EXIT_FAILURE;
84 85 86
	}

	decoder_plugin_deinit_all();
87
	input_stream_global_finish();
88 89

	if (!decoder.initialized) {
90 91
		fprintf(stderr, "Decoding failed\n");
		return EXIT_FAILURE;
92 93
	}

94 95 96 97
	return EXIT_SUCCESS;
} catch (const std::exception &e) {
	LogError(e);
	return EXIT_FAILURE;
98
}