run_decoder.cxx 2.45 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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
#include "stdbin.h"
32 33 34

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

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

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

49
	const ScopeIOThread io_thread;
50

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

57 58
	decoder_plugin_init_all();

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

65
	if (plugin->file_decode != nullptr) {
66
		plugin->FileDecode(decoder, Path::FromFS(uri));
67
	} else if (plugin->stream_decode != nullptr) {
68
		InputStream *is =
69 70
			InputStream::OpenReady(uri, decoder.mutex,
					       decoder.cond, error);
71
		if (is == NULL) {
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

82
		delete is;
83
	} else {
84 85
		fprintf(stderr, "Decoder plugin is not usable\n");
		return EXIT_FAILURE;
86 87 88
	}

	decoder_plugin_deinit_all();
89
	input_stream_global_finish();
90 91

	if (!decoder.initialized) {
92 93
		fprintf(stderr, "Decoding failed\n");
		return EXIT_FAILURE;
94 95 96 97
	}

	return 0;
}