2009-03-11 09:35:16 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-03-11 09:35:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-08-26 08:27:09 +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:27:09 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2008-08-26 08:27:09 +02:00
|
|
|
#include "player_thread.h"
|
2008-08-26 08:44:38 +02:00
|
|
|
#include "player_control.h"
|
2008-08-26 08:44:19 +02:00
|
|
|
#include "decoder_control.h"
|
2009-01-25 13:44:39 +01:00
|
|
|
#include "decoder_thread.h"
|
2009-02-10 18:51:49 +01:00
|
|
|
#include "output_all.h"
|
2009-01-07 18:05:38 +01:00
|
|
|
#include "pcm_volume.h"
|
2008-08-26 08:27:09 +02:00
|
|
|
#include "path.h"
|
2009-01-01 18:12:00 +01:00
|
|
|
#include "event_pipe.h"
|
2008-08-26 08:27:09 +02:00
|
|
|
#include "crossfade.h"
|
2008-10-08 10:49:11 +02:00
|
|
|
#include "song.h"
|
2009-02-15 18:33:31 +01:00
|
|
|
#include "tag.h"
|
2008-11-02 14:15:47 +01:00
|
|
|
#include "pipe.h"
|
2009-03-03 22:23:25 +01:00
|
|
|
#include "chunk.h"
|
2008-11-02 17:13:26 +01:00
|
|
|
#include "idle.h"
|
2012-09-28 00:40:00 +02:00
|
|
|
#include "Main.hxx"
|
2009-03-06 00:42:03 +01:00
|
|
|
#include "buffer.h"
|
2010-09-25 15:00:43 +02:00
|
|
|
#include "mpd_error.h"
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2008-11-24 14:47:44 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2008-12-29 17:29:14 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "player_thread"
|
|
|
|
|
2008-08-26 08:27:09 +02:00
|
|
|
enum xfade_state {
|
|
|
|
XFADE_DISABLED = -1,
|
|
|
|
XFADE_UNKNOWN = 0,
|
|
|
|
XFADE_ENABLED = 1
|
|
|
|
};
|
|
|
|
|
2008-10-12 00:02:23 +02:00
|
|
|
struct player {
|
2009-11-03 21:08:48 +01:00
|
|
|
struct player_control *pc;
|
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
struct decoder_control *dc;
|
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
struct music_pipe *pipe;
|
|
|
|
|
2008-10-12 00:02:23 +02:00
|
|
|
/**
|
2008-10-12 01:21:35 +02:00
|
|
|
* are we waiting for buffered_before_play?
|
2008-10-12 00:02:23 +02:00
|
|
|
*/
|
2008-10-12 01:21:35 +02:00
|
|
|
bool buffering;
|
2008-10-12 00:02:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* true if the decoder is starting and did not provide data
|
|
|
|
* yet
|
|
|
|
*/
|
|
|
|
bool decoder_starting;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* is the player paused?
|
|
|
|
*/
|
|
|
|
bool paused;
|
|
|
|
|
2008-10-12 00:07:54 +02:00
|
|
|
/**
|
|
|
|
* is there a new song in pc.next_song?
|
|
|
|
*/
|
|
|
|
bool queued;
|
|
|
|
|
2011-10-06 20:30:46 +02:00
|
|
|
/**
|
|
|
|
* Was any audio output opened successfully? It might have
|
|
|
|
* failed meanwhile, but was not explicitly closed by the
|
|
|
|
* player thread. When this flag is unset, some output
|
|
|
|
* methods must not be called.
|
|
|
|
*/
|
|
|
|
bool output_open;
|
|
|
|
|
2008-11-02 17:10:26 +01:00
|
|
|
/**
|
|
|
|
* the song currently being played
|
|
|
|
*/
|
|
|
|
struct song *song;
|
|
|
|
|
2008-10-12 00:02:23 +02:00
|
|
|
/**
|
|
|
|
* is cross fading enabled?
|
|
|
|
*/
|
|
|
|
enum xfade_state xfade;
|
2009-03-07 23:11:43 +01:00
|
|
|
|
2009-03-09 19:14:06 +01:00
|
|
|
/**
|
|
|
|
* has cross-fading begun?
|
|
|
|
*/
|
|
|
|
bool cross_fading;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of chunks used for crossfading.
|
|
|
|
*/
|
|
|
|
unsigned cross_fade_chunks;
|
|
|
|
|
2010-03-17 21:57:35 +01:00
|
|
|
/**
|
|
|
|
* The tag of the "next" song during cross-fade. It is
|
|
|
|
* postponed, and sent to the output thread when the new song
|
|
|
|
* really begins.
|
|
|
|
*/
|
|
|
|
struct tag *cross_fade_tag;
|
|
|
|
|
2009-03-07 23:11:43 +01:00
|
|
|
/**
|
|
|
|
* The current audio format for the audio outputs.
|
|
|
|
*/
|
|
|
|
struct audio_format play_audio_format;
|
2009-10-30 16:28:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The time stamp of the chunk most recently sent to the
|
|
|
|
* output thread. This attribute is only used if
|
|
|
|
* audio_output_all_get_elapsed_time() didn't return a usable
|
|
|
|
* value; the output thread can estimate the elapsed time more
|
2011-03-31 21:43:53 +02:00
|
|
|
* precisely.
|
2009-10-30 16:28:15 +01:00
|
|
|
*/
|
|
|
|
float elapsed_time;
|
2008-10-12 00:02:23 +02:00
|
|
|
};
|
|
|
|
|
2009-03-09 19:12:06 +01:00
|
|
|
static struct music_buffer *player_buffer;
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
static void
|
|
|
|
player_command_finished_locked(struct player_control *pc)
|
2008-08-26 08:45:15 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
assert(pc->command != PLAYER_COMMAND_NONE);
|
2008-08-26 08:45:15 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->command = PLAYER_COMMAND_NONE;
|
2009-10-31 17:02:12 +01:00
|
|
|
g_cond_signal(main_cond);
|
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
static void
|
|
|
|
player_command_finished(struct player_control *pc)
|
2009-10-31 17:02:12 +01:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
|
|
|
player_command_finished_locked(pc);
|
|
|
|
player_unlock(pc);
|
2008-08-26 08:45:15 +02:00
|
|
|
}
|
|
|
|
|
2009-11-03 21:01:56 +01:00
|
|
|
/**
|
|
|
|
* Start the decoder.
|
|
|
|
*
|
|
|
|
* Player lock is not held.
|
|
|
|
*/
|
|
|
|
static void
|
2009-11-03 21:18:22 +01:00
|
|
|
player_dc_start(struct player *player, struct music_pipe *pipe)
|
2009-11-03 21:01:56 +01:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
struct player_control *pc = player->pc;
|
2009-11-03 21:01:56 +01:00
|
|
|
struct decoder_control *dc = player->dc;
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
assert(player->queued || pc->command == PLAYER_COMMAND_SEEK);
|
|
|
|
assert(pc->next_song != NULL);
|
2009-11-03 21:01:56 +01:00
|
|
|
|
2011-10-06 22:45:02 +02:00
|
|
|
unsigned start_ms = pc->next_song->start_ms;
|
|
|
|
if (pc->command == PLAYER_COMMAND_SEEK)
|
|
|
|
start_ms += (unsigned)(pc->seek_where * 1000);
|
2011-10-05 22:13:13 +02:00
|
|
|
|
2012-08-09 20:55:18 +02:00
|
|
|
dc_start(dc, song_dup_detached(pc->next_song),
|
2011-10-06 22:45:02 +02:00
|
|
|
start_ms, pc->next_song->end_ms,
|
2011-10-05 22:37:59 +02:00
|
|
|
player_buffer, pipe);
|
2009-11-03 21:01:56 +01:00
|
|
|
}
|
|
|
|
|
2010-11-05 19:08:54 +01:00
|
|
|
/**
|
|
|
|
* Is the decoder still busy on the same song as the player?
|
|
|
|
*
|
|
|
|
* Note: this function does not check if the decoder is already
|
|
|
|
* finished.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
player_dc_at_current_song(const struct player *player)
|
|
|
|
{
|
|
|
|
assert(player != NULL);
|
|
|
|
assert(player->pipe != NULL);
|
|
|
|
|
2010-11-07 15:30:18 +01:00
|
|
|
return player->dc->pipe == player->pipe;
|
2010-11-05 19:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-11-07 15:30:18 +01:00
|
|
|
* Returns true if the decoder is decoding the next song (or has begun
|
|
|
|
* decoding it, or has finished doing it), and the player hasn't
|
|
|
|
* switched to that song yet.
|
2010-11-05 19:08:54 +01:00
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
player_dc_at_next_song(const struct player *player)
|
|
|
|
{
|
2010-11-07 15:30:18 +01:00
|
|
|
return player->dc->pipe != NULL && !player_dc_at_current_song(player);
|
2010-11-05 19:08:54 +01:00
|
|
|
}
|
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/**
|
|
|
|
* Stop the decoder and clears (and frees) its music pipe.
|
2009-10-31 17:02:12 +01:00
|
|
|
*
|
|
|
|
* Player lock is not held.
|
2009-03-11 09:35:16 +01:00
|
|
|
*/
|
2009-03-06 00:42:03 +01:00
|
|
|
static void
|
|
|
|
player_dc_stop(struct player *player)
|
|
|
|
{
|
2009-10-31 19:22:56 +01:00
|
|
|
struct decoder_control *dc = player->dc;
|
|
|
|
|
|
|
|
dc_stop(dc);
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
if (dc->pipe != NULL) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* clear and free the decoder pipe */
|
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
music_pipe_clear(dc->pipe, player_buffer);
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
if (dc->pipe != player->pipe)
|
|
|
|
music_pipe_free(dc->pipe);
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
dc->pipe = NULL;
|
2009-03-06 00:42:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/**
|
|
|
|
* After the decoder has been started asynchronously, wait for the
|
|
|
|
* "START" command to finish. The decoder may not be initialized yet,
|
|
|
|
* i.e. there is no audio_format information yet.
|
2009-10-31 17:02:12 +01:00
|
|
|
*
|
|
|
|
* The player lock is not held.
|
2009-03-11 09:35:16 +01:00
|
|
|
*/
|
2009-01-21 16:31:15 +01:00
|
|
|
static bool
|
|
|
|
player_wait_for_decoder(struct player *player)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
struct player_control *pc = player->pc;
|
2009-10-31 19:22:56 +01:00
|
|
|
struct decoder_control *dc = player->dc;
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
assert(player->queued || pc->command == PLAYER_COMMAND_SEEK);
|
|
|
|
assert(pc->next_song != NULL);
|
2009-11-03 21:02:54 +01:00
|
|
|
|
|
|
|
player->queued = false;
|
|
|
|
|
2012-08-08 22:18:08 +02:00
|
|
|
GError *error = dc_lock_get_error(dc);
|
|
|
|
if (error != NULL) {
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2012-08-08 22:18:08 +02:00
|
|
|
pc_set_error(pc, PLAYER_ERROR_DECODER, error);
|
2012-08-09 22:19:39 +02:00
|
|
|
|
|
|
|
song_free(pc->next_song);
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->next_song = NULL;
|
2012-08-09 22:19:39 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-01-21 16:31:15 +01:00
|
|
|
return false;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2012-08-09 22:19:39 +02:00
|
|
|
if (player->song != NULL)
|
|
|
|
song_free(player->song);
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player->song = pc->next_song;
|
2009-11-03 21:02:54 +01:00
|
|
|
player->elapsed_time = 0.0;
|
|
|
|
|
|
|
|
/* set the "starting" flag, which will be cleared by
|
|
|
|
player_check_decoder_startup() */
|
|
|
|
player->decoder_starting = true;
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2009-11-03 21:02:54 +01:00
|
|
|
|
|
|
|
/* update player_control's song information */
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->total_time = song_get_duration(pc->next_song);
|
|
|
|
pc->bit_rate = 0;
|
|
|
|
audio_format_clear(&pc->audio_format);
|
2008-10-12 00:07:54 +02:00
|
|
|
|
2009-11-03 21:02:54 +01:00
|
|
|
/* clear the queued song */
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->next_song = NULL;
|
2009-03-11 09:35:16 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2009-01-03 20:49:51 +01:00
|
|
|
/* call syncPlaylistWithQueue() in the main thread */
|
|
|
|
event_pipe_emit(PIPE_EVENT_PLAYLIST);
|
|
|
|
|
2009-01-21 16:31:15 +01:00
|
|
|
return true;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2009-12-25 23:12:10 +01:00
|
|
|
/**
|
|
|
|
* Returns the real duration of the song, comprising the duration
|
|
|
|
* indicated by the decoder plugin.
|
|
|
|
*/
|
|
|
|
static double
|
|
|
|
real_song_duration(const struct song *song, double decoder_duration)
|
|
|
|
{
|
|
|
|
assert(song != NULL);
|
|
|
|
|
|
|
|
if (decoder_duration <= 0.0)
|
|
|
|
/* the decoder plugin didn't provide information; fall
|
|
|
|
back to song_get_duration() */
|
|
|
|
return song_get_duration(song);
|
|
|
|
|
|
|
|
if (song->end_ms > 0 && song->end_ms / 1000.0 < decoder_duration)
|
|
|
|
return (song->end_ms - song->start_ms) / 1000.0;
|
|
|
|
|
|
|
|
return decoder_duration - song->start_ms / 1000.0;
|
|
|
|
}
|
|
|
|
|
2011-10-06 20:55:52 +02:00
|
|
|
/**
|
|
|
|
* Wrapper for audio_output_all_open(). Upon failure, it pauses the
|
|
|
|
* player.
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
player_open_output(struct player *player)
|
|
|
|
{
|
2011-10-06 22:45:02 +02:00
|
|
|
struct player_control *pc = player->pc;
|
|
|
|
|
2011-10-06 20:55:52 +02:00
|
|
|
assert(audio_format_defined(&player->play_audio_format));
|
2011-10-06 22:45:02 +02:00
|
|
|
assert(pc->state == PLAYER_STATE_PLAY ||
|
|
|
|
pc->state == PLAYER_STATE_PAUSE);
|
2011-10-06 20:55:52 +02:00
|
|
|
|
2012-08-08 22:47:51 +02:00
|
|
|
GError *error = NULL;
|
|
|
|
if (audio_output_all_open(&player->play_audio_format, player_buffer,
|
|
|
|
&error)) {
|
2011-10-06 20:30:46 +02:00
|
|
|
player->output_open = true;
|
2011-10-06 20:55:52 +02:00
|
|
|
player->paused = false;
|
|
|
|
|
2011-10-06 22:45:02 +02:00
|
|
|
player_lock(pc);
|
|
|
|
pc->state = PLAYER_STATE_PLAY;
|
|
|
|
player_unlock(pc);
|
2011-10-06 20:55:52 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
2012-08-08 22:47:51 +02:00
|
|
|
g_warning("%s", error->message);
|
|
|
|
|
2011-10-06 20:30:46 +02:00
|
|
|
player->output_open = false;
|
|
|
|
|
2011-10-06 20:55:52 +02:00
|
|
|
/* pause: the user may resume playback as soon as an
|
|
|
|
audio output becomes available */
|
|
|
|
player->paused = true;
|
|
|
|
|
2011-10-06 22:45:02 +02:00
|
|
|
player_lock(pc);
|
2012-08-08 22:18:08 +02:00
|
|
|
pc_set_error(pc, PLAYER_ERROR_OUTPUT, error);
|
2011-10-06 22:45:02 +02:00
|
|
|
pc->state = PLAYER_STATE_PAUSE;
|
|
|
|
player_unlock(pc);
|
2011-10-06 20:55:52 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/**
|
|
|
|
* The decoder has acknowledged the "START" command (see
|
|
|
|
* player_wait_for_decoder()). This function checks if the decoder
|
|
|
|
* initialization has completed yet.
|
2009-10-31 17:02:12 +01:00
|
|
|
*
|
|
|
|
* The player lock is not held.
|
2009-03-11 09:35:16 +01:00
|
|
|
*/
|
2009-03-07 23:11:43 +01:00
|
|
|
static bool
|
|
|
|
player_check_decoder_startup(struct player *player)
|
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
struct player_control *pc = player->pc;
|
2009-10-31 19:22:56 +01:00
|
|
|
struct decoder_control *dc = player->dc;
|
|
|
|
|
2009-03-07 23:11:43 +01:00
|
|
|
assert(player->decoder_starting);
|
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_lock(dc);
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2012-08-08 22:18:08 +02:00
|
|
|
GError *error = dc_get_error(dc);
|
|
|
|
if (error != NULL) {
|
2009-03-07 23:11:43 +01:00
|
|
|
/* the decoder failed */
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_unlock(dc);
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2012-08-08 22:18:08 +02:00
|
|
|
pc_set_error(pc, PLAYER_ERROR_DECODER, error);
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2009-03-07 23:11:43 +01:00
|
|
|
|
|
|
|
return false;
|
2009-10-31 19:22:56 +01:00
|
|
|
} else if (!decoder_is_starting(dc)) {
|
2009-03-07 23:11:43 +01:00
|
|
|
/* the decoder is ready and ok */
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_unlock(dc);
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2011-10-06 20:30:46 +02:00
|
|
|
if (player->output_open &&
|
2009-11-03 21:08:48 +01:00
|
|
|
!audio_output_all_wait(pc, 1))
|
2009-03-09 19:25:26 +01:00
|
|
|
/* the output devices havn't finished playing
|
|
|
|
all chunks yet - wait for that */
|
|
|
|
return true;
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
|
|
|
pc->total_time = real_song_duration(dc->song, dc->total_time);
|
|
|
|
pc->audio_format = dc->in_audio_format;
|
|
|
|
player_unlock(pc);
|
2009-11-03 21:23:48 +01:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
player->play_audio_format = dc->out_audio_format;
|
2009-03-07 23:11:43 +01:00
|
|
|
player->decoder_starting = false;
|
|
|
|
|
2011-10-06 20:55:52 +02:00
|
|
|
if (!player->paused && !player_open_output(player)) {
|
2009-11-03 20:02:19 +01:00
|
|
|
char *uri = song_get_uri(dc->song);
|
2009-03-07 23:11:43 +01:00
|
|
|
g_warning("problems opening audio device "
|
|
|
|
"while playing \"%s\"", uri);
|
|
|
|
g_free(uri);
|
|
|
|
|
2009-04-25 11:36:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
2009-03-07 23:11:43 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
/* the decoder is not yet ready; wait
|
|
|
|
some more */
|
2009-11-03 21:08:48 +01:00
|
|
|
player_wait_decoder(pc, dc);
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_unlock(dc);
|
2009-03-07 23:11:43 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-10 20:43:19 +01:00
|
|
|
/**
|
|
|
|
* Sends a chunk of silence to the audio outputs. This is called when
|
|
|
|
* there is not enough decoded data in the pipe yet, to prevent
|
|
|
|
* underruns in the hardware buffers.
|
2009-10-31 17:02:12 +01:00
|
|
|
*
|
|
|
|
* The player lock is not held.
|
2009-03-10 20:43:19 +01:00
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
player_send_silence(struct player *player)
|
|
|
|
{
|
2011-10-06 20:30:46 +02:00
|
|
|
assert(player->output_open);
|
2009-03-10 20:43:19 +01:00
|
|
|
assert(audio_format_defined(&player->play_audio_format));
|
|
|
|
|
2011-01-07 22:29:47 +01:00
|
|
|
struct music_chunk *chunk = music_buffer_allocate(player_buffer);
|
2009-03-10 20:43:19 +01:00
|
|
|
if (chunk == NULL) {
|
|
|
|
g_warning("Failed to allocate silence buffer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
chunk->audio_format = player->play_audio_format;
|
|
|
|
#endif
|
|
|
|
|
2011-01-07 22:29:47 +01:00
|
|
|
size_t frame_size =
|
|
|
|
audio_format_frame_size(&player->play_audio_format);
|
|
|
|
/* this formula ensures that we don't send
|
|
|
|
partial frames */
|
|
|
|
unsigned num_frames = sizeof(chunk->data) / frame_size;
|
|
|
|
|
2009-11-12 18:41:25 +01:00
|
|
|
chunk->times = -1.0; /* undefined time stamp */
|
2009-03-10 20:43:19 +01:00
|
|
|
chunk->length = num_frames * frame_size;
|
|
|
|
memset(chunk->data, 0, chunk->length);
|
|
|
|
|
2012-08-08 22:47:51 +02:00
|
|
|
GError *error = NULL;
|
|
|
|
if (!audio_output_all_play(chunk, &error)) {
|
|
|
|
g_warning("%s", error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
|
2009-03-10 20:43:19 +01:00
|
|
|
music_buffer_return(player_buffer, chunk);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/**
|
|
|
|
* This is the handler for the #PLAYER_COMMAND_SEEK command.
|
2009-10-31 17:02:12 +01:00
|
|
|
*
|
|
|
|
* The player lock is not held.
|
2009-03-11 09:35:16 +01:00
|
|
|
*/
|
2008-11-03 21:49:29 +01:00
|
|
|
static bool player_seek_decoder(struct player *player)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
struct player_control *pc = player->pc;
|
|
|
|
struct song *song = pc->next_song;
|
2009-10-31 19:22:56 +01:00
|
|
|
struct decoder_control *dc = player->dc;
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
assert(pc->next_song != NULL);
|
2009-03-10 18:04:09 +01:00
|
|
|
|
2012-08-15 22:51:48 +02:00
|
|
|
const unsigned start_ms = song->start_ms;
|
|
|
|
|
2012-08-15 17:49:23 +02:00
|
|
|
if (!decoder_lock_is_current_song(dc, song)) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* the decoder is already decoding the "next" song -
|
|
|
|
stop it and start the previous song again */
|
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
player_dc_stop(player);
|
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/* clear music chunks which might still reside in the
|
|
|
|
pipe */
|
2009-03-09 19:12:06 +01:00
|
|
|
music_pipe_clear(player->pipe, player_buffer);
|
2009-01-21 16:36:30 +01:00
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/* re-start the decoder */
|
2009-11-03 21:18:22 +01:00
|
|
|
player_dc_start(player, player->pipe);
|
2011-01-07 22:29:47 +01:00
|
|
|
if (!player_wait_for_decoder(player)) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* decoder failure */
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished(pc);
|
2009-01-21 16:36:30 +01:00
|
|
|
return false;
|
2009-03-10 18:04:09 +01:00
|
|
|
}
|
2009-02-10 00:17:34 +01:00
|
|
|
} else {
|
2010-11-05 19:24:42 +01:00
|
|
|
if (!player_dc_at_current_song(player)) {
|
|
|
|
/* the decoder is already decoding the "next" song,
|
|
|
|
but it is the same song file; exchange the pipe */
|
|
|
|
music_pipe_clear(player->pipe, player_buffer);
|
|
|
|
music_pipe_free(player->pipe);
|
2010-11-07 15:30:18 +01:00
|
|
|
player->pipe = dc->pipe;
|
2010-11-05 19:24:42 +01:00
|
|
|
}
|
|
|
|
|
2012-08-09 22:19:39 +02:00
|
|
|
song_free(pc->next_song);
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->next_song = NULL;
|
2009-02-10 00:17:34 +01:00
|
|
|
player->queued = false;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
2008-08-26 08:27:18 +02:00
|
|
|
|
2009-03-25 19:48:14 +01:00
|
|
|
/* wait for the decoder to complete initialization */
|
|
|
|
|
|
|
|
while (player->decoder_starting) {
|
2011-01-07 22:29:47 +01:00
|
|
|
if (!player_check_decoder_startup(player)) {
|
2009-03-25 19:48:14 +01:00
|
|
|
/* decoder failure */
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished(pc);
|
2009-03-25 19:48:14 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/* send the SEEK command */
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
double where = pc->seek_where;
|
|
|
|
if (where > pc->total_time)
|
|
|
|
where = pc->total_time - 0.1;
|
2008-08-26 08:27:18 +02:00
|
|
|
if (where < 0.0)
|
|
|
|
where = 0.0;
|
|
|
|
|
2012-08-15 22:51:48 +02:00
|
|
|
if (!dc_seek(dc, where + start_ms / 1000.0)) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* decoder failure */
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished(pc);
|
2009-03-10 17:52:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2009-10-30 16:28:15 +01:00
|
|
|
player->elapsed_time = where;
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2009-03-10 17:52:38 +01:00
|
|
|
player->xfade = XFADE_UNKNOWN;
|
|
|
|
|
2009-03-10 21:19:51 +01:00
|
|
|
/* re-fill the buffer after seeking */
|
|
|
|
player->buffering = true;
|
2009-03-10 17:52:38 +01:00
|
|
|
|
|
|
|
audio_output_all_cancel();
|
|
|
|
|
|
|
|
return true;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2009-10-31 17:02:12 +01:00
|
|
|
/**
|
|
|
|
* Player lock must be held before calling.
|
|
|
|
*/
|
2008-11-03 21:49:29 +01:00
|
|
|
static void player_process_command(struct player *player)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
struct player_control *pc = player->pc;
|
2009-12-14 23:22:51 +01:00
|
|
|
G_GNUC_UNUSED struct decoder_control *dc = player->dc;
|
2009-10-31 19:22:56 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
switch (pc->command) {
|
2008-08-26 08:27:09 +02:00
|
|
|
case PLAYER_COMMAND_NONE:
|
|
|
|
case PLAYER_COMMAND_STOP:
|
2008-08-26 08:27:16 +02:00
|
|
|
case PLAYER_COMMAND_EXIT:
|
2008-08-26 08:27:09 +02:00
|
|
|
case PLAYER_COMMAND_CLOSE_AUDIO:
|
|
|
|
break;
|
|
|
|
|
2009-10-23 10:55:52 +02:00
|
|
|
case PLAYER_COMMAND_UPDATE_AUDIO:
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2009-10-23 10:55:52 +02:00
|
|
|
audio_output_all_enable_disable();
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
|
|
|
player_command_finished_locked(pc);
|
2009-10-23 10:55:52 +02:00
|
|
|
break;
|
|
|
|
|
2008-10-12 00:07:54 +02:00
|
|
|
case PLAYER_COMMAND_QUEUE:
|
2009-11-03 21:08:48 +01:00
|
|
|
assert(pc->next_song != NULL);
|
2008-11-14 17:55:51 +01:00
|
|
|
assert(!player->queued);
|
2010-11-05 19:08:54 +01:00
|
|
|
assert(!player_dc_at_next_song(player));
|
2008-11-14 17:55:51 +01:00
|
|
|
|
2008-10-12 00:07:54 +02:00
|
|
|
player->queued = true;
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_COMMAND_PAUSE:
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2008-10-12 00:02:23 +02:00
|
|
|
player->paused = !player->paused;
|
|
|
|
if (player->paused) {
|
2009-02-10 18:51:51 +01:00
|
|
|
audio_output_all_pause();
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->state = PLAYER_STATE_PAUSE;
|
2009-03-10 21:00:52 +01:00
|
|
|
} else if (!audio_format_defined(&player->play_audio_format)) {
|
|
|
|
/* the decoder hasn't provided an audio format
|
|
|
|
yet - don't open the audio device yet */
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2009-03-10 21:00:52 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->state = PLAYER_STATE_PLAY;
|
2008-08-26 08:27:09 +02:00
|
|
|
} else {
|
2011-10-06 20:55:52 +02:00
|
|
|
player_open_output(player);
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
2009-03-10 21:00:52 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_COMMAND_SEEK:
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2009-03-10 17:52:38 +01:00
|
|
|
player_seek_decoder(player);
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
2008-10-12 00:07:54 +02:00
|
|
|
|
|
|
|
case PLAYER_COMMAND_CANCEL:
|
2009-11-03 21:08:48 +01:00
|
|
|
if (pc->next_song == NULL) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* the cancel request arrived too late, we're
|
2008-10-12 00:07:54 +02:00
|
|
|
already playing the queued song... stop
|
|
|
|
everything now */
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->command = PLAYER_COMMAND_STOP;
|
2008-10-12 00:07:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-07 15:30:18 +01:00
|
|
|
if (player_dc_at_next_song(player)) {
|
2008-10-12 00:07:54 +02:00
|
|
|
/* the decoder is already decoding the song -
|
|
|
|
stop it and reset the position */
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2009-03-06 00:42:03 +01:00
|
|
|
player_dc_stop(player);
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
}
|
2008-10-12 00:07:54 +02:00
|
|
|
|
2012-08-09 22:19:39 +02:00
|
|
|
song_free(pc->next_song);
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->next_song = NULL;
|
2008-10-12 00:07:54 +02:00
|
|
|
player->queued = false;
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2008-10-12 00:07:54 +02:00
|
|
|
break;
|
2009-10-08 22:09:25 +02:00
|
|
|
|
|
|
|
case PLAYER_COMMAND_REFRESH:
|
2011-10-06 20:30:46 +02:00
|
|
|
if (player->output_open && !player->paused) {
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2009-10-08 22:09:25 +02:00
|
|
|
audio_output_all_check();
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
}
|
2009-10-08 22:09:25 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->elapsed_time = audio_output_all_get_elapsed_time();
|
|
|
|
if (pc->elapsed_time < 0.0)
|
|
|
|
pc->elapsed_time = player->elapsed_time;
|
2009-10-30 16:28:15 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2009-10-08 22:09:25 +02:00
|
|
|
break;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-23 12:26:26 +02:00
|
|
|
static void
|
|
|
|
update_song_tag(struct song *song, const struct tag *new_tag)
|
|
|
|
{
|
|
|
|
if (song_is_file(song))
|
|
|
|
/* don't update tags of local files, only remote
|
|
|
|
streams may change tags dynamically */
|
|
|
|
return;
|
|
|
|
|
2011-01-07 22:29:47 +01:00
|
|
|
struct tag *old_tag = song->tag;
|
2009-07-23 12:26:26 +02:00
|
|
|
song->tag = tag_dup(new_tag);
|
|
|
|
|
|
|
|
if (old_tag != NULL)
|
|
|
|
tag_free(old_tag);
|
|
|
|
|
|
|
|
/* the main thread will update the playlist version when he
|
|
|
|
receives this event */
|
|
|
|
event_pipe_emit(PIPE_EVENT_TAG);
|
|
|
|
|
|
|
|
/* notify all clients that the tag of the current song has
|
|
|
|
changed */
|
|
|
|
idle_add(IDLE_PLAYER);
|
|
|
|
}
|
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/**
|
|
|
|
* Plays a #music_chunk object (after applying software volume). If
|
|
|
|
* it contains a (stream) tag, copy it to the current song, so MPD's
|
|
|
|
* playlist reflects the new stream tag.
|
2009-10-31 17:02:12 +01:00
|
|
|
*
|
|
|
|
* Player lock is not held.
|
2009-03-11 09:35:16 +01:00
|
|
|
*/
|
2009-01-21 16:31:15 +01:00
|
|
|
static bool
|
2009-11-03 21:08:48 +01:00
|
|
|
play_chunk(struct player_control *pc,
|
|
|
|
struct song *song, struct music_chunk *chunk,
|
2012-08-08 22:47:51 +02:00
|
|
|
const struct audio_format *format,
|
|
|
|
GError **error_r)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2009-03-08 13:45:24 +01:00
|
|
|
assert(music_chunk_check_format(chunk, format));
|
|
|
|
|
2009-07-23 12:26:26 +02:00
|
|
|
if (chunk->tag != NULL)
|
|
|
|
update_song_tag(song, chunk->tag);
|
2008-11-02 17:13:26 +01:00
|
|
|
|
2009-06-02 08:37:30 +02:00
|
|
|
if (chunk->length == 0) {
|
|
|
|
music_buffer_return(player_buffer, chunk);
|
2009-03-17 20:28:37 +01:00
|
|
|
return true;
|
2009-06-02 08:37:30 +02:00
|
|
|
}
|
2009-03-17 20:28:37 +01:00
|
|
|
|
2011-07-20 21:46:05 +02:00
|
|
|
player_lock(pc);
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->bit_rate = chunk->bit_rate;
|
2011-07-20 21:46:05 +02:00
|
|
|
player_unlock(pc);
|
2009-03-17 20:28:37 +01:00
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/* send the chunk to the audio outputs */
|
|
|
|
|
2012-08-08 22:47:51 +02:00
|
|
|
if (!audio_output_all_play(chunk, error_r))
|
2009-01-21 16:31:15 +01:00
|
|
|
return false;
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->total_play_time += (double)chunk->length /
|
2009-07-23 12:27:05 +02:00
|
|
|
audio_format_time_to_size(format);
|
2009-01-21 16:31:15 +01:00
|
|
|
return true;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2009-03-09 19:14:06 +01:00
|
|
|
/**
|
|
|
|
* Obtains the next chunk from the music pipe, optionally applies
|
|
|
|
* cross-fading, and sends it to all audio outputs.
|
|
|
|
*
|
|
|
|
* @return true on success, false on error (playback will be stopped)
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
play_next_chunk(struct player *player)
|
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
struct player_control *pc = player->pc;
|
2009-10-31 19:22:56 +01:00
|
|
|
struct decoder_control *dc = player->dc;
|
2009-03-09 19:14:06 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
if (!audio_output_all_wait(pc, 64))
|
2009-03-11 09:35:16 +01:00
|
|
|
/* the output pipe is still large enough, don't send
|
|
|
|
another chunk */
|
2009-03-09 19:25:26 +01:00
|
|
|
return true;
|
|
|
|
|
2011-01-07 22:29:47 +01:00
|
|
|
unsigned cross_fade_position;
|
|
|
|
struct music_chunk *chunk = NULL;
|
2009-03-09 19:14:06 +01:00
|
|
|
if (player->xfade == XFADE_ENABLED &&
|
2010-11-05 19:08:54 +01:00
|
|
|
player_dc_at_next_song(player) &&
|
2009-03-09 19:14:06 +01:00
|
|
|
(cross_fade_position = music_pipe_size(player->pipe))
|
|
|
|
<= player->cross_fade_chunks) {
|
|
|
|
/* perform cross fade */
|
|
|
|
struct music_chunk *other_chunk =
|
2009-10-31 19:22:56 +01:00
|
|
|
music_pipe_shift(dc->pipe);
|
2009-03-09 19:14:06 +01:00
|
|
|
|
|
|
|
if (!player->cross_fading) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* beginning of the cross fade - adjust
|
|
|
|
crossFadeChunks which might be bigger than
|
|
|
|
the remaining number of chunks in the old
|
|
|
|
song */
|
2009-03-09 19:14:06 +01:00
|
|
|
player->cross_fade_chunks = cross_fade_position;
|
|
|
|
player->cross_fading = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (other_chunk != NULL) {
|
|
|
|
chunk = music_pipe_shift(player->pipe);
|
|
|
|
assert(chunk != NULL);
|
2010-05-02 15:31:31 +02:00
|
|
|
assert(chunk->other == NULL);
|
2009-03-09 19:14:06 +01:00
|
|
|
|
2010-03-17 21:57:35 +01:00
|
|
|
/* don't send the tags of the new song (which
|
|
|
|
is being faded in) yet; postpone it until
|
|
|
|
the current song is faded out */
|
|
|
|
player->cross_fade_tag =
|
|
|
|
tag_merge_replace(player->cross_fade_tag,
|
|
|
|
other_chunk->tag);
|
|
|
|
other_chunk->tag = NULL;
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
if (isnan(pc->mixramp_delay_seconds)) {
|
2010-05-02 15:31:31 +02:00
|
|
|
chunk->mix_ratio = ((float)cross_fade_position)
|
2010-03-21 18:21:47 +01:00
|
|
|
/ player->cross_fade_chunks;
|
|
|
|
} else {
|
2010-05-02 15:31:31 +02:00
|
|
|
chunk->mix_ratio = nan("");
|
2010-03-21 18:21:47 +01:00
|
|
|
}
|
2011-01-07 23:45:51 +01:00
|
|
|
|
|
|
|
if (music_chunk_is_empty(other_chunk)) {
|
|
|
|
/* the "other" chunk was a music_chunk
|
|
|
|
which had only a tag, but no music
|
|
|
|
data - we cannot cross-fade that;
|
|
|
|
but since this happens only at the
|
|
|
|
beginning of the new song, we can
|
|
|
|
easily recover by throwing it away
|
|
|
|
now */
|
|
|
|
music_buffer_return(player_buffer,
|
|
|
|
other_chunk);
|
|
|
|
other_chunk = NULL;
|
|
|
|
}
|
2010-03-21 18:21:47 +01:00
|
|
|
|
2010-05-02 15:31:31 +02:00
|
|
|
chunk->other = other_chunk;
|
2009-03-09 19:14:06 +01:00
|
|
|
} else {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* there are not enough decoded chunks yet */
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_lock(dc);
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
if (decoder_is_idle(dc)) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* the decoder isn't running, abort
|
2009-03-09 19:14:06 +01:00
|
|
|
cross fading */
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_unlock(dc);
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2009-03-09 19:14:06 +01:00
|
|
|
player->xfade = XFADE_DISABLED;
|
|
|
|
} else {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* wait for the decoder */
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_signal(dc);
|
2009-11-03 21:08:48 +01:00
|
|
|
player_wait_decoder(pc, dc);
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_unlock(dc);
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2009-03-09 19:14:06 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chunk == NULL)
|
|
|
|
chunk = music_pipe_shift(player->pipe);
|
|
|
|
|
|
|
|
assert(chunk != NULL);
|
|
|
|
|
2010-03-17 21:57:35 +01:00
|
|
|
/* insert the postponed tag if cross-fading is finished */
|
|
|
|
|
|
|
|
if (player->xfade != XFADE_ENABLED && player->cross_fade_tag != NULL) {
|
|
|
|
chunk->tag = tag_merge_replace(chunk->tag,
|
|
|
|
player->cross_fade_tag);
|
|
|
|
player->cross_fade_tag = NULL;
|
|
|
|
}
|
|
|
|
|
2009-03-09 19:14:06 +01:00
|
|
|
/* play the current chunk */
|
|
|
|
|
2012-08-08 22:47:51 +02:00
|
|
|
GError *error = NULL;
|
2009-11-03 21:08:48 +01:00
|
|
|
if (!play_chunk(player->pc, player->song, chunk,
|
2012-08-08 22:47:51 +02:00
|
|
|
&player->play_audio_format, &error)) {
|
|
|
|
g_warning("%s", error->message);
|
|
|
|
|
2009-03-09 19:25:26 +01:00
|
|
|
music_buffer_return(player_buffer, chunk);
|
2009-04-25 11:55:36 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2012-08-08 22:18:08 +02:00
|
|
|
pc_set_error(pc, PLAYER_ERROR_OUTPUT, error);
|
2009-10-29 22:39:48 +01:00
|
|
|
|
2009-04-25 11:55:36 +02:00
|
|
|
/* pause: the user may resume playback as soon as an
|
|
|
|
audio output becomes available */
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->state = PLAYER_STATE_PAUSE;
|
2009-04-25 11:55:36 +02:00
|
|
|
player->paused = true;
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-03-09 19:14:06 +01:00
|
|
|
return false;
|
2009-03-09 19:25:26 +01:00
|
|
|
}
|
2009-03-09 19:14:06 +01:00
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/* this formula should prevent that the decoder gets woken up
|
|
|
|
with each chunk; it is more efficient to make it decode a
|
2009-03-09 19:14:06 +01:00
|
|
|
larger block at a time */
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_lock(dc);
|
|
|
|
if (!decoder_is_idle(dc) &&
|
2009-11-03 21:08:48 +01:00
|
|
|
music_pipe_size(dc->pipe) <= (pc->buffered_before_play +
|
2009-03-09 19:14:06 +01:00
|
|
|
music_buffer_size(player_buffer) * 3) / 4)
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_signal(dc);
|
|
|
|
decoder_unlock(dc);
|
2009-03-09 19:14:06 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-09 19:15:14 +01:00
|
|
|
/**
|
|
|
|
* This is called at the border between two songs: the audio output
|
|
|
|
* has consumed all chunks of the current song, and we should start
|
|
|
|
* sending chunks from the next one.
|
|
|
|
*
|
2009-10-31 17:02:12 +01:00
|
|
|
* The player lock is not held.
|
|
|
|
*
|
2009-03-09 19:15:14 +01:00
|
|
|
* @return true on success, false on error (playback will be stopped)
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
player_song_border(struct player *player)
|
|
|
|
{
|
|
|
|
player->xfade = XFADE_UNKNOWN;
|
|
|
|
|
2011-01-07 22:29:47 +01:00
|
|
|
char *uri = song_get_uri(player->song);
|
2009-07-06 22:29:58 +02:00
|
|
|
g_message("played \"%s\"", uri);
|
|
|
|
g_free(uri);
|
|
|
|
|
2009-03-09 19:15:14 +01:00
|
|
|
music_pipe_free(player->pipe);
|
2009-10-31 19:22:56 +01:00
|
|
|
player->pipe = player->dc->pipe;
|
2009-03-09 19:15:14 +01:00
|
|
|
|
2010-01-02 21:16:51 +01:00
|
|
|
audio_output_all_song_border();
|
|
|
|
|
2009-03-09 19:25:26 +01:00
|
|
|
if (!player_wait_for_decoder(player))
|
|
|
|
return false;
|
|
|
|
|
2012-08-25 08:44:31 +02:00
|
|
|
struct player_control *const pc = player->pc;
|
|
|
|
player_lock(pc);
|
|
|
|
|
|
|
|
if (pc->border_pause) {
|
|
|
|
player->paused = true;
|
|
|
|
pc->state = PLAYER_STATE_PAUSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
player_unlock(pc);
|
|
|
|
|
2009-03-09 19:25:26 +01:00
|
|
|
return true;
|
2009-03-09 19:15:14 +01:00
|
|
|
}
|
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/*
|
|
|
|
* The main loop of the player thread, during playback. This is
|
|
|
|
* basically a state machine, which multiplexes data between the
|
|
|
|
* decoder thread and the output threads.
|
|
|
|
*/
|
2009-11-03 21:08:48 +01:00
|
|
|
static void do_play(struct player_control *pc, struct decoder_control *dc)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2008-10-12 00:02:23 +02:00
|
|
|
struct player player = {
|
2009-11-03 21:08:48 +01:00
|
|
|
.pc = pc,
|
2009-10-31 19:22:56 +01:00
|
|
|
.dc = dc,
|
2008-10-12 01:21:35 +02:00
|
|
|
.buffering = true,
|
2008-10-12 00:02:23 +02:00
|
|
|
.decoder_starting = false,
|
|
|
|
.paused = false,
|
2009-11-03 21:01:56 +01:00
|
|
|
.queued = true,
|
2011-10-06 20:30:46 +02:00
|
|
|
.output_open = false,
|
2008-11-02 17:10:26 +01:00
|
|
|
.song = NULL,
|
2008-10-12 00:02:23 +02:00
|
|
|
.xfade = XFADE_UNKNOWN,
|
2009-03-09 19:14:06 +01:00
|
|
|
.cross_fading = false,
|
|
|
|
.cross_fade_chunks = 0,
|
2010-03-17 21:57:35 +01:00
|
|
|
.cross_fade_tag = NULL,
|
2009-10-30 16:28:15 +01:00
|
|
|
.elapsed_time = 0.0,
|
2008-10-12 00:02:23 +02:00
|
|
|
};
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
player.pipe = music_pipe_new();
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2009-11-03 21:18:22 +01:00
|
|
|
player_dc_start(&player, player.pipe);
|
2009-01-21 16:31:15 +01:00
|
|
|
if (!player_wait_for_decoder(&player)) {
|
2012-08-09 22:19:39 +02:00
|
|
|
assert(player.song == NULL);
|
|
|
|
|
2009-03-11 09:20:34 +01:00
|
|
|
player_dc_stop(&player);
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished(pc);
|
2009-03-06 00:42:03 +01:00
|
|
|
music_pipe_free(player.pipe);
|
2009-03-11 09:20:34 +01:00
|
|
|
event_pipe_emit(PIPE_EVENT_PLAYLIST);
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
return;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
|
|
|
pc->state = PLAYER_STATE_PLAY;
|
2011-10-05 22:13:13 +02:00
|
|
|
|
2011-10-06 22:45:02 +02:00
|
|
|
if (pc->command == PLAYER_COMMAND_SEEK)
|
|
|
|
player.elapsed_time = pc->seek_where;
|
2011-10-05 22:13:13 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2009-01-21 16:31:15 +01:00
|
|
|
while (true) {
|
2008-11-03 21:49:29 +01:00
|
|
|
player_process_command(&player);
|
2009-11-03 21:08:48 +01:00
|
|
|
if (pc->command == PLAYER_COMMAND_STOP ||
|
|
|
|
pc->command == PLAYER_COMMAND_EXIT ||
|
|
|
|
pc->command == PLAYER_COMMAND_CLOSE_AUDIO) {
|
|
|
|
player_unlock(pc);
|
2009-02-10 18:51:51 +01:00
|
|
|
audio_output_all_cancel();
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2008-10-12 01:21:35 +02:00
|
|
|
if (player.buffering) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* buffering at the start of the song - wait
|
|
|
|
until the buffer is large enough, to
|
|
|
|
prevent stuttering on slow machines */
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
if (music_pipe_size(player.pipe) < pc->buffered_before_play &&
|
2009-10-31 19:22:56 +01:00
|
|
|
!decoder_lock_is_idle(dc)) {
|
2008-08-26 08:27:09 +02:00
|
|
|
/* not enough decoded buffer space yet */
|
2009-03-10 21:19:51 +01:00
|
|
|
|
|
|
|
if (!player.paused &&
|
2011-10-06 20:30:46 +02:00
|
|
|
player.output_open &&
|
2009-03-10 21:19:51 +01:00
|
|
|
audio_output_all_check() < 4 &&
|
|
|
|
!player_send_silence(&player))
|
|
|
|
break;
|
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_lock(dc);
|
2009-10-31 17:02:12 +01:00
|
|
|
/* XXX race condition: check decoder again */
|
2009-11-03 21:08:48 +01:00
|
|
|
player_wait_decoder(pc, dc);
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_unlock(dc);
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* buffering is complete */
|
2008-10-12 01:21:35 +02:00
|
|
|
player.buffering = false;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-12 00:02:23 +02:00
|
|
|
if (player.decoder_starting) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* wait until the decoder is initialized completely */
|
2008-10-29 22:17:42 +01:00
|
|
|
|
2011-01-07 22:29:47 +01:00
|
|
|
if (!player_check_decoder_startup(&player))
|
2009-03-07 23:11:43 +01:00
|
|
|
break;
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2009-03-07 23:11:43 +01:00
|
|
|
continue;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2008-11-13 02:06:58 +01:00
|
|
|
#ifndef NDEBUG
|
2008-11-23 22:16:54 +01:00
|
|
|
/*
|
2008-11-13 02:06:58 +01:00
|
|
|
music_pipe_check_format(&play_audio_format,
|
|
|
|
player.next_song_chunk,
|
2009-10-31 19:22:56 +01:00
|
|
|
&dc->out_audio_format);
|
2008-11-23 22:16:54 +01:00
|
|
|
*/
|
2008-11-13 02:06:58 +01:00
|
|
|
#endif
|
|
|
|
|
2009-11-03 23:29:50 +01:00
|
|
|
if (decoder_lock_is_idle(dc) && player.queued &&
|
|
|
|
dc->pipe == player.pipe) {
|
2008-08-26 08:27:09 +02:00
|
|
|
/* the decoder has finished the current song;
|
|
|
|
make it decode the next song */
|
2008-10-12 00:07:54 +02:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
assert(dc->pipe == NULL || dc->pipe == player.pipe);
|
2008-10-12 00:07:54 +02:00
|
|
|
|
2009-11-03 21:18:22 +01:00
|
|
|
player_dc_start(&player, music_pipe_new());
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2012-08-25 08:44:31 +02:00
|
|
|
if (/* no cross-fading if MPD is going to pause at the
|
|
|
|
end of the current song */
|
|
|
|
!pc->border_pause &&
|
|
|
|
player_dc_at_next_song(&player) &&
|
2008-10-12 00:02:23 +02:00
|
|
|
player.xfade == XFADE_UNKNOWN &&
|
2009-10-31 19:22:56 +01:00
|
|
|
!decoder_lock_is_starting(dc)) {
|
2008-08-26 08:27:09 +02:00
|
|
|
/* enable cross fading in this song? if yes,
|
|
|
|
calculate how many chunks will be required
|
|
|
|
for it */
|
2009-03-09 19:14:06 +01:00
|
|
|
player.cross_fade_chunks =
|
2009-11-03 21:08:48 +01:00
|
|
|
cross_fade_calc(pc->cross_fade_seconds, dc->total_time,
|
|
|
|
pc->mixramp_db,
|
|
|
|
pc->mixramp_delay_seconds,
|
2010-05-08 09:19:44 +02:00
|
|
|
dc->replay_gain_db,
|
|
|
|
dc->replay_gain_prev_db,
|
2010-03-21 18:21:47 +01:00
|
|
|
dc->mixramp_start,
|
|
|
|
dc->mixramp_prev_end,
|
2009-10-31 19:22:56 +01:00
|
|
|
&dc->out_audio_format,
|
2009-03-07 23:11:43 +01:00
|
|
|
&player.play_audio_format,
|
2009-03-09 19:12:06 +01:00
|
|
|
music_buffer_size(player_buffer) -
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->buffered_before_play);
|
2009-03-09 19:14:06 +01:00
|
|
|
if (player.cross_fade_chunks > 0) {
|
2008-10-12 00:02:23 +02:00
|
|
|
player.xfade = XFADE_ENABLED;
|
2009-03-09 19:14:06 +01:00
|
|
|
player.cross_fading = false;
|
2008-08-26 08:27:09 +02:00
|
|
|
} else
|
|
|
|
/* cross fading is disabled or the
|
|
|
|
next song is too short */
|
2008-10-12 00:02:23 +02:00
|
|
|
player.xfade = XFADE_DISABLED;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2009-10-31 17:02:12 +01:00
|
|
|
if (player.paused) {
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2009-11-02 20:20:13 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
if (pc->command == PLAYER_COMMAND_NONE)
|
|
|
|
player_wait(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
continue;
|
2010-11-07 15:30:18 +01:00
|
|
|
} else if (!music_pipe_empty(player.pipe)) {
|
2009-03-09 19:14:06 +01:00
|
|
|
/* at least one music chunk is ready - send it
|
|
|
|
to the audio output */
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2009-04-25 11:55:36 +02:00
|
|
|
play_next_chunk(&player);
|
2009-03-09 19:25:26 +01:00
|
|
|
} else if (audio_output_all_check() > 0) {
|
|
|
|
/* not enough data from decoder, but the
|
|
|
|
output thread is still busy, so it's
|
|
|
|
okay */
|
|
|
|
|
|
|
|
/* XXX synchronize in a better way */
|
|
|
|
g_usleep(10000);
|
2010-11-05 19:08:54 +01:00
|
|
|
} else if (player_dc_at_next_song(&player)) {
|
2008-08-26 08:27:09 +02:00
|
|
|
/* at the beginning of a new song */
|
|
|
|
|
2009-03-09 19:15:14 +01:00
|
|
|
if (!player_song_border(&player))
|
2009-01-21 16:44:32 +01:00
|
|
|
break;
|
2009-10-31 19:22:56 +01:00
|
|
|
} else if (decoder_lock_is_idle(dc)) {
|
2009-06-02 08:39:17 +02:00
|
|
|
/* check the size of the pipe again, because
|
|
|
|
the decoder thread may have added something
|
|
|
|
since we last checked */
|
2010-11-07 15:30:18 +01:00
|
|
|
if (music_pipe_empty(player.pipe)) {
|
2009-11-09 22:22:27 +01:00
|
|
|
/* wait for the hardware to finish
|
|
|
|
playback */
|
|
|
|
audio_output_all_drain();
|
2009-06-02 08:39:17 +02:00
|
|
|
break;
|
2009-11-09 22:22:27 +01:00
|
|
|
}
|
2011-10-06 20:30:46 +02:00
|
|
|
} else if (player.output_open) {
|
2009-03-09 19:25:26 +01:00
|
|
|
/* the decoder is too busy and hasn't provided
|
|
|
|
new PCM data in time: send silence (if the
|
|
|
|
output pipe is empty) */
|
2009-03-10 20:43:19 +01:00
|
|
|
if (!player_send_silence(&player))
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2009-03-11 09:20:34 +01:00
|
|
|
player_dc_stop(&player);
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2009-03-09 19:12:06 +01:00
|
|
|
music_pipe_clear(player.pipe, player_buffer);
|
2009-03-06 00:42:03 +01:00
|
|
|
music_pipe_free(player.pipe);
|
2009-03-11 09:20:34 +01:00
|
|
|
|
2010-03-17 21:57:35 +01:00
|
|
|
if (player.cross_fade_tag != NULL)
|
|
|
|
tag_free(player.cross_fade_tag);
|
|
|
|
|
2012-08-09 22:19:39 +02:00
|
|
|
if (player.song != NULL)
|
|
|
|
song_free(player.song);
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2009-11-03 23:32:37 +01:00
|
|
|
|
|
|
|
if (player.queued) {
|
2009-11-03 21:08:48 +01:00
|
|
|
assert(pc->next_song != NULL);
|
2012-08-09 22:19:39 +02:00
|
|
|
song_free(pc->next_song);
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->next_song = NULL;
|
2009-11-03 23:32:37 +01:00
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->state = PLAYER_STATE_STOP;
|
2009-11-03 23:32:37 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-03-11 09:20:34 +01:00
|
|
|
event_pipe_emit(PIPE_EVENT_PLAYLIST);
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
static gpointer
|
|
|
|
player_task(gpointer arg)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
struct player_control *pc = arg;
|
2009-10-31 19:22:56 +01:00
|
|
|
|
2011-01-10 21:27:43 +01:00
|
|
|
struct decoder_control *dc = dc_new(pc->cond);
|
2011-01-10 20:46:04 +01:00
|
|
|
decoder_thread_start(dc);
|
2009-01-25 13:44:39 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_buffer = music_buffer_new(pc->buffer_chunks);
|
2009-03-09 19:12:06 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2008-08-26 08:27:09 +02:00
|
|
|
while (1) {
|
2009-11-03 21:08:48 +01:00
|
|
|
switch (pc->command) {
|
2011-10-05 22:13:13 +02:00
|
|
|
case PLAYER_COMMAND_SEEK:
|
2008-10-12 00:07:54 +02:00
|
|
|
case PLAYER_COMMAND_QUEUE:
|
2009-11-03 21:08:48 +01:00
|
|
|
assert(pc->next_song != NULL);
|
2009-02-10 08:18:28 +01:00
|
|
|
|
2011-01-10 20:46:04 +01:00
|
|
|
do_play(pc, dc);
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_COMMAND_STOP:
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2009-03-09 19:25:26 +01:00
|
|
|
audio_output_all_cancel();
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-03-09 19:25:26 +01:00
|
|
|
/* fall through */
|
|
|
|
|
2008-08-26 08:27:09 +02:00
|
|
|
case PLAYER_COMMAND_PAUSE:
|
2012-08-09 22:19:39 +02:00
|
|
|
if (pc->next_song != NULL) {
|
|
|
|
song_free(pc->next_song);
|
|
|
|
pc->next_song = NULL;
|
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_COMMAND_CLOSE_AUDIO:
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2010-03-03 20:29:33 +01:00
|
|
|
audio_output_all_release();
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
|
|
|
player_command_finished_locked(pc);
|
2009-05-29 23:34:51 +02:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
/* in the DEBUG build, check for leaked
|
|
|
|
music_chunk objects by freeing the
|
|
|
|
music_buffer */
|
|
|
|
music_buffer_free(player_buffer);
|
2009-11-03 21:08:48 +01:00
|
|
|
player_buffer = music_buffer_new(pc->buffer_chunks);
|
2009-05-29 23:34:51 +02:00
|
|
|
#endif
|
|
|
|
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
2009-10-23 10:55:52 +02:00
|
|
|
case PLAYER_COMMAND_UPDATE_AUDIO:
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2009-10-23 10:55:52 +02:00
|
|
|
audio_output_all_enable_disable();
|
2009-11-03 21:08:48 +01:00
|
|
|
player_lock(pc);
|
|
|
|
player_command_finished_locked(pc);
|
2009-10-23 10:55:52 +02:00
|
|
|
break;
|
|
|
|
|
2008-08-26 08:27:16 +02:00
|
|
|
case PLAYER_COMMAND_EXIT:
|
2009-11-03 21:08:48 +01:00
|
|
|
player_unlock(pc);
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2011-01-10 20:46:04 +01:00
|
|
|
dc_quit(dc);
|
|
|
|
dc_free(dc);
|
2009-02-10 18:51:51 +01:00
|
|
|
audio_output_all_close();
|
2009-03-09 19:12:06 +01:00
|
|
|
music_buffer_free(player_buffer);
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished(pc);
|
2009-10-31 18:01:05 +01:00
|
|
|
return NULL;
|
2008-08-26 08:27:16 +02:00
|
|
|
|
2008-10-12 00:07:54 +02:00
|
|
|
case PLAYER_COMMAND_CANCEL:
|
2012-08-09 22:19:39 +02:00
|
|
|
if (pc->next_song != NULL) {
|
|
|
|
song_free(pc->next_song);
|
|
|
|
pc->next_song = NULL;
|
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
2009-10-08 22:09:25 +02:00
|
|
|
case PLAYER_COMMAND_REFRESH:
|
|
|
|
/* no-op when not playing */
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2009-10-08 22:09:25 +02:00
|
|
|
break;
|
|
|
|
|
2008-08-26 08:27:09 +02:00
|
|
|
case PLAYER_COMMAND_NONE:
|
2009-11-03 21:08:48 +01:00
|
|
|
player_wait(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
void
|
|
|
|
player_create(struct player_control *pc)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
assert(pc->thread == NULL);
|
2009-01-25 13:44:33 +01:00
|
|
|
|
2011-01-07 22:29:47 +01:00
|
|
|
GError *e = NULL;
|
2009-11-03 21:08:48 +01:00
|
|
|
pc->thread = g_thread_create(player_task, pc, true, &e);
|
|
|
|
if (pc->thread == NULL)
|
2010-09-25 15:00:43 +02:00
|
|
|
MPD_ERROR("Failed to spawn player task: %s", e->message);
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|