player_control.c 4.98 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * http://www.musicpd.org
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.
18 19
 */

20 21 22
#include "player_control.h"
#include "path.h"
#include "log.h"
Max Kellermann's avatar
Max Kellermann committed
23
#include "tag.h"
24
#include "song.h"
25
#include "idle.h"
26
#include "pcm_volume.h"
27
#include "main.h"
28

Max Kellermann's avatar
Max Kellermann committed
29 30 31
#include <assert.h>
#include <stdio.h>

32
struct player_control pc;
33

34
void pc_init(unsigned buffer_chunks, unsigned int buffered_before_play)
35
{
36
	pc.buffer_chunks = buffer_chunks;
37 38 39 40 41
	pc.buffered_before_play = buffered_before_play;
	notify_init(&pc.notify);
	pc.command = PLAYER_COMMAND_NONE;
	pc.error = PLAYER_ERROR_NOERROR;
	pc.state = PLAYER_STATE_STOP;
Max Kellermann's avatar
Max Kellermann committed
42
	pc.cross_fade_seconds = 0;
43
	pc.software_volume = PCM_VOLUME_1;
44 45
}

46 47 48 49 50
void pc_deinit(void)
{
	notify_deinit(&pc.notify);
}

51 52 53
void
pc_song_deleted(const struct song *song)
{
54 55
	if (pc.errored_song == song) {
		pc.error = PLAYER_ERROR_NOERROR;
56
		pc.errored_song = NULL;
57
	}
58 59
}

60 61
static void player_command(enum player_command cmd)
{
62 63
	assert(pc.command == PLAYER_COMMAND_NONE);

64 65 66
	pc.command = cmd;
	while (pc.command != PLAYER_COMMAND_NONE) {
		notify_signal(&pc.notify);
67
		notify_wait(&main_notify);
68 69 70
	}
}

71 72
void
playerPlay(struct song *song)
73
{
74
	assert(song != NULL);
75 76 77 78

	if (pc.state != PLAYER_STATE_STOP)
		player_command(PLAYER_COMMAND_STOP);

79
	pc.next_song = song;
80
	player_command(PLAYER_COMMAND_PLAY);
81 82

	idle_add(IDLE_PLAYER);
83 84
}

85
void pc_cancel(void)
86
{
87 88
	player_command(PLAYER_COMMAND_CANCEL);
}
89

90 91
void playerWait(void)
{
92
	player_command(PLAYER_COMMAND_CLOSE_AUDIO);
93 94

	idle_add(IDLE_PLAYER);
95 96 97 98
}

void playerKill(void)
{
99 100
	assert(pc.thread != NULL);

101
	player_command(PLAYER_COMMAND_EXIT);
102 103
	g_thread_join(pc.thread);
	pc.thread = NULL;
104 105

	idle_add(IDLE_PLAYER);
106 107 108 109
}

void playerPause(void)
{
110
	if (pc.state != PLAYER_STATE_STOP) {
111
		player_command(PLAYER_COMMAND_PAUSE);
112 113
		idle_add(IDLE_PLAYER);
	}
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
}

void playerSetPause(int pause_flag)
{
	switch (pc.state) {
	case PLAYER_STATE_STOP:
		break;

	case PLAYER_STATE_PLAY:
		if (pause_flag)
			playerPause();
		break;
	case PLAYER_STATE_PAUSE:
		if (!pause_flag)
			playerPause();
		break;
	}
}

int getPlayerElapsedTime(void)
{
Max Kellermann's avatar
Max Kellermann committed
135
	return (int)(pc.elapsed_time + 0.5);
136 137 138 139
}

unsigned long getPlayerBitRate(void)
{
Max Kellermann's avatar
Max Kellermann committed
140
	return pc.bit_rate;
141 142 143 144
}

int getPlayerTotalTime(void)
{
Max Kellermann's avatar
Max Kellermann committed
145
	return (int)(pc.total_time + 0.5);
146 147 148 149 150 151 152 153 154 155 156 157
}

enum player_state getPlayerState(void)
{
	return pc.state;
}

void clearPlayerError(void)
{
	pc.error = 0;
}

158
enum player_error getPlayerError(void)
159 160 161 162
{
	return pc.error;
}

163
static char *
164 165
pc_errored_song_uri(void)
{
166
	return song_get_uri(pc.errored_song);
167 168
}

169 170 171 172 173
char *getPlayerErrorStr(void)
{
	/* static OK here, only one user in main task */
	static char error[MPD_PATH_MAX + 64]; /* still too much */
	static const size_t errorlen = sizeof(error);
174 175
	char *uri;

176 177 178
	*error = '\0'; /* likely */

	switch (pc.error) {
179 180 181
	case PLAYER_ERROR_NOERROR:
		break;

182
	case PLAYER_ERROR_FILENOTFOUND:
183
		uri = pc_errored_song_uri();
184
		snprintf(error, errorlen,
185 186
			 "file \"%s\" does not exist or is inaccessible", uri);
		g_free(uri);
187
		break;
188

189
	case PLAYER_ERROR_FILE:
190 191 192
		uri = pc_errored_song_uri();
		snprintf(error, errorlen, "problems decoding \"%s\"", uri);
		g_free(uri);
193
		break;
194

195 196 197
	case PLAYER_ERROR_AUDIO:
		strcpy(error, "problems opening audio device");
		break;
198

199 200 201
	case PLAYER_ERROR_SYSTEM:
		strcpy(error, "system error occured");
		break;
202

203
	case PLAYER_ERROR_UNKTYPE:
204 205 206 207 208
		uri = pc_errored_song_uri();
		snprintf(error, errorlen,
			 "file type of \"%s\" is unknown", uri);
		g_free(uri);
		break;
209 210 211 212
	}
	return *error ? error : NULL;
}

213 214
void
queueSong(struct song *song)
215
{
216
	assert(song != NULL);
217
	assert(pc.next_song == NULL);
218

219
	pc.next_song = song;
220
	player_command(PLAYER_COMMAND_QUEUE);
221 222
}

223 224
bool
pc_seek(struct song *song, float seek_time)
225 226 227 228
{
	assert(song != NULL);

	if (pc.state == PLAYER_STATE_STOP)
229
		return false;
230

231
	pc.next_song = song;
232 233
	pc.seek_where = seek_time;
	player_command(PLAYER_COMMAND_SEEK);
234

235 236
	assert(pc.next_song == NULL);

237
	idle_add(IDLE_PLAYER);
238

239
	return true;
240 241 242 243
}

float getPlayerCrossFade(void)
{
Max Kellermann's avatar
Max Kellermann committed
244
	return pc.cross_fade_seconds;
245 246 247 248 249 250
}

void setPlayerCrossFade(float crossFadeInSeconds)
{
	if (crossFadeInSeconds < 0)
		crossFadeInSeconds = 0;
Max Kellermann's avatar
Max Kellermann committed
251
	pc.cross_fade_seconds = crossFadeInSeconds;
252 253

	idle_add(IDLE_OPTIONS);
254 255 256 257
}

void setPlayerSoftwareVolume(int volume)
{
258 259 260 261 262
	if (volume > PCM_VOLUME_1)
		volume = PCM_VOLUME_1;
	else if (volume < 0)
		volume = 0;

Max Kellermann's avatar
Max Kellermann committed
263
	pc.software_volume = volume;
264 265 266 267
}

double getPlayerTotalPlayTime(void)
{
Max Kellermann's avatar
Max Kellermann committed
268
	return pc.total_play_time;
269
}