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"
|
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-11-02 16:58:49 +01:00
|
|
|
static void
|
|
|
|
music_chunk_init(struct music_chunk *chunk)
|
|
|
|
{
|
|
|
|
chunk->length = 0;
|
2008-11-02 17:01:00 +01:00
|
|
|
chunk->tag = NULL;
|
2008-11-02 16:58:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
music_chunk_free(struct music_chunk *chunk)
|
|
|
|
{
|
2008-11-02 17:01:00 +01:00
|
|
|
if (chunk->tag != NULL)
|
|
|
|
tag_free(chunk->tag);
|
2008-11-02 16:58:49 +01: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_flush(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-02 16:56:09 +01:00
|
|
|
struct music_chunk *chunk = music_pipe_get_chunk(music_pipe.end);
|
2008-04-12 06:21:13 +02:00
|
|
|
|
2008-11-02 16:57:16 +01:00
|
|
|
if (chunk->length > 0) {
|
2008-11-02 16:56:09 +01:00
|
|
|
unsigned int next = successor(music_pipe.end);
|
|
|
|
if (next == music_pipe.begin)
|
2008-04-12 06:21:13 +02:00
|
|
|
/* all buffers are full; we have to wait for
|
|
|
|
the player to free one, so don't flush
|
|
|
|
right now */
|
|
|
|
return;
|
|
|
|
|
2008-04-13 03:16:15 +02:00
|
|
|
output_buffer_expand(next);
|
2008-04-12 06:21:13 +02:00
|
|
|
}
|
2004-05-07 21:11:43 +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
|
|
|
}
|
|
|
|
|
2008-04-12 06:11:23 +02:00
|
|
|
/**
|
2008-08-26 08:27:05 +02:00
|
|
|
* Return the tail chunk which has room for additional data.
|
2008-04-12 06:11:23 +02:00
|
|
|
*
|
2008-08-26 08:27:05 +02:00
|
|
|
* @return the chunk which has room for more data; NULL if there is no
|
2008-08-26 08:27:05 +02:00
|
|
|
* room.
|
2008-04-12 06:11:23 +02:00
|
|
|
*/
|
2008-11-02 14:15:47 +01:00
|
|
|
static struct music_chunk *
|
2008-11-02 16:57:15 +01:00
|
|
|
tail_chunk(size_t frame_size)
|
2008-04-12 06:11:23 +02:00
|
|
|
{
|
|
|
|
unsigned int next;
|
2008-11-02 14:15:47 +01:00
|
|
|
struct music_chunk *chunk;
|
2008-04-12 06:11:23 +02:00
|
|
|
|
2008-11-02 16:56:09 +01:00
|
|
|
chunk = music_pipe_get_chunk(music_pipe.end);
|
2008-11-02 16:57:16 +01:00
|
|
|
assert(chunk->length <= sizeof(chunk->data));
|
2008-11-13 02:06:52 +01:00
|
|
|
assert((chunk->length % frame_size) == 0);
|
|
|
|
|
2008-11-02 16:57:16 +01:00
|
|
|
if (chunk->length + frame_size > sizeof(chunk->data)) {
|
2008-04-12 06:21:13 +02:00
|
|
|
/* this chunk is full; allocate a new chunk */
|
2008-11-02 16:56:09 +01:00
|
|
|
next = successor(music_pipe.end);
|
|
|
|
if (music_pipe.begin == next)
|
2008-08-26 08:27:05 +02:00
|
|
|
/* no chunks available */
|
2008-08-26 08:27:05 +02:00
|
|
|
return NULL;
|
2008-04-12 06:21:13 +02:00
|
|
|
|
2008-04-13 03:16:15 +02:00
|
|
|
output_buffer_expand(next);
|
2008-11-02 14:18:34 +01:00
|
|
|
chunk = music_pipe_get_chunk(next);
|
2008-11-02 16:57:16 +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
|
|
|
}
|
|
|
|
|
2008-11-02 16:56:49 +01:00
|
|
|
static size_t
|
|
|
|
music_chunk_append(struct music_chunk *chunk, const void *data, size_t length,
|
|
|
|
size_t frame_size)
|
|
|
|
{
|
2008-11-02 16:57:16 +01:00
|
|
|
size_t rest = sizeof(chunk->data) - chunk->length;
|
2008-11-02 16:56:49 +01:00
|
|
|
|
|
|
|
assert(rest >= frame_size);
|
|
|
|
|
|
|
|
if (length > rest)
|
|
|
|
length = rest;
|
|
|
|
|
|
|
|
/* don't split frames */
|
|
|
|
length /= frame_size;
|
|
|
|
length *= frame_size;
|
|
|
|
|
2008-11-02 16:57:16 +01:00
|
|
|
memcpy(chunk->data + chunk->length, data, length);
|
|
|
|
chunk->length += length;
|
2008-11-02 16:56:49 +01:00
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2008-11-02 14:18:34 +01:00
|
|
|
size_t music_pipe_append(const void *data0, size_t datalen,
|
2008-11-02 16:55:43 +01:00
|
|
|
const struct audio_format *audio_format,
|
2008-11-02 16:57:16 +01:00
|
|
|
float data_time, uint16_t bit_rate)
|
2004-05-07 04:42:49 +02:00
|
|
|
{
|
2008-08-26 08:27:05 +02:00
|
|
|
const unsigned char *data = data0;
|
2008-11-02 16:55:43 +01:00
|
|
|
const size_t frame_size = audio_format_frame_size(audio_format);
|
2008-11-02 16:56:49 +01:00
|
|
|
size_t ret = 0, nbytes;
|
2008-11-02 14:15:47 +01:00
|
|
|
struct music_chunk *chunk = NULL;
|
2004-05-10 16:06:23 +02:00
|
|
|
|
2008-10-23 16:48:49 +02:00
|
|
|
/* no partial frames allowed */
|
|
|
|
assert((datalen % frame_size) == 0);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (datalen) {
|
2008-11-02 16:57:15 +01:00
|
|
|
chunk = tail_chunk(frame_size);
|
2008-08-26 08:27:05 +02:00
|
|
|
if (chunk == NULL)
|
2008-08-26 08:27:05 +02:00
|
|
|
return ret;
|
2004-05-07 04:42:49 +02:00
|
|
|
|
2008-11-02 16:57:16 +01:00
|
|
|
if (chunk->length == 0) {
|
2008-11-02 16:57:15 +01:00
|
|
|
/* if the chunk is empty, nobody has set bitRate and
|
|
|
|
times yet */
|
|
|
|
|
2008-11-02 16:57:16 +01:00
|
|
|
chunk->bit_rate = bit_rate;
|
2008-11-02 16:57:15 +01:00
|
|
|
chunk->times = data_time;
|
|
|
|
}
|
|
|
|
|
2008-11-02 16:56:49 +01:00
|
|
|
nbytes = music_chunk_append(chunk, data, datalen,
|
|
|
|
frame_size);
|
|
|
|
assert(nbytes > 0);
|
2008-10-23 16:48:49 +02:00
|
|
|
|
2008-11-02 16:56:49 +01:00
|
|
|
datalen -= nbytes;
|
|
|
|
data += nbytes;
|
|
|
|
ret += nbytes;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2004-05-07 04:42:49 +02:00
|
|
|
|
2008-11-02 16:57:16 +01:00
|
|
|
if (chunk != NULL && chunk->length == sizeof(chunk->data))
|
2008-11-02 14:18:34 +01:00
|
|
|
music_pipe_flush();
|
2008-04-12 06:21:34 +02:00
|
|
|
|
2008-08-26 08:27:05 +02:00
|
|
|
return ret;
|
2004-05-07 04:42:49 +02:00
|
|
|
}
|
2004-06-06 18:42:14 +02:00
|
|
|
|
2008-11-02 17:01:00 +01:00
|
|
|
bool music_pipe_tag(const struct tag *tag)
|
|
|
|
{
|
|
|
|
struct music_chunk *chunk;
|
|
|
|
|
|
|
|
chunk = music_pipe_get_chunk(music_pipe.end);
|
|
|
|
if (chunk->length > 0 || chunk->tag != NULL) {
|
|
|
|
/* this chunk is not empty; allocate a new chunk,
|
|
|
|
because chunk.tag refers to the beginning of the
|
|
|
|
chunk data */
|
|
|
|
unsigned next = successor(music_pipe.end);
|
|
|
|
if (music_pipe.begin == next)
|
|
|
|
/* no chunks available */
|
|
|
|
return false;
|
|
|
|
|
|
|
|
output_buffer_expand(next);
|
|
|
|
chunk = music_pipe_get_chunk(next);
|
|
|
|
assert(chunk->length == 0 && chunk->tag == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
chunk->tag = tag_dup(tag);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|