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

#include "config.h"
21 22
#include "AutoConvertFilterPlugin.hxx"
#include "ConvertFilterPlugin.hxx"
23 24 25
#include "filter/FilterPlugin.hxx"
#include "filter/FilterInternal.hxx"
#include "filter/FilterRegistry.hxx"
26
#include "AudioFormat.hxx"
27
#include "config/ConfigData.hxx"
28
#include "util/ConstBuffer.hxx"
29

30 31
#include <assert.h>

32
class AutoConvertFilter final : public Filter {
33 34 35
	/**
	 * The underlying filter.
	 */
36
	Filter *filter;
37 38

	/**
39
	 * A convert_filter, just in case conversion is needed.  nullptr
40 41
	 * if unused.
	 */
42
	Filter *convert;
43

44 45
public:
	AutoConvertFilter(Filter *_filter):filter(_filter) {}
46
	~AutoConvertFilter() {
47
		delete filter;
48
	}
49

50 51
	virtual AudioFormat Open(AudioFormat &af, Error &error) override;
	virtual void Close() override;
52 53
	virtual ConstBuffer<void> FilterPCM(ConstBuffer<void> src,
					    Error &error) override;
54
};
55

56
AudioFormat
57
AutoConvertFilter::Open(AudioFormat &in_audio_format, Error &error)
58
{
59
	assert(in_audio_format.IsValid());
60 61 62

	/* open the "real" filter */

63 64
	AudioFormat child_audio_format = in_audio_format;
	AudioFormat out_audio_format = filter->Open(child_audio_format, error);
65 66
	if (!out_audio_format.IsDefined())
		return out_audio_format;
67 68 69

	/* need to convert? */

70
	if (in_audio_format != child_audio_format) {
71 72
		/* yes - create a convert_filter */

73
		const config_param empty;
74
		convert = filter_new(&convert_filter_plugin, empty, error);
75 76
		if (convert == nullptr) {
			filter->Close();
77
			return AudioFormat::Undefined();
78 79
		}

80 81
		AudioFormat audio_format2 = in_audio_format;
		AudioFormat audio_format3 =
82
			convert->Open(audio_format2, error);
83
		if (!audio_format3.IsDefined()) {
84 85
			delete convert;
			filter->Close();
86
			return AudioFormat::Undefined();
87 88
		}

89
		assert(audio_format2 == in_audio_format);
90

91 92 93 94 95
		if (!convert_filter_set(convert, child_audio_format, error)) {
			delete convert;
			filter->Close();
			return AudioFormat::Undefined();
		}
96 97
	} else
		/* no */
98
		convert = nullptr;
99 100 101 102

	return out_audio_format;
}

103 104
void
AutoConvertFilter::Close()
105
{
106 107 108
	if (convert != nullptr) {
		convert->Close();
		delete convert;
109 110
	}

111
	filter->Close();
112 113
}

114 115
ConstBuffer<void>
AutoConvertFilter::FilterPCM(ConstBuffer<void> src, Error &error)
116
{
117
	if (convert != nullptr) {
118 119
		src = convert->FilterPCM(src, error);
		if (src.IsNull())
120
			return nullptr;
121 122
	}

123
	return filter->FilterPCM(src, error);
124 125
}

126 127
Filter *
autoconvert_filter_new(Filter *filter)
128
{
129
	return new AutoConvertFilter(filter);
130
}