run_convert.cxx 3.78 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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.
 */

/*
 * This program is a command line interface to MPD's PCM conversion
 * library (pcm_convert.c).
 *
 */

26
#include "ConfigGlue.hxx"
27 28
#include "pcm/AudioParser.hxx"
#include "pcm/AudioFormat.hxx"
29
#include "pcm/Convert.hxx"
30
#include "fs/Path.hxx"
31
#include "fs/NarrowPath.hxx"
32
#include "io/FileDescriptor.hxx"
33
#include "util/ConstBuffer.hxx"
34
#include "util/StaticFifoBuffer.hxx"
35
#include "util/OptionDef.hxx"
36
#include "util/OptionParser.hxx"
37
#include "util/PrintException.hxx"
38 39
#include "Log.hxx"
#include "LogBackend.hxx"
40

41
#include <cassert>
42 43
#include <stdexcept>

44
#include <stddef.h>
45
#include <stdlib.h>
46
#include <stdio.h>
47 48
#include <unistd.h>

49 50
struct CommandLine {
	AudioFormat in_audio_format, out_audio_format;
51

52
	FromNarrowPath config_path;
53

54 55 56 57
	bool verbose = false;
};

enum Option {
58
	OPTION_CONFIG,
59 60 61 62
	OPTION_VERBOSE,
};

static constexpr OptionDef option_defs[] = {
63
	{"config", 0, true, "Load a MPD configuration file"},
64
	{"verbose", 'v', false, "Verbose logging"},
65 66 67 68 69 70 71
};

static CommandLine
ParseCommandLine(int argc, char **argv)
{
	CommandLine c;

72 73 74
	OptionParser option_parser(option_defs, argc, argv);
	while (auto o = option_parser.Next()) {
		switch (Option(o.index)) {
75
		case OPTION_CONFIG:
76
			c.config_path = o.value;
77 78
			break;

79 80 81 82
		case OPTION_VERBOSE:
			c.verbose = true;
			break;
		}
83 84
	}

85 86 87
	auto args = option_parser.GetRemaining();
	if (args.size != 2)
		throw std::runtime_error("Usage: run_convert IN_FORMAT OUT_FORMAT <IN >OUT");
88

89 90 91 92 93
	c.in_audio_format = ParseAudioFormat(args[0], false);
	c.out_audio_format = c.in_audio_format.WithMask(ParseAudioFormat(args[1], false));
	return c;
}

94 95 96 97 98 99 100 101 102 103 104
class GlobalInit {
	const ConfigData config;

public:
	explicit GlobalInit(Path config_path)
		:config(AutoLoadConfigFile(config_path))
	{
		pcm_convert_global_init(config);
	}
};

105
static void
106 107
RunConvert(PcmConvert &convert, size_t in_frame_size,
	   FileDescriptor in_fd, FileDescriptor out_fd)
108
{
109 110 111
	in_fd.SetBinaryMode();
	out_fd.SetBinaryMode();

112
	StaticFifoBuffer<std::byte, 4096> buffer;
113 114

	while (true) {
115 116
		{
			const auto dest = buffer.Write();
117
			assert(!dest.empty());
118

119
			ssize_t nbytes = in_fd.Read(dest.data, dest.size);
120 121
			if (nbytes <= 0)
				break;
122

123 124
			buffer.Append(nbytes);
		}
125

126
		auto src = buffer.Read();
127
		assert(!src.empty());
128

129
		src.size -= src.size % in_frame_size;
130
		if (src.empty())
131 132
			continue;

133
		buffer.Consume(src.size);
134

135
		auto output = convert.Convert({src.data, src.size});
136
		out_fd.FullWrite(output.data, output.size);
137 138
	}

139
	while (true) {
140
		auto output = convert.Flush();
141 142 143
		if (output.IsNull())
			break;

144
		out_fd.FullWrite(output.data, output.size);
145
	}
146 147 148 149 150 151 152 153 154 155 156
}

int
main(int argc, char **argv)
try {
	const auto c = ParseCommandLine(argc, argv);

	SetLogThreshold(c.verbose ? LogLevel::DEBUG : LogLevel::INFO);
	const GlobalInit init(c.config_path);

	PcmConvert state(c.in_audio_format, c.out_audio_format);
157 158 159
	RunConvert(state, c.in_audio_format.GetFrameSize(),
		   FileDescriptor(STDIN_FILENO),
		   FileDescriptor(STDOUT_FILENO));
160

161
	return EXIT_SUCCESS;
162 163
} catch (...) {
	PrintException(std::current_exception());
164
	return EXIT_FAILURE;
165
}