Control.cxx 5.51 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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
#include "Control.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "Idle.hxx"
23
#include "DetachedSong.hxx"
24
#include "output/MultipleOutputs.hxx"
25

26
#include <algorithm>
27

Max Kellermann's avatar
Max Kellermann committed
28
#include <assert.h>
29

30 31
PlayerControl::PlayerControl(PlayerListener &_listener,
			     MultipleOutputs &_outputs,
32
			     unsigned _buffer_chunks,
33
			     unsigned _buffered_before_play,
34
			     AudioFormat _configured_audio_format,
35
			     const ReplayGainConfig &_replay_gain_config)
36
	:listener(_listener), outputs(_outputs),
37
	 buffer_chunks(_buffer_chunks),
38
	 buffered_before_play(_buffered_before_play),
39
	 configured_audio_format(_configured_audio_format),
40
	 replay_gain_config(_replay_gain_config)
41 42 43
{
}

44
PlayerControl::~PlayerControl()
45
{
46 47
	delete next_song;
	delete tagged_song;
48 49
}

50 51 52 53 54 55 56 57 58 59 60 61
bool
PlayerControl::WaitOutputConsumed(unsigned threshold)
{
	bool result = outputs.Check() < threshold;
	if (!result && command == PlayerCommand::NONE) {
		Wait();
		result = outputs.Check() < threshold;
	}

	return result;
}

62 63
void
PlayerControl::Play(DetachedSong *song)
64
{
65
	assert(song != nullptr);
66

67
	const std::lock_guard<Mutex> protect(mutex);
68
	SeekLocked(song, SongTime::zero());
69

70
	if (state == PlayerState::PAUSE)
71 72 73
		/* if the player was paused previously, we need to
		   unpause it */
		PauseLocked();
74 75
}

76
void
77
PlayerControl::LockCancel()
78
{
79
	LockSynchronousCommand(PlayerCommand::CANCEL);
80
	assert(next_song == nullptr);
81
}
82

83
void
84
PlayerControl::LockStop()
85
{
86
	LockSynchronousCommand(PlayerCommand::CLOSE_AUDIO);
87
	assert(next_song == nullptr);
88 89

	idle_add(IDLE_PLAYER);
90 91
}

92
void
93
PlayerControl::LockUpdateAudio()
94
{
95
	LockSynchronousCommand(PlayerCommand::UPDATE_AUDIO);
96 97
}

98
void
99
PlayerControl::Kill()
100
{
101
	assert(thread.IsDefined());
102

103
	LockSynchronousCommand(PlayerCommand::EXIT);
104
	thread.Join();
105 106

	idle_add(IDLE_PLAYER);
107 108
}

109
void
110
PlayerControl::PauseLocked()
111
{
112 113
	if (state != PlayerState::STOP) {
		SynchronousCommand(PlayerCommand::PAUSE);
114 115 116 117
		idle_add(IDLE_PLAYER);
	}
}

118
void
119
PlayerControl::LockPause()
120
{
121
	const std::lock_guard<Mutex> protect(mutex);
122
	PauseLocked();
123 124
}

125
void
126
PlayerControl::LockSetPause(bool pause_flag)
127
{
128
	const std::lock_guard<Mutex> protect(mutex);
129

130
	switch (state) {
131
	case PlayerState::STOP:
132 133
		break;

134
	case PlayerState::PLAY:
135
		if (pause_flag)
136
			PauseLocked();
137
		break;
138

139
	case PlayerState::PAUSE:
140
		if (!pause_flag)
141
			PauseLocked();
142 143 144 145
		break;
	}
}

146
void
147
PlayerControl::LockSetBorderPause(bool _border_pause)
148
{
149
	const std::lock_guard<Mutex> protect(mutex);
150
	border_pause = _border_pause;
151 152
}

153
player_status
154
PlayerControl::LockGetStatus()
155
{
156
	player_status status;
157

158
	const std::lock_guard<Mutex> protect(mutex);
159
	SynchronousCommand(PlayerCommand::REFRESH);
160

161 162
	status.state = state;

163
	if (state != PlayerState::STOP) {
164 165 166 167
		status.bit_rate = bit_rate;
		status.audio_format = audio_format;
		status.total_time = total_time;
		status.elapsed_time = elapsed_time;
168
	}
169

170
	return status;
171 172
}

173
void
174
PlayerControl::SetError(PlayerError type, std::exception_ptr &&_error)
175
{
176
	assert(type != PlayerError::NONE);
177
	assert(_error);
178

179
	error_type = type;
180
	error = std::move(_error);
181 182
}

183
void
184
PlayerControl::LockClearError()
185
{
186
	const std::lock_guard<Mutex> protect(mutex);
187
	ClearError();
188 189
}

190
void
191
PlayerControl::LockSetTaggedSong(const DetachedSong &song)
192
{
193
	const std::lock_guard<Mutex> protect(mutex);
194 195
	delete tagged_song;
	tagged_song = new DetachedSong(song);
196 197 198
}

void
199
PlayerControl::ClearTaggedSong()
200
{
201 202
	delete tagged_song;
	tagged_song = nullptr;
203 204
}

205
void
206
PlayerControl::LockEnqueueSong(DetachedSong *song)
207
{
208
	assert(song != nullptr);
209

210
	const std::lock_guard<Mutex> protect(mutex);
211
	EnqueueSongLocked(song);
212 213
}

214 215
void
PlayerControl::SeekLocked(DetachedSong *song, SongTime t)
216
{
217
	assert(song != nullptr);
218

219 220 221 222
	/* to issue the SEEK command below, we need to clear the
	   "next_song" attribute with the CANCEL command */
	/* optimization TODO: if the decoder happens to decode that
	   song already, don't cancel that */
223 224 225 226 227
	if (next_song != nullptr)
		SynchronousCommand(PlayerCommand::CANCEL);

	assert(next_song == nullptr);

228
	ClearError();
229
	next_song = song;
230
	seek_time = t;
231
	SynchronousCommand(PlayerCommand::SEEK);
232

233
	assert(next_song == nullptr);
234 235

	if (error_type != PlayerError::NONE) {
236 237
		assert(error);
		std::rethrow_exception(error);
238 239
	}

240
	assert(!error);
241 242
}

243 244
void
PlayerControl::LockSeek(DetachedSong *song, SongTime t)
245 246 247
{
	assert(song != nullptr);

248
	{
249
		const std::lock_guard<Mutex> protect(mutex);
250
		SeekLocked(song, t);
251
	}
252

253
	idle_add(IDLE_PLAYER);
254 255
}

256
void
257
PlayerControl::SetCrossFade(float _cross_fade_seconds)
258
{
259 260
	if (_cross_fade_seconds < 0)
		_cross_fade_seconds = 0;
261
	cross_fade.duration = _cross_fade_seconds;
262 263

	idle_add(IDLE_OPTIONS);
264 265
}

266
void
267
PlayerControl::SetMixRampDb(float _mixramp_db)
268
{
269
	cross_fade.mixramp_db = _mixramp_db;
270 271 272 273 274

	idle_add(IDLE_OPTIONS);
}

void
275
PlayerControl::SetMixRampDelay(float _mixramp_delay_seconds)
276
{
277
	cross_fade.mixramp_delay = _mixramp_delay_seconds;
278 279 280

	idle_add(IDLE_OPTIONS);
}