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"
|
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);
|
|
|
|
music_pipe.size = size;
|
|
|
|
music_pipe.begin = 0;
|
|
|
|
music_pipe.end = 0;
|
|
|
|
music_pipe.lazy = false;
|
|
|
|
music_pipe.notify = notify;
|
|
|
|
music_pipe.chunks[0].chunkSize = 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);
|
|
|
|
g_free(music_pipe.chunks);
|
2008-04-12 06:18:38 +02:00
|
|
|
}
|
|
|
|
|
2008-11-02 14:18:34 +01:00
|
|
|
void music_pipe_clear(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-02 16:56:09 +01:00
|
|
|
music_pipe.end = music_pipe.begin;
|
|
|
|
music_pipe.chunks[music_pipe.end].chunkSize = 0;
|
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-02 16:56:09 +01:00
|
|
|
assert(i <= music_pipe.size);
|
2008-04-12 06:14:55 +02:00
|
|
|
|
|
|
|
++i;
|
2008-11-02 16:56:09 +01:00
|
|
|
return i == music_pipe.size ? 0 : i;
|
2008-04-12 06:14:55 +02:00
|
|
|
}
|
|
|
|
|
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:56:09 +01:00
|
|
|
assert(i == (music_pipe.end + 1) % music_pipe.size);
|
|
|
|
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;
|
|
|
|
music_pipe.chunks[i].chunkSize = 0;
|
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
|
|
|
|
|
|
|
if (chunk->chunkSize > 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);
|
|
|
|
assert(music_pipe.begin < music_pipe.size);
|
2008-04-12 06:13:51 +02:00
|
|
|
|
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:56:09 +01:00
|
|
|
return i + music_pipe.size - 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)
|
|
|
|
max += music_pipe.size;
|
|
|
|
i = (unsigned)music_pipe.begin + relative;
|
2008-04-12 06:12:42 +02:00
|
|
|
if (i >= max)
|
|
|
|
return -1;
|
|
|
|
|
2008-11-02 16:56:09 +01:00
|
|
|
if (i >= music_pipe.size)
|
|
|
|
i -= music_pipe.size;
|
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:56:09 +01:00
|
|
|
assert(i < music_pipe.size);
|
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 14:18:34 +01:00
|
|
|
tail_chunk(float data_time, uint16_t bitRate, 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-04-12 06:21:13 +02:00
|
|
|
assert(chunk->chunkSize <= sizeof(chunk->data));
|
2008-10-23 16:48:49 +02:00
|
|
|
if (chunk->chunkSize + 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-04-12 06:21:13 +02:00
|
|
|
assert(chunk->chunkSize == 0);
|
2008-04-12 06:11:23 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:21:13 +02:00
|
|
|
if (chunk->chunkSize == 0) {
|
|
|
|
/* if the chunk is empty, nobody has set bitRate and
|
|
|
|
times yet */
|
|
|
|
|
|
|
|
chunk->bitRate = bitRate;
|
|
|
|
chunk->times = data_time;
|
|
|
|
}
|
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 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 14:18:34 +01:00
|
|
|
float data_time, uint16_t bitRate)
|
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-08-26 08:27:05 +02:00
|
|
|
size_t ret = 0, dataToSend;
|
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 14:18:34 +01:00
|
|
|
chunk = tail_chunk(data_time, bitRate, 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-04-12 06:21:22 +02:00
|
|
|
dataToSend = sizeof(chunk->data) - chunk->chunkSize;
|
|
|
|
if (dataToSend > datalen)
|
|
|
|
dataToSend = datalen;
|
2004-05-07 04:42:49 +02:00
|
|
|
|
2008-10-23 16:48:49 +02:00
|
|
|
/* don't split frames */
|
|
|
|
dataToSend /= frame_size;
|
|
|
|
dataToSend *= frame_size;
|
|
|
|
|
2008-04-12 06:13:24 +02:00
|
|
|
memcpy(chunk->data + chunk->chunkSize, data, dataToSend);
|
|
|
|
chunk->chunkSize += dataToSend;
|
2006-07-20 18:02:40 +02:00
|
|
|
datalen -= dataToSend;
|
|
|
|
data += dataToSend;
|
2008-08-26 08:27:05 +02:00
|
|
|
ret += dataToSend;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2004-05-07 04:42:49 +02:00
|
|
|
|
2008-04-12 06:21:34 +02:00
|
|
|
if (chunk != NULL && chunk->chunkSize == 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 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-04-12 06:18:19 +02:00
|
|
|
if (i >= 0)
|
2008-11-02 16:56:09 +01:00
|
|
|
music_pipe.begin = i;
|
2008-04-12 06:18:19 +02:00
|
|
|
}
|