OutputCommand.cxx 2.53 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 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 "config.h"
28
#include "OutputCommand.hxx"
29
#include "MultipleOutputs.hxx"
30
#include "Filtered.hxx"
31
#include "Client.hxx"
32
#include "player/Control.hxx"
Max Kellermann's avatar
Max Kellermann committed
33
#include "mixer/MixerControl.hxx"
34
#include "mixer/Volume.hxx"
Max Kellermann's avatar
Max Kellermann committed
35
#include "Idle.hxx"
36

37 38
extern unsigned audio_output_state_version;

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

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

	idle_add(IDLE_OUTPUT);

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

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

58 59
	++audio_output_state_version;

60 61 62 63
	return true;
}

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

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

	idle_add(IDLE_OUTPUT);

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

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

84 85
	++audio_output_state_version;

86 87
	return true;
}
88 89

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

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

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

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

	++audio_output_state_version;

	return true;
}