decoder_control.c 4.61 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 * 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 "config.h"
21
#include "decoder_control.h"
22
#include "pipe.h"
23
#include "song.h"
24

25
#include <assert.h>
26 27 28

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "decoder_control"
29

30
struct decoder_control *
31
dc_new(GCond *client_cond)
32
{
33 34
	struct decoder_control *dc = g_new(struct decoder_control, 1);

35 36 37 38
	dc->thread = NULL;

	dc->mutex = g_mutex_new();
	dc->cond = g_cond_new();
39
	dc->client_cond = client_cond;
40

41 42
	dc->state = DECODE_STATE_STOP;
	dc->command = DECODE_COMMAND_NONE;
43

44 45
	dc->song = NULL;

46 47
	dc->replay_gain_db = 0;
	dc->replay_gain_prev_db = 0;
48 49 50
	dc->mixramp_start = NULL;
	dc->mixramp_end = NULL;
	dc->mixramp_prev_end = NULL;
51 52

	return dc;
53 54
}

55
void
56
dc_free(struct decoder_control *dc)
57
{
58 59
	dc_clear_error(dc);

60 61 62
	if (dc->song != NULL)
		song_free(dc->song);

63 64
	g_cond_free(dc->cond);
	g_mutex_free(dc->mutex);
65 66 67
	g_free(dc->mixramp_start);
	g_free(dc->mixramp_end);
	g_free(dc->mixramp_prev_end);
68
	g_free(dc);
69 70
}

71
static void
72
dc_command_wait_locked(struct decoder_control *dc)
Max Kellermann's avatar
Max Kellermann committed
73
{
74
	while (dc->command != DECODE_COMMAND_NONE)
75
		g_cond_wait(dc->client_cond, dc->mutex);
Max Kellermann's avatar
Max Kellermann committed
76 77
}

78
static void
79
dc_command_locked(struct decoder_control *dc, enum decoder_command cmd)
Max Kellermann's avatar
Max Kellermann committed
80
{
81
	dc->command = cmd;
82
	decoder_signal(dc);
83
	dc_command_wait_locked(dc);
84 85 86
}

static void
87
dc_command(struct decoder_control *dc, enum decoder_command cmd)
88
{
89
	decoder_lock(dc);
90
	dc_clear_error(dc);
91 92
	dc_command_locked(dc, cmd);
	decoder_unlock(dc);
Max Kellermann's avatar
Max Kellermann committed
93 94
}

95 96
static void
dc_command_async(struct decoder_control *dc, enum decoder_command cmd)
Max Kellermann's avatar
Max Kellermann committed
97
{
98
	decoder_lock(dc);
99

100 101
	dc->command = cmd;
	decoder_signal(dc);
102

103
	decoder_unlock(dc);
Max Kellermann's avatar
Max Kellermann committed
104 105
}

106 107 108 109 110 111 112 113 114 115 116 117 118 119
bool
decoder_is_current_song(const struct decoder_control *dc,
			const struct song *song)
{
	assert(dc != NULL);
	assert(song != NULL);

	switch (dc->state) {
	case DECODE_STATE_STOP:
	case DECODE_STATE_ERROR:
		return false;

	case DECODE_STATE_START:
	case DECODE_STATE_DECODE:
120
		return song_equals(dc->song, song);
121 122 123 124 125 126
	}

	assert(false);
	return false;
}

127
void
128
dc_start(struct decoder_control *dc, struct song *song,
129
	 unsigned start_ms, unsigned end_ms,
130
	 struct music_buffer *buffer, struct music_pipe *pipe)
Max Kellermann's avatar
Max Kellermann committed
131 132
{
	assert(song != NULL);
133 134
	assert(buffer != NULL);
	assert(pipe != NULL);
135
	assert(music_pipe_empty(pipe));
Max Kellermann's avatar
Max Kellermann committed
136

137 138 139
	if (dc->song != NULL)
		song_free(dc->song);

140
	dc->song = song;
141 142
	dc->start_ms = start_ms;
	dc->end_ms = end_ms;
143 144
	dc->buffer = buffer;
	dc->pipe = pipe;
145
	dc_command(dc, DECODE_COMMAND_START);
Max Kellermann's avatar
Max Kellermann committed
146 147
}

148
void
149
dc_stop(struct decoder_control *dc)
Max Kellermann's avatar
Max Kellermann committed
150
{
151
	decoder_lock(dc);
152

153
	if (dc->command != DECODE_COMMAND_NONE)
154 155 156 157
		/* 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). */
158
		dc_command_locked(dc, DECODE_COMMAND_STOP);
159

160 161
	if (dc->state != DECODE_STATE_STOP && dc->state != DECODE_STATE_ERROR)
		dc_command_locked(dc, DECODE_COMMAND_STOP);
162

163
	decoder_unlock(dc);
Max Kellermann's avatar
Max Kellermann committed
164 165
}

166
bool
167
dc_seek(struct decoder_control *dc, double where)
Max Kellermann's avatar
Max Kellermann committed
168
{
169
	assert(dc->state != DECODE_STATE_START);
Max Kellermann's avatar
Max Kellermann committed
170 171
	assert(where >= 0.0);

172 173
	if (dc->state == DECODE_STATE_STOP ||
	    dc->state == DECODE_STATE_ERROR || !dc->seekable)
174
		return false;
Max Kellermann's avatar
Max Kellermann committed
175

176 177 178
	dc->seek_where = where;
	dc->seek_error = false;
	dc_command(dc, DECODE_COMMAND_SEEK);
Max Kellermann's avatar
Max Kellermann committed
179

180
	if (dc->seek_error)
181
		return false;
Max Kellermann's avatar
Max Kellermann committed
182

183
	return true;
Max Kellermann's avatar
Max Kellermann committed
184
}
185 186

void
187
dc_quit(struct decoder_control *dc)
188
{
189
	assert(dc->thread != NULL);
190

191 192
	dc->quit = true;
	dc_command_async(dc, DECODE_COMMAND_STOP);
193

194 195
	g_thread_join(dc->thread);
	dc->thread = NULL;
196
}
197 198 199 200 201 202

void
dc_mixramp_start(struct decoder_control *dc, char *mixramp_start)
{
	assert(dc != NULL);

203
	g_free(dc->mixramp_start);
204 205 206 207 208 209 210 211
	dc->mixramp_start = mixramp_start;
}

void
dc_mixramp_end(struct decoder_control *dc, char *mixramp_end)
{
	assert(dc != NULL);

212
	g_free(dc->mixramp_end);
213 214 215 216 217 218 219 220
	dc->mixramp_end = mixramp_end;
}

void
dc_mixramp_prev_end(struct decoder_control *dc, char *mixramp_prev_end)
{
	assert(dc != NULL);

221
	g_free(dc->mixramp_prev_end);
222 223
	dc->mixramp_prev_end = mixramp_prev_end;
}