decoder_control.c 2.24 KB
Newer Older
Max Kellermann's avatar
Max Kellermann committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* the Music Player Daemon (MPD)
 * Copyright (C) 2008 Max Kellermann <max@duempel.org>
 * This project's homepage is: 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.
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

19
#include "decoder_control.h"
20

21 22
#include <assert.h>

23
struct decoder_control dc;
Max Kellermann's avatar
Max Kellermann committed
24

25 26 27 28 29 30 31
void dc_init(void)
{
	notify_init(&dc.notify);
	dc.state = DECODE_STATE_STOP;
	dc.command = DECODE_COMMAND_NONE;
}

32 33 34 35 36
void dc_deinit(void)
{
	notify_deinit(&dc.notify);
}

37 38
void
dc_command_wait(struct notify *notify)
Max Kellermann's avatar
Max Kellermann committed
39 40 41 42 43 44 45
{
	while (dc.command != DECODE_COMMAND_NONE) {
		notify_signal(&dc.notify);
		notify_wait(notify);
	}
}

46 47
static void
dc_command(struct notify *notify, enum decoder_command cmd)
Max Kellermann's avatar
Max Kellermann committed
48 49 50 51 52 53 54 55 56 57 58
{
	dc.command = cmd;
	dc_command_wait(notify);
}

static void dc_command_async(enum decoder_command cmd)
{
	dc.command = cmd;
	notify_signal(&dc.notify);
}

59
void
60
dc_start(struct notify *notify, struct song *song)
Max Kellermann's avatar
Max Kellermann committed
61 62 63 64 65 66 67
{
	assert(song != NULL);

	dc.next_song = song;
	dc_command(notify, DECODE_COMMAND_START);
}

68 69
void
dc_start_async(struct song *song)
Max Kellermann's avatar
Max Kellermann committed
70 71 72 73 74 75 76
{
	assert(song != NULL);

	dc.next_song = song;
	dc_command_async(DECODE_COMMAND_START);
}

77 78
void
dc_stop(struct notify *notify)
Max Kellermann's avatar
Max Kellermann committed
79 80
{
	if (dc.command == DECODE_COMMAND_START ||
81
	    (dc.state != DECODE_STATE_STOP && dc.state != DECODE_STATE_ERROR))
Max Kellermann's avatar
Max Kellermann committed
82 83 84
		dc_command(notify, DECODE_COMMAND_STOP);
}

85
bool
86
dc_seek(struct notify *notify, double where)
Max Kellermann's avatar
Max Kellermann committed
87 88 89
{
	assert(where >= 0.0);

90 91
	if (dc.state == DECODE_STATE_STOP ||
	    dc.state == DECODE_STATE_ERROR || !dc.seekable)
92
		return false;
Max Kellermann's avatar
Max Kellermann committed
93

Max Kellermann's avatar
Max Kellermann committed
94 95
	dc.seek_where = where;
	dc.seek_error = false;
Max Kellermann's avatar
Max Kellermann committed
96 97
	dc_command(notify, DECODE_COMMAND_SEEK);

Max Kellermann's avatar
Max Kellermann committed
98
	if (dc.seek_error)
99
		return false;
Max Kellermann's avatar
Max Kellermann committed
100

101
	return true;
Max Kellermann's avatar
Max Kellermann committed
102
}