2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-01-04 08:41:16 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-04 08:41:16 +01:00
|
|
|
|
|
|
|
extern "C" {
|
2008-08-26 08:44:38 +02:00
|
|
|
#include "player_control.h"
|
2008-10-14 22:38:14 +02:00
|
|
|
#include "idle.h"
|
2013-01-04 08:41:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "song.h"
|
|
|
|
#include "DecoderControl.hxx"
|
2012-09-28 00:40:00 +02:00
|
|
|
#include "Main.hxx"
|
2008-08-26 08:41:05 +02:00
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
2010-03-21 18:21:47 +01:00
|
|
|
#include <math.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
|
2010-06-19 13:17:53 +02:00
|
|
|
static void
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_enqueue_song_locked(struct player_control *pc, struct song *song);
|
2010-06-19 13:17:53 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
struct player_control *
|
|
|
|
pc_new(unsigned buffer_chunks, unsigned int buffered_before_play)
|
2008-08-26 08:45:14 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
struct player_control *pc = g_new0(struct player_control, 1);
|
|
|
|
|
|
|
|
pc->buffer_chunks = buffer_chunks;
|
|
|
|
pc->buffered_before_play = buffered_before_play;
|
|
|
|
|
|
|
|
pc->mutex = g_mutex_new();
|
|
|
|
pc->cond = g_cond_new();
|
|
|
|
|
|
|
|
pc->command = PLAYER_COMMAND_NONE;
|
2012-08-08 22:25:02 +02:00
|
|
|
pc->error_type = PLAYER_ERROR_NONE;
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->state = PLAYER_STATE_STOP;
|
|
|
|
pc->cross_fade_seconds = 0;
|
|
|
|
pc->mixramp_db = 0;
|
|
|
|
pc->mixramp_delay_seconds = nanf("");
|
|
|
|
|
|
|
|
return pc;
|
2008-08-26 08:45:14 +02:00
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
void
|
|
|
|
pc_free(struct player_control *pc)
|
2008-09-24 07:14:11 +02:00
|
|
|
{
|
2012-08-09 22:19:39 +02:00
|
|
|
if (pc->next_song != NULL)
|
|
|
|
song_free(pc->next_song);
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
g_cond_free(pc->cond);
|
|
|
|
g_mutex_free(pc->mutex);
|
|
|
|
g_free(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-11-03 21:08:48 +01:00
|
|
|
player_wait_decoder(struct player_control *pc, struct decoder_control *dc)
|
2009-10-31 17:02:12 +01:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
assert(pc != NULL);
|
|
|
|
assert(dc != NULL);
|
2011-01-10 21:27:43 +01:00
|
|
|
assert(dc->client_cond == pc->cond);
|
2009-11-03 21:08:48 +01:00
|
|
|
|
2009-10-31 17:02:12 +01:00
|
|
|
/* during this function, the decoder lock is held, because
|
|
|
|
we're waiting for the decoder thread */
|
2009-11-03 21:08:48 +01:00
|
|
|
g_cond_wait(pc->cond, dc->mutex);
|
2008-09-24 07:14:11 +02:00
|
|
|
}
|
|
|
|
|
2009-10-31 17:02:12 +01:00
|
|
|
static void
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_wait_locked(struct player_control *pc)
|
2009-10-31 17:02:12 +01:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
while (pc->command != PLAYER_COMMAND_NONE)
|
|
|
|
g_cond_wait(main_cond, pc->mutex);
|
2009-10-31 17:02:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_locked(struct player_control *pc, enum player_command cmd)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
assert(pc->command == PLAYER_COMMAND_NONE);
|
2009-02-10 08:18:28 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->command = cmd;
|
|
|
|
player_signal(pc);
|
|
|
|
player_command_wait_locked(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command(struct player_control *pc, enum player_command cmd)
|
2009-10-31 17:02:12 +01:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
|
|
|
player_command_locked(pc, cmd);
|
|
|
|
player_unlock(pc);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
void
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_play(struct player_control *pc, 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
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2010-06-19 13:17:53 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
if (pc->state != PLAYER_STATE_STOP)
|
|
|
|
player_command_locked(pc, PLAYER_COMMAND_STOP);
|
2008-08-26 08:44:38 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
assert(pc->next_song == NULL);
|
2009-10-08 21:17:00 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_enqueue_song_locked(pc, song);
|
2008-10-14 22:38:14 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
assert(pc->next_song == NULL);
|
2009-10-08 21:17:00 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2010-06-19 13:17:53 +02:00
|
|
|
|
2008-10-14 22:38:14 +02:00
|
|
|
idle_add(IDLE_PLAYER);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
void
|
|
|
|
pc_cancel(struct player_control *pc)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command(pc, PLAYER_COMMAND_CANCEL);
|
|
|
|
assert(pc->next_song == NULL);
|
2008-10-12 00:07:54 +02:00
|
|
|
}
|
2008-08-26 08:44:38 +02:00
|
|
|
|
2009-10-08 21:12:57 +02:00
|
|
|
void
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_stop(struct player_control *pc)
|
2008-10-12 00:07:54 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command(pc, PLAYER_COMMAND_CLOSE_AUDIO);
|
|
|
|
assert(pc->next_song == NULL);
|
2008-10-14 22:38:14 +02:00
|
|
|
|
|
|
|
idle_add(IDLE_PLAYER);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2009-10-23 10:55:52 +02:00
|
|
|
void
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_update_audio(struct player_control *pc)
|
2009-10-23 10:55:52 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command(pc, PLAYER_COMMAND_UPDATE_AUDIO);
|
2009-10-23 10:55:52 +02:00
|
|
|
}
|
|
|
|
|
2009-10-08 21:12:57 +02:00
|
|
|
void
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_kill(struct player_control *pc)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
assert(pc->thread != NULL);
|
2009-01-25 13:44:33 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command(pc, PLAYER_COMMAND_EXIT);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-10-08 21:12:57 +02:00
|
|
|
void
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_pause(struct player_control *pc)
|
2010-06-19 13:17:53 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2010-06-19 13:17:53 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
if (pc->state != PLAYER_STATE_STOP) {
|
|
|
|
player_command_locked(pc, PLAYER_COMMAND_PAUSE);
|
2010-06-19 13:17:53 +02:00
|
|
|
idle_add(IDLE_PLAYER);
|
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2010-06-19 13:17:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_pause_locked(struct player_control *pc)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
if (pc->state != PLAYER_STATE_STOP) {
|
|
|
|
player_command_locked(pc, PLAYER_COMMAND_PAUSE);
|
2008-10-14 22:38:14 +02:00
|
|
|
idle_add(IDLE_PLAYER);
|
|
|
|
}
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2009-10-08 21:12:57 +02:00
|
|
|
void
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_set_pause(struct player_control *pc, bool pause_flag)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2010-06-19 13:17:53 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
switch (pc->state) {
|
2008-08-26 08:44:38 +02:00
|
|
|
case PLAYER_STATE_STOP:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_STATE_PLAY:
|
|
|
|
if (pause_flag)
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_pause_locked(pc);
|
2008-08-26 08:44:38 +02:00
|
|
|
break;
|
2009-10-08 21:12:57 +02:00
|
|
|
|
2008-08-26 08:44:38 +02:00
|
|
|
case PLAYER_STATE_PAUSE:
|
|
|
|
if (!pause_flag)
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_pause_locked(pc);
|
2008-08-26 08:44:38 +02:00
|
|
|
break;
|
|
|
|
}
|
2010-06-19 13:17:53 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2012-08-25 08:44:31 +02:00
|
|
|
void
|
|
|
|
pc_set_border_pause(struct player_control *pc, bool border_pause)
|
|
|
|
{
|
|
|
|
player_lock(pc);
|
|
|
|
pc->border_pause = border_pause;
|
|
|
|
player_unlock(pc);
|
|
|
|
}
|
|
|
|
|
2009-10-08 20:48:07 +02:00
|
|
|
void
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_get_status(struct player_control *pc, struct player_status *status)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
|
|
|
player_command_locked(pc, PLAYER_COMMAND_REFRESH);
|
2009-10-08 22:09:25 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
status->state = pc->state;
|
2008-08-26 08:44:38 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
if (pc->state != PLAYER_STATE_STOP) {
|
|
|
|
status->bit_rate = pc->bit_rate;
|
|
|
|
status->audio_format = pc->audio_format;
|
|
|
|
status->total_time = pc->total_time;
|
|
|
|
status->elapsed_time = pc->elapsed_time;
|
2009-10-08 20:48:07 +02:00
|
|
|
}
|
2009-11-12 18:40:36 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2009-10-08 21:12:57 +02:00
|
|
|
void
|
2012-08-08 22:18:08 +02:00
|
|
|
pc_set_error(struct player_control *pc, enum player_error type,
|
|
|
|
GError *error)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2012-08-08 22:18:08 +02:00
|
|
|
assert(pc != NULL);
|
|
|
|
assert(type != PLAYER_ERROR_NONE);
|
|
|
|
assert(error != NULL);
|
|
|
|
|
|
|
|
if (pc->error_type != PLAYER_ERROR_NONE)
|
|
|
|
g_error_free(pc->error);
|
|
|
|
|
|
|
|
pc->error_type = type;
|
|
|
|
pc->error = error;
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2012-08-08 22:18:08 +02:00
|
|
|
void
|
|
|
|
pc_clear_error(struct player_control *pc)
|
2008-12-17 16:46:12 +01:00
|
|
|
{
|
2012-08-08 22:18:08 +02:00
|
|
|
player_lock(pc);
|
|
|
|
|
|
|
|
if (pc->error_type != PLAYER_ERROR_NONE) {
|
|
|
|
pc->error_type = PLAYER_ERROR_NONE;
|
|
|
|
g_error_free(pc->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
player_unlock(pc);
|
2008-12-17 16:46:12 +01:00
|
|
|
}
|
|
|
|
|
2009-10-08 21:12:57 +02:00
|
|
|
char *
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_get_error_message(struct player_control *pc)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2012-08-08 22:18:08 +02:00
|
|
|
player_lock(pc);
|
|
|
|
char *message = pc->error_type != PLAYER_ERROR_NONE
|
|
|
|
? g_strdup(pc->error->message)
|
|
|
|
: NULL;
|
|
|
|
player_unlock(pc);
|
|
|
|
return message;
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2010-06-19 13:17:53 +02:00
|
|
|
static void
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_enqueue_song_locked(struct player_control *pc, struct song *song)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2008-10-11 12:52:57 +02:00
|
|
|
assert(song != NULL);
|
2009-11-03 21:08:48 +01:00
|
|
|
assert(pc->next_song == NULL);
|
2009-11-03 23:17:44 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->next_song = song;
|
|
|
|
player_command_locked(pc, PLAYER_COMMAND_QUEUE);
|
2010-06-19 13:17:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_enqueue_song(struct player_control *pc, struct song *song)
|
2010-06-19 13:17:53 +02:00
|
|
|
{
|
|
|
|
assert(song != NULL);
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
|
|
|
pc_enqueue_song_locked(pc, song);
|
|
|
|
player_unlock(pc);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2009-05-06 18:35:22 +02:00
|
|
|
bool
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_seek(struct player_control *pc, struct song *song, float seek_time)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
|
|
|
assert(song != NULL);
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2012-08-09 22:19:39 +02:00
|
|
|
|
|
|
|
if (pc->next_song != NULL)
|
|
|
|
song_free(pc->next_song);
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->next_song = song;
|
|
|
|
pc->seek_where = seek_time;
|
|
|
|
player_command_locked(pc, PLAYER_COMMAND_SEEK);
|
|
|
|
player_unlock(pc);
|
2008-08-26 08:44:38 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
assert(pc->next_song == NULL);
|
2009-05-06 18:46:59 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-10-08 21:12:57 +02:00
|
|
|
float
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_get_cross_fade(const struct player_control *pc)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
return pc->cross_fade_seconds;
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2009-10-08 21:12:57 +02:00
|
|
|
void
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_set_cross_fade(struct player_control *pc, float cross_fade_seconds)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2009-10-08 21:12:57 +02:00
|
|
|
if (cross_fade_seconds < 0)
|
|
|
|
cross_fade_seconds = 0;
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->cross_fade_seconds = cross_fade_seconds;
|
2008-10-14 22:38:14 +02:00
|
|
|
|
|
|
|
idle_add(IDLE_OPTIONS);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2010-03-21 18:21:47 +01:00
|
|
|
void
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_set_mixramp_db(struct player_control *pc, float mixramp_db)
|
2010-03-21 18:21:47 +01:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->mixramp_db = mixramp_db;
|
2010-03-21 18:21:47 +01:00
|
|
|
|
|
|
|
idle_add(IDLE_OPTIONS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_set_mixramp_delay(struct player_control *pc, float mixramp_delay_seconds)
|
2010-03-21 18:21:47 +01:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->mixramp_delay_seconds = mixramp_delay_seconds;
|
2010-03-21 18:21:47 +01:00
|
|
|
|
|
|
|
idle_add(IDLE_OPTIONS);
|
|
|
|
}
|