run_decoder.cxx 2.25 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 "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
	const ScopeIOThread io_thread;
50

51
	input_stream_global_init();
52

53 54
	decoder_plugin_init_all();

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

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

	decoder_plugin_deinit_all();
73
	input_stream_global_finish();
74 75

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

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