decoder_control.c 3.13 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
#include "notify.h"
22

23 24
#include <assert.h>

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

27 28
void dc_init(void)
{
29 30 31
	dc.mutex = g_mutex_new();
	dc.cond = g_cond_new();

32 33 34 35
	dc.state = DECODE_STATE_STOP;
	dc.command = DECODE_COMMAND_NONE;
}

36 37
void dc_deinit(void)
{
38 39
	g_cond_free(dc.cond);
	g_mutex_free(dc.mutex);
40 41
}

42 43
static void
dc_command_wait_locked(struct notify *notify)
Max Kellermann's avatar
Max Kellermann committed
44 45
{
	while (dc.command != DECODE_COMMAND_NONE) {
46 47 48
		decoder_signal();
		decoder_unlock();

Max Kellermann's avatar
Max Kellermann committed
49
		notify_wait(notify);
50 51

		decoder_lock();
Max Kellermann's avatar
Max Kellermann committed
52 53 54
	}
}

55 56 57 58 59 60 61 62
void
dc_command_wait(struct notify *notify)
{
	decoder_lock();
	dc_command_wait_locked(notify);
	decoder_unlock();
}

63
static void
64
dc_command_locked(struct notify *notify, enum decoder_command cmd)
Max Kellermann's avatar
Max Kellermann committed
65 66
{
	dc.command = cmd;
67 68 69 70 71 72 73 74 75
	dc_command_wait_locked(notify);
}

static void
dc_command(struct notify *notify, enum decoder_command cmd)
{
	decoder_lock();
	dc_command_locked(notify, cmd);
	decoder_unlock();
Max Kellermann's avatar
Max Kellermann committed
76 77 78 79
}

static void dc_command_async(enum decoder_command cmd)
{
80 81
	decoder_lock();

Max Kellermann's avatar
Max Kellermann committed
82
	dc.command = cmd;
83 84 85
	decoder_signal();

	decoder_unlock();
Max Kellermann's avatar
Max Kellermann committed
86 87
}

88
void
89
dc_start(struct notify *notify, struct song *song)
Max Kellermann's avatar
Max Kellermann committed
90
{
91
	assert(dc.pipe != NULL);
Max Kellermann's avatar
Max Kellermann committed
92 93 94 95 96 97
	assert(song != NULL);

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

98 99
void
dc_start_async(struct song *song)
Max Kellermann's avatar
Max Kellermann committed
100
{
101
	assert(dc.pipe != NULL);
Max Kellermann's avatar
Max Kellermann committed
102 103 104 105 106 107
	assert(song != NULL);

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

108 109
void
dc_stop(struct notify *notify)
Max Kellermann's avatar
Max Kellermann committed
110
{
111 112
	decoder_lock();

113 114 115 116 117
	if (dc.command != DECODE_COMMAND_NONE)
		/* Attempt to cancel the current command.  If it's too
		   late and the decoder thread is already executing
		   the old command, we'll call STOP again in this
		   function (see below). */
118
		dc_command_locked(notify, DECODE_COMMAND_STOP);
119 120

	if (dc.state != DECODE_STATE_STOP && dc.state != DECODE_STATE_ERROR)
121 122 123
		dc_command_locked(notify, DECODE_COMMAND_STOP);

	decoder_unlock();
Max Kellermann's avatar
Max Kellermann committed
124 125
}

126
bool
127
dc_seek(struct notify *notify, double where)
Max Kellermann's avatar
Max Kellermann committed
128
{
129
	assert(dc.state != DECODE_STATE_START);
Max Kellermann's avatar
Max Kellermann committed
130 131
	assert(where >= 0.0);

132 133
	if (dc.state == DECODE_STATE_STOP ||
	    dc.state == DECODE_STATE_ERROR || !dc.seekable)
134
		return false;
Max Kellermann's avatar
Max Kellermann committed
135

Max Kellermann's avatar
Max Kellermann committed
136 137
	dc.seek_where = where;
	dc.seek_error = false;
Max Kellermann's avatar
Max Kellermann committed
138 139
	dc_command(notify, DECODE_COMMAND_SEEK);

Max Kellermann's avatar
Max Kellermann committed
140
	if (dc.seek_error)
141
		return false;
Max Kellermann's avatar
Max Kellermann committed
142

143
	return true;
Max Kellermann's avatar
Max Kellermann committed
144
}
145 146

void
147
dc_quit(void)
148
{
149 150
	assert(dc.thread != NULL);

151
	dc.quit = true;
152 153 154 155
	dc_command_async(DECODE_COMMAND_STOP);

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