MixerControl.cxx 2.83 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
 * 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 21
#include "MixerControl.hxx"
#include "MixerInternal.hxx"
22

23
#include <cassert>
24

25
Mixer *
26
mixer_new(EventLoop &event_loop,
27
	  const MixerPlugin &plugin, AudioOutput &ao,
28
	  MixerListener &listener,
29
	  const ConfigBlock &block)
30
{
31
	Mixer *mixer = plugin.init(event_loop, ao, listener, block);
32

33
	assert(mixer == nullptr || mixer->IsPlugin(plugin));
34 35 36 37 38

	return mixer;
}

void
39
mixer_free(Mixer *mixer)
40
{
41
	assert(mixer != nullptr);
42

43 44 45 46
	/* mixers with the "global" flag set might still be open at
	   this point (see mixer_auto_close()) */
	mixer_close(mixer);

47
	delete mixer;
48 49
}

50 51
void
mixer_open(Mixer *mixer)
52
{
53
	assert(mixer != nullptr);
54

55
	const std::lock_guard<Mutex> protect(mixer->mutex);
56

57 58 59 60 61 62 63 64 65 66 67
	if (mixer->open)
		return;

	try {
		mixer->Open();
		mixer->open = true;
		mixer->failed = false;
	} catch (...) {
		mixer->failed = true;
		throw;
	}
68 69
}

70
static void
71
mixer_close_internal(Mixer *mixer)
72
{
73
	assert(mixer != nullptr);
74 75
	assert(mixer->open);

76
	mixer->Close();
77 78 79
	mixer->open = false;
}

80
void
81
mixer_close(Mixer *mixer)
82
{
83
	assert(mixer != nullptr);
84

85
	const std::lock_guard<Mutex> protect(mixer->mutex);
86

87 88
	if (mixer->open)
		mixer_close_internal(mixer);
89 90
}

91
void
92
mixer_auto_close(Mixer *mixer)
93
{
94
	if (!mixer->plugin.global)
95 96 97
		mixer_close(mixer);
}

98 99 100 101 102
/*
 * Close the mixer due to failure.  The mutex must be locked before
 * calling this function.
 */
static void
103
mixer_failed(Mixer *mixer)
104 105 106
{
	assert(mixer->open);

107
	mixer_close_internal(mixer);
108 109

	mixer->failed = true;
110 111
}

112
int
113
mixer_get_volume(Mixer *mixer)
114
{
115 116
	int volume;

117
	assert(mixer != nullptr);
118

119 120
	if (mixer->plugin.global && !mixer->failed)
		mixer_open(mixer);
121

122
	const std::lock_guard<Mutex> protect(mixer->mutex);
123 124

	if (mixer->open) {
125 126 127
		try {
			volume = mixer->GetVolume();
		} catch (...) {
128
			mixer_failed(mixer);
129 130
			throw;
		}
131 132 133
	} else
		volume = -1;

134
	return volume;
135 136
}

137 138
void
mixer_set_volume(Mixer *mixer, unsigned volume)
139
{
140
	assert(mixer != nullptr);
141
	assert(volume <= 100);
142

143 144
	if (mixer->plugin.global && !mixer->failed)
		mixer_open(mixer);
145

146
	const std::lock_guard<Mutex> protect(mixer->mutex);
147

148 149
	if (mixer->open)
		mixer->SetVolume(volume);
150
}