2 * Copyright (C) 2003-2009 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "decoder_control.h"
25 struct decoder_control dc
;
29 dc
.mutex
= g_mutex_new();
30 dc
.cond
= g_cond_new();
32 dc
.state
= DECODE_STATE_STOP
;
33 dc
.command
= DECODE_COMMAND_NONE
;
39 g_mutex_free(dc
.mutex
);
43 dc_command_wait_locked(struct notify
*notify
)
45 while (dc
.command
!= DECODE_COMMAND_NONE
) {
56 dc_command_wait(struct notify
*notify
)
59 dc_command_wait_locked(notify
);
64 dc_command_locked(struct notify
*notify
, enum decoder_command cmd
)
67 dc_command_wait_locked(notify
);
71 dc_command(struct notify
*notify
, enum decoder_command cmd
)
74 dc_command_locked(notify
, cmd
);
78 static void dc_command_async(enum decoder_command cmd
)
89 dc_start(struct notify
*notify
, struct song
*song
)
91 assert(dc
.pipe
!= NULL
);
95 dc_command(notify
, DECODE_COMMAND_START
);
99 dc_start_async(struct song
*song
)
101 assert(dc
.pipe
!= NULL
);
102 assert(song
!= NULL
);
105 dc_command_async(DECODE_COMMAND_START
);
109 dc_stop(struct notify
*notify
)
113 if (dc
.command
!= DECODE_COMMAND_NONE
)
114 /* Attempt to cancel the current command. If it's too
115 late and the decoder thread is already executing
116 the old command, we'll call STOP again in this
117 function (see below). */
118 dc_command_locked(notify
, DECODE_COMMAND_STOP
);
120 if (dc
.state
!= DECODE_STATE_STOP
&& dc
.state
!= DECODE_STATE_ERROR
)
121 dc_command_locked(notify
, DECODE_COMMAND_STOP
);
127 dc_seek(struct notify
*notify
, double where
)
129 assert(dc
.state
!= DECODE_STATE_START
);
130 assert(where
>= 0.0);
132 if (dc
.state
== DECODE_STATE_STOP
||
133 dc
.state
== DECODE_STATE_ERROR
|| !dc
.seekable
)
136 dc
.seek_where
= where
;
137 dc
.seek_error
= false;
138 dc_command(notify
, DECODE_COMMAND_SEEK
);
149 assert(dc
.thread
!= NULL
);
152 dc_command_async(DECODE_COMMAND_STOP
);
154 g_thread_join(dc
.thread
);