OutputCommand.cxx 2.51 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 22 23 24 25 26
 */

/*
 * Glue functions for controlling the audio outputs over the MPD
 * protocol.  These functions perform extra validation on all
 * parameters, because they might be from an untrusted source.
 *
 */

27
#include "OutputCommand.hxx"
28
#include "MultipleOutputs.hxx"
29
#include "Filtered.hxx"
30
#include "Client.hxx"
31
#include "player/Control.hxx"
Max Kellermann's avatar
Max Kellermann committed
32
#include "mixer/MixerControl.hxx"
33
#include "mixer/Volume.hxx"
Max Kellermann's avatar
Max Kellermann committed
34
#include "Idle.hxx"
35

36 37
extern unsigned audio_output_state_version;

38
bool
39
audio_output_enable_index(MultipleOutputs &outputs, unsigned idx)
40
{
41
	if (idx >= outputs.Size())
42 43
		return false;

44 45
	auto &ao = outputs.Get(idx);
	if (!ao.LockSetEnabled(true))
46
		return true;
47 48 49

	idle_add(IDLE_OUTPUT);

50
	if (ao.GetMixer() != nullptr) {
51 52 53 54
		InvalidateHardwareVolume();
		idle_add(IDLE_MIXER);
	}

55
	ao.GetClient().ApplyEnabled();
56

57 58
	++audio_output_state_version;

59 60 61 62
	return true;
}

bool
63
audio_output_disable_index(MultipleOutputs &outputs, unsigned idx)
64
{
65
	if (idx >= outputs.Size())
66 67
		return false;

68 69
	auto &ao = outputs.Get(idx);
	if (!ao.LockSetEnabled(false))
70
		return true;
71 72 73

	idle_add(IDLE_OUTPUT);

74
	auto *mixer = ao.GetMixer();
75
	if (mixer != nullptr) {
76
		mixer_close(mixer);
77
		InvalidateHardwareVolume();
78 79 80
		idle_add(IDLE_MIXER);
	}

81
	ao.GetClient().ApplyEnabled();
82

83 84
	++audio_output_state_version;

85 86
	return true;
}
87 88

bool
89
audio_output_toggle_index(MultipleOutputs &outputs, unsigned idx)
90
{
91
	if (idx >= outputs.Size())
92 93
		return false;

94 95
	auto &ao = outputs.Get(idx);
	const bool enabled = ao.LockToggleEnabled();
96 97 98
	idle_add(IDLE_OUTPUT);

	if (!enabled) {
99
		auto *mixer = ao.GetMixer();
100 101
		if (mixer != nullptr) {
			mixer_close(mixer);
102
			InvalidateHardwareVolume();
103 104 105 106
			idle_add(IDLE_MIXER);
		}
	}

107
	ao.GetClient().ApplyEnabled();
108 109 110 111 112

	++audio_output_state_version;

	return true;
}