PlayerControl.cxx 5.8 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 "PlayerControl.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "Idle.hxx"
23
#include "Song.hxx"
24
#include "DecoderControl.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "Main.hxx"
26

27 28
#include <cmath>

Max Kellermann's avatar
Max Kellermann committed
29 30 31
#include <assert.h>
#include <stdio.h>

32
static void
33
pc_enqueue_song_locked(struct player_control *pc, Song *song);
34

35 36 37 38
player_control::player_control(unsigned _buffer_chunks,
			       unsigned _buffered_before_play)
	:buffer_chunks(_buffer_chunks),
	 buffered_before_play(_buffered_before_play),
39
	 thread(nullptr),
40 41 42
	 command(PLAYER_COMMAND_NONE),
	 state(PLAYER_STATE_STOP),
	 error_type(PLAYER_ERROR_NONE),
43 44
	 error(nullptr),
	 next_song(nullptr),
45 46
	 cross_fade_seconds(0),
	 mixramp_db(0),
47 48 49 50 51
#if defined(__GLIBCXX__) && !defined(_GLIBCXX_USE_C99_MATH_TR1)
	 /* workaround: on MacPorts, this option is disabled on gcc47,
	    and therefore std::nanf() is not available */
	 mixramp_delay_seconds(nanf("")),
#else
52
	 mixramp_delay_seconds(std::nanf("")),
53
#endif
54 55
	 total_play_time(0),
	 border_pause(false)
56 57 58
{
}

59
player_control::~player_control()
60
{
61
	if (next_song != nullptr)
62
		next_song->Free();
63 64 65
}

static void
66
player_command_wait_locked(struct player_control *pc)
67
{
68
	while (pc->command != PLAYER_COMMAND_NONE)
69
		pc->ClientWait();
70 71 72
}

static void
73
player_command_locked(struct player_control *pc, enum player_command cmd)
74
{
75
	assert(pc->command == PLAYER_COMMAND_NONE);
76

77
	pc->command = cmd;
78
	pc->Signal();
79
	player_command_wait_locked(pc);
80 81 82
}

static void
83
player_command(struct player_control *pc, enum player_command cmd)
84
{
85
	pc->Lock();
86
	player_command_locked(pc, cmd);
87
	pc->Unlock();
88 89
}

90
void
91
player_control::Play(Song *song)
92
{
93
	assert(song != NULL);
94

95
	Lock();
96

97 98
	if (state != PLAYER_STATE_STOP)
		player_command_locked(this, PLAYER_COMMAND_STOP);
99

100
	assert(next_song == nullptr);
101

102
	pc_enqueue_song_locked(this, song);
103

104
	assert(next_song == nullptr);
105

106
	Unlock();
107 108
}

109
void
110
player_control::Cancel()
111
{
112 113
	player_command(this, PLAYER_COMMAND_CANCEL);
	assert(next_song == NULL);
114
}
115

116
void
117
player_control::Stop()
118
{
119 120
	player_command(this, PLAYER_COMMAND_CLOSE_AUDIO);
	assert(next_song == nullptr);
121 122

	idle_add(IDLE_PLAYER);
123 124
}

125
void
126
player_control::UpdateAudio()
127
{
128
	player_command(this, PLAYER_COMMAND_UPDATE_AUDIO);
129 130
}

131
void
132
player_control::Kill()
133
{
134
	assert(thread != NULL);
135

136 137 138
	player_command(this, PLAYER_COMMAND_EXIT);
	g_thread_join(thread);
	thread = NULL;
139 140

	idle_add(IDLE_PLAYER);
141 142
}

143
void
144
player_control::Pause()
145
{
146
	Lock();
147

148 149
	if (state != PLAYER_STATE_STOP) {
		player_command_locked(this, PLAYER_COMMAND_PAUSE);
150 151 152
		idle_add(IDLE_PLAYER);
	}

153
	Unlock();
154 155 156
}

static void
157
pc_pause_locked(struct player_control *pc)
158
{
159 160
	if (pc->state != PLAYER_STATE_STOP) {
		player_command_locked(pc, PLAYER_COMMAND_PAUSE);
161 162
		idle_add(IDLE_PLAYER);
	}
163 164
}

165
void
166
player_control::SetPause(bool pause_flag)
167
{
168
	Lock();
169

170
	switch (state) {
171 172 173 174 175
	case PLAYER_STATE_STOP:
		break;

	case PLAYER_STATE_PLAY:
		if (pause_flag)
176
			pc_pause_locked(this);
177
		break;
178

179 180
	case PLAYER_STATE_PAUSE:
		if (!pause_flag)
181
			pc_pause_locked(this);
182 183
		break;
	}
184

185
	Unlock();
186 187
}

188
void
189
player_control::SetBorderPause(bool _border_pause)
190
{
191 192 193
	Lock();
	border_pause = _border_pause;
	Unlock();
194 195
}

196 197
player_status
player_control::GetStatus()
198
{
199
	player_status status;
200

201 202
	Lock();
	player_command_locked(this, PLAYER_COMMAND_REFRESH);
203

204 205 206 207 208 209 210
	status.state = state;

	if (state != PLAYER_STATE_STOP) {
		status.bit_rate = bit_rate;
		status.audio_format = audio_format;
		status.total_time = total_time;
		status.elapsed_time = elapsed_time;
211
	}
212

213 214 215
	Unlock();

	return status;
216 217
}

218
void
219
player_control::SetError(player_error type, GError *_error)
220
{
221
	assert(type != PLAYER_ERROR_NONE);
222
	assert(_error != NULL);
223

224 225
	if (error_type != PLAYER_ERROR_NONE)
	    g_error_free(error);
226

227 228
	error_type = type;
	error = _error;
229 230
}

231
void
232
player_control::ClearError()
233
{
234
	Lock();
235

236 237 238
	if (error_type != PLAYER_ERROR_NONE) {
	    error_type = PLAYER_ERROR_NONE;
	    g_error_free(error);
239 240
	}

241
	Unlock();
242 243
}

244
char *
245
player_control::GetErrorMessage() const
246
{
247 248 249
	Lock();
	char *message = error_type != PLAYER_ERROR_NONE
		? g_strdup(error->message)
250
		: NULL;
251
	Unlock();
252
	return message;
253 254
}

255
static void
256
pc_enqueue_song_locked(struct player_control *pc, Song *song)
257
{
258
	assert(song != NULL);
259
	assert(pc->next_song == NULL);
260

261 262
	pc->next_song = song;
	player_command_locked(pc, PLAYER_COMMAND_QUEUE);
263 264 265
}

void
266
player_control::EnqueueSong(Song *song)
267 268 269
{
	assert(song != NULL);

270 271 272
	Lock();
	pc_enqueue_song_locked(this, song);
	Unlock();
273 274
}

275
bool
276
player_control::Seek(Song *song, float seek_time)
277 278 279
{
	assert(song != NULL);

280
	Lock();
281

282
	if (next_song != nullptr)
283
		next_song->Free();
284

285 286 287 288
	next_song = song;
	seek_where = seek_time;
	player_command_locked(this, PLAYER_COMMAND_SEEK);
	Unlock();
289

290
	assert(next_song == nullptr);
291

292
	idle_add(IDLE_PLAYER);
293

294
	return true;
295 296
}

297
void
298
player_control::SetCrossFade(float _cross_fade_seconds)
299
{
300 301 302
	if (_cross_fade_seconds < 0)
		_cross_fade_seconds = 0;
	cross_fade_seconds = _cross_fade_seconds;
303 304

	idle_add(IDLE_OPTIONS);
305 306
}

307
void
308
player_control::SetMixRampDb(float _mixramp_db)
309
{
310
	mixramp_db = _mixramp_db;
311 312 313 314 315

	idle_add(IDLE_OPTIONS);
}

void
316
player_control::SetMixRampDelay(float _mixramp_delay_seconds)
317
{
318
	mixramp_delay_seconds = _mixramp_delay_seconds;
319 320 321

	idle_add(IDLE_OPTIONS);
}