MixerControl.cxx 2.85 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 22
#include "MixerControl.hxx"
#include "MixerInternal.hxx"
23

24
#include <assert.h>
25

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

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

	return mixer;
}

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

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

48
	delete mixer;
49 50
}

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

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

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

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

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

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

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

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

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

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

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

108
	mixer_close_internal(mixer);
109 110

	mixer->failed = true;
111 112
}

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

118
	assert(mixer != nullptr);
119

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

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

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

135
	return volume;
136 137
}

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

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

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

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