Update copyright notices.
[mpd-mk.git] / src / player_control.c
blobd8aed14bacbbe012f7d5458532ff6400d4d21a5f
1 /*
2 * Copyright (C) 2003-2010 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 "config.h"
21 #include "player_control.h"
22 #include "decoder_control.h"
23 #include "path.h"
24 #include "log.h"
25 #include "tag.h"
26 #include "song.h"
27 #include "idle.h"
28 #include "pcm_volume.h"
29 #include "main.h"
31 #include <assert.h>
32 #include <stdio.h>
34 struct player_control pc;
36 void pc_init(unsigned buffer_chunks, unsigned int buffered_before_play)
38 pc.buffer_chunks = buffer_chunks;
39 pc.buffered_before_play = buffered_before_play;
41 pc.mutex = g_mutex_new();
42 pc.cond = g_cond_new();
44 pc.command = PLAYER_COMMAND_NONE;
45 pc.error = PLAYER_ERROR_NOERROR;
46 pc.state = PLAYER_STATE_STOP;
47 pc.cross_fade_seconds = 0;
50 void pc_deinit(void)
52 g_cond_free(pc.cond);
53 g_mutex_free(pc.mutex);
56 void
57 player_wait_decoder(struct decoder_control *dc)
59 /* during this function, the decoder lock is held, because
60 we're waiting for the decoder thread */
61 g_cond_wait(pc.cond, dc->mutex);
64 void
65 pc_song_deleted(const struct song *song)
67 if (pc.errored_song == song) {
68 pc.error = PLAYER_ERROR_NOERROR;
69 pc.errored_song = NULL;
73 static void
74 player_command_wait_locked(void)
76 while (pc.command != PLAYER_COMMAND_NONE)
77 g_cond_wait(main_cond, pc.mutex);
80 static void
81 player_command_locked(enum player_command cmd)
83 assert(pc.command == PLAYER_COMMAND_NONE);
85 pc.command = cmd;
86 player_signal();
87 player_command_wait_locked();
90 static void
91 player_command(enum player_command cmd)
93 player_lock();
94 player_command_locked(cmd);
95 player_unlock();
98 void
99 pc_play(struct song *song)
101 assert(song != NULL);
103 if (pc.state != PLAYER_STATE_STOP)
104 player_command(PLAYER_COMMAND_STOP);
106 assert(pc.next_song == NULL);
108 pc_enqueue_song(song);
110 assert(pc.next_song == NULL);
112 idle_add(IDLE_PLAYER);
115 void pc_cancel(void)
117 player_command(PLAYER_COMMAND_CANCEL);
118 assert(pc.next_song == NULL);
121 void
122 pc_stop(void)
124 player_command(PLAYER_COMMAND_CLOSE_AUDIO);
125 assert(pc.next_song == NULL);
127 idle_add(IDLE_PLAYER);
130 void
131 pc_update_audio(void)
133 player_command(PLAYER_COMMAND_UPDATE_AUDIO);
136 void
137 pc_kill(void)
139 assert(pc.thread != NULL);
141 player_command(PLAYER_COMMAND_EXIT);
142 g_thread_join(pc.thread);
143 pc.thread = NULL;
145 idle_add(IDLE_PLAYER);
148 void
149 pc_pause(void)
151 if (pc.state != PLAYER_STATE_STOP) {
152 player_command(PLAYER_COMMAND_PAUSE);
153 idle_add(IDLE_PLAYER);
157 void
158 pc_set_pause(bool pause_flag)
160 switch (pc.state) {
161 case PLAYER_STATE_STOP:
162 break;
164 case PLAYER_STATE_PLAY:
165 if (pause_flag)
166 pc_pause();
167 break;
169 case PLAYER_STATE_PAUSE:
170 if (!pause_flag)
171 pc_pause();
172 break;
176 void
177 pc_get_status(struct player_status *status)
179 player_lock();
180 player_command_locked(PLAYER_COMMAND_REFRESH);
182 status->state = pc.state;
184 if (pc.state != PLAYER_STATE_STOP) {
185 status->bit_rate = pc.bit_rate;
186 status->audio_format = pc.audio_format;
187 status->total_time = pc.total_time;
188 status->elapsed_time = pc.elapsed_time;
191 player_unlock();
194 enum player_state
195 pc_get_state(void)
197 return pc.state;
200 void
201 pc_clear_error(void)
203 pc.error = PLAYER_ERROR_NOERROR;
204 pc.errored_song = NULL;
207 enum player_error
208 pc_get_error(void)
210 return pc.error;
213 static char *
214 pc_errored_song_uri(void)
216 return song_get_uri(pc.errored_song);
219 char *
220 pc_get_error_message(void)
222 char *error;
223 char *uri;
225 switch (pc.error) {
226 case PLAYER_ERROR_NOERROR:
227 return NULL;
229 case PLAYER_ERROR_FILENOTFOUND:
230 uri = pc_errored_song_uri();
231 error = g_strdup_printf("file \"%s\" does not exist or is inaccessible", uri);
232 g_free(uri);
233 return error;
235 case PLAYER_ERROR_FILE:
236 uri = pc_errored_song_uri();
237 error = g_strdup_printf("problems decoding \"%s\"", uri);
238 g_free(uri);
239 return error;
241 case PLAYER_ERROR_AUDIO:
242 return g_strdup("problems opening audio device");
244 case PLAYER_ERROR_SYSTEM:
245 return g_strdup("system error occured");
247 case PLAYER_ERROR_UNKTYPE:
248 uri = pc_errored_song_uri();
249 error = g_strdup_printf("file type of \"%s\" is unknown", uri);
250 g_free(uri);
251 return error;
254 assert(false);
255 return NULL;
258 void
259 pc_enqueue_song(struct song *song)
261 assert(song != NULL);
263 player_lock();
264 assert(pc.next_song == NULL);
266 pc.next_song = song;
267 player_command_locked(PLAYER_COMMAND_QUEUE);
268 player_unlock();
271 bool
272 pc_seek(struct song *song, float seek_time)
274 assert(song != NULL);
276 if (pc.state == PLAYER_STATE_STOP)
277 return false;
279 player_lock();
280 pc.next_song = song;
281 pc.seek_where = seek_time;
282 player_command_locked(PLAYER_COMMAND_SEEK);
283 player_unlock();
285 assert(pc.next_song == NULL);
287 idle_add(IDLE_PLAYER);
289 return true;
292 float
293 pc_get_cross_fade(void)
295 return pc.cross_fade_seconds;
298 void
299 pc_set_cross_fade(float cross_fade_seconds)
301 if (cross_fade_seconds < 0)
302 cross_fade_seconds = 0;
303 pc.cross_fade_seconds = cross_fade_seconds;
305 idle_add(IDLE_OPTIONS);
308 double
309 pc_get_total_play_time(void)
311 return pc.total_play_time;