player_control.c 5.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* the Music Player Daemon (MPD)
 * Copyright (C) 2008 Max Kellermann <max@duempel.org>
 * This project's homepage is: http://www.musicpd.org
 *
 * 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.
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

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

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

31
struct player_control pc;
32

33
void pc_init(unsigned buffer_chunks, unsigned int buffered_before_play)
34
{
35
	pc.buffer_chunks = buffer_chunks;
36 37 38 39 40
	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
41
	pc.cross_fade_seconds = 0;
42
	pc.software_volume = PCM_VOLUME_1;
43 44
}

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

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

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

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

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

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

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

	idle_add(IDLE_PLAYER);
82 83
}

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

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

	idle_add(IDLE_PLAYER);
94 95 96 97
}

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

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

	idle_add(IDLE_PLAYER);
105 106 107 108
}

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

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
134
	return (int)(pc.elapsed_time + 0.5);
135 136 137 138
}

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

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

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

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

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

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

168 169 170 171 172
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);
173 174
	char *uri;

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

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

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

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

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

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

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

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

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

222 223
int
playerSeek(struct song *song, float seek_time)
224 225 226 227 228 229
{
	assert(song != NULL);

	if (pc.state == PLAYER_STATE_STOP)
		return -1;

230
	pc.next_song = song;
231 232

	if (pc.error == PLAYER_ERROR_NOERROR) {
Max Kellermann's avatar
Max Kellermann committed
233
		pc.seek_where = seek_time;
234
		player_command(PLAYER_COMMAND_SEEK);
235 236

		idle_add(IDLE_PLAYER);
237 238 239 240 241 242 243
	}

	return 0;
}

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 270 271
}

/* this actually creates a dupe of the current metadata */
272 273
struct song *
playerCurrentDecodeSong(void)
274 275 276
{
	return NULL;
}