doc: Remove superfluous comment already described in footnotes.
[mpd-mk.git] / src / decoder_control.c
blob3b993431c20b7c8985608d4777210558c1928beb
1 /*
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"
21 #include "notify.h"
23 #include <assert.h>
25 struct decoder_control dc;
27 void dc_init(void)
29 dc.mutex = g_mutex_new();
30 dc.cond = g_cond_new();
32 dc.state = DECODE_STATE_STOP;
33 dc.command = DECODE_COMMAND_NONE;
36 void dc_deinit(void)
38 g_cond_free(dc.cond);
39 g_mutex_free(dc.mutex);
42 static void
43 dc_command_wait_locked(struct notify *notify)
45 while (dc.command != DECODE_COMMAND_NONE) {
46 decoder_signal();
47 decoder_unlock();
49 notify_wait(notify);
51 decoder_lock();
55 void
56 dc_command_wait(struct notify *notify)
58 decoder_lock();
59 dc_command_wait_locked(notify);
60 decoder_unlock();
63 static void
64 dc_command_locked(struct notify *notify, enum decoder_command cmd)
66 dc.command = cmd;
67 dc_command_wait_locked(notify);
70 static void
71 dc_command(struct notify *notify, enum decoder_command cmd)
73 decoder_lock();
74 dc_command_locked(notify, cmd);
75 decoder_unlock();
78 static void dc_command_async(enum decoder_command cmd)
80 decoder_lock();
82 dc.command = cmd;
83 decoder_signal();
85 decoder_unlock();
88 void
89 dc_start(struct notify *notify, struct song *song)
91 assert(dc.pipe != NULL);
92 assert(song != NULL);
94 dc.next_song = song;
95 dc_command(notify, DECODE_COMMAND_START);
98 void
99 dc_start_async(struct song *song)
101 assert(dc.pipe != NULL);
102 assert(song != NULL);
104 dc.next_song = song;
105 dc_command_async(DECODE_COMMAND_START);
108 void
109 dc_stop(struct notify *notify)
111 decoder_lock();
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);
123 decoder_unlock();
126 bool
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)
134 return false;
136 dc.seek_where = where;
137 dc.seek_error = false;
138 dc_command(notify, DECODE_COMMAND_SEEK);
140 if (dc.seek_error)
141 return false;
143 return true;
146 void
147 dc_quit(void)
149 assert(dc.thread != NULL);
151 dc.quit = true;
152 dc_command_async(DECODE_COMMAND_STOP);
154 g_thread_join(dc.thread);
155 dc.thread = NULL;