PlayerControl.cxx 6.62 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"
22 23

extern "C" {
24
#include "idle.h"
25 26 27 28
}

#include "song.h"
#include "DecoderControl.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "Main.hxx"
30

Max Kellermann's avatar
Max Kellermann committed
31 32
#include <assert.h>
#include <stdio.h>
33
#include <math.h>
Max Kellermann's avatar
Max Kellermann committed
34

35
static void
36
pc_enqueue_song_locked(struct player_control *pc, struct song *song);
37

38 39 40 41
player_control::player_control(unsigned _buffer_chunks,
			       unsigned _buffered_before_play)
	:buffer_chunks(_buffer_chunks),
	 buffered_before_play(_buffered_before_play),
42
	 thread(nullptr),
43 44 45 46 47
	 mutex(g_mutex_new()),
	 cond(g_cond_new()),
	 command(PLAYER_COMMAND_NONE),
	 state(PLAYER_STATE_STOP),
	 error_type(PLAYER_ERROR_NONE),
48 49
	 error(nullptr),
	 next_song(nullptr),
50 51
	 cross_fade_seconds(0),
	 mixramp_db(0),
52 53
	 mixramp_delay_seconds(nanf("")),
	 total_play_time(0)
54 55 56
{
}

57
player_control::~player_control()
58
{
59 60
	if (next_song != nullptr)
		song_free(next_song);
61

62 63
	g_cond_free(cond);
	g_mutex_free(mutex);
64 65 66
}

void
67
player_wait_decoder(struct player_control *pc, struct decoder_control *dc)
68
{
69 70
	assert(pc != NULL);
	assert(dc != NULL);
71
	assert(dc->client_cond == pc->cond);
72

73 74
	/* during this function, the decoder lock is held, because
	   we're waiting for the decoder thread */
75
	g_cond_wait(pc->cond, dc->mutex);
76 77
}

78
static void
79
player_command_wait_locked(struct player_control *pc)
80
{
81 82
	while (pc->command != PLAYER_COMMAND_NONE)
		g_cond_wait(main_cond, pc->mutex);
83 84 85
}

static void
86
player_command_locked(struct player_control *pc, enum player_command cmd)
87
{
88
	assert(pc->command == PLAYER_COMMAND_NONE);
89

90 91 92
	pc->command = cmd;
	player_signal(pc);
	player_command_wait_locked(pc);
93 94 95
}

static void
96
player_command(struct player_control *pc, enum player_command cmd)
97
{
98 99 100
	player_lock(pc);
	player_command_locked(pc, cmd);
	player_unlock(pc);
101 102
}

103
void
104
pc_play(struct player_control *pc, struct song *song)
105
{
106
	assert(song != NULL);
107

108
	player_lock(pc);
109

110 111
	if (pc->state != PLAYER_STATE_STOP)
		player_command_locked(pc, PLAYER_COMMAND_STOP);
112

113
	assert(pc->next_song == NULL);
114

115
	pc_enqueue_song_locked(pc, song);
116

117
	assert(pc->next_song == NULL);
118

119
	player_unlock(pc);
120

121
	idle_add(IDLE_PLAYER);
122 123
}

124 125
void
pc_cancel(struct player_control *pc)
126
{
127 128
	player_command(pc, PLAYER_COMMAND_CANCEL);
	assert(pc->next_song == NULL);
129
}
130

131
void
132
pc_stop(struct player_control *pc)
133
{
134 135
	player_command(pc, PLAYER_COMMAND_CLOSE_AUDIO);
	assert(pc->next_song == NULL);
136 137

	idle_add(IDLE_PLAYER);
138 139
}

140
void
141
pc_update_audio(struct player_control *pc)
142
{
143
	player_command(pc, PLAYER_COMMAND_UPDATE_AUDIO);
144 145
}

146
void
147
pc_kill(struct player_control *pc)
148
{
149
	assert(pc->thread != NULL);
150

151 152 153
	player_command(pc, PLAYER_COMMAND_EXIT);
	g_thread_join(pc->thread);
	pc->thread = NULL;
154 155

	idle_add(IDLE_PLAYER);
156 157
}

158
void
159
pc_pause(struct player_control *pc)
160
{
161
	player_lock(pc);
162

163 164
	if (pc->state != PLAYER_STATE_STOP) {
		player_command_locked(pc, PLAYER_COMMAND_PAUSE);
165 166 167
		idle_add(IDLE_PLAYER);
	}

168
	player_unlock(pc);
169 170 171
}

static void
172
pc_pause_locked(struct player_control *pc)
173
{
174 175
	if (pc->state != PLAYER_STATE_STOP) {
		player_command_locked(pc, PLAYER_COMMAND_PAUSE);
176 177
		idle_add(IDLE_PLAYER);
	}
178 179
}

180
void
181
pc_set_pause(struct player_control *pc, bool pause_flag)
182
{
183
	player_lock(pc);
184

185
	switch (pc->state) {
186 187 188 189 190
	case PLAYER_STATE_STOP:
		break;

	case PLAYER_STATE_PLAY:
		if (pause_flag)
191
			pc_pause_locked(pc);
192
		break;
193

194 195
	case PLAYER_STATE_PAUSE:
		if (!pause_flag)
196
			pc_pause_locked(pc);
197 198
		break;
	}
199

200
	player_unlock(pc);
201 202
}

203 204 205 206 207 208 209 210
void
pc_set_border_pause(struct player_control *pc, bool border_pause)
{
	player_lock(pc);
	pc->border_pause = border_pause;
	player_unlock(pc);
}

211
void
212
pc_get_status(struct player_control *pc, struct player_status *status)
213
{
214 215
	player_lock(pc);
	player_command_locked(pc, PLAYER_COMMAND_REFRESH);
216

217
	status->state = pc->state;
218

219 220 221 222 223
	if (pc->state != PLAYER_STATE_STOP) {
		status->bit_rate = pc->bit_rate;
		status->audio_format = pc->audio_format;
		status->total_time = pc->total_time;
		status->elapsed_time = pc->elapsed_time;
224
	}
225

226
	player_unlock(pc);
227 228
}

229
void
230 231
pc_set_error(struct player_control *pc, enum player_error type,
	     GError *error)
232
{
233 234 235 236 237 238 239 240 241
	assert(pc != NULL);
	assert(type != PLAYER_ERROR_NONE);
	assert(error != NULL);

	if (pc->error_type != PLAYER_ERROR_NONE)
	    g_error_free(pc->error);

	pc->error_type = type;
	pc->error = error;
242 243
}

244 245
void
pc_clear_error(struct player_control *pc)
246
{
247 248 249 250 251 252 253 254
	player_lock(pc);

	if (pc->error_type != PLAYER_ERROR_NONE) {
	    pc->error_type = PLAYER_ERROR_NONE;
	    g_error_free(pc->error);
	}

	player_unlock(pc);
255 256
}

257
char *
258
pc_get_error_message(struct player_control *pc)
259
{
260 261 262 263 264 265
	player_lock(pc);
	char *message = pc->error_type != PLAYER_ERROR_NONE
		? g_strdup(pc->error->message)
		: NULL;
	player_unlock(pc);
	return message;
266 267
}

268
static void
269
pc_enqueue_song_locked(struct player_control *pc, struct song *song)
270
{
271
	assert(song != NULL);
272
	assert(pc->next_song == NULL);
273

274 275
	pc->next_song = song;
	player_command_locked(pc, PLAYER_COMMAND_QUEUE);
276 277 278
}

void
279
pc_enqueue_song(struct player_control *pc, struct song *song)
280 281 282
{
	assert(song != NULL);

283 284 285
	player_lock(pc);
	pc_enqueue_song_locked(pc, song);
	player_unlock(pc);
286 287
}

288
bool
289
pc_seek(struct player_control *pc, struct song *song, float seek_time)
290 291 292
{
	assert(song != NULL);

293
	player_lock(pc);
294 295 296 297

	if (pc->next_song != NULL)
		song_free(pc->next_song);

298 299 300 301
	pc->next_song = song;
	pc->seek_where = seek_time;
	player_command_locked(pc, PLAYER_COMMAND_SEEK);
	player_unlock(pc);
302

303
	assert(pc->next_song == NULL);
304

305
	idle_add(IDLE_PLAYER);
306

307
	return true;
308 309
}

310
float
311
pc_get_cross_fade(const struct player_control *pc)
312
{
313
	return pc->cross_fade_seconds;
314 315
}

316
void
317
pc_set_cross_fade(struct player_control *pc, float cross_fade_seconds)
318
{
319 320
	if (cross_fade_seconds < 0)
		cross_fade_seconds = 0;
321
	pc->cross_fade_seconds = cross_fade_seconds;
322 323

	idle_add(IDLE_OPTIONS);
324 325
}

326
void
327
pc_set_mixramp_db(struct player_control *pc, float mixramp_db)
328
{
329
	pc->mixramp_db = mixramp_db;
330 331 332 333 334

	idle_add(IDLE_OPTIONS);
}

void
335
pc_set_mixramp_delay(struct player_control *pc, float mixramp_delay_seconds)
336
{
337
	pc->mixramp_delay_seconds = mixramp_delay_seconds;
338 339 340

	idle_add(IDLE_OPTIONS);
}