AutoConvertFilterPlugin.cxx 3.09 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20 21
#include "AutoConvertFilterPlugin.hxx"
#include "ConvertFilterPlugin.hxx"
22 23
#include "filter/Filter.hxx"
#include "filter/Prepared.hxx"
24
#include "pcm/AudioFormat.hxx"
25
#include "util/ConstBuffer.hxx"
26

27
#include <cassert>
28 29
#include <memory>

30
class AutoConvertFilter final : public Filter {
31 32 33
	/**
	 * The underlying filter.
	 */
34
	std::unique_ptr<Filter> filter;
35 36

	/**
37
	 * A convert_filter, just in case conversion is needed.  nullptr
38 39
	 * if unused.
	 */
40
	std::unique_ptr<Filter> convert;
41

42
public:
43 44
	AutoConvertFilter(std::unique_ptr<Filter> &&_filter,
			  std::unique_ptr<Filter> &&_convert)
45 46
		:Filter(_filter->GetOutAudioFormat()),
		 filter(std::move(_filter)), convert(std::move(_convert)) {}
47

48
	void Reset() noexcept override {
49 50 51 52 53 54
		filter->Reset();

		if (convert)
			convert->Reset();
	}

55
	ConstBuffer<void> FilterPCM(ConstBuffer<void> src) override;
56
	ConstBuffer<void> Flush() override;
57
};
58

59 60 61 62
class PreparedAutoConvertFilter final : public PreparedFilter {
	/**
	 * The underlying filter.
	 */
63
	std::unique_ptr<PreparedFilter> filter;
64 65

public:
Max Kellermann's avatar
Max Kellermann committed
66
	explicit PreparedAutoConvertFilter(std::unique_ptr<PreparedFilter> _filter) noexcept
67
		:filter(std::move(_filter)) {}
68

69
	std::unique_ptr<Filter> Open(AudioFormat &af) override;
70 71
};

72
std::unique_ptr<Filter>
73
PreparedAutoConvertFilter::Open(AudioFormat &in_audio_format)
74
{
75
	assert(in_audio_format.IsValid());
76 77 78

	/* open the "real" filter */

79
	AudioFormat child_audio_format = in_audio_format;
80
	auto new_filter = filter->Open(child_audio_format);
81 82 83

	/* need to convert? */

84
	std::unique_ptr<Filter> convert;
85
	if (in_audio_format != child_audio_format) {
86 87
		/* yes - create a convert_filter */

88
		convert.reset(convert_filter_new(in_audio_format,
89
						 child_audio_format));
90 91
	}

92 93
	return std::make_unique<AutoConvertFilter>(std::move(new_filter),
						   std::move(convert));
94 95
}

96
ConstBuffer<void>
97
AutoConvertFilter::FilterPCM(ConstBuffer<void> src)
98
{
99
	if (convert != nullptr)
100
		src = convert->FilterPCM(src);
101

102
	return filter->FilterPCM(src);
103 104
}

105 106 107 108 109 110 111 112 113 114 115 116
ConstBuffer<void>
AutoConvertFilter::Flush()
{
	if (convert != nullptr) {
		auto result = convert->Flush();
		if (!result.IsNull())
			return filter->FilterPCM(result);
	}

	return filter->Flush();
}

117 118
std::unique_ptr<PreparedFilter>
autoconvert_filter_new(std::unique_ptr<PreparedFilter> filter) noexcept
119
{
120
	return std::make_unique<PreparedAutoConvertFilter>(std::move(filter));
121
}