2009-03-13 18:43:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
2008-08-26 08:41:05 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-08-26 08:41:05 +02:00
|
|
|
*/
|
|
|
|
|
2008-08-26 08:44:38 +02:00
|
|
|
#include "player_control.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "log.h"
|
2008-09-06 20:28:31 +02:00
|
|
|
#include "tag.h"
|
2008-10-08 10:49:11 +02:00
|
|
|
#include "song.h"
|
2008-10-14 22:38:14 +02:00
|
|
|
#include "idle.h"
|
2009-01-07 18:05:38 +01:00
|
|
|
#include "pcm_volume.h"
|
2009-01-02 11:20:41 +01:00
|
|
|
#include "main.h"
|
2008-08-26 08:41:05 +02:00
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2008-08-26 08:41:05 +02:00
|
|
|
struct player_control pc;
|
2008-08-26 08:44:38 +02:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
void pc_init(unsigned buffer_chunks, unsigned int buffered_before_play)
|
2008-08-26 08:45:14 +02:00
|
|
|
{
|
2009-03-06 00:42:03 +01:00
|
|
|
pc.buffer_chunks = buffer_chunks;
|
2008-08-26 08:45:14 +02:00
|
|
|
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;
|
2008-11-03 21:49:29 +01:00
|
|
|
pc.cross_fade_seconds = 0;
|
2008-08-26 08:45:14 +02:00
|
|
|
}
|
|
|
|
|
2008-09-24 07:14:11 +02:00
|
|
|
void pc_deinit(void)
|
|
|
|
{
|
|
|
|
notify_deinit(&pc.notify);
|
|
|
|
}
|
|
|
|
|
2008-12-17 16:45:49 +01:00
|
|
|
void
|
|
|
|
pc_song_deleted(const struct song *song)
|
|
|
|
{
|
2009-01-29 21:39:29 +01:00
|
|
|
if (pc.errored_song == song) {
|
|
|
|
pc.error = PLAYER_ERROR_NOERROR;
|
2008-12-17 16:45:49 +01:00
|
|
|
pc.errored_song = NULL;
|
2009-01-29 21:39:29 +01:00
|
|
|
}
|
2008-12-17 16:45:49 +01:00
|
|
|
}
|
|
|
|
|
2008-08-26 08:44:38 +02:00
|
|
|
static void player_command(enum player_command cmd)
|
|
|
|
{
|
2009-02-10 08:18:28 +01:00
|
|
|
assert(pc.command == PLAYER_COMMAND_NONE);
|
|
|
|
|
2008-08-26 08:44:38 +02:00
|
|
|
pc.command = cmd;
|
|
|
|
while (pc.command != PLAYER_COMMAND_NONE) {
|
|
|
|
notify_signal(&pc.notify);
|
2009-01-02 11:20:41 +01:00
|
|
|
notify_wait(&main_notify);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
void
|
|
|
|
playerPlay(struct song *song)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2008-10-11 12:52:57 +02:00
|
|
|
assert(song != NULL);
|
2008-08-26 08:44:38 +02:00
|
|
|
|
|
|
|
if (pc.state != PLAYER_STATE_STOP)
|
|
|
|
player_command(PLAYER_COMMAND_STOP);
|
|
|
|
|
2008-10-11 12:52:57 +02:00
|
|
|
pc.next_song = song;
|
2008-08-26 08:44:38 +02:00
|
|
|
player_command(PLAYER_COMMAND_PLAY);
|
2008-10-14 22:38:14 +02:00
|
|
|
|
|
|
|
idle_add(IDLE_PLAYER);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 00:07:54 +02:00
|
|
|
void pc_cancel(void)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2008-10-12 00:07:54 +02:00
|
|
|
player_command(PLAYER_COMMAND_CANCEL);
|
|
|
|
}
|
2008-08-26 08:44:38 +02:00
|
|
|
|
2008-10-12 00:07:54 +02:00
|
|
|
void playerWait(void)
|
|
|
|
{
|
2008-08-26 08:44:38 +02:00
|
|
|
player_command(PLAYER_COMMAND_CLOSE_AUDIO);
|
2008-10-14 22:38:14 +02:00
|
|
|
|
|
|
|
idle_add(IDLE_PLAYER);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void playerKill(void)
|
|
|
|
{
|
2009-01-25 13:44:33 +01:00
|
|
|
assert(pc.thread != NULL);
|
|
|
|
|
2008-08-26 08:44:38 +02:00
|
|
|
player_command(PLAYER_COMMAND_EXIT);
|
2009-01-25 13:44:33 +01:00
|
|
|
g_thread_join(pc.thread);
|
|
|
|
pc.thread = NULL;
|
2008-10-14 22:38:14 +02:00
|
|
|
|
|
|
|
idle_add(IDLE_PLAYER);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void playerPause(void)
|
|
|
|
{
|
2008-10-14 22:38:14 +02:00
|
|
|
if (pc.state != PLAYER_STATE_STOP) {
|
2008-08-26 08:44:38 +02:00
|
|
|
player_command(PLAYER_COMMAND_PAUSE);
|
2008-10-14 22:38:14 +02:00
|
|
|
idle_add(IDLE_PLAYER);
|
|
|
|
}
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2008-11-03 21:49:29 +01:00
|
|
|
return (int)(pc.elapsed_time + 0.5);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long getPlayerBitRate(void)
|
|
|
|
{
|
2008-11-03 21:49:29 +01:00
|
|
|
return pc.bit_rate;
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int getPlayerTotalTime(void)
|
|
|
|
{
|
2008-11-03 21:49:29 +01:00
|
|
|
return (int)(pc.total_time + 0.5);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enum player_state getPlayerState(void)
|
|
|
|
{
|
|
|
|
return pc.state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearPlayerError(void)
|
|
|
|
{
|
|
|
|
pc.error = 0;
|
|
|
|
}
|
|
|
|
|
2008-11-03 21:49:40 +01:00
|
|
|
enum player_error getPlayerError(void)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
|
|
|
return pc.error;
|
|
|
|
}
|
|
|
|
|
2009-01-04 19:09:34 +01:00
|
|
|
static char *
|
2008-12-17 16:46:12 +01:00
|
|
|
pc_errored_song_uri(void)
|
|
|
|
{
|
2009-01-04 19:09:34 +01:00
|
|
|
return song_get_uri(pc.errored_song);
|
2008-12-17 16:46:12 +01:00
|
|
|
}
|
|
|
|
|
2008-08-26 08:44:38 +02:00
|
|
|
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);
|
2009-01-04 19:09:34 +01:00
|
|
|
char *uri;
|
|
|
|
|
2008-08-26 08:44:38 +02:00
|
|
|
*error = '\0'; /* likely */
|
|
|
|
|
|
|
|
switch (pc.error) {
|
2008-11-03 21:49:40 +01:00
|
|
|
case PLAYER_ERROR_NOERROR:
|
|
|
|
break;
|
|
|
|
|
2008-08-26 08:44:38 +02:00
|
|
|
case PLAYER_ERROR_FILENOTFOUND:
|
2009-01-04 19:09:34 +01:00
|
|
|
uri = pc_errored_song_uri();
|
2008-08-26 08:44:38 +02:00
|
|
|
snprintf(error, errorlen,
|
2009-01-04 19:09:34 +01:00
|
|
|
"file \"%s\" does not exist or is inaccessible", uri);
|
|
|
|
g_free(uri);
|
2008-08-26 08:44:38 +02:00
|
|
|
break;
|
2009-01-04 19:09:34 +01:00
|
|
|
|
2008-08-26 08:44:38 +02:00
|
|
|
case PLAYER_ERROR_FILE:
|
2009-01-04 19:09:34 +01:00
|
|
|
uri = pc_errored_song_uri();
|
|
|
|
snprintf(error, errorlen, "problems decoding \"%s\"", uri);
|
|
|
|
g_free(uri);
|
2008-08-26 08:44:38 +02:00
|
|
|
break;
|
2009-01-04 19:09:34 +01:00
|
|
|
|
2008-08-26 08:44:38 +02:00
|
|
|
case PLAYER_ERROR_AUDIO:
|
|
|
|
strcpy(error, "problems opening audio device");
|
|
|
|
break;
|
2009-01-04 19:09:34 +01:00
|
|
|
|
2008-08-26 08:44:38 +02:00
|
|
|
case PLAYER_ERROR_SYSTEM:
|
|
|
|
strcpy(error, "system error occured");
|
|
|
|
break;
|
2009-01-04 19:09:34 +01:00
|
|
|
|
2008-08-26 08:44:38 +02:00
|
|
|
case PLAYER_ERROR_UNKTYPE:
|
2009-01-04 19:09:34 +01:00
|
|
|
uri = pc_errored_song_uri();
|
|
|
|
snprintf(error, errorlen,
|
|
|
|
"file type of \"%s\" is unknown", uri);
|
|
|
|
g_free(uri);
|
|
|
|
break;
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
return *error ? error : NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
void
|
|
|
|
queueSong(struct song *song)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2008-10-11 12:52:57 +02:00
|
|
|
assert(song != NULL);
|
2008-10-12 00:07:54 +02:00
|
|
|
assert(pc.next_song == NULL);
|
2008-08-26 08:44:38 +02:00
|
|
|
|
2008-10-11 12:52:57 +02:00
|
|
|
pc.next_song = song;
|
2008-10-12 00:07:54 +02:00
|
|
|
player_command(PLAYER_COMMAND_QUEUE);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2009-05-06 18:35:22 +02:00
|
|
|
bool
|
|
|
|
pc_seek(struct song *song, float seek_time)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
|
|
|
assert(song != NULL);
|
|
|
|
|
|
|
|
if (pc.state == PLAYER_STATE_STOP)
|
2009-05-06 18:35:22 +02:00
|
|
|
return false;
|
2008-08-26 08:44:38 +02:00
|
|
|
|
2008-10-11 12:52:57 +02:00
|
|
|
pc.next_song = song;
|
2009-05-06 18:46:52 +02:00
|
|
|
pc.seek_where = seek_time;
|
|
|
|
player_command(PLAYER_COMMAND_SEEK);
|
2008-08-26 08:44:38 +02:00
|
|
|
|
2009-05-06 18:46:59 +02:00
|
|
|
assert(pc.next_song == NULL);
|
|
|
|
|
2009-05-06 18:46:52 +02:00
|
|
|
idle_add(IDLE_PLAYER);
|
2008-08-26 08:44:38 +02:00
|
|
|
|
2009-05-06 18:35:22 +02:00
|
|
|
return true;
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
float getPlayerCrossFade(void)
|
|
|
|
{
|
2008-11-03 21:49:29 +01:00
|
|
|
return pc.cross_fade_seconds;
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void setPlayerCrossFade(float crossFadeInSeconds)
|
|
|
|
{
|
|
|
|
if (crossFadeInSeconds < 0)
|
|
|
|
crossFadeInSeconds = 0;
|
2008-11-03 21:49:29 +01:00
|
|
|
pc.cross_fade_seconds = crossFadeInSeconds;
|
2008-10-14 22:38:14 +02:00
|
|
|
|
|
|
|
idle_add(IDLE_OPTIONS);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double getPlayerTotalPlayTime(void)
|
|
|
|
{
|
2008-11-03 21:49:29 +01:00
|
|
|
return pc.total_play_time;
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|