decoder_control.c 2.39 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * http://www.musicpd.org
Max Kellermann's avatar
Max Kellermann committed
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.
Max Kellermann's avatar
Max Kellermann committed
18 19
 */

20
#include "decoder_control.h"
21

22 23
#include <assert.h>

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

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

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

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

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

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

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

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

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

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

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

86
bool
87
dc_seek(struct notify *notify, double where)
Max Kellermann's avatar
Max Kellermann committed
88
{
89
	assert(dc.state != DECODE_STATE_START);
Max Kellermann's avatar
Max Kellermann committed
90 91
	assert(where >= 0.0);

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

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

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

103
	return true;
Max Kellermann's avatar
Max Kellermann committed
104
}
105 106

void
107
dc_quit(void)
108
{
109 110
	assert(dc.thread != NULL);

111
	dc.quit = true;
112 113 114 115
	dc_command_async(DECODE_COMMAND_STOP);

	g_thread_join(dc.thread);
	dc.thread = NULL;
116
}