2007-10-25 21:27:45 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Nicolas Pennequin
|
|
|
|
*
|
2008-06-28 18:10:04 +00: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.
|
2007-10-25 21:27:45 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
2010-08-12 14:01:28 +00:00
|
|
|
#include <inttypes.h>
|
2007-10-25 21:27:45 +00:00
|
|
|
#include "buffering.h"
|
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
#include "storage.h"
|
2007-10-25 21:27:45 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "file.h"
|
|
|
|
#include "panic.h"
|
|
|
|
#include "lcd.h"
|
|
|
|
#include "font.h"
|
|
|
|
#include "button.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "tree.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "codecs.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "mp3_playback.h"
|
|
|
|
#include "usb.h"
|
|
|
|
#include "screens.h"
|
|
|
|
#include "playlist.h"
|
|
|
|
#include "pcmbuf.h"
|
2008-10-16 10:38:03 +00:00
|
|
|
#include "appevents.h"
|
2008-04-14 16:17:47 +00:00
|
|
|
#include "metadata.h"
|
2008-12-09 23:07:59 +00:00
|
|
|
#ifdef HAVE_ALBUMART
|
|
|
|
#include "albumart.h"
|
2009-05-01 23:31:43 +00:00
|
|
|
#include "jpeg_load.h"
|
2009-10-16 19:14:41 +00:00
|
|
|
#include "bmp.h"
|
2011-02-09 20:13:13 +00:00
|
|
|
#include "playback.h"
|
2008-12-09 23:07:59 +00:00
|
|
|
#endif
|
2007-10-25 21:27:45 +00:00
|
|
|
|
|
|
|
#define GUARD_BUFSIZE (32*1024)
|
|
|
|
|
|
|
|
/* Define LOGF_ENABLE to enable logf output in this file */
|
2011-04-27 03:08:23 +00:00
|
|
|
/* #define LOGF_ENABLE */
|
2007-10-25 21:27:45 +00:00
|
|
|
#include "logf.h"
|
|
|
|
|
|
|
|
/* macros to enable logf for queues
|
|
|
|
logging on SYS_TIMEOUT can be disabled */
|
|
|
|
#ifdef SIMULATOR
|
|
|
|
/* Define this for logf output of all queuing except SYS_TIMEOUT */
|
|
|
|
#define BUFFERING_LOGQUEUES
|
|
|
|
/* Define this to logf SYS_TIMEOUT messages */
|
|
|
|
/* #define BUFFERING_LOGQUEUES_SYS_TIMEOUT */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BUFFERING_LOGQUEUES
|
|
|
|
#define LOGFQUEUE logf
|
|
|
|
#else
|
|
|
|
#define LOGFQUEUE(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BUFFERING_LOGQUEUES_SYS_TIMEOUT
|
|
|
|
#define LOGFQUEUE_SYS_TIMEOUT logf
|
|
|
|
#else
|
|
|
|
#define LOGFQUEUE_SYS_TIMEOUT(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* amount of data to read in one read() call */
|
2008-03-29 20:52:56 +00:00
|
|
|
#define BUFFERING_DEFAULT_FILECHUNK (1024*32)
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2007-11-04 19:01:02 +00:00
|
|
|
#define BUF_HANDLE_MASK 0x7FFFFFFF
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2007-10-27 01:37:33 +00:00
|
|
|
/* assert(sizeof(struct memory_handle)%4==0) */
|
2007-10-25 21:27:45 +00:00
|
|
|
struct memory_handle {
|
2007-11-03 22:06:56 +00:00
|
|
|
int id; /* A unique ID for the handle */
|
2007-10-27 01:37:33 +00:00
|
|
|
enum data_type type; /* Type of data buffered with this handle */
|
2011-04-27 03:08:23 +00:00
|
|
|
int8_t pinned; /* Count of references */
|
|
|
|
int8_t signaled; /* Stop any attempt at waiting to get the data */
|
2007-10-27 01:37:33 +00:00
|
|
|
char path[MAX_PATH]; /* Path if data originated in a file */
|
|
|
|
int fd; /* File descriptor to path (-1 if closed) */
|
2011-02-14 08:36:29 +00:00
|
|
|
size_t data; /* Start index of the handle's data buffer */
|
2007-10-27 01:37:33 +00:00
|
|
|
volatile size_t ridx; /* Read pointer, relative to the main buffer */
|
2011-02-14 08:36:29 +00:00
|
|
|
size_t widx; /* Write pointer, relative to the main buffer */
|
2007-10-27 01:37:33 +00:00
|
|
|
size_t filesize; /* File total length */
|
|
|
|
size_t filerem; /* Remaining bytes of file NOT in buffer */
|
|
|
|
volatile size_t available; /* Available bytes to read from buffer */
|
|
|
|
size_t offset; /* Offset at which we started reading the file */
|
2007-10-25 21:27:45 +00:00
|
|
|
struct memory_handle *next;
|
|
|
|
};
|
2007-10-27 01:37:33 +00:00
|
|
|
/* invariant: filesize == offset + available + filerem */
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
|
|
|
|
struct buf_message_data
|
|
|
|
{
|
|
|
|
int handle_id;
|
|
|
|
intptr_t data;
|
|
|
|
};
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
static char *buffer;
|
|
|
|
static char *guard_buffer;
|
|
|
|
|
|
|
|
static size_t buffer_len;
|
|
|
|
|
|
|
|
static volatile size_t buf_widx; /* current writing position */
|
|
|
|
static volatile size_t buf_ridx; /* current reading position */
|
|
|
|
/* buf_*idx are values relative to the buffer, not real pointers. */
|
|
|
|
|
|
|
|
/* Configuration */
|
|
|
|
static size_t conf_watermark = 0; /* Level to trigger filebuf fill */
|
|
|
|
static size_t high_watermark = 0; /* High watermark for rebuffer */
|
|
|
|
|
|
|
|
/* current memory handle in the linked list. NULL when the list is empty. */
|
|
|
|
static struct memory_handle *cur_handle;
|
|
|
|
/* first memory handle in the linked list. NULL when the list is empty. */
|
|
|
|
static struct memory_handle *first_handle;
|
|
|
|
|
|
|
|
static int num_handles; /* number of handles in the list */
|
|
|
|
|
2007-11-03 22:06:56 +00:00
|
|
|
static int base_handle_id;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
/* Main lock for adding / removing handles */
|
|
|
|
static struct mutex llist_mutex SHAREDBSS_ATTR;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
|
|
|
/* Handle cache (makes find_handle faster).
|
2007-10-27 01:37:33 +00:00
|
|
|
This is global so that move_handle and rm_handle can invalidate it. */
|
2007-10-25 21:27:45 +00:00
|
|
|
static struct memory_handle *cached_handle = NULL;
|
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
static struct data_counters
|
|
|
|
{
|
2007-10-25 21:27:45 +00:00
|
|
|
size_t remaining; /* Amount of data needing to be buffered */
|
|
|
|
size_t wasted; /* Amount of space available for freeing */
|
|
|
|
size_t buffered; /* Amount of data currently in the buffer */
|
|
|
|
size_t useful; /* Amount of data still useful to the user */
|
|
|
|
} data_counters;
|
|
|
|
|
|
|
|
|
|
|
|
/* Messages available to communicate with the buffering thread */
|
2011-02-14 09:18:58 +00:00
|
|
|
enum
|
|
|
|
{
|
2007-11-05 17:48:21 +00:00
|
|
|
Q_BUFFER_HANDLE = 1, /* Request buffering of a handle, this should not be
|
|
|
|
used in a low buffer situation. */
|
2011-02-14 08:36:29 +00:00
|
|
|
Q_REBUFFER_HANDLE, /* Request reset and rebuffering of a handle at a new
|
|
|
|
file starting position. */
|
2007-10-25 21:27:45 +00:00
|
|
|
Q_CLOSE_HANDLE, /* Request closing a handle */
|
|
|
|
|
|
|
|
/* Configuration: */
|
2007-11-05 17:51:55 +00:00
|
|
|
Q_START_FILL, /* Request that the buffering thread initiate a buffer
|
2007-11-05 03:11:58 +00:00
|
|
|
fill at its earliest convenience */
|
2008-02-12 23:15:59 +00:00
|
|
|
Q_HANDLE_ADDED, /* Inform the buffering thread that a handle was added,
|
|
|
|
(which means the disk is spinning) */
|
2007-10-25 21:27:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Buffering thread */
|
2008-03-26 08:57:25 +00:00
|
|
|
static void buffering_thread(void);
|
2007-10-25 21:27:45 +00:00
|
|
|
static long buffering_stack[(DEFAULT_STACK_SIZE + 0x2000)/sizeof(long)];
|
|
|
|
static const char buffering_thread_name[] = "buffering";
|
2008-12-10 08:57:10 +00:00
|
|
|
static unsigned int buffering_thread_id = 0;
|
2011-02-14 11:27:45 +00:00
|
|
|
static struct event_queue buffering_queue SHAREDBSS_ATTR;
|
|
|
|
static struct queue_sender_list buffering_queue_sender_list SHAREDBSS_ATTR;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
|
|
|
|
2007-11-26 21:13:08 +00:00
|
|
|
|
2010-02-12 13:12:59 +00:00
|
|
|
/* Ring buffer helper functions */
|
2010-02-20 15:13:53 +00:00
|
|
|
|
|
|
|
static inline uintptr_t ringbuf_offset(const void *ptr)
|
|
|
|
{
|
|
|
|
return (uintptr_t)(ptr - (void*)buffer);
|
|
|
|
}
|
|
|
|
|
2010-02-12 13:12:59 +00:00
|
|
|
/* Buffer pointer (p) plus value (v), wrapped if necessary */
|
|
|
|
static inline uintptr_t ringbuf_add(uintptr_t p, size_t v)
|
|
|
|
{
|
|
|
|
uintptr_t res = p + v;
|
|
|
|
if (res >= buffer_len)
|
|
|
|
res -= buffer_len; /* wrap if necssary */
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Buffer pointer (p) minus value (v), wrapped if necessary */
|
|
|
|
static inline uintptr_t ringbuf_sub(uintptr_t p, size_t v)
|
|
|
|
{
|
|
|
|
uintptr_t res = p;
|
|
|
|
if (p < v)
|
|
|
|
res += buffer_len; /* wrap */
|
|
|
|
|
|
|
|
return res - v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* How far value (v) plus buffer pointer (p1) will cross buffer pointer (p2) */
|
|
|
|
static inline ssize_t ringbuf_add_cross(uintptr_t p1, size_t v, uintptr_t p2)
|
|
|
|
{
|
|
|
|
ssize_t res = p1 + v - p2;
|
|
|
|
if (p1 >= p2) /* wrap if necessary */
|
|
|
|
res -= buffer_len;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bytes available in the buffer */
|
|
|
|
#define BUF_USED ringbuf_sub(buf_widx, buf_ridx)
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Real buffer watermark */
|
|
|
|
#define BUF_WATERMARK MIN(conf_watermark, high_watermark)
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
/*
|
|
|
|
LINKED LIST MANAGEMENT
|
|
|
|
======================
|
|
|
|
|
|
|
|
add_handle : Add a handle to the list
|
|
|
|
rm_handle : Remove a handle from the list
|
|
|
|
find_handle : Get a handle pointer from an ID
|
|
|
|
move_handle : Move a handle in the buffer (with or without its data)
|
|
|
|
|
|
|
|
These functions only handle the linked list structure. They don't touch the
|
|
|
|
contents of the struct memory_handle headers. They also change the buf_*idx
|
|
|
|
pointers when necessary and manage the handle IDs.
|
|
|
|
|
|
|
|
The first and current (== last) handle are kept track of.
|
|
|
|
A new handle is added at buf_widx and becomes the current one.
|
|
|
|
buf_widx always points to the current writing position for the current handle
|
|
|
|
buf_ridx always points to the location of the first handle.
|
|
|
|
buf_ridx == buf_widx means the buffer is empty.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Add a new handle to the linked list and return it. It will have become the
|
2007-10-27 04:49:04 +00:00
|
|
|
new current handle.
|
|
|
|
data_size must contain the size of what will be in the handle.
|
|
|
|
can_wrap tells us whether this type of data may wrap on buffer
|
|
|
|
alloc_all tells us if we must immediately be able to allocate data_size
|
2007-10-28 19:19:54 +00:00
|
|
|
returns a valid memory handle if all conditions for allocation are met.
|
|
|
|
NULL if there memory_handle itself cannot be allocated or if the
|
2011-02-13 10:44:13 +00:00
|
|
|
data_size cannot be allocated and alloc_all is set. */
|
2008-03-28 12:51:33 +00:00
|
|
|
static struct memory_handle *add_handle(size_t data_size, bool can_wrap,
|
|
|
|
bool alloc_all)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2007-11-03 22:06:56 +00:00
|
|
|
/* gives each handle a unique id */
|
2007-11-04 19:01:02 +00:00
|
|
|
static int cur_handle_id = 0;
|
2007-10-27 04:49:04 +00:00
|
|
|
size_t shift;
|
2011-02-13 11:02:37 +00:00
|
|
|
size_t widx, new_widx;
|
2007-10-27 04:49:04 +00:00
|
|
|
size_t len;
|
2011-02-09 08:44:37 +00:00
|
|
|
ssize_t overlap;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2007-11-03 02:54:34 +00:00
|
|
|
if (num_handles >= BUF_MAX_HANDLES)
|
|
|
|
return NULL;
|
|
|
|
|
2011-02-13 11:02:37 +00:00
|
|
|
widx = buf_widx;
|
2011-02-13 10:44:13 +00:00
|
|
|
|
2007-10-30 14:11:03 +00:00
|
|
|
if (cur_handle && cur_handle->filerem > 0) {
|
|
|
|
/* the current handle hasn't finished buffering. We can only add
|
|
|
|
a new one if there is already enough free space to finish
|
|
|
|
the buffering. */
|
2011-02-14 02:14:26 +00:00
|
|
|
size_t req = cur_handle->filerem;
|
2010-02-12 13:12:59 +00:00
|
|
|
if (ringbuf_add_cross(cur_handle->widx, req, buf_ridx) >= 0) {
|
2011-02-14 02:14:26 +00:00
|
|
|
/* Not enough space to finish allocation */
|
2007-10-30 14:11:03 +00:00
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
/* Allocate the remainder of the space for the current handle */
|
2011-02-13 11:02:37 +00:00
|
|
|
widx = ringbuf_add(cur_handle->widx, cur_handle->filerem);
|
2007-10-30 14:11:03 +00:00
|
|
|
}
|
|
|
|
}
|
2007-10-27 04:49:04 +00:00
|
|
|
|
2011-02-14 02:14:26 +00:00
|
|
|
/* align to 4 bytes up always leaving a gap */
|
|
|
|
new_widx = ringbuf_add(widx, 4) & ~3;
|
2007-10-27 04:49:04 +00:00
|
|
|
|
|
|
|
len = data_size + sizeof(struct memory_handle);
|
|
|
|
|
|
|
|
/* First, will the handle wrap? */
|
|
|
|
/* If the handle would wrap, move to the beginning of the buffer,
|
2009-02-22 10:12:34 +00:00
|
|
|
* or if the data must not but would wrap, move it to the beginning */
|
2011-02-14 09:18:58 +00:00
|
|
|
if (new_widx + sizeof(struct memory_handle) > buffer_len ||
|
|
|
|
(!can_wrap && new_widx + len > buffer_len)) {
|
2007-10-27 04:49:04 +00:00
|
|
|
new_widx = 0;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 11:02:37 +00:00
|
|
|
/* How far we shifted the new_widx to align things, must be < buffer_len */
|
|
|
|
shift = ringbuf_sub(new_widx, widx);
|
2007-10-28 15:54:10 +00:00
|
|
|
|
2007-10-27 04:49:04 +00:00
|
|
|
/* How much space are we short in the actual ring buffer? */
|
2011-02-13 11:02:37 +00:00
|
|
|
overlap = ringbuf_add_cross(widx, shift + len, buf_ridx);
|
2011-02-09 08:44:37 +00:00
|
|
|
if (overlap >= 0 && (alloc_all || (size_t)overlap >= data_size)) {
|
2007-10-27 04:49:04 +00:00
|
|
|
/* Not enough space for required allocations */
|
2007-10-25 21:27:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-10-27 04:49:04 +00:00
|
|
|
/* There is enough space for the required data, advance the buf_widx and
|
|
|
|
* initialize the struct */
|
|
|
|
buf_widx = new_widx;
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
struct memory_handle *new_handle =
|
|
|
|
(struct memory_handle *)(&buffer[buf_widx]);
|
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
/* Prevent buffering thread from looking at it */
|
|
|
|
new_handle->filerem = 0;
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Handle can be moved by default */
|
|
|
|
new_handle->pinned = 0;
|
|
|
|
|
|
|
|
/* Handle data can be waited for by default */
|
|
|
|
new_handle->signaled = 0;
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
/* only advance the buffer write index of the size of the struct */
|
2010-02-12 13:12:59 +00:00
|
|
|
buf_widx = ringbuf_add(buf_widx, sizeof(struct memory_handle));
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2007-10-27 04:49:04 +00:00
|
|
|
new_handle->id = cur_handle_id;
|
2007-11-03 22:06:56 +00:00
|
|
|
/* Wrap signed int is safe and 0 doesn't happen */
|
2007-11-04 19:01:02 +00:00
|
|
|
cur_handle_id = (cur_handle_id + 1) & BUF_HANDLE_MASK;
|
2007-10-27 04:49:04 +00:00
|
|
|
new_handle->next = NULL;
|
|
|
|
num_handles++;
|
|
|
|
|
|
|
|
if (!first_handle)
|
2007-10-25 21:27:45 +00:00
|
|
|
/* the new handle is the first one */
|
|
|
|
first_handle = new_handle;
|
|
|
|
|
2007-10-27 04:49:04 +00:00
|
|
|
if (cur_handle)
|
2007-10-25 21:27:45 +00:00
|
|
|
cur_handle->next = new_handle;
|
|
|
|
|
|
|
|
cur_handle = new_handle;
|
|
|
|
|
2007-10-27 04:49:04 +00:00
|
|
|
return new_handle;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete a given memory handle from the linked list
|
|
|
|
and return true for success. Nothing is actually erased from memory. */
|
2007-10-27 01:37:33 +00:00
|
|
|
static bool rm_handle(const struct memory_handle *h)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2007-10-27 01:37:33 +00:00
|
|
|
if (h == NULL)
|
2007-11-04 19:01:02 +00:00
|
|
|
return true;
|
2007-10-27 01:37:33 +00:00
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
if (h == first_handle) {
|
|
|
|
first_handle = h->next;
|
|
|
|
if (h == cur_handle) {
|
|
|
|
/* h was the first and last handle: the buffer is now empty */
|
|
|
|
cur_handle = NULL;
|
2007-10-27 01:37:33 +00:00
|
|
|
buf_ridx = buf_widx = 0;
|
2007-10-25 21:27:45 +00:00
|
|
|
} else {
|
|
|
|
/* update buf_ridx to point to the new first handle */
|
2010-02-20 15:13:53 +00:00
|
|
|
buf_ridx = (size_t)ringbuf_offset(first_handle);
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct memory_handle *m = first_handle;
|
2007-11-26 21:13:08 +00:00
|
|
|
/* Find the previous handle */
|
2007-10-25 21:27:45 +00:00
|
|
|
while (m && m->next != h) {
|
|
|
|
m = m->next;
|
|
|
|
}
|
2007-10-27 01:37:33 +00:00
|
|
|
if (m && m->next == h) {
|
2007-10-25 21:27:45 +00:00
|
|
|
m->next = h->next;
|
|
|
|
if (h == cur_handle) {
|
|
|
|
cur_handle = m;
|
|
|
|
buf_widx = cur_handle->widx;
|
|
|
|
}
|
|
|
|
} else {
|
2011-04-27 03:08:23 +00:00
|
|
|
/* If we don't find ourselves, this is a seriously incoherent
|
|
|
|
state with a corrupted list and severe action is needed! */
|
|
|
|
panicf("rm_handle fail: %d", h->id);
|
2007-10-25 21:27:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Invalidate the cache to prevent it from keeping the old location of h */
|
|
|
|
if (h == cached_handle)
|
|
|
|
cached_handle = NULL;
|
|
|
|
|
|
|
|
num_handles--;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a pointer to the memory handle of given ID.
|
|
|
|
NULL if the handle wasn't found */
|
2008-03-28 12:51:33 +00:00
|
|
|
static struct memory_handle *find_handle(int handle_id)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2012-05-21 06:18:46 +00:00
|
|
|
if (handle_id < 0 || !first_handle)
|
2007-10-25 21:27:45 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* simple caching because most of the time the requested handle
|
|
|
|
will either be the same as the last, or the one after the last */
|
2011-04-27 03:08:23 +00:00
|
|
|
if (cached_handle) {
|
2007-10-25 21:27:45 +00:00
|
|
|
if (cached_handle->id == handle_id) {
|
|
|
|
return cached_handle;
|
|
|
|
} else if (cached_handle->next &&
|
|
|
|
(cached_handle->next->id == handle_id)) {
|
|
|
|
cached_handle = cached_handle->next;
|
|
|
|
return cached_handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct memory_handle *m = first_handle;
|
|
|
|
while (m && m->id != handle_id) {
|
|
|
|
m = m->next;
|
|
|
|
}
|
|
|
|
/* This condition can only be reached with !m or m->id == handle_id */
|
2007-10-27 01:37:33 +00:00
|
|
|
if (m)
|
2007-10-25 21:27:45 +00:00
|
|
|
cached_handle = m;
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2007-10-28 20:18:59 +00:00
|
|
|
/* Move a memory handle and data_size of its data delta bytes along the buffer.
|
|
|
|
delta maximum bytes available to move the handle. If the move is performed
|
|
|
|
it is set to the actual distance moved.
|
|
|
|
data_size is the amount of data to move along with the struct.
|
2010-05-24 10:49:36 +00:00
|
|
|
returns true if the move is successful and false if the handle is NULL,
|
|
|
|
the move would be less than the size of a memory_handle after
|
|
|
|
correcting for wraps or if the handle is not found in the linked
|
|
|
|
list for adjustment. This function has no side effects if false
|
|
|
|
is returned. */
|
2007-11-02 14:06:48 +00:00
|
|
|
static bool move_handle(struct memory_handle **h, size_t *delta,
|
2008-03-28 12:51:33 +00:00
|
|
|
size_t data_size, bool can_wrap)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2007-10-27 04:49:04 +00:00
|
|
|
struct memory_handle *dest;
|
2007-10-29 16:48:16 +00:00
|
|
|
const struct memory_handle *src;
|
2011-02-12 12:18:09 +00:00
|
|
|
size_t final_delta = *delta, size_to_move;
|
2010-02-20 15:13:53 +00:00
|
|
|
uintptr_t oldpos, newpos;
|
|
|
|
intptr_t overlap, overlap_old;
|
2007-10-27 04:49:04 +00:00
|
|
|
|
2007-10-29 16:48:16 +00:00
|
|
|
if (h == NULL || (src = *h) == NULL)
|
|
|
|
return false;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2007-10-27 04:49:04 +00:00
|
|
|
size_to_move = sizeof(struct memory_handle) + data_size;
|
|
|
|
|
|
|
|
/* Align to four bytes, down */
|
2007-10-29 16:48:16 +00:00
|
|
|
final_delta &= ~3;
|
|
|
|
if (final_delta < sizeof(struct memory_handle)) {
|
2007-10-28 20:18:59 +00:00
|
|
|
/* It's not legal to move less than the size of the struct */
|
2007-10-29 16:48:16 +00:00
|
|
|
return false;
|
2007-10-28 20:18:59 +00:00
|
|
|
}
|
|
|
|
|
2010-02-20 15:13:53 +00:00
|
|
|
oldpos = ringbuf_offset(src);
|
2010-02-12 13:12:59 +00:00
|
|
|
newpos = ringbuf_add(oldpos, final_delta);
|
2011-02-12 12:18:09 +00:00
|
|
|
overlap = ringbuf_add_cross(newpos, size_to_move, buffer_len);
|
|
|
|
overlap_old = ringbuf_add_cross(oldpos, size_to_move, buffer_len);
|
2007-10-27 04:49:04 +00:00
|
|
|
|
|
|
|
if (overlap > 0) {
|
2007-10-28 20:18:59 +00:00
|
|
|
/* Some part of the struct + data would wrap, maybe ok */
|
2011-02-12 12:18:09 +00:00
|
|
|
ssize_t correction = 0;
|
2007-10-27 04:49:04 +00:00
|
|
|
/* If the overlap lands inside the memory_handle */
|
2009-11-21 17:00:38 +00:00
|
|
|
if (!can_wrap) {
|
2007-10-28 20:18:59 +00:00
|
|
|
/* Otherwise the overlap falls in the data area and must all be
|
|
|
|
* backed out. This may become conditional if ever we move
|
|
|
|
* data that is allowed to wrap (ie audio) */
|
2007-10-27 04:49:04 +00:00
|
|
|
correction = overlap;
|
2010-02-20 15:13:53 +00:00
|
|
|
} else if ((uintptr_t)overlap > data_size) {
|
2009-11-21 17:00:38 +00:00
|
|
|
/* Correct the position and real delta to prevent the struct from
|
2011-02-12 12:18:09 +00:00
|
|
|
* wrapping, this guarantees an aligned delta if the struct size is
|
|
|
|
* aligned and the buffer is aligned */
|
2009-11-21 17:00:38 +00:00
|
|
|
correction = overlap - data_size;
|
2007-10-27 04:49:04 +00:00
|
|
|
}
|
2007-11-02 14:06:48 +00:00
|
|
|
if (correction) {
|
2009-11-21 17:00:38 +00:00
|
|
|
/* Align correction to four bytes up */
|
2009-11-22 13:51:25 +00:00
|
|
|
correction = (correction + 3) & ~3;
|
2007-11-02 14:06:48 +00:00
|
|
|
if (final_delta < correction + sizeof(struct memory_handle)) {
|
|
|
|
/* Delta cannot end up less than the size of the struct */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
newpos -= correction;
|
|
|
|
overlap -= correction;/* Used below to know how to split the data */
|
|
|
|
final_delta -= correction;
|
|
|
|
}
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
2007-10-28 15:54:10 +00:00
|
|
|
|
2007-10-27 04:49:04 +00:00
|
|
|
dest = (struct memory_handle *)(&buffer[newpos]);
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2007-10-29 16:48:16 +00:00
|
|
|
if (src == first_handle) {
|
2007-10-25 21:27:45 +00:00
|
|
|
first_handle = dest;
|
|
|
|
buf_ridx = newpos;
|
|
|
|
} else {
|
|
|
|
struct memory_handle *m = first_handle;
|
2007-10-29 16:48:16 +00:00
|
|
|
while (m && m->next != src) {
|
2007-10-25 21:27:45 +00:00
|
|
|
m = m->next;
|
|
|
|
}
|
2007-10-29 16:48:16 +00:00
|
|
|
if (m && m->next == src) {
|
2007-10-25 21:27:45 +00:00
|
|
|
m->next = dest;
|
|
|
|
} else {
|
2007-10-29 16:48:16 +00:00
|
|
|
return false;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-27 04:49:04 +00:00
|
|
|
/* Update the cache to prevent it from keeping the old location of h */
|
2007-10-29 16:48:16 +00:00
|
|
|
if (src == cached_handle)
|
2007-10-27 04:49:04 +00:00
|
|
|
cached_handle = dest;
|
|
|
|
|
|
|
|
/* the cur_handle pointer might need updating */
|
2007-10-29 16:48:16 +00:00
|
|
|
if (src == cur_handle)
|
2007-10-27 04:49:04 +00:00
|
|
|
cur_handle = dest;
|
|
|
|
|
2011-02-12 12:18:09 +00:00
|
|
|
/* x = handle(s) following this one...
|
|
|
|
* ...if last handle, unmoveable if metadata, only shrinkable if audio.
|
|
|
|
* In other words, no legal move can be made that would have the src head
|
|
|
|
* and dest tail of the data overlap itself. These facts reduce the
|
|
|
|
* problem to four essential permutations.
|
|
|
|
*
|
|
|
|
* movement: always "clockwise" >>>>
|
|
|
|
*
|
|
|
|
* (src nowrap, dest nowrap)
|
|
|
|
* |0123 x |
|
|
|
|
* | 0123x | etc...
|
|
|
|
* move: "0123"
|
|
|
|
*
|
|
|
|
* (src nowrap, dest wrap)
|
|
|
|
* | x0123 |
|
|
|
|
* |23x 01|
|
|
|
|
* move: "23", "01"
|
|
|
|
*
|
|
|
|
* (src wrap, dest nowrap)
|
|
|
|
* |23 x01|
|
|
|
|
* | 0123x |
|
|
|
|
* move: "23", "01"
|
|
|
|
*
|
|
|
|
* (src wrap, dest wrap)
|
|
|
|
* |23 x 01|
|
|
|
|
* |123x 0|
|
|
|
|
* move: "23", "1", "0"
|
|
|
|
*/
|
|
|
|
if (overlap_old > 0) {
|
|
|
|
/* Move over already wrapped data by the final delta */
|
|
|
|
memmove(&buffer[final_delta], buffer, overlap_old);
|
|
|
|
if (overlap <= 0)
|
|
|
|
size_to_move -= overlap_old;
|
|
|
|
}
|
2009-11-21 17:00:38 +00:00
|
|
|
|
2011-02-12 12:18:09 +00:00
|
|
|
if (overlap > 0) {
|
|
|
|
/* Move data that now wraps to the beginning */
|
|
|
|
size_to_move -= overlap;
|
|
|
|
memmove(buffer, SKIPBYTES(src, size_to_move),
|
|
|
|
overlap_old > 0 ? final_delta : (size_t)overlap);
|
2009-11-22 13:51:25 +00:00
|
|
|
}
|
2009-11-21 17:00:38 +00:00
|
|
|
|
2011-02-12 12:18:09 +00:00
|
|
|
/* Move leading fragment containing handle struct */
|
|
|
|
memmove(dest, src, size_to_move);
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2007-10-29 16:48:16 +00:00
|
|
|
/* Update the caller with the new location of h and the distance moved */
|
|
|
|
*h = dest;
|
|
|
|
*delta = final_delta;
|
2010-05-24 10:49:36 +00:00
|
|
|
return true;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
BUFFER SPACE MANAGEMENT
|
|
|
|
=======================
|
|
|
|
|
2007-11-05 21:11:54 +00:00
|
|
|
update_data_counters: Updates the values in data_counters
|
|
|
|
buffer_is_low : Returns true if the amount of useful data in the buffer is low
|
2007-10-25 21:27:45 +00:00
|
|
|
buffer_handle : Buffer data for a handle
|
|
|
|
rebuffer_handle : Seek to a nonbuffered part of a handle by rebuffering the data
|
|
|
|
shrink_handle : Free buffer space by moving a handle
|
|
|
|
fill_buffer : Call buffer_handle for all handles that have data to buffer
|
|
|
|
|
|
|
|
These functions are used by the buffering thread to manage buffer space.
|
|
|
|
*/
|
2011-03-02 04:41:29 +00:00
|
|
|
static size_t handle_size_available(const struct memory_handle *h)
|
|
|
|
{
|
|
|
|
/* Obtain proper distances from data start */
|
|
|
|
size_t rd = ringbuf_sub(h->ridx, h->data);
|
|
|
|
size_t wr = ringbuf_sub(h->widx, h->data);
|
|
|
|
|
|
|
|
if (LIKELY(wr > rd))
|
|
|
|
return wr - rd;
|
|
|
|
|
|
|
|
return 0; /* ridx is ahead of or equal to widx at this time */
|
|
|
|
}
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
static void update_data_counters(struct data_counters *dc)
|
2007-11-05 21:11:54 +00:00
|
|
|
{
|
|
|
|
size_t buffered = 0;
|
|
|
|
size_t wasted = 0;
|
|
|
|
size_t remaining = 0;
|
|
|
|
size_t useful = 0;
|
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
struct memory_handle *m;
|
|
|
|
bool is_useful;
|
|
|
|
|
|
|
|
if (dc == NULL)
|
|
|
|
dc = &data_counters;
|
|
|
|
|
2008-05-13 20:51:06 +00:00
|
|
|
mutex_lock(&llist_mutex);
|
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
m = find_handle(base_handle_id);
|
|
|
|
is_useful = m == NULL;
|
|
|
|
|
2007-11-05 21:11:54 +00:00
|
|
|
m = first_handle;
|
|
|
|
while (m) {
|
|
|
|
buffered += m->available;
|
2011-03-02 04:41:29 +00:00
|
|
|
/* wasted could come out larger than the buffer size if ridx's are
|
|
|
|
overlapping data ahead of their handles' buffered data */
|
2010-02-12 13:12:59 +00:00
|
|
|
wasted += ringbuf_sub(m->ridx, m->data);
|
2007-11-05 21:11:54 +00:00
|
|
|
remaining += m->filerem;
|
|
|
|
|
|
|
|
if (m->id == base_handle_id)
|
|
|
|
is_useful = true;
|
|
|
|
|
|
|
|
if (is_useful)
|
2011-03-02 04:41:29 +00:00
|
|
|
useful += handle_size_available(m);
|
2007-11-05 21:11:54 +00:00
|
|
|
|
|
|
|
m = m->next;
|
|
|
|
}
|
|
|
|
|
2008-05-13 20:51:06 +00:00
|
|
|
mutex_unlock(&llist_mutex);
|
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
dc->buffered = buffered;
|
|
|
|
dc->wasted = wasted;
|
|
|
|
dc->remaining = remaining;
|
|
|
|
dc->useful = useful;
|
2007-11-05 21:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool buffer_is_low(void)
|
|
|
|
{
|
2011-02-14 08:36:29 +00:00
|
|
|
update_data_counters(NULL);
|
2011-04-27 03:08:23 +00:00
|
|
|
return data_counters.useful < BUF_WATERMARK / 2;
|
2007-11-05 21:11:54 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
/* Q_BUFFER_HANDLE event and buffer data for the given handle.
|
2007-11-03 17:55:45 +00:00
|
|
|
Return whether or not the buffering should continue explicitly. */
|
2011-02-14 08:36:29 +00:00
|
|
|
static bool buffer_handle(int handle_id, size_t to_buffer)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2011-04-27 03:08:23 +00:00
|
|
|
logf("buffer_handle(%d, %lu)", handle_id, (unsigned long)to_buffer);
|
2007-10-25 21:27:45 +00:00
|
|
|
struct memory_handle *h = find_handle(handle_id);
|
2010-02-18 15:38:30 +00:00
|
|
|
bool stop = false;
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
if (!h)
|
2007-11-03 21:57:27 +00:00
|
|
|
return true;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
logf(" type: %d", (int)h->type);
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
if (h->filerem == 0) {
|
|
|
|
/* nothing left to buffer */
|
2007-11-03 21:57:27 +00:00
|
|
|
return true;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
if (h->fd < 0) { /* file closed, reopen */
|
2007-10-25 21:27:45 +00:00
|
|
|
if (*h->path)
|
|
|
|
h->fd = open(h->path, O_RDONLY);
|
|
|
|
|
|
|
|
if (h->fd < 0)
|
2007-11-04 05:57:48 +00:00
|
|
|
{
|
|
|
|
/* could not open the file, truncate it where it is */
|
|
|
|
h->filesize -= h->filerem;
|
|
|
|
h->filerem = 0;
|
2007-11-03 21:57:27 +00:00
|
|
|
return true;
|
2007-11-04 05:57:48 +00:00
|
|
|
}
|
2007-10-25 21:27:45 +00:00
|
|
|
|
|
|
|
if (h->offset)
|
|
|
|
lseek(h->fd, h->offset, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
trigger_cpu_boost();
|
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
if (h->type == TYPE_ID3) {
|
|
|
|
if (!get_metadata((struct mp3entry *)(buffer + h->data),
|
|
|
|
h->fd, h->path)) {
|
2008-04-16 16:18:05 +00:00
|
|
|
/* metadata parsing failed: clear the buffer. */
|
2011-04-27 03:08:23 +00:00
|
|
|
wipe_mp3entry((struct mp3entry *)(buffer + h->data));
|
2008-04-16 16:18:05 +00:00
|
|
|
}
|
2008-04-14 16:17:47 +00:00
|
|
|
close(h->fd);
|
|
|
|
h->fd = -1;
|
|
|
|
h->filerem = 0;
|
|
|
|
h->available = sizeof(struct mp3entry);
|
2011-04-27 03:08:23 +00:00
|
|
|
h->widx = ringbuf_add(h->widx, sizeof(struct mp3entry));
|
2011-02-14 08:36:29 +00:00
|
|
|
send_event(BUFFER_EVENT_FINISHED, &handle_id);
|
2008-04-14 16:17:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-18 15:38:30 +00:00
|
|
|
while (h->filerem > 0 && !stop)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
|
|
|
/* max amount to copy */
|
2011-02-10 05:56:21 +00:00
|
|
|
ssize_t copy_n = MIN( MIN(h->filerem, BUFFERING_DEFAULT_FILECHUNK),
|
2007-10-25 21:27:45 +00:00
|
|
|
buffer_len - h->widx);
|
2011-02-10 05:56:21 +00:00
|
|
|
uintptr_t offset = h->next ? ringbuf_offset(h->next) : buf_ridx;
|
2011-02-14 02:14:26 +00:00
|
|
|
ssize_t overlap = ringbuf_add_cross(h->widx, copy_n, offset) + 1;
|
2010-02-18 15:38:30 +00:00
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
if (overlap > 0) {
|
2011-02-10 05:56:21 +00:00
|
|
|
/* read only up to available space and stop if it would overwrite
|
2011-02-14 02:14:26 +00:00
|
|
|
or be on top of the reading position or the next handle */
|
2010-02-18 15:38:30 +00:00
|
|
|
stop = true;
|
|
|
|
copy_n -= overlap;
|
|
|
|
}
|
2007-11-01 05:12:55 +00:00
|
|
|
|
2011-02-10 05:56:21 +00:00
|
|
|
if (copy_n <= 0)
|
|
|
|
return false; /* no space for read */
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
/* rc is the actual amount read */
|
|
|
|
int rc = read(h->fd, &buffer[h->widx], copy_n);
|
|
|
|
|
2011-03-21 15:57:07 +00:00
|
|
|
if (rc <= 0) {
|
2007-11-03 17:55:45 +00:00
|
|
|
/* Some kind of filesystem error, maybe recoverable if not codec */
|
2007-10-25 21:27:45 +00:00
|
|
|
if (h->type == TYPE_CODEC) {
|
|
|
|
logf("Partial codec");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
logf("File ended %ld bytes early\n", (long)h->filerem);
|
2007-10-25 21:27:45 +00:00
|
|
|
h->filesize -= h->filerem;
|
|
|
|
h->filerem = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance buffer */
|
2010-02-12 13:12:59 +00:00
|
|
|
h->widx = ringbuf_add(h->widx, rc);
|
2007-10-25 21:27:45 +00:00
|
|
|
if (h == cur_handle)
|
|
|
|
buf_widx = h->widx;
|
|
|
|
h->available += rc;
|
|
|
|
h->filerem -= rc;
|
|
|
|
|
2007-11-05 21:11:54 +00:00
|
|
|
/* If this is a large file, see if we need to break or give the codec
|
2007-11-04 02:40:24 +00:00
|
|
|
* more time */
|
2007-11-20 22:45:46 +00:00
|
|
|
if (h->type == TYPE_PACKET_AUDIO &&
|
2011-02-14 09:18:58 +00:00
|
|
|
pcmbuf_is_lowdata() && !buffer_is_low()) {
|
2007-11-20 22:45:46 +00:00
|
|
|
sleep(1);
|
2011-02-14 09:18:58 +00:00
|
|
|
} else {
|
2007-11-20 22:45:46 +00:00
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
if (to_buffer == 0) {
|
2011-02-14 08:36:29 +00:00
|
|
|
/* Normal buffering - check queue */
|
|
|
|
if(!queue_empty(&buffering_queue))
|
|
|
|
break;
|
2011-02-14 09:18:58 +00:00
|
|
|
} else {
|
2011-02-14 08:36:29 +00:00
|
|
|
if (to_buffer <= (size_t)rc)
|
|
|
|
break; /* Done */
|
|
|
|
to_buffer -= rc;
|
|
|
|
}
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (h->filerem == 0) {
|
|
|
|
/* finished buffering the file */
|
|
|
|
close(h->fd);
|
|
|
|
h->fd = -1;
|
2011-02-14 08:36:29 +00:00
|
|
|
send_event(BUFFER_EVENT_FINISHED, &handle_id);
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
2010-02-18 15:38:30 +00:00
|
|
|
return !stop;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
/* Close the specified handle id and free its allocation. */
|
2008-03-28 12:51:33 +00:00
|
|
|
static bool close_handle(int handle_id)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2011-02-14 08:36:29 +00:00
|
|
|
bool retval = true;
|
|
|
|
struct memory_handle *h;
|
|
|
|
|
|
|
|
mutex_lock(&llist_mutex);
|
|
|
|
h = find_handle(handle_id);
|
2007-11-04 19:01:02 +00:00
|
|
|
|
|
|
|
/* If the handle is not found, it is closed */
|
2011-02-14 08:36:29 +00:00
|
|
|
if (h) {
|
|
|
|
if (h->fd >= 0) {
|
|
|
|
close(h->fd);
|
|
|
|
h->fd = -1;
|
|
|
|
}
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
/* rm_handle returns true unless the handle somehow persists after
|
|
|
|
exit */
|
|
|
|
retval = rm_handle(h);
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
mutex_unlock(&llist_mutex);
|
|
|
|
return retval;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free buffer space by moving the handle struct right before the useful
|
|
|
|
part of its data buffer or by moving all the data. */
|
2007-11-03 02:54:34 +00:00
|
|
|
static void shrink_handle(struct memory_handle *h)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
|
|
|
if (!h)
|
|
|
|
return;
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (h->type == TYPE_PACKET_AUDIO) {
|
|
|
|
/* only move the handle struct */
|
|
|
|
/* data is pinned by default - if we start moving packet audio,
|
|
|
|
the semantics will determine whether or not data is movable
|
|
|
|
but the handle will remain movable in either case */
|
|
|
|
size_t delta = ringbuf_sub(h->ridx, h->data);
|
|
|
|
|
|
|
|
/* The value of delta might change for alignment reasons */
|
|
|
|
if (!move_handle(&h, &delta, 0, true))
|
|
|
|
return;
|
|
|
|
|
|
|
|
h->data = ringbuf_add(h->data, delta);
|
|
|
|
h->available -= delta;
|
|
|
|
h->offset += delta;
|
|
|
|
} else {
|
2007-10-25 21:27:45 +00:00
|
|
|
/* metadata handle: we can move all of it */
|
2011-04-27 03:08:23 +00:00
|
|
|
if (h->pinned || !h->next || h->filerem != 0)
|
|
|
|
return; /* Pinned, last handle or not finished loading */
|
2011-02-10 05:56:21 +00:00
|
|
|
|
2010-02-20 15:13:53 +00:00
|
|
|
uintptr_t handle_distance =
|
|
|
|
ringbuf_sub(ringbuf_offset(h->next), h->data);
|
2011-04-27 03:08:23 +00:00
|
|
|
size_t delta = handle_distance - h->available;
|
2007-10-27 04:49:04 +00:00
|
|
|
|
|
|
|
/* The value of delta might change for alignment reasons */
|
2007-11-02 14:06:48 +00:00
|
|
|
if (!move_handle(&h, &delta, h->available, h->type==TYPE_CODEC))
|
2007-10-29 16:48:16 +00:00
|
|
|
return;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
|
|
|
size_t olddata = h->data;
|
2010-02-12 13:12:59 +00:00
|
|
|
h->data = ringbuf_add(h->data, delta);
|
|
|
|
h->ridx = ringbuf_add(h->ridx, delta);
|
|
|
|
h->widx = ringbuf_add(h->widx, delta);
|
2007-10-25 21:27:45 +00:00
|
|
|
|
|
|
|
if (h->type == TYPE_ID3 && h->filesize == sizeof(struct mp3entry)) {
|
2007-11-11 12:29:37 +00:00
|
|
|
/* when moving an mp3entry we need to readjust its pointers. */
|
2007-10-25 21:27:45 +00:00
|
|
|
adjust_mp3entry((struct mp3entry *)&buffer[h->data],
|
|
|
|
(void *)&buffer[h->data],
|
2008-03-26 08:57:25 +00:00
|
|
|
(const void *)&buffer[olddata]);
|
2007-11-11 12:29:37 +00:00
|
|
|
} else if (h->type == TYPE_BITMAP) {
|
|
|
|
/* adjust the bitmap's pointer */
|
|
|
|
struct bitmap *bmp = (struct bitmap *)&buffer[h->data];
|
|
|
|
bmp->data = &buffer[h->data + sizeof(struct bitmap)];
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill the buffer by buffering as much data as possible for handles that still
|
2007-11-03 06:21:32 +00:00
|
|
|
have data left to buffer
|
|
|
|
Return whether or not to continue filling after this */
|
|
|
|
static bool fill_buffer(void)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
|
|
|
logf("fill_buffer()");
|
2011-02-14 08:36:29 +00:00
|
|
|
struct memory_handle *m = first_handle;
|
|
|
|
|
|
|
|
shrink_handle(m);
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
while (queue_empty(&buffering_queue) && m) {
|
|
|
|
if (m->filerem > 0) {
|
2011-02-14 08:36:29 +00:00
|
|
|
if (!buffer_handle(m->id, 0)) {
|
2007-11-03 17:55:45 +00:00
|
|
|
m = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
m = m->next;
|
|
|
|
}
|
|
|
|
|
2007-11-03 06:21:32 +00:00
|
|
|
if (m) {
|
|
|
|
return true;
|
2011-02-14 09:18:58 +00:00
|
|
|
} else {
|
2007-10-25 21:27:45 +00:00
|
|
|
/* only spin the disk down if the filling wasn't interrupted by an
|
|
|
|
event arriving in the queue. */
|
2008-11-01 16:14:28 +00:00
|
|
|
storage_sleep();
|
2007-11-03 06:21:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
2007-11-11 13:15:36 +00:00
|
|
|
#ifdef HAVE_ALBUMART
|
2007-11-11 12:29:37 +00:00
|
|
|
/* Given a file descriptor to a bitmap file, write the bitmap data to the
|
|
|
|
buffer, with a struct bitmap and the actual data immediately following.
|
|
|
|
Return value is the total size (struct + data). */
|
2011-02-14 09:18:58 +00:00
|
|
|
static int load_image(int fd, const char *path,
|
|
|
|
struct bufopen_bitmap_data *data)
|
2007-11-11 12:29:37 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct bitmap *bmp = (struct bitmap *)&buffer[buf_widx];
|
2011-02-09 20:13:13 +00:00
|
|
|
struct dim *dim = data->dim;
|
|
|
|
struct mp3_albumart *aa = data->embedded_albumart;
|
2009-10-16 19:14:41 +00:00
|
|
|
|
|
|
|
/* get the desired image size */
|
|
|
|
bmp->width = dim->width, bmp->height = dim->height;
|
2007-11-11 12:29:37 +00:00
|
|
|
/* FIXME: alignment may be needed for the data buffer. */
|
|
|
|
bmp->data = &buffer[buf_widx + sizeof(struct bitmap)];
|
2009-05-26 20:26:05 +00:00
|
|
|
#ifndef HAVE_JPEG
|
|
|
|
(void) path;
|
|
|
|
#endif
|
2007-11-11 13:15:36 +00:00
|
|
|
#if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
|
2007-11-11 12:29:37 +00:00
|
|
|
bmp->maskdata = NULL;
|
2007-11-11 13:15:36 +00:00
|
|
|
#endif
|
|
|
|
|
2008-12-09 23:07:59 +00:00
|
|
|
int free = (int)MIN(buffer_len - BUF_USED, buffer_len - buf_widx)
|
|
|
|
- sizeof(struct bitmap);
|
|
|
|
|
2009-05-04 15:46:41 +00:00
|
|
|
#ifdef HAVE_JPEG
|
2011-02-14 09:18:58 +00:00
|
|
|
if (aa != NULL) {
|
2011-02-09 20:13:13 +00:00
|
|
|
lseek(fd, aa->pos, SEEK_SET);
|
|
|
|
rc = clip_jpeg_fd(fd, aa->size, bmp, free, FORMAT_NATIVE|FORMAT_DITHER|
|
|
|
|
FORMAT_RESIZE|FORMAT_KEEP_ASPECT, NULL);
|
|
|
|
}
|
|
|
|
else if (strcmp(path + strlen(path) - 4, ".bmp"))
|
2009-05-01 23:31:43 +00:00
|
|
|
rc = read_jpeg_fd(fd, bmp, free, FORMAT_NATIVE|FORMAT_DITHER|
|
|
|
|
FORMAT_RESIZE|FORMAT_KEEP_ASPECT, NULL);
|
|
|
|
else
|
2009-05-04 15:46:41 +00:00
|
|
|
#endif
|
2009-05-01 23:31:43 +00:00
|
|
|
rc = read_bmp_fd(fd, bmp, free, FORMAT_NATIVE|FORMAT_DITHER|
|
|
|
|
FORMAT_RESIZE|FORMAT_KEEP_ASPECT, NULL);
|
2007-11-11 12:29:37 +00:00
|
|
|
return rc + (rc > 0 ? sizeof(struct bitmap) : 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
MAIN BUFFERING API CALLS
|
|
|
|
========================
|
|
|
|
|
|
|
|
bufopen : Request the opening of a new handle for a file
|
|
|
|
bufalloc : Open a new handle for data other than a file.
|
|
|
|
bufclose : Close an open handle
|
|
|
|
bufseek : Set the read pointer in a handle
|
|
|
|
bufadvance : Move the read pointer in a handle
|
|
|
|
bufread : Copy data from a handle into a given buffer
|
|
|
|
bufgetdata : Give a pointer to the handle's data
|
|
|
|
|
|
|
|
These functions are exported, to allow interaction with the buffer.
|
|
|
|
They take care of the content of the structs, and rely on the linked list
|
|
|
|
management functions for all the actual handle management work.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Reserve space in the buffer for a file.
|
|
|
|
filename: name of the file to open
|
|
|
|
offset: offset at which to start buffering the file, useful when the first
|
2011-02-14 08:36:29 +00:00
|
|
|
offset bytes of the file aren't needed.
|
2009-10-16 19:14:41 +00:00
|
|
|
type: one of the data types supported (audio, image, cuesheet, others
|
|
|
|
user_data: user data passed possibly passed in subcalls specific to a
|
|
|
|
data_type (only used for image (albumart) buffering so far )
|
2007-10-25 21:27:45 +00:00
|
|
|
return value: <0 if the file cannot be opened, or one file already
|
|
|
|
queued to be opened, otherwise the handle for the file in the buffer
|
|
|
|
*/
|
2009-10-16 19:14:41 +00:00
|
|
|
int bufopen(const char *file, size_t offset, enum data_type type,
|
|
|
|
void *user_data)
|
2008-03-29 14:09:14 +00:00
|
|
|
{
|
2009-10-16 19:14:41 +00:00
|
|
|
#ifndef HAVE_ALBUMART
|
|
|
|
/* currently only used for aa loading */
|
|
|
|
(void)user_data;
|
|
|
|
#endif
|
2011-02-14 08:36:29 +00:00
|
|
|
int handle_id = ERR_BUFFER_FULL;
|
|
|
|
|
|
|
|
/* No buffer refs until after the mutex_lock call! */
|
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
if (type == TYPE_ID3) {
|
2008-04-14 16:17:47 +00:00
|
|
|
/* ID3 case: allocate space, init the handle and return. */
|
2011-02-14 08:36:29 +00:00
|
|
|
mutex_lock(&llist_mutex);
|
2008-04-14 16:17:47 +00:00
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
struct memory_handle *h =
|
|
|
|
add_handle(sizeof(struct mp3entry), false, true);
|
|
|
|
|
|
|
|
if (h) {
|
2011-02-14 08:36:29 +00:00
|
|
|
handle_id = h->id;
|
|
|
|
h->fd = -1;
|
|
|
|
h->filesize = sizeof(struct mp3entry);
|
|
|
|
h->offset = 0;
|
|
|
|
h->data = buf_widx;
|
|
|
|
h->ridx = buf_widx;
|
|
|
|
h->widx = buf_widx;
|
|
|
|
h->available = 0;
|
|
|
|
h->type = type;
|
|
|
|
strlcpy(h->path, file, MAX_PATH);
|
|
|
|
|
2011-03-13 11:56:51 +00:00
|
|
|
buf_widx = ringbuf_add(buf_widx, sizeof(struct mp3entry));
|
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
h->filerem = sizeof(struct mp3entry);
|
|
|
|
|
|
|
|
/* Inform the buffering thread that we added a handle */
|
|
|
|
LOGFQUEUE("buffering > Q_HANDLE_ADDED %d", handle_id);
|
|
|
|
queue_post(&buffering_queue, Q_HANDLE_ADDED, handle_id);
|
|
|
|
}
|
2008-12-02 21:07:12 +00:00
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
mutex_unlock(&llist_mutex);
|
|
|
|
return handle_id;
|
2008-04-14 16:17:47 +00:00
|
|
|
}
|
2011-04-27 03:08:23 +00:00
|
|
|
else if (type == TYPE_UNKNOWN)
|
|
|
|
return ERR_UNSUPPORTED_TYPE;
|
2011-02-09 20:27:23 +00:00
|
|
|
#ifdef APPLICATION
|
|
|
|
/* loading code from memory is not supported in application builds */
|
|
|
|
else if (type == TYPE_CODEC)
|
|
|
|
return ERR_UNSUPPORTED_TYPE;
|
|
|
|
#endif
|
2008-04-14 16:17:47 +00:00
|
|
|
/* Other cases: there is a little more work. */
|
2007-10-25 21:27:45 +00:00
|
|
|
int fd = open(file, O_RDONLY);
|
|
|
|
if (fd < 0)
|
2007-10-29 14:15:59 +00:00
|
|
|
return ERR_FILE_ERROR;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-02-09 20:13:13 +00:00
|
|
|
size_t size = 0;
|
|
|
|
#ifdef HAVE_ALBUMART
|
2011-02-14 09:18:58 +00:00
|
|
|
if (type == TYPE_BITMAP) {
|
|
|
|
/* if albumart is embedded, the complete file is not buffered,
|
2011-02-09 20:13:13 +00:00
|
|
|
* but only the jpeg part; filesize() would be wrong */
|
|
|
|
struct bufopen_bitmap_data *aa = (struct bufopen_bitmap_data*)user_data;
|
|
|
|
if (aa->embedded_albumart)
|
|
|
|
size = aa->embedded_albumart->size;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (size == 0)
|
|
|
|
size = filesize(fd);
|
2007-11-02 14:06:48 +00:00
|
|
|
bool can_wrap = type==TYPE_PACKET_AUDIO || type==TYPE_CODEC;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2008-04-14 16:17:47 +00:00
|
|
|
size_t adjusted_offset = offset;
|
2008-03-28 11:24:24 +00:00
|
|
|
if (adjusted_offset > size)
|
|
|
|
adjusted_offset = 0;
|
2008-01-08 23:48:51 +00:00
|
|
|
|
2010-02-01 17:16:39 +00:00
|
|
|
/* Reserve extra space because alignment can move data forward */
|
2010-03-25 23:01:56 +00:00
|
|
|
size_t padded_size = STORAGE_PAD(size-adjusted_offset);
|
2011-02-14 08:36:29 +00:00
|
|
|
|
|
|
|
mutex_lock(&llist_mutex);
|
|
|
|
|
2010-03-25 23:01:56 +00:00
|
|
|
struct memory_handle *h = add_handle(padded_size, can_wrap, false);
|
2011-02-14 09:18:58 +00:00
|
|
|
if (!h) {
|
2010-02-18 15:38:30 +00:00
|
|
|
DEBUGF("%s(): failed to add handle\n", __func__);
|
2011-02-14 08:36:29 +00:00
|
|
|
mutex_unlock(&llist_mutex);
|
2007-10-25 21:27:45 +00:00
|
|
|
close(fd);
|
2007-10-29 14:15:59 +00:00
|
|
|
return ERR_BUFFER_FULL;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
handle_id = h->id;
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(h->path, file, MAX_PATH);
|
2008-03-28 11:24:24 +00:00
|
|
|
h->offset = adjusted_offset;
|
2010-02-01 17:16:39 +00:00
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
#ifdef STORAGE_WANTS_ALIGN
|
2010-02-01 17:16:39 +00:00
|
|
|
/* Don't bother to storage align bitmaps because they are not
|
|
|
|
* loaded directly into the buffer.
|
|
|
|
*/
|
2011-02-14 09:18:58 +00:00
|
|
|
if (type != TYPE_BITMAP) {
|
2010-02-01 17:16:39 +00:00
|
|
|
/* Align to desired storage alignment */
|
2011-02-14 08:36:29 +00:00
|
|
|
size_t alignment_pad = STORAGE_OVERLAP(adjusted_offset -
|
|
|
|
(size_t)(&buffer[buf_widx]));
|
2010-02-12 13:12:59 +00:00
|
|
|
buf_widx = ringbuf_add(buf_widx, alignment_pad);
|
2010-02-01 17:16:39 +00:00
|
|
|
}
|
2011-02-14 08:36:29 +00:00
|
|
|
#endif /* STORAGE_WANTS_ALIGN */
|
2010-02-01 17:16:39 +00:00
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
h->fd = -1;
|
|
|
|
h->data = buf_widx;
|
2007-10-25 21:27:45 +00:00
|
|
|
h->ridx = buf_widx;
|
2009-05-15 00:14:38 +00:00
|
|
|
h->widx = buf_widx;
|
|
|
|
h->available = 0;
|
2007-10-25 21:27:45 +00:00
|
|
|
h->type = type;
|
|
|
|
|
2007-11-11 13:28:24 +00:00
|
|
|
#ifdef HAVE_ALBUMART
|
2011-02-14 09:18:58 +00:00
|
|
|
if (type == TYPE_BITMAP) {
|
2007-11-11 12:29:37 +00:00
|
|
|
/* Bitmap file: we load the data instead of the file */
|
2007-11-12 15:16:41 +00:00
|
|
|
int rc;
|
2011-02-09 20:13:13 +00:00
|
|
|
rc = load_image(fd, file, (struct bufopen_bitmap_data*)user_data);
|
2011-02-14 09:18:58 +00:00
|
|
|
if (rc <= 0) {
|
2007-11-12 15:16:41 +00:00
|
|
|
rm_handle(h);
|
2011-02-14 08:36:29 +00:00
|
|
|
handle_id = ERR_FILE_ERROR;
|
2011-02-14 09:18:58 +00:00
|
|
|
} else {
|
2011-02-14 08:36:29 +00:00
|
|
|
h->filesize = rc;
|
|
|
|
h->available = rc;
|
2011-03-13 11:56:51 +00:00
|
|
|
buf_widx = ringbuf_add(buf_widx, rc);
|
|
|
|
h->widx = buf_widx;
|
2007-11-12 15:16:41 +00:00
|
|
|
}
|
2007-11-11 12:29:37 +00:00
|
|
|
}
|
2007-11-12 15:16:41 +00:00
|
|
|
else
|
2007-11-11 12:29:37 +00:00
|
|
|
#endif
|
2007-11-12 15:16:41 +00:00
|
|
|
{
|
2011-02-14 08:36:29 +00:00
|
|
|
if (type == TYPE_CUESHEET)
|
|
|
|
h->fd = fd;
|
|
|
|
|
2007-11-12 15:16:41 +00:00
|
|
|
h->filesize = size;
|
|
|
|
h->available = 0;
|
|
|
|
h->widx = buf_widx;
|
2011-02-14 08:36:29 +00:00
|
|
|
h->filerem = size - adjusted_offset;
|
2007-11-12 15:16:41 +00:00
|
|
|
}
|
2007-11-11 12:29:37 +00:00
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
mutex_unlock(&llist_mutex);
|
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
if (type == TYPE_CUESHEET) {
|
2007-10-27 04:49:04 +00:00
|
|
|
/* Immediately start buffering those */
|
2011-02-14 08:36:29 +00:00
|
|
|
LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", handle_id);
|
|
|
|
queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id);
|
2011-02-14 09:18:58 +00:00
|
|
|
} else {
|
2007-10-27 04:49:04 +00:00
|
|
|
/* Other types will get buffered in the course of normal operations */
|
|
|
|
close(fd);
|
2008-02-12 23:15:59 +00:00
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
if (handle_id >= 0) {
|
2011-02-14 08:36:29 +00:00
|
|
|
/* Inform the buffering thread that we added a handle */
|
|
|
|
LOGFQUEUE("buffering > Q_HANDLE_ADDED %d", handle_id);
|
|
|
|
queue_post(&buffering_queue, Q_HANDLE_ADDED, handle_id);
|
|
|
|
}
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
logf("bufopen: new hdl %d", handle_id);
|
|
|
|
return handle_id;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Open a new handle from data that needs to be copied from memory.
|
|
|
|
src is the source buffer from which to copy data. It can be NULL to simply
|
|
|
|
reserve buffer space.
|
|
|
|
size is the requested size. The call will only be successful if the
|
|
|
|
requested amount of data can entirely fit in the buffer without wrapping.
|
|
|
|
Return value is the handle id for success or <0 for failure.
|
|
|
|
*/
|
2008-03-28 12:51:33 +00:00
|
|
|
int bufalloc(const void *src, size_t size, enum data_type type)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2011-04-27 03:08:23 +00:00
|
|
|
int handle_id;
|
|
|
|
|
|
|
|
if (type == TYPE_UNKNOWN)
|
|
|
|
return ERR_UNSUPPORTED_TYPE;
|
|
|
|
|
|
|
|
handle_id = ERR_BUFFER_FULL;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
mutex_lock(&llist_mutex);
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
struct memory_handle *h = add_handle(size, false, true);
|
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
if (h) {
|
2011-02-14 08:36:29 +00:00
|
|
|
handle_id = h->id;
|
|
|
|
|
|
|
|
if (src) {
|
|
|
|
if (type == TYPE_ID3 && size == sizeof(struct mp3entry)) {
|
|
|
|
/* specially take care of struct mp3entry */
|
|
|
|
copy_mp3entry((struct mp3entry *)&buffer[buf_widx],
|
|
|
|
(const struct mp3entry *)src);
|
|
|
|
} else {
|
|
|
|
memcpy(&buffer[buf_widx], src, size);
|
|
|
|
}
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
2011-02-14 08:36:29 +00:00
|
|
|
|
|
|
|
h->fd = -1;
|
|
|
|
*h->path = 0;
|
|
|
|
h->filesize = size;
|
|
|
|
h->offset = 0;
|
|
|
|
h->ridx = buf_widx;
|
|
|
|
h->data = buf_widx;
|
2011-03-13 11:56:51 +00:00
|
|
|
buf_widx = ringbuf_add(buf_widx, size);
|
|
|
|
h->widx = buf_widx;
|
2011-02-14 08:36:29 +00:00
|
|
|
h->available = size;
|
|
|
|
h->type = type;
|
|
|
|
}
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
mutex_unlock(&llist_mutex);
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
logf("bufalloc: new hdl %d", handle_id);
|
|
|
|
return handle_id;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the handle. Return true for success and false for failure */
|
2008-03-28 12:51:33 +00:00
|
|
|
bool bufclose(int handle_id)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
|
|
|
logf("bufclose(%d)", handle_id);
|
2011-04-27 03:08:23 +00:00
|
|
|
#if 0
|
|
|
|
/* Don't interrupt the buffering thread if the handle is already
|
|
|
|
stale */
|
|
|
|
if (!find_handle(handle_id)) {
|
|
|
|
logf(" handle already closed");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
2007-10-27 01:25:47 +00:00
|
|
|
LOGFQUEUE("buffering >| Q_CLOSE_HANDLE %d", handle_id);
|
2007-10-25 21:27:45 +00:00
|
|
|
return queue_send(&buffering_queue, Q_CLOSE_HANDLE, handle_id);
|
|
|
|
}
|
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
/* Backend to bufseek and bufadvance. Call only in response to
|
|
|
|
Q_REBUFFER_HANDLE! */
|
|
|
|
static void rebuffer_handle(int handle_id, size_t newpos)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
|
|
|
struct memory_handle *h = find_handle(handle_id);
|
2011-02-14 09:18:58 +00:00
|
|
|
|
|
|
|
if (!h) {
|
2011-02-14 08:36:29 +00:00
|
|
|
queue_reply(&buffering_queue, ERR_HANDLE_NOT_FOUND);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* When seeking foward off of the buffer, if it is a short seek attempt to
|
|
|
|
avoid rebuffering the whole track, just read enough to satisfy */
|
2011-02-14 09:18:58 +00:00
|
|
|
if (newpos > h->offset &&
|
|
|
|
newpos - h->offset < BUFFERING_DEFAULT_FILECHUNK) {
|
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
size_t amount = newpos - h->offset;
|
|
|
|
h->ridx = ringbuf_add(h->data, amount);
|
2011-02-14 09:18:58 +00:00
|
|
|
|
|
|
|
if (buffer_handle(handle_id, amount + 1)) {
|
2011-03-02 06:24:50 +00:00
|
|
|
size_t rd = ringbuf_sub(h->ridx, h->data);
|
|
|
|
size_t wr = ringbuf_sub(h->widx, h->data);
|
|
|
|
if (wr >= rd) {
|
|
|
|
/* It really did succeed */
|
|
|
|
queue_reply(&buffering_queue, 0);
|
|
|
|
buffer_handle(handle_id, 0); /* Ok, try the rest */
|
|
|
|
return;
|
|
|
|
}
|
2011-02-14 08:36:29 +00:00
|
|
|
}
|
2011-03-02 06:24:50 +00:00
|
|
|
/* Data collision or other file error - must reset */
|
|
|
|
|
|
|
|
if (newpos > h->filesize)
|
|
|
|
newpos = h->filesize; /* file truncation happened above */
|
2011-02-14 08:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the handle to its new position */
|
|
|
|
h->offset = newpos;
|
|
|
|
|
|
|
|
size_t next = h->next ? ringbuf_offset(h->next) : buf_ridx;
|
|
|
|
|
|
|
|
#ifdef STORAGE_WANTS_ALIGN
|
|
|
|
/* Strip alignment padding then redo */
|
|
|
|
size_t new_index = ringbuf_add(ringbuf_offset(h), sizeof (*h));
|
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
/* Align to desired storage alignment if space permits - handle could
|
|
|
|
have been shrunken too close to the following one after a previous
|
|
|
|
rebuffer. */
|
2011-02-14 08:36:29 +00:00
|
|
|
size_t alignment_pad =
|
|
|
|
STORAGE_OVERLAP(h->offset - (size_t)(&buffer[new_index]));
|
|
|
|
|
|
|
|
if (ringbuf_add_cross(new_index, alignment_pad, next) >= 0)
|
|
|
|
alignment_pad = 0; /* Forego storage alignment this time */
|
|
|
|
|
|
|
|
new_index = ringbuf_add(new_index, alignment_pad);
|
|
|
|
#else
|
|
|
|
/* Just clear the data buffer */
|
|
|
|
size_t new_index = h->data;
|
|
|
|
#endif /* STORAGE_WANTS_ALIGN */
|
|
|
|
|
|
|
|
h->ridx = h->widx = h->data = new_index;
|
|
|
|
|
|
|
|
if (h == cur_handle)
|
|
|
|
buf_widx = new_index;
|
|
|
|
|
|
|
|
h->available = 0;
|
|
|
|
h->filerem = h->filesize - h->offset;
|
|
|
|
|
|
|
|
if (h->fd >= 0)
|
|
|
|
lseek(h->fd, h->offset, SEEK_SET);
|
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
if (h->next && ringbuf_sub(next, h->data) <= h->filesize - newpos) {
|
2011-02-14 08:36:29 +00:00
|
|
|
/* There isn't enough space to rebuffer all of the track from its new
|
|
|
|
offset, so we ask the user to free some */
|
|
|
|
DEBUGF("%s(): space is needed\n", __func__);
|
|
|
|
int hid = handle_id;
|
|
|
|
send_event(BUFFER_EVENT_REBUFFER, &hid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we do the rebuffer */
|
|
|
|
queue_reply(&buffering_queue, 0);
|
|
|
|
buffer_handle(handle_id, 0);
|
|
|
|
}
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
/* Backend to bufseek and bufadvance */
|
|
|
|
static int seek_handle(struct memory_handle *h, size_t newpos)
|
|
|
|
{
|
2007-10-25 21:27:45 +00:00
|
|
|
if (newpos > h->filesize) {
|
|
|
|
/* access beyond the end of the file */
|
2007-10-29 14:15:59 +00:00
|
|
|
return ERR_INVALID_VALUE;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
2011-02-14 08:36:29 +00:00
|
|
|
else if ((newpos < h->offset || h->offset + h->available <= newpos) &&
|
|
|
|
(newpos < h->filesize || h->filerem > 0)) {
|
|
|
|
/* access before or after buffered data and not to end of file or file
|
|
|
|
is not buffered to the end-- a rebuffer is needed. */
|
|
|
|
struct buf_message_data parm = { h->id, newpos };
|
|
|
|
return queue_send(&buffering_queue, Q_REBUFFER_HANDLE,
|
|
|
|
(intptr_t)&parm);
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-02-12 13:12:59 +00:00
|
|
|
h->ridx = ringbuf_add(h->data, newpos - h->offset);
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
2011-02-14 08:36:29 +00:00
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
/* Set reading index in handle (relatively to the start of the file).
|
|
|
|
Access before the available data will trigger a rebuffer.
|
2011-04-27 03:08:23 +00:00
|
|
|
Return 0 for success and for failure:
|
|
|
|
ERR_HANDLE_NOT_FOUND if the handle wasn't found
|
|
|
|
ERR_INVALID_VALUE if the new requested position was beyond the end of
|
|
|
|
the file
|
2011-02-14 08:36:29 +00:00
|
|
|
*/
|
|
|
|
int bufseek(int handle_id, size_t newpos)
|
|
|
|
{
|
|
|
|
struct memory_handle *h = find_handle(handle_id);
|
|
|
|
if (!h)
|
|
|
|
return ERR_HANDLE_NOT_FOUND;
|
|
|
|
|
|
|
|
return seek_handle(h, newpos);
|
|
|
|
}
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
/* Advance the reading index in a handle (relatively to its current position).
|
2011-04-27 03:08:23 +00:00
|
|
|
Return 0 for success and for failure:
|
|
|
|
ERR_HANDLE_NOT_FOUND if the handle wasn't found
|
|
|
|
ERR_INVALID_VALUE if the new requested position was beyond the end of
|
|
|
|
the file
|
|
|
|
*/
|
2008-03-28 12:51:33 +00:00
|
|
|
int bufadvance(int handle_id, off_t offset)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2011-02-14 08:36:29 +00:00
|
|
|
struct memory_handle *h = find_handle(handle_id);
|
2007-10-25 21:27:45 +00:00
|
|
|
if (!h)
|
2007-10-29 14:15:59 +00:00
|
|
|
return ERR_HANDLE_NOT_FOUND;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2010-02-12 13:12:59 +00:00
|
|
|
size_t newpos = h->offset + ringbuf_sub(h->ridx, h->data) + offset;
|
2011-02-14 08:36:29 +00:00
|
|
|
return seek_handle(h, newpos);
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Get the read position from the start of the file
|
|
|
|
Returns the offset from byte 0 of the file and for failure:
|
|
|
|
ERR_HANDLE_NOT_FOUND if the handle wasn't found
|
|
|
|
*/
|
|
|
|
off_t bufftell(int handle_id)
|
|
|
|
{
|
|
|
|
const struct memory_handle *h = find_handle(handle_id);
|
|
|
|
if (!h)
|
|
|
|
return ERR_HANDLE_NOT_FOUND;
|
|
|
|
return h->offset + ringbuf_sub(h->ridx, h->data);
|
|
|
|
}
|
|
|
|
|
2007-11-06 16:49:30 +00:00
|
|
|
/* Used by bufread and bufgetdata to prepare the buffer and retrieve the
|
|
|
|
* actual amount of data available for reading. This function explicitly
|
|
|
|
* does not check the validity of the input handle. It does do range checks
|
|
|
|
* on size and returns a valid (and explicit) amount of data for reading */
|
2008-03-28 12:51:33 +00:00
|
|
|
static struct memory_handle *prep_bufdata(int handle_id, size_t *size,
|
|
|
|
bool guardbuf_limit)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2007-11-08 15:34:23 +00:00
|
|
|
struct memory_handle *h = find_handle(handle_id);
|
2011-02-14 08:36:29 +00:00
|
|
|
size_t realsize;
|
|
|
|
|
2007-11-08 15:34:23 +00:00
|
|
|
if (!h)
|
|
|
|
return NULL;
|
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
size_t avail = handle_size_available(h);
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
if (avail == 0 && h->filerem == 0) {
|
2007-10-25 21:27:45 +00:00
|
|
|
/* File is finished reading */
|
2007-11-08 18:27:19 +00:00
|
|
|
*size = 0;
|
|
|
|
return h;
|
|
|
|
}
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
realsize = *size;
|
|
|
|
|
|
|
|
if (realsize == 0 || realsize > avail + h->filerem)
|
|
|
|
realsize = avail + h->filerem;
|
2007-11-06 16:49:30 +00:00
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
if (guardbuf_limit && h->type == TYPE_PACKET_AUDIO
|
2011-02-14 09:18:58 +00:00
|
|
|
&& realsize > GUARD_BUFSIZE) {
|
2007-11-08 15:52:10 +00:00
|
|
|
logf("data request > guardbuf");
|
|
|
|
/* If more than the size of the guardbuf is requested and this is a
|
|
|
|
* bufgetdata, limit to guard_bufsize over the end of the buffer */
|
2011-02-14 08:36:29 +00:00
|
|
|
realsize = MIN(realsize, buffer_len - h->ridx + GUARD_BUFSIZE);
|
2007-11-08 16:12:28 +00:00
|
|
|
/* this ensures *size <= buffer_len - h->ridx + GUARD_BUFSIZE */
|
2007-11-06 16:49:30 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
if (h->filerem > 0 && avail < realsize) {
|
2007-11-02 19:13:03 +00:00
|
|
|
/* Data isn't ready. Request buffering */
|
2011-05-09 21:19:11 +00:00
|
|
|
LOGFQUEUE("buffering >| Q_START_FILL %d",handle_id);
|
|
|
|
queue_send(&buffering_queue, Q_START_FILL, handle_id);
|
2007-11-02 19:13:03 +00:00
|
|
|
/* Wait for the data to be ready */
|
|
|
|
do
|
|
|
|
{
|
2011-02-14 08:36:29 +00:00
|
|
|
sleep(0);
|
2007-11-08 16:06:24 +00:00
|
|
|
/* it is not safe for a non-buffering thread to sleep while
|
|
|
|
* holding a handle */
|
2007-11-08 15:34:23 +00:00
|
|
|
h = find_handle(handle_id);
|
2011-04-27 03:08:23 +00:00
|
|
|
if (!h || h->signaled != 0)
|
2007-11-08 16:06:24 +00:00
|
|
|
return NULL;
|
2011-02-14 08:36:29 +00:00
|
|
|
avail = handle_size_available(h);
|
2007-11-02 19:13:03 +00:00
|
|
|
}
|
2011-02-14 08:36:29 +00:00
|
|
|
while (h->filerem > 0 && avail < realsize);
|
2007-11-02 19:13:03 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
*size = MIN(realsize, avail);
|
2007-11-08 15:34:23 +00:00
|
|
|
return h;
|
2007-11-06 16:49:30 +00:00
|
|
|
}
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
|
|
|
|
/* Note: It is safe for the thread responsible for handling the rebuffer
|
|
|
|
* cleanup request to call bufread or bufgetdata only when the data will
|
|
|
|
* be available-- not if it could be blocked waiting for it in prep_bufdata.
|
|
|
|
* It should be apparent that if said thread is being forced to wait for
|
|
|
|
* buffering but has not yet responded to the cleanup request, the space
|
|
|
|
* can never be cleared to allow further reading of the file because it is
|
|
|
|
* not listening to callbacks any longer. */
|
|
|
|
|
2007-11-06 16:49:30 +00:00
|
|
|
/* Copy data from the given handle to the dest buffer.
|
|
|
|
Return the number of bytes copied or < 0 for failure (handle not found).
|
|
|
|
The caller is blocked until the requested amount of data is available.
|
|
|
|
*/
|
2008-03-28 12:51:33 +00:00
|
|
|
ssize_t bufread(int handle_id, size_t size, void *dest)
|
2007-11-06 16:49:30 +00:00
|
|
|
{
|
2008-03-29 14:09:14 +00:00
|
|
|
const struct memory_handle *h;
|
2008-03-28 11:24:24 +00:00
|
|
|
size_t adjusted_size = size;
|
2007-11-08 15:34:23 +00:00
|
|
|
|
2008-03-28 11:24:24 +00:00
|
|
|
h = prep_bufdata(handle_id, &adjusted_size, false);
|
2007-11-06 16:49:30 +00:00
|
|
|
if (!h)
|
|
|
|
return ERR_HANDLE_NOT_FOUND;
|
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
if (h->ridx + adjusted_size > buffer_len) {
|
2007-10-25 21:27:45 +00:00
|
|
|
/* the data wraps around the end of the buffer */
|
|
|
|
size_t read = buffer_len - h->ridx;
|
|
|
|
memcpy(dest, &buffer[h->ridx], read);
|
2008-03-28 11:24:24 +00:00
|
|
|
memcpy(dest+read, buffer, adjusted_size - read);
|
2011-02-14 09:18:58 +00:00
|
|
|
} else {
|
2008-03-28 11:24:24 +00:00
|
|
|
memcpy(dest, &buffer[h->ridx], adjusted_size);
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
2008-03-28 11:24:24 +00:00
|
|
|
return adjusted_size;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the "data" pointer to make the handle's data available to the caller.
|
2007-11-02 19:13:03 +00:00
|
|
|
Return the length of the available linear data or < 0 for failure (handle
|
|
|
|
not found).
|
|
|
|
The caller is blocked until the requested amount of data is available.
|
2007-10-25 21:27:45 +00:00
|
|
|
size is the amount of linear data requested. it can be 0 to get as
|
|
|
|
much as possible.
|
2007-11-02 19:13:03 +00:00
|
|
|
The guard buffer may be used to provide the requested size. This means it's
|
|
|
|
unsafe to request more than the size of the guard buffer.
|
|
|
|
*/
|
2008-03-28 12:51:33 +00:00
|
|
|
ssize_t bufgetdata(int handle_id, size_t size, void **data)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2007-11-08 15:34:23 +00:00
|
|
|
const struct memory_handle *h;
|
2008-03-29 14:09:14 +00:00
|
|
|
size_t adjusted_size = size;
|
2007-11-08 15:34:23 +00:00
|
|
|
|
2008-03-28 11:24:24 +00:00
|
|
|
h = prep_bufdata(handle_id, &adjusted_size, true);
|
2007-10-25 21:27:45 +00:00
|
|
|
if (!h)
|
2007-10-29 14:15:59 +00:00
|
|
|
return ERR_HANDLE_NOT_FOUND;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
if (h->ridx + adjusted_size > buffer_len) {
|
2007-10-25 21:27:45 +00:00
|
|
|
/* the data wraps around the end of the buffer :
|
|
|
|
use the guard buffer to provide the requested amount of data. */
|
2008-03-28 11:24:24 +00:00
|
|
|
size_t copy_n = h->ridx + adjusted_size - buffer_len;
|
2011-02-14 09:18:58 +00:00
|
|
|
/* prep_bufdata ensures
|
|
|
|
adjusted_size <= buffer_len - h->ridx + GUARD_BUFSIZE,
|
2007-11-08 16:12:28 +00:00
|
|
|
so copy_n <= GUARD_BUFSIZE */
|
2008-03-26 08:57:25 +00:00
|
|
|
memcpy(guard_buffer, (const unsigned char *)buffer, copy_n);
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
2007-12-16 01:38:56 +00:00
|
|
|
if (data)
|
|
|
|
*data = &buffer[h->ridx];
|
|
|
|
|
2008-03-28 11:24:24 +00:00
|
|
|
return adjusted_size;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
2008-03-28 12:51:33 +00:00
|
|
|
ssize_t bufgettail(int handle_id, size_t size, void **data)
|
2007-11-28 04:58:16 +00:00
|
|
|
{
|
|
|
|
size_t tidx;
|
|
|
|
|
|
|
|
const struct memory_handle *h;
|
2007-12-16 01:38:56 +00:00
|
|
|
|
2007-11-28 04:58:16 +00:00
|
|
|
h = find_handle(handle_id);
|
|
|
|
|
|
|
|
if (!h)
|
|
|
|
return ERR_HANDLE_NOT_FOUND;
|
|
|
|
|
|
|
|
if (h->filerem)
|
|
|
|
return ERR_HANDLE_NOT_DONE;
|
|
|
|
|
|
|
|
/* We don't support tail requests of > guardbuf_size, for simplicity */
|
|
|
|
if (size > GUARD_BUFSIZE)
|
|
|
|
return ERR_INVALID_VALUE;
|
|
|
|
|
2010-02-12 13:12:59 +00:00
|
|
|
tidx = ringbuf_sub(h->widx, size);
|
2007-11-28 04:58:16 +00:00
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
if (tidx + size > buffer_len) {
|
2007-11-28 04:58:16 +00:00
|
|
|
size_t copy_n = tidx + size - buffer_len;
|
2008-03-26 08:57:25 +00:00
|
|
|
memcpy(guard_buffer, (const unsigned char *)buffer, copy_n);
|
2007-11-28 04:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*data = &buffer[tidx];
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2008-03-28 12:51:33 +00:00
|
|
|
ssize_t bufcuttail(int handle_id, size_t size)
|
2007-11-28 04:58:16 +00:00
|
|
|
{
|
2008-03-29 14:09:14 +00:00
|
|
|
struct memory_handle *h;
|
2008-03-28 11:24:24 +00:00
|
|
|
size_t adjusted_size = size;
|
2007-12-16 01:38:56 +00:00
|
|
|
|
2007-11-28 04:58:16 +00:00
|
|
|
h = find_handle(handle_id);
|
|
|
|
|
|
|
|
if (!h)
|
|
|
|
return ERR_HANDLE_NOT_FOUND;
|
|
|
|
|
|
|
|
if (h->filerem)
|
|
|
|
return ERR_HANDLE_NOT_DONE;
|
|
|
|
|
2008-03-28 11:24:24 +00:00
|
|
|
if (h->available < adjusted_size)
|
|
|
|
adjusted_size = h->available;
|
2007-11-28 04:58:16 +00:00
|
|
|
|
2008-03-28 11:24:24 +00:00
|
|
|
h->available -= adjusted_size;
|
|
|
|
h->filesize -= adjusted_size;
|
2010-02-12 13:12:59 +00:00
|
|
|
h->widx = ringbuf_sub(h->widx, adjusted_size);
|
2008-03-26 08:57:25 +00:00
|
|
|
if (h == cur_handle)
|
2007-11-28 16:39:58 +00:00
|
|
|
buf_widx = h->widx;
|
2008-03-26 08:57:25 +00:00
|
|
|
|
2008-03-28 11:24:24 +00:00
|
|
|
return adjusted_size;
|
2007-11-28 04:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
/*
|
|
|
|
SECONDARY EXPORTED FUNCTIONS
|
|
|
|
============================
|
|
|
|
|
|
|
|
buf_handle_offset
|
|
|
|
buf_set_base_handle
|
2011-04-27 03:08:23 +00:00
|
|
|
buf_handle_data_type
|
|
|
|
buf_is_handle
|
|
|
|
buf_pin_handle
|
|
|
|
buf_signal_handle
|
|
|
|
buf_length
|
2007-10-25 21:27:45 +00:00
|
|
|
buf_used
|
2011-04-27 03:08:23 +00:00
|
|
|
buf_set_watermark
|
|
|
|
buf_get_watermark
|
2007-10-25 21:27:45 +00:00
|
|
|
|
|
|
|
These functions are exported, to allow interaction with the buffer.
|
|
|
|
They take care of the content of the structs, and rely on the linked list
|
|
|
|
management functions for all the actual handle management work.
|
|
|
|
*/
|
|
|
|
|
2008-03-28 12:51:33 +00:00
|
|
|
ssize_t buf_handle_offset(int handle_id)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2007-10-27 01:37:33 +00:00
|
|
|
const struct memory_handle *h = find_handle(handle_id);
|
2007-10-25 21:27:45 +00:00
|
|
|
if (!h)
|
2007-10-29 14:15:59 +00:00
|
|
|
return ERR_HANDLE_NOT_FOUND;
|
2007-10-25 21:27:45 +00:00
|
|
|
return h->offset;
|
|
|
|
}
|
|
|
|
|
2008-03-28 12:51:33 +00:00
|
|
|
void buf_set_base_handle(int handle_id)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2011-04-27 03:08:23 +00:00
|
|
|
mutex_lock(&llist_mutex);
|
|
|
|
base_handle_id = handle_id;
|
|
|
|
mutex_unlock(&llist_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum data_type buf_handle_data_type(int handle_id)
|
|
|
|
{
|
|
|
|
const struct memory_handle *h = find_handle(handle_id);
|
|
|
|
if (!h)
|
|
|
|
return TYPE_UNKNOWN;
|
|
|
|
return h->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t buf_handle_remaining(int handle_id)
|
|
|
|
{
|
|
|
|
const struct memory_handle *h = find_handle(handle_id);
|
|
|
|
if (!h)
|
|
|
|
return ERR_HANDLE_NOT_FOUND;
|
|
|
|
return h->filerem;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool buf_is_handle(int handle_id)
|
|
|
|
{
|
|
|
|
return find_handle(handle_id) != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool buf_pin_handle(int handle_id, bool pin)
|
|
|
|
{
|
|
|
|
struct memory_handle *h = find_handle(handle_id);
|
|
|
|
if (!h)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pin) {
|
|
|
|
h->pinned++;
|
|
|
|
} else if (h->pinned > 0) {
|
|
|
|
h->pinned--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool buf_signal_handle(int handle_id, bool signal)
|
|
|
|
{
|
|
|
|
struct memory_handle *h = find_handle(handle_id);
|
|
|
|
if (!h)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
h->signaled = signal ? 1 : 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the size of the ringbuffer */
|
|
|
|
size_t buf_length(void)
|
|
|
|
{
|
|
|
|
return buffer_len;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the amount of buffer space used */
|
|
|
|
size_t buf_used(void)
|
|
|
|
{
|
|
|
|
return BUF_USED;
|
|
|
|
}
|
|
|
|
|
2008-03-28 12:51:33 +00:00
|
|
|
void buf_set_watermark(size_t bytes)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2009-01-10 21:10:56 +00:00
|
|
|
conf_watermark = bytes;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
size_t buf_get_watermark(void)
|
|
|
|
{
|
|
|
|
return BUF_WATERMARK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_IO_PRIORITY
|
|
|
|
void buf_back_off_storage(bool back_off)
|
|
|
|
{
|
|
|
|
int priority = back_off ?
|
|
|
|
IO_PRIORITY_BACKGROUND : IO_PRIORITY_IMMEDIATE;
|
|
|
|
thread_set_io_priority(buffering_thread_id, priority);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** -- buffer thread helpers -- **/
|
2008-03-29 14:09:14 +00:00
|
|
|
static void shrink_buffer_inner(struct memory_handle *h)
|
2008-03-26 08:57:25 +00:00
|
|
|
{
|
2007-11-03 02:54:34 +00:00
|
|
|
if (h == NULL)
|
|
|
|
return;
|
|
|
|
|
2007-11-05 15:24:21 +00:00
|
|
|
shrink_buffer_inner(h->next);
|
2007-11-03 02:54:34 +00:00
|
|
|
|
|
|
|
shrink_handle(h);
|
2007-10-29 16:48:16 +00:00
|
|
|
}
|
|
|
|
|
2008-03-29 14:09:14 +00:00
|
|
|
static void shrink_buffer(void)
|
2008-03-26 08:57:25 +00:00
|
|
|
{
|
2007-11-05 15:24:21 +00:00
|
|
|
logf("shrink_buffer()");
|
|
|
|
shrink_buffer_inner(first_handle);
|
|
|
|
}
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
static void NORETURN_ATTR buffering_thread(void)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2007-11-03 06:21:32 +00:00
|
|
|
bool filling = false;
|
2007-10-25 21:27:45 +00:00
|
|
|
struct queue_event ev;
|
2011-02-14 08:36:29 +00:00
|
|
|
struct buf_message_data *parm;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2011-04-27 03:08:23 +00:00
|
|
|
if (num_handles > 0) {
|
|
|
|
if (!filling) {
|
|
|
|
cancel_cpu_boost();
|
|
|
|
}
|
|
|
|
queue_wait_w_tmo(&buffering_queue, &ev, filling ? 1 : HZ/2);
|
|
|
|
} else {
|
|
|
|
filling = false;
|
2008-03-29 17:28:30 +00:00
|
|
|
cancel_cpu_boost();
|
2011-04-27 03:08:23 +00:00
|
|
|
queue_wait(&buffering_queue, &ev);
|
2008-03-29 17:40:04 +00:00
|
|
|
}
|
2008-03-29 17:28:30 +00:00
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
switch (ev.id)
|
|
|
|
{
|
2007-11-05 17:51:55 +00:00
|
|
|
case Q_START_FILL:
|
2008-07-18 23:42:47 +00:00
|
|
|
LOGFQUEUE("buffering < Q_START_FILL %d", (int)ev.data);
|
2007-11-05 15:24:21 +00:00
|
|
|
shrink_buffer();
|
2007-11-05 03:11:58 +00:00
|
|
|
queue_reply(&buffering_queue, 1);
|
2011-05-09 21:19:11 +00:00
|
|
|
if (buffer_handle((int)ev.data, 0)) {
|
|
|
|
filling = true;
|
|
|
|
}
|
|
|
|
else if (num_handles > 0 && conf_watermark > 0) {
|
|
|
|
update_data_counters(NULL);
|
|
|
|
if (data_counters.useful >= BUF_WATERMARK) {
|
|
|
|
send_event(BUFFER_EVENT_BUFFER_LOW, NULL);
|
|
|
|
}
|
|
|
|
}
|
2007-11-05 03:11:58 +00:00
|
|
|
break;
|
2007-11-05 15:24:21 +00:00
|
|
|
|
2007-11-05 03:11:58 +00:00
|
|
|
case Q_BUFFER_HANDLE:
|
2008-07-18 23:42:47 +00:00
|
|
|
LOGFQUEUE("buffering < Q_BUFFER_HANDLE %d", (int)ev.data);
|
2007-11-05 03:11:58 +00:00
|
|
|
queue_reply(&buffering_queue, 1);
|
2011-02-14 08:36:29 +00:00
|
|
|
buffer_handle((int)ev.data, 0);
|
2007-10-25 21:27:45 +00:00
|
|
|
break;
|
|
|
|
|
2011-02-14 08:36:29 +00:00
|
|
|
case Q_REBUFFER_HANDLE:
|
|
|
|
parm = (struct buf_message_data *)ev.data;
|
|
|
|
LOGFQUEUE("buffering < Q_REBUFFER_HANDLE %d %ld",
|
|
|
|
parm->handle_id, parm->data);
|
|
|
|
rebuffer_handle(parm->handle_id, parm->data);
|
2007-10-25 21:27:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Q_CLOSE_HANDLE:
|
2008-07-18 23:42:47 +00:00
|
|
|
LOGFQUEUE("buffering < Q_CLOSE_HANDLE %d", (int)ev.data);
|
2007-10-25 21:27:45 +00:00
|
|
|
queue_reply(&buffering_queue, close_handle((int)ev.data));
|
|
|
|
break;
|
|
|
|
|
2008-02-12 23:15:59 +00:00
|
|
|
case Q_HANDLE_ADDED:
|
|
|
|
LOGFQUEUE("buffering < Q_HANDLE_ADDED %d", (int)ev.data);
|
|
|
|
/* A handle was added: the disk is spinning, so we can fill */
|
|
|
|
filling = true;
|
|
|
|
break;
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
case SYS_TIMEOUT:
|
|
|
|
LOGFQUEUE_SYS_TIMEOUT("buffering < SYS_TIMEOUT");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (num_handles == 0 || !queue_empty(&buffering_queue))
|
|
|
|
continue;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
update_data_counters(NULL);
|
2007-11-04 00:23:22 +00:00
|
|
|
#if 0
|
|
|
|
/* TODO: This needs to be fixed to use the idle callback, disable it
|
|
|
|
* for simplicity until its done right */
|
2011-02-02 17:43:32 +00:00
|
|
|
#if MEMORYSIZE > 8
|
2007-10-25 21:27:45 +00:00
|
|
|
/* If the disk is spinning, take advantage by filling the buffer */
|
2011-04-27 03:08:23 +00:00
|
|
|
else if (storage_disk_is_active()) {
|
2007-11-03 03:46:22 +00:00
|
|
|
if (num_handles > 0 && data_counters.useful <= high_watermark)
|
2008-10-16 10:38:03 +00:00
|
|
|
send_event(BUFFER_EVENT_BUFFER_LOW, 0);
|
2007-11-03 06:21:32 +00:00
|
|
|
|
2011-02-14 09:18:58 +00:00
|
|
|
if (data_counters.remaining > 0 && BUF_USED <= high_watermark) {
|
2007-11-03 17:55:45 +00:00
|
|
|
/* This is a new fill, shrink the buffer up first */
|
|
|
|
if (!filling)
|
2007-11-05 15:24:21 +00:00
|
|
|
shrink_buffer();
|
2007-11-03 06:21:32 +00:00
|
|
|
filling = fill_buffer();
|
2011-02-14 08:36:29 +00:00
|
|
|
update_data_counters(NULL);
|
2007-10-30 17:24:31 +00:00
|
|
|
}
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
2007-11-04 00:23:22 +00:00
|
|
|
#endif
|
2007-10-25 21:27:45 +00:00
|
|
|
#endif
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (filling) {
|
2011-05-09 21:19:11 +00:00
|
|
|
filling = data_counters.remaining > 0 ? fill_buffer() : false;
|
2011-04-27 03:08:23 +00:00
|
|
|
} else if (ev.id == SYS_TIMEOUT) {
|
|
|
|
if (data_counters.useful < BUF_WATERMARK) {
|
|
|
|
/* The buffer is low and we're idle, just watching the levels
|
|
|
|
- call the callbacks to get new data */
|
|
|
|
send_event(BUFFER_EVENT_BUFFER_LOW, NULL);
|
|
|
|
|
|
|
|
/* Continue anything else we haven't finished - it might
|
|
|
|
get booted off or stop early because the receiver hasn't
|
|
|
|
had a chance to clear anything yet */
|
|
|
|
if (data_counters.remaining > 0) {
|
2007-11-05 15:24:21 +00:00
|
|
|
shrink_buffer();
|
2007-11-03 17:55:45 +00:00
|
|
|
filling = fill_buffer();
|
2007-11-05 03:11:58 +00:00
|
|
|
}
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-29 14:09:14 +00:00
|
|
|
void buffering_init(void)
|
2008-03-26 08:57:25 +00:00
|
|
|
{
|
2007-10-27 04:16:41 +00:00
|
|
|
mutex_init(&llist_mutex);
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Thread should absolutely not respond to USB because if it waits first,
|
|
|
|
then it cannot properly service the handles and leaks will happen -
|
|
|
|
this is a worker thread and shouldn't need to care about any system
|
|
|
|
notifications.
|
|
|
|
***
|
|
|
|
Whoever is using buffering should be responsible enough to clear all
|
|
|
|
the handles at the right time. */
|
|
|
|
queue_init(&buffering_queue, false);
|
2008-12-10 08:57:10 +00:00
|
|
|
buffering_thread_id = create_thread( buffering_thread, buffering_stack,
|
2007-10-27 04:29:46 +00:00
|
|
|
sizeof(buffering_stack), CREATE_THREAD_FROZEN,
|
2008-03-29 23:21:19 +00:00
|
|
|
buffering_thread_name IF_PRIO(, PRIORITY_BUFFERING)
|
2007-10-27 04:16:41 +00:00
|
|
|
IF_COP(, CPU));
|
2008-03-25 02:34:12 +00:00
|
|
|
|
|
|
|
queue_enable_queue_send(&buffering_queue, &buffering_queue_sender_list,
|
2008-12-10 08:57:10 +00:00
|
|
|
buffering_thread_id);
|
2007-10-27 04:16:41 +00:00
|
|
|
}
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
/* Initialise the buffering subsystem */
|
2008-03-28 12:51:33 +00:00
|
|
|
bool buffering_reset(char *buf, size_t buflen)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2011-02-09 09:30:09 +00:00
|
|
|
/* Wraps of storage-aligned data must also be storage aligned,
|
|
|
|
thus buf and buflen must be a aligned to an integer multiple of
|
|
|
|
the storage alignment */
|
2011-04-27 03:08:23 +00:00
|
|
|
|
2012-05-21 06:18:46 +00:00
|
|
|
if (buf) {
|
|
|
|
buflen -= MIN(buflen, GUARD_BUFSIZE);
|
|
|
|
|
|
|
|
STORAGE_ALIGN_BUFFER(buf, buflen);
|
2011-04-27 03:08:23 +00:00
|
|
|
|
2012-05-21 06:18:46 +00:00
|
|
|
if (!buf || !buflen)
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
buflen = 0;
|
|
|
|
}
|
2011-02-09 09:30:09 +00:00
|
|
|
|
2012-05-21 06:18:46 +00:00
|
|
|
send_event(BUFFER_EVENT_BUFFER_RESET, NULL);
|
|
|
|
|
|
|
|
/* If handles weren't closed above, just do it */
|
|
|
|
while (num_handles != 0)
|
|
|
|
bufclose(first_handle->id);
|
2007-10-25 21:27:45 +00:00
|
|
|
|
|
|
|
buffer = buf;
|
2010-06-23 04:34:18 +00:00
|
|
|
buffer_len = buflen;
|
2007-10-25 21:27:45 +00:00
|
|
|
guard_buffer = buf + buflen;
|
|
|
|
|
|
|
|
buf_widx = 0;
|
|
|
|
buf_ridx = 0;
|
|
|
|
|
|
|
|
first_handle = NULL;
|
2007-10-27 01:25:47 +00:00
|
|
|
cur_handle = NULL;
|
|
|
|
cached_handle = NULL;
|
2007-10-25 21:27:45 +00:00
|
|
|
num_handles = 0;
|
2007-11-04 19:01:02 +00:00
|
|
|
base_handle_id = -1;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Set the high watermark as 75% full...or 25% empty :)
|
|
|
|
This is the greatest fullness that will trigger low-buffer events
|
|
|
|
no matter what the setting because high-bitrate files can have
|
|
|
|
ludicrous margins that even exceed the buffer size - most common
|
|
|
|
with a huge anti-skip buffer but even without that setting,
|
|
|
|
staying constantly active in buffering is pointless */
|
2007-10-25 21:27:45 +00:00
|
|
|
high_watermark = 3*buflen / 4;
|
|
|
|
|
2008-12-10 08:57:10 +00:00
|
|
|
thread_thaw(buffering_thread_id);
|
2007-10-27 04:29:46 +00:00
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void buffering_get_debugdata(struct buffering_debug *dbgdata)
|
|
|
|
{
|
2011-02-14 08:36:29 +00:00
|
|
|
struct data_counters dc;
|
|
|
|
update_data_counters(&dc);
|
2007-10-25 21:27:45 +00:00
|
|
|
dbgdata->num_handles = num_handles;
|
2011-02-14 08:36:29 +00:00
|
|
|
dbgdata->data_rem = dc.remaining;
|
|
|
|
dbgdata->wasted_space = dc.wasted;
|
|
|
|
dbgdata->buffered_data = dc.buffered;
|
|
|
|
dbgdata->useful_data = dc.useful;
|
2011-04-27 03:08:23 +00:00
|
|
|
dbgdata->watermark = BUF_WATERMARK;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|