2004-05-07 04:42:49 +02:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-05-07 04:42:49 +02:00
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2008-11-02 14:12:52 +01:00
|
|
|
#include "pipe.h"
|
2009-03-03 22:23:25 +01:00
|
|
|
#include "chunk.h"
|
2008-10-08 10:49:16 +02:00
|
|
|
#include "notify.h"
|
2008-11-02 16:55:43 +01:00
|
|
|
#include "audio_format.h"
|
2008-11-02 17:01:00 +01:00
|
|
|
#include "tag.h"
|
2004-05-07 04:42:49 +02:00
|
|
|
|
2008-11-02 16:55:48 +01:00
|
|
|
#include <glib.h>
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-11-02 16:56:09 +01:00
|
|
|
struct music_pipe music_pipe;
|
2008-08-26 08:41:05 +02:00
|
|
|
|
2008-10-08 10:49:16 +02:00
|
|
|
void
|
2008-11-02 14:18:34 +01:00
|
|
|
music_pipe_init(unsigned int size, struct notify *notify)
|
2008-03-26 11:38:12 +01:00
|
|
|
{
|
2008-04-12 06:18:04 +02:00
|
|
|
assert(size > 0);
|
|
|
|
|
2008-11-02 16:56:09 +01:00
|
|
|
music_pipe.chunks = g_new(struct music_chunk, size);
|
2008-11-02 16:57:37 +01:00
|
|
|
music_pipe.num_chunks = size;
|
2008-11-02 16:56:09 +01:00
|
|
|
music_pipe.begin = 0;
|
|
|
|
music_pipe.end = 0;
|
|
|
|
music_pipe.lazy = false;
|
|
|
|
music_pipe.notify = notify;
|
2008-11-02 16:58:49 +01:00
|
|
|
music_chunk_init(&music_pipe.chunks[0]);
|
2004-06-07 00:13:23 +02:00
|
|
|
}
|
|
|
|
|
2008-11-02 14:18:34 +01:00
|
|
|
void music_pipe_free(void)
|
2008-04-12 06:18:38 +02:00
|
|
|
{
|
2008-11-02 16:56:09 +01:00
|
|
|
assert(music_pipe.chunks != NULL);
|
2008-04-12 06:18:38 +02:00
|
|
|
|
2008-11-02 16:58:49 +01:00
|
|
|
music_pipe_clear();
|
|
|
|
|
|
|
|
g_free(music_pipe.chunks);
|
2004-05-18 05:37:55 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:14:55 +02:00
|
|
|
/** return the index of the chunk after i */
|
2008-04-13 03:16:15 +02:00
|
|
|
static inline unsigned successor(unsigned i)
|
2008-04-12 06:14:55 +02:00
|
|
|
{
|
2008-11-18 19:54:39 +01:00
|
|
|
assert(i < music_pipe.num_chunks);
|
2008-04-12 06:14:55 +02:00
|
|
|
|
|
|
|
++i;
|
2008-11-02 16:57:37 +01:00
|
|
|
return i == music_pipe.num_chunks ? 0 : i;
|
2008-04-12 06:14:55 +02:00
|
|
|
}
|
|
|
|
|
2008-11-02 16:58:49 +01:00
|
|
|
void music_pipe_clear(void)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = music_pipe.begin; i != music_pipe.end; i = successor(i))
|
|
|
|
music_chunk_free(&music_pipe.chunks[i]);
|
|
|
|
|
|
|
|
music_chunk_free(&music_pipe.chunks[music_pipe.end]);
|
|
|
|
|
|
|
|
music_pipe.end = music_pipe.begin;
|
|
|
|
music_chunk_init(&music_pipe.chunks[music_pipe.end]);
|
|
|
|
}
|
|
|
|
|
2008-04-12 06:21:09 +02:00
|
|
|
/**
|
|
|
|
* Mark the tail chunk as "full" and wake up the player if is waiting
|
|
|
|
* for the decoder.
|
|
|
|
*/
|
2008-04-13 03:16:15 +02:00
|
|
|
static void output_buffer_expand(unsigned i)
|
2008-04-12 06:21:09 +02:00
|
|
|
{
|
2008-11-02 16:56:09 +01:00
|
|
|
int was_empty = music_pipe.notify != NULL && (!music_pipe.lazy || music_pipe_is_empty());
|
2008-04-12 06:21:09 +02:00
|
|
|
|
2008-11-02 16:57:37 +01:00
|
|
|
assert(i == (music_pipe.end + 1) % music_pipe.num_chunks);
|
2008-11-02 16:56:09 +01:00
|
|
|
assert(i != music_pipe.end);
|
2008-04-12 06:21:09 +02:00
|
|
|
|
2008-11-02 16:56:09 +01:00
|
|
|
music_pipe.end = i;
|
2008-11-02 16:58:49 +01:00
|
|
|
music_chunk_init(&music_pipe.chunks[i]);
|
|
|
|
|
2008-04-12 06:21:09 +02:00
|
|
|
if (was_empty)
|
|
|
|
/* if the buffer was empty, the player thread might be
|
|
|
|
waiting for us; wake it up now that another decoded
|
|
|
|
buffer has become available. */
|
2008-11-02 16:56:09 +01:00
|
|
|
notify_signal(music_pipe.notify);
|
2008-04-12 06:21:09 +02:00
|
|
|
}
|
|
|
|
|
2008-11-02 14:18:34 +01:00
|
|
|
void music_pipe_set_lazy(bool lazy)
|
2008-04-15 07:57:22 +02:00
|
|
|
{
|
2008-11-02 16:56:09 +01:00
|
|
|
music_pipe.lazy = lazy;
|
2008-04-15 07:57:22 +02:00
|
|
|
}
|
|
|
|
|
2008-11-02 14:18:34 +01:00
|
|
|
void music_pipe_shift(void)
|
2008-04-12 06:13:51 +02:00
|
|
|
{
|
2008-11-02 16:56:09 +01:00
|
|
|
assert(music_pipe.begin != music_pipe.end);
|
2008-11-02 16:57:37 +01:00
|
|
|
assert(music_pipe.begin < music_pipe.num_chunks);
|
2008-04-12 06:13:51 +02:00
|
|
|
|
2008-11-02 16:58:49 +01:00
|
|
|
music_chunk_free(&music_pipe.chunks[music_pipe.begin]);
|
|
|
|
|
2008-11-02 16:56:09 +01:00
|
|
|
music_pipe.begin = successor(music_pipe.begin);
|
2008-04-12 06:13:51 +02:00
|
|
|
}
|
|
|
|
|
2008-11-02 14:18:34 +01:00
|
|
|
unsigned int music_pipe_relative(const unsigned i)
|
2008-04-12 06:11:41 +02:00
|
|
|
{
|
2008-11-02 16:56:09 +01:00
|
|
|
if (i >= music_pipe.begin)
|
|
|
|
return i - music_pipe.begin;
|
2008-04-12 06:11:41 +02:00
|
|
|
else
|
2008-11-02 16:57:37 +01:00
|
|
|
return i + music_pipe.num_chunks - music_pipe.begin;
|
2008-04-12 06:12:53 +02:00
|
|
|
}
|
|
|
|
|
2008-11-02 14:18:34 +01:00
|
|
|
unsigned music_pipe_available(void)
|
2008-04-12 06:12:53 +02:00
|
|
|
{
|
2008-11-02 16:56:09 +01:00
|
|
|
return music_pipe_relative(music_pipe.end);
|
2008-04-12 06:11:41 +02:00
|
|
|
}
|
|
|
|
|
2008-11-02 14:18:34 +01:00
|
|
|
int music_pipe_absolute(const unsigned relative)
|
2008-04-12 06:12:42 +02:00
|
|
|
{
|
|
|
|
unsigned i, max;
|
|
|
|
|
2008-11-02 16:56:09 +01:00
|
|
|
max = music_pipe.end;
|
|
|
|
if (max < music_pipe.begin)
|
2008-11-02 16:57:37 +01:00
|
|
|
max += music_pipe.num_chunks;
|
2008-11-02 16:56:09 +01:00
|
|
|
i = (unsigned)music_pipe.begin + relative;
|
2008-04-12 06:12:42 +02:00
|
|
|
if (i >= max)
|
|
|
|
return -1;
|
|
|
|
|
2008-11-02 16:57:37 +01:00
|
|
|
if (i >= music_pipe.num_chunks)
|
|
|
|
i -= music_pipe.num_chunks;
|
2008-04-12 06:12:42 +02:00
|
|
|
|
|
|
|
return (int)i;
|
|
|
|
}
|
|
|
|
|
2008-11-02 14:15:47 +01:00
|
|
|
struct music_chunk *
|
2008-11-02 14:18:34 +01:00
|
|
|
music_pipe_get_chunk(const unsigned i)
|
2008-04-12 06:13:11 +02:00
|
|
|
{
|
2008-11-02 16:57:37 +01:00
|
|
|
assert(i < music_pipe.num_chunks);
|
2008-04-12 06:13:11 +02:00
|
|
|
|
2008-11-02 16:56:09 +01:00
|
|
|
return &music_pipe.chunks[i];
|
2008-04-12 06:13:11 +02:00
|
|
|
}
|
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
struct music_chunk *
|
|
|
|
music_pipe_allocate(void)
|
2008-04-12 06:11:23 +02:00
|
|
|
{
|
2008-11-02 14:15:47 +01:00
|
|
|
struct music_chunk *chunk;
|
2008-04-12 06:11:23 +02:00
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
/* the music_pipe.end chunk is always kept initialized */
|
2008-11-02 16:56:09 +01:00
|
|
|
chunk = music_pipe_get_chunk(music_pipe.end);
|
2009-03-06 00:42:01 +01:00
|
|
|
assert(chunk->length == 0);
|
2008-04-12 06:11:23 +02:00
|
|
|
|
2008-08-26 08:27:05 +02:00
|
|
|
return chunk;
|
2008-04-12 06:11:23 +02:00
|
|
|
}
|
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
bool
|
|
|
|
music_pipe_push(struct music_chunk *chunk)
|
2009-01-17 13:09:29 +01:00
|
|
|
{
|
2009-03-06 00:42:01 +01:00
|
|
|
unsigned int next;
|
2009-01-17 13:09:29 +01:00
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
assert(chunk == music_pipe_get_chunk(music_pipe.end));
|
2009-01-17 13:09:29 +01:00
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
next = successor(music_pipe.end);
|
|
|
|
if (music_pipe.begin == next)
|
|
|
|
/* no room */
|
|
|
|
return false;
|
2008-11-02 16:56:49 +01:00
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
output_buffer_expand(next);
|
|
|
|
return true;
|
2008-11-02 16:56:49 +01:00
|
|
|
}
|
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
void
|
|
|
|
music_pipe_cancel(struct music_chunk *chunk)
|
2008-11-02 17:01:00 +01:00
|
|
|
{
|
2009-03-06 00:42:01 +01:00
|
|
|
assert(chunk == music_pipe_get_chunk(music_pipe.end));
|
2008-11-02 17:01:00 +01:00
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
music_chunk_free(chunk);
|
2008-11-02 17:01:00 +01:00
|
|
|
}
|
|
|
|
|
2008-11-02 14:18:34 +01:00
|
|
|
void music_pipe_skip(unsigned num)
|
2008-04-12 06:18:19 +02:00
|
|
|
{
|
2008-11-02 14:18:34 +01:00
|
|
|
int i = music_pipe_absolute(num);
|
2008-11-02 16:58:49 +01:00
|
|
|
if (i < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (music_pipe.begin != (unsigned)i)
|
|
|
|
music_pipe_shift();
|
2008-04-12 06:18:19 +02:00
|
|
|
}
|
2008-11-13 02:06:58 +01:00
|
|
|
|
2008-11-13 02:09:33 +01:00
|
|
|
void music_pipe_chop(unsigned first)
|
|
|
|
{
|
|
|
|
for (unsigned i = first; i != music_pipe.end; i = successor(i))
|
|
|
|
music_chunk_free(&music_pipe.chunks[i]);
|
|
|
|
|
|
|
|
music_chunk_free(&music_pipe.chunks[music_pipe.end]);
|
|
|
|
|
|
|
|
music_pipe.end = first;
|
|
|
|
music_chunk_init(&music_pipe.chunks[first]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-11-13 02:06:58 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
void music_pipe_check_format(const struct audio_format *current,
|
|
|
|
int next_index, const struct audio_format *next)
|
|
|
|
{
|
|
|
|
const struct audio_format *audio_format = current;
|
|
|
|
|
|
|
|
for (unsigned i = music_pipe.begin; i != music_pipe.end;
|
|
|
|
i = successor(i)) {
|
|
|
|
const struct music_chunk *chunk = music_pipe_get_chunk(i);
|
|
|
|
|
|
|
|
if (next_index > 0 && i == (unsigned)next_index)
|
|
|
|
audio_format = next;
|
|
|
|
|
|
|
|
assert(chunk->length % audio_format_frame_size(audio_format) == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|