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>
|
|
|
|
#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 "memory.h"
|
|
|
|
#include "lcd.h"
|
|
|
|
#include "font.h"
|
|
|
|
#include "button.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "tree.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "sprintf.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"
|
2007-11-11 12:29:37 +00:00
|
|
|
#include "bmp.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"
|
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 */
|
2007-11-05 23:46:45 +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
|
|
|
|
|
|
|
|
/* default point to start buffer refill */
|
2009-01-10 21:10:56 +00:00
|
|
|
#define BUFFERING_DEFAULT_WATERMARK (1024*128)
|
2007-10-25 21:27:45 +00:00
|
|
|
/* 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 */
|
|
|
|
char path[MAX_PATH]; /* Path if data originated in a file */
|
|
|
|
int fd; /* File descriptor to path (-1 if closed) */
|
2010-02-01 17:16:39 +00:00
|
|
|
size_t start; /* Start index of the handle's data buffer,
|
|
|
|
for use by reset_handle. */
|
|
|
|
size_t data; /* Start index of the handle's data */
|
2007-10-27 01:37:33 +00:00
|
|
|
volatile size_t ridx; /* Read pointer, relative to the main buffer */
|
|
|
|
size_t widx; /* Write pointer */
|
|
|
|
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
|
|
|
|
|
|
|
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 */
|
|
|
|
#if MEM > 8
|
|
|
|
static size_t high_watermark = 0; /* High watermark for rebuffer */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
static struct mutex llist_mutex;
|
2009-05-15 00:14:38 +00:00
|
|
|
static struct mutex llist_mod_mutex;
|
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;
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
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 */
|
|
|
|
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. */
|
2007-10-25 21:27:45 +00:00
|
|
|
Q_RESET_HANDLE, /* (internal) Request resetting of a handle to its
|
|
|
|
offset (the offset has to be set beforehand) */
|
|
|
|
Q_CLOSE_HANDLE, /* Request closing a handle */
|
|
|
|
Q_BASE_HANDLE, /* Set the reference handle for buf_useful_data */
|
|
|
|
|
|
|
|
/* 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;
|
2007-10-25 21:27:45 +00:00
|
|
|
static struct event_queue buffering_queue;
|
|
|
|
static struct queue_sender_list buffering_queue_sender_list;
|
|
|
|
|
|
|
|
|
2007-11-26 21:13:08 +00:00
|
|
|
|
2010-02-12 13:12:59 +00:00
|
|
|
/* Ring buffer helper functions */
|
|
|
|
/* 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)
|
|
|
|
|
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
|
2007-10-30 15:09:52 +00:00
|
|
|
data_size cannot be allocated and alloc_all is set. This function's
|
|
|
|
only potential side effect is to allocate space for the cur_handle
|
|
|
|
if it returns NULL.
|
2007-10-27 04:49:04 +00:00
|
|
|
*/
|
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;
|
2007-10-30 15:09:52 +00:00
|
|
|
size_t new_widx;
|
2007-10-27 04:49:04 +00:00
|
|
|
size_t len;
|
|
|
|
int 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;
|
|
|
|
|
2007-10-27 04:49:04 +00:00
|
|
|
mutex_lock(&llist_mutex);
|
2009-05-15 00:14:38 +00:00
|
|
|
mutex_lock(&llist_mod_mutex);
|
2007-10-25 21:27:45 +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. */
|
|
|
|
size_t req = cur_handle->filerem + sizeof(struct memory_handle);
|
2010-02-12 13:12:59 +00:00
|
|
|
if (ringbuf_add_cross(cur_handle->widx, req, buf_ridx) >= 0) {
|
2007-10-30 14:11:03 +00:00
|
|
|
/* Not enough space */
|
2009-05-15 00:14:38 +00:00
|
|
|
mutex_unlock(&llist_mod_mutex);
|
2007-10-30 14:11:03 +00:00
|
|
|
mutex_unlock(&llist_mutex);
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
/* Allocate the remainder of the space for the current handle */
|
2010-02-12 13:12:59 +00:00
|
|
|
buf_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
|
|
|
|
2007-10-30 15:09:52 +00:00
|
|
|
/* align to 4 bytes up */
|
2010-02-12 13:12:59 +00:00
|
|
|
new_widx = ringbuf_add(buf_widx, 3) & ~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 */
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-10-30 15:09:52 +00:00
|
|
|
/* How far we shifted buf_widx to align things, must be < buffer_len */
|
2010-02-12 13:12:59 +00:00
|
|
|
shift = ringbuf_sub(new_widx, buf_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? */
|
2010-02-12 13:12:59 +00:00
|
|
|
overlap = ringbuf_add_cross(buf_widx, shift + len, buf_ridx);
|
2007-10-27 04:49:04 +00:00
|
|
|
if (overlap >= 0 && (alloc_all || (unsigned)overlap > data_size)) {
|
|
|
|
/* Not enough space for required allocations */
|
2009-05-15 00:14:38 +00:00
|
|
|
mutex_unlock(&llist_mod_mutex);
|
2007-10-25 21:27:45 +00:00
|
|
|
mutex_unlock(&llist_mutex);
|
|
|
|
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]);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2009-05-15 00:14:38 +00:00
|
|
|
mutex_unlock(&llist_mod_mutex);
|
2007-10-25 21:27:45 +00:00
|
|
|
mutex_unlock(&llist_mutex);
|
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
|
|
|
mutex_lock(&llist_mutex);
|
2009-05-15 00:14:38 +00:00
|
|
|
mutex_lock(&llist_mod_mutex);
|
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 */
|
|
|
|
buf_ridx = (void *)first_handle - (void *)buffer;
|
|
|
|
}
|
|
|
|
} 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 {
|
2009-05-15 00:14:38 +00:00
|
|
|
mutex_unlock(&llist_mod_mutex);
|
2007-10-25 21:27:45 +00:00
|
|
|
mutex_unlock(&llist_mutex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Invalidate the cache to prevent it from keeping the old location of h */
|
|
|
|
if (h == cached_handle)
|
|
|
|
cached_handle = NULL;
|
|
|
|
|
|
|
|
num_handles--;
|
|
|
|
|
2009-05-15 00:14:38 +00:00
|
|
|
mutex_unlock(&llist_mod_mutex);
|
2007-10-25 21:27:45 +00:00
|
|
|
mutex_unlock(&llist_mutex);
|
|
|
|
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
|
|
|
{
|
2007-11-04 19:01:02 +00:00
|
|
|
if (handle_id < 0)
|
2007-10-25 21:27:45 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mutex_lock(&llist_mutex);
|
|
|
|
|
|
|
|
/* simple caching because most of the time the requested handle
|
|
|
|
will either be the same as the last, or the one after the last */
|
|
|
|
if (cached_handle)
|
|
|
|
{
|
|
|
|
if (cached_handle->id == handle_id) {
|
|
|
|
mutex_unlock(&llist_mutex);
|
|
|
|
return cached_handle;
|
|
|
|
} else if (cached_handle->next &&
|
|
|
|
(cached_handle->next->id == handle_id)) {
|
|
|
|
cached_handle = cached_handle->next;
|
|
|
|
mutex_unlock(&llist_mutex);
|
|
|
|
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;
|
|
|
|
|
|
|
|
mutex_unlock(&llist_mutex);
|
|
|
|
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.
|
|
|
|
returns a valid memory_handle if the move is successful
|
|
|
|
NULL 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 NULL 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;
|
2009-11-21 17:00:38 +00:00
|
|
|
int32_t *here;
|
|
|
|
int32_t *there;
|
|
|
|
int32_t *end;
|
|
|
|
int32_t *begin;
|
|
|
|
size_t oldpos;
|
2007-10-27 04:49:04 +00:00
|
|
|
size_t newpos;
|
|
|
|
size_t size_to_move;
|
2007-10-29 16:48:16 +00:00
|
|
|
size_t final_delta = *delta;
|
2009-11-21 17:00:38 +00:00
|
|
|
size_t n;
|
2007-10-27 04:49:04 +00:00
|
|
|
int overlap;
|
2009-11-21 17:00:38 +00:00
|
|
|
int 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
|
|
|
}
|
|
|
|
|
|
|
|
mutex_lock(&llist_mutex);
|
2009-05-15 00:14:38 +00:00
|
|
|
mutex_lock(&llist_mod_mutex);
|
2007-10-28 20:18:59 +00:00
|
|
|
|
2009-11-21 17:00:38 +00:00
|
|
|
oldpos = (void *)src - (void *)buffer;
|
2010-02-12 13:12:59 +00:00
|
|
|
newpos = ringbuf_add(oldpos, final_delta);
|
|
|
|
overlap = ringbuf_add_cross(newpos, size_to_move, buffer_len - 1);
|
|
|
|
overlap_old = ringbuf_add_cross(oldpos, size_to_move, buffer_len -1);
|
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 */
|
2007-11-02 14:06:48 +00:00
|
|
|
size_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;
|
2009-11-21 17:00:38 +00:00
|
|
|
} else if ((unsigned)overlap > data_size) {
|
|
|
|
/* Correct the position and real delta to prevent the struct from
|
|
|
|
* wrapping, this guarantees an aligned delta, I think */
|
|
|
|
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 */
|
2009-05-15 00:14:38 +00:00
|
|
|
mutex_unlock(&llist_mod_mutex);
|
2007-11-02 14:06:48 +00:00
|
|
|
mutex_unlock(&llist_mutex);
|
|
|
|
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 {
|
2009-05-15 00:14:38 +00:00
|
|
|
mutex_unlock(&llist_mod_mutex);
|
2007-10-25 21:27:45 +00:00
|
|
|
mutex_unlock(&llist_mutex);
|
2007-10-29 16:48:16 +00:00
|
|
|
return false;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-28 20:18:59 +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;
|
|
|
|
|
2009-11-21 17:00:38 +00:00
|
|
|
|
2009-11-22 13:51:25 +00:00
|
|
|
/* Copying routine takes into account that the handles have a
|
2009-11-21 17:00:38 +00:00
|
|
|
* distance between each other which is a multiple of four. Faster 2 word
|
|
|
|
* copy may be ok but do this for safety and because wrapped copies should
|
2009-11-22 13:51:25 +00:00
|
|
|
* be fairly uncommon */
|
2009-11-21 17:00:38 +00:00
|
|
|
|
2010-02-12 13:12:59 +00:00
|
|
|
here = (int32_t *)((ringbuf_add(oldpos, size_to_move - 1) & ~3)+ (intptr_t)buffer);
|
|
|
|
there =(int32_t *)((ringbuf_add(newpos, size_to_move - 1) & ~3)+ (intptr_t)buffer);
|
2009-11-21 17:00:38 +00:00
|
|
|
end = (int32_t *)(( intptr_t)buffer + buffer_len - 4);
|
|
|
|
begin =(int32_t *)buffer;
|
2009-11-22 13:51:25 +00:00
|
|
|
|
2009-11-21 17:00:38 +00:00
|
|
|
n = (size_to_move & ~3)/4;
|
|
|
|
|
2009-11-22 13:51:25 +00:00
|
|
|
if ( overlap_old > 0 || overlap > 0 ) {
|
|
|
|
/* Old or moved handle wraps */
|
|
|
|
while (n--) {
|
|
|
|
if (here < begin)
|
|
|
|
here = end;
|
|
|
|
if (there < begin)
|
|
|
|
there = end;
|
|
|
|
*there-- = *here--;
|
|
|
|
}
|
2007-10-27 04:49:04 +00:00
|
|
|
} else {
|
2009-11-21 17:00:38 +00:00
|
|
|
/* both handles do not wrap */
|
|
|
|
memmove(dest,src,size_to_move);
|
2009-11-22 13:51:25 +00:00
|
|
|
}
|
2009-11-21 17:00:38 +00:00
|
|
|
|
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;
|
2009-05-15 00:14:38 +00:00
|
|
|
mutex_unlock(&llist_mod_mutex);
|
2007-10-25 21:27:45 +00:00
|
|
|
mutex_unlock(&llist_mutex);
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
2007-11-05 21:11:54 +00:00
|
|
|
reset_handle : Reset write position and data buffer of a handle to its offset
|
2007-10-25 21:27:45 +00:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2007-11-05 21:11:54 +00:00
|
|
|
static void update_data_counters(void)
|
|
|
|
{
|
|
|
|
struct memory_handle *m = find_handle(base_handle_id);
|
|
|
|
bool is_useful = m==NULL;
|
|
|
|
|
|
|
|
size_t buffered = 0;
|
|
|
|
size_t wasted = 0;
|
|
|
|
size_t remaining = 0;
|
|
|
|
size_t useful = 0;
|
|
|
|
|
2008-05-13 20:51:06 +00:00
|
|
|
mutex_lock(&llist_mutex);
|
|
|
|
|
2007-11-05 21:11:54 +00:00
|
|
|
m = first_handle;
|
|
|
|
while (m) {
|
|
|
|
buffered += m->available;
|
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)
|
2010-02-12 13:12:59 +00:00
|
|
|
useful += ringbuf_sub(m->widx, m->ridx);
|
2007-11-05 21:11:54 +00:00
|
|
|
|
|
|
|
m = m->next;
|
|
|
|
}
|
|
|
|
|
2008-05-13 20:51:06 +00:00
|
|
|
mutex_unlock(&llist_mutex);
|
|
|
|
|
2007-11-05 21:11:54 +00:00
|
|
|
data_counters.buffered = buffered;
|
|
|
|
data_counters.wasted = wasted;
|
|
|
|
data_counters.remaining = remaining;
|
|
|
|
data_counters.useful = useful;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool buffer_is_low(void)
|
|
|
|
{
|
|
|
|
update_data_counters();
|
2009-01-10 21:10:56 +00:00
|
|
|
return data_counters.useful < (conf_watermark / 2);
|
2007-11-05 21:11:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-03 17:55:45 +00:00
|
|
|
/* Buffer data for the given handle.
|
|
|
|
Return whether or not the buffering should continue explicitly. */
|
2008-03-28 12:51:33 +00:00
|
|
|
static bool buffer_handle(int handle_id)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
|
|
|
logf("buffer_handle(%d)", handle_id);
|
|
|
|
struct memory_handle *h = find_handle(handle_id);
|
|
|
|
if (!h)
|
2007-11-03 21:57:27 +00:00
|
|
|
return true;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (h->fd < 0) /* file closed, reopen */
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
2008-04-14 16:17:47 +00:00
|
|
|
if (h->type == TYPE_ID3)
|
|
|
|
{
|
2008-04-16 16:18:05 +00:00
|
|
|
if (!get_metadata((struct mp3entry *)(buffer + h->data), h->fd, h->path))
|
|
|
|
{
|
|
|
|
/* metadata parsing failed: clear the buffer. */
|
|
|
|
memset(buffer + h->data, 0, sizeof(struct mp3entry));
|
|
|
|
}
|
2008-04-14 16:17:47 +00:00
|
|
|
close(h->fd);
|
|
|
|
h->fd = -1;
|
|
|
|
h->filerem = 0;
|
|
|
|
h->available = sizeof(struct mp3entry);
|
|
|
|
h->widx += sizeof(struct mp3entry);
|
2008-10-16 10:38:03 +00:00
|
|
|
send_event(BUFFER_EVENT_FINISHED, &h->id);
|
2008-04-14 16:17:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
while (h->filerem > 0)
|
|
|
|
{
|
|
|
|
/* max amount to copy */
|
2007-11-05 17:48:21 +00:00
|
|
|
size_t copy_n = MIN( MIN(h->filerem, BUFFERING_DEFAULT_FILECHUNK),
|
2007-10-25 21:27:45 +00:00
|
|
|
buffer_len - h->widx);
|
|
|
|
|
2007-11-01 05:12:55 +00:00
|
|
|
/* stop copying if it would overwrite the reading position */
|
2010-02-12 13:12:59 +00:00
|
|
|
if (ringbuf_add_cross(h->widx, copy_n, buf_ridx) >= 0)
|
2007-11-04 05:57:48 +00:00
|
|
|
return false;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2009-11-22 13:51:25 +00:00
|
|
|
/* This would read into the next handle, this is broken
|
2010-02-12 13:12:59 +00:00
|
|
|
if (h->next && ringbuf_add_cross(h->widx, copy_n,
|
2007-11-01 05:12:55 +00:00
|
|
|
(unsigned)((void *)h->next - (void *)buffer)) > 0) {
|
2009-11-22 13:51:25 +00:00
|
|
|
Try to recover by truncating this file
|
2010-02-12 13:12:59 +00:00
|
|
|
copy_n = ringbuf_add_cross(h->widx, copy_n,
|
2007-11-04 05:57:48 +00:00
|
|
|
(unsigned)((void *)h->next - (void *)buffer));
|
2007-11-05 01:56:12 +00:00
|
|
|
h->filerem -= copy_n;
|
|
|
|
h->filesize -= copy_n;
|
2007-11-05 18:19:38 +00:00
|
|
|
logf("buf alloc short %ld", (long)copy_n);
|
2007-11-04 05:57:48 +00:00
|
|
|
if (h->filerem)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
break;
|
2009-11-21 17:00:38 +00:00
|
|
|
} */
|
2007-11-01 05:12:55 +00:00
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
/* rc is the actual amount read */
|
|
|
|
int rc = read(h->fd, &buffer[h->widx], copy_n);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGF("File ended %ld bytes early\n", (long)h->filerem);
|
|
|
|
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 &&
|
|
|
|
pcmbuf_is_lowdata() && !buffer_is_low())
|
|
|
|
{
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!queue_empty(&buffering_queue))
|
2007-11-05 21:11:54 +00:00
|
|
|
break;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (h->filerem == 0) {
|
|
|
|
/* finished buffering the file */
|
|
|
|
close(h->fd);
|
|
|
|
h->fd = -1;
|
2008-10-16 10:38:03 +00:00
|
|
|
send_event(BUFFER_EVENT_FINISHED, &h->id);
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 05:57:48 +00:00
|
|
|
return true;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset writing position and data buffer of a handle to its current offset.
|
|
|
|
Use this after having set the new offset to use. */
|
2008-03-28 12:51:33 +00:00
|
|
|
static void reset_handle(int handle_id)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2010-02-01 17:16:39 +00:00
|
|
|
size_t alignment_pad;
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
logf("reset_handle(%d)", handle_id);
|
|
|
|
|
|
|
|
struct memory_handle *h = find_handle(handle_id);
|
|
|
|
if (!h)
|
|
|
|
return;
|
|
|
|
|
2010-02-01 17:16:39 +00:00
|
|
|
/* Align to desired storage alignment */
|
|
|
|
alignment_pad = (h->offset - (size_t)(&buffer[h->start]))
|
|
|
|
& STORAGE_ALIGN_MASK;
|
2010-02-12 13:12:59 +00:00
|
|
|
h->ridx = h->widx = h->data = ringbuf_add(h->start, alignment_pad);
|
2010-02-01 17:16:39 +00:00
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
if (h == cur_handle)
|
|
|
|
buf_widx = h->widx;
|
|
|
|
h->available = 0;
|
|
|
|
h->filerem = h->filesize - h->offset;
|
|
|
|
|
|
|
|
if (h->fd >= 0) {
|
|
|
|
lseek(h->fd, h->offset, SEEK_SET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Seek to a nonbuffered part of a handle by rebuffering the data. */
|
2008-03-28 12:51:33 +00:00
|
|
|
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);
|
|
|
|
if (!h)
|
|
|
|
return;
|
|
|
|
|
2007-11-26 21:13:08 +00:00
|
|
|
/* When seeking foward off of the buffer, if it is a short seek don't
|
|
|
|
rebuffer the whole track, just read enough to satisfy */
|
2007-11-05 17:50:51 +00:00
|
|
|
if (newpos > h->offset && newpos - h->offset < BUFFERING_DEFAULT_FILECHUNK)
|
|
|
|
{
|
2008-07-18 23:42:47 +00:00
|
|
|
LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", handle_id);
|
2007-11-05 17:50:51 +00:00
|
|
|
queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id);
|
|
|
|
h->ridx = h->data + newpos;
|
|
|
|
return;
|
|
|
|
}
|
2007-11-26 21:13:08 +00:00
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
h->offset = newpos;
|
|
|
|
|
2007-11-26 21:13:08 +00:00
|
|
|
/* Reset the handle to its new offset */
|
2008-07-18 23:42:47 +00:00
|
|
|
LOGFQUEUE("buffering >| Q_RESET_HANDLE %d", handle_id);
|
2007-10-25 21:27:45 +00:00
|
|
|
queue_send(&buffering_queue, Q_RESET_HANDLE, handle_id);
|
|
|
|
|
2007-11-26 23:51:36 +00:00
|
|
|
size_t next = (unsigned)((void *)h->next - (void *)buffer);
|
2010-02-12 13:12:59 +00:00
|
|
|
if (ringbuf_sub(next, h->data) < h->filesize - newpos)
|
2007-11-26 21:13:08 +00:00
|
|
|
{
|
2007-11-26 23:51:36 +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 */
|
2007-11-26 21:13:08 +00:00
|
|
|
DEBUGF("rebuffer_handle: space is needed\n");
|
2008-10-16 10:38:03 +00:00
|
|
|
send_event(BUFFER_EVENT_REBUFFER, &handle_id);
|
2007-11-26 21:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we ask for a rebuffer */
|
2008-07-18 23:42:47 +00:00
|
|
|
LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", handle_id);
|
2007-10-25 21:27:45 +00:00
|
|
|
queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id);
|
|
|
|
}
|
|
|
|
|
2008-03-28 12:51:33 +00:00
|
|
|
static bool close_handle(int handle_id)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
|
|
|
struct memory_handle *h = find_handle(handle_id);
|
2007-11-04 19:01:02 +00:00
|
|
|
|
|
|
|
/* If the handle is not found, it is closed */
|
2007-10-25 21:27:45 +00:00
|
|
|
if (!h)
|
2007-11-04 19:01:02 +00:00
|
|
|
return true;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
|
|
|
if (h->fd >= 0) {
|
|
|
|
close(h->fd);
|
|
|
|
h->fd = -1;
|
|
|
|
}
|
|
|
|
|
2007-11-04 19:01:02 +00:00
|
|
|
/* rm_handle returns true unless the handle somehow persists after exit */
|
|
|
|
return rm_handle(h);
|
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
|
|
|
{
|
2007-10-27 04:49:04 +00:00
|
|
|
size_t delta;
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
if (!h)
|
|
|
|
return;
|
|
|
|
|
2007-11-02 14:06:48 +00:00
|
|
|
if (h->next && h->filerem == 0 &&
|
|
|
|
(h->type == TYPE_ID3 || h->type == TYPE_CUESHEET ||
|
2007-11-11 12:29:37 +00:00
|
|
|
h->type == TYPE_BITMAP || h->type == TYPE_CODEC ||
|
2007-11-02 14:06:48 +00:00
|
|
|
h->type == TYPE_ATOMIC_AUDIO))
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
|
|
|
/* metadata handle: we can move all of it */
|
2007-11-26 21:13:08 +00:00
|
|
|
size_t handle_distance =
|
2010-02-12 13:12:59 +00:00
|
|
|
ringbuf_sub((unsigned)((void *)h->next - (void*)buffer), h->data);
|
2007-10-27 04:49:04 +00:00
|
|
|
delta = handle_distance - h->available;
|
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* only move the handle struct */
|
2010-02-12 13:12:59 +00:00
|
|
|
delta = ringbuf_sub(h->ridx, h->data);
|
2007-11-02 14:06:48 +00:00
|
|
|
if (!move_handle(&h, &delta, 0, true))
|
2007-10-29 16:48:16 +00:00
|
|
|
return;
|
2007-10-27 04:49:04 +00:00
|
|
|
|
2010-02-12 13:12:59 +00:00
|
|
|
h->data = ringbuf_add(h->data, delta);
|
|
|
|
h->start = ringbuf_add(h->start, delta);
|
2007-10-25 21:27:45 +00:00
|
|
|
h->available -= delta;
|
|
|
|
h->offset += delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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()");
|
2008-07-11 12:44:27 +00:00
|
|
|
struct memory_handle *m;
|
|
|
|
shrink_handle(first_handle);
|
|
|
|
m = first_handle;
|
2007-10-25 21:27:45 +00:00
|
|
|
while (queue_empty(&buffering_queue) && m) {
|
|
|
|
if (m->filerem > 0) {
|
2007-11-03 17:55:45 +00:00
|
|
|
if (!buffer_handle(m->id)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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). */
|
2009-10-16 19:14:41 +00:00
|
|
|
static int load_image(int fd, const char *path, struct dim *dim)
|
2007-11-11 12:29:37 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct bitmap *bmp = (struct bitmap *)&buffer[buf_widx];
|
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
|
|
|
|
int pathlen = strlen(path);
|
2009-05-01 23:31:43 +00:00
|
|
|
if (strcmp(path + pathlen - 4, ".bmp"))
|
|
|
|
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
|
|
|
|
(offset-1) 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
|
2008-04-14 16:17:47 +00:00
|
|
|
if (type == TYPE_ID3)
|
|
|
|
{
|
|
|
|
/* ID3 case: allocate space, init the handle and return. */
|
|
|
|
|
|
|
|
struct memory_handle *h = add_handle(sizeof(struct mp3entry), false, true);
|
|
|
|
if (!h)
|
|
|
|
return ERR_BUFFER_FULL;
|
|
|
|
|
|
|
|
h->fd = -1;
|
|
|
|
h->filesize = sizeof(struct mp3entry);
|
|
|
|
h->filerem = sizeof(struct mp3entry);
|
|
|
|
h->offset = 0;
|
|
|
|
h->data = buf_widx;
|
|
|
|
h->ridx = buf_widx;
|
|
|
|
h->widx = buf_widx;
|
|
|
|
h->available = 0;
|
|
|
|
h->type = type;
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(h->path, file, MAX_PATH);
|
2008-04-14 16:17:47 +00:00
|
|
|
|
|
|
|
buf_widx += sizeof(struct mp3entry); /* safe because the handle
|
|
|
|
can't wrap */
|
2008-12-02 21:07:12 +00:00
|
|
|
|
|
|
|
/* Inform the buffering thread that we added a handle */
|
|
|
|
LOGFQUEUE("buffering > Q_HANDLE_ADDED %d", h->id);
|
|
|
|
queue_post(&buffering_queue, Q_HANDLE_ADDED, h->id);
|
|
|
|
|
2008-04-14 16:17:47 +00:00
|
|
|
return h->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Other cases: there is a little more work. */
|
2008-03-28 11:24:24 +00:00
|
|
|
|
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
|
|
|
|
2007-10-27 04:49:04 +00:00
|
|
|
size_t 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 */
|
|
|
|
struct memory_handle *h = add_handle(size-adjusted_offset+STORAGE_ALIGN_MASK,
|
|
|
|
can_wrap, false);
|
2007-10-25 21:27:45 +00:00
|
|
|
if (!h)
|
|
|
|
{
|
|
|
|
DEBUGF("bufopen: failed to add handle\n");
|
|
|
|
close(fd);
|
2007-10-29 14:15:59 +00:00
|
|
|
return ERR_BUFFER_FULL;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
/* Don't bother to storage align bitmaps because they are not
|
|
|
|
* loaded directly into the buffer.
|
|
|
|
*/
|
|
|
|
if (type != TYPE_BITMAP)
|
|
|
|
{
|
|
|
|
size_t alignment_pad;
|
|
|
|
|
|
|
|
/* Remember where data area starts, for use by reset_handle */
|
|
|
|
h->start = buf_widx;
|
|
|
|
|
|
|
|
/* Align to desired storage alignment */
|
|
|
|
alignment_pad = (adjusted_offset - (size_t)(&buffer[buf_widx]))
|
|
|
|
& STORAGE_ALIGN_MASK;
|
2010-02-12 13:12:59 +00:00
|
|
|
buf_widx = ringbuf_add(buf_widx, alignment_pad);
|
2010-02-01 17:16:39 +00:00
|
|
|
}
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
h->ridx = buf_widx;
|
2009-05-15 00:14:38 +00:00
|
|
|
h->widx = buf_widx;
|
2007-10-25 21:27:45 +00:00
|
|
|
h->data = buf_widx;
|
2009-05-15 00:14:38 +00:00
|
|
|
h->available = 0;
|
|
|
|
h->filerem = 0;
|
2007-10-25 21:27:45 +00:00
|
|
|
h->type = type;
|
|
|
|
|
2007-11-11 13:28:24 +00:00
|
|
|
#ifdef HAVE_ALBUMART
|
2007-11-12 15:16:41 +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;
|
2009-05-15 00:14:38 +00:00
|
|
|
mutex_lock(&llist_mod_mutex); /* Lock because load_bitmap yields */
|
2009-10-16 19:14:41 +00:00
|
|
|
rc = load_image(fd, file, (struct dim*)user_data);
|
2009-05-15 00:14:38 +00:00
|
|
|
mutex_unlock(&llist_mod_mutex);
|
2007-11-12 15:16:41 +00:00
|
|
|
if (rc <= 0)
|
|
|
|
{
|
|
|
|
rm_handle(h);
|
|
|
|
close(fd);
|
2007-11-11 12:29:37 +00:00
|
|
|
return ERR_FILE_ERROR;
|
2007-11-12 15:16:41 +00:00
|
|
|
}
|
2007-11-11 12:29:37 +00:00
|
|
|
h->filerem = 0;
|
2007-11-12 15:16:41 +00:00
|
|
|
h->filesize = rc;
|
|
|
|
h->available = rc;
|
|
|
|
h->widx = buf_widx + rc; /* safe because the data doesn't wrap */
|
|
|
|
buf_widx += rc; /* safe too */
|
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
|
|
|
{
|
2008-03-28 11:24:24 +00:00
|
|
|
h->filerem = size - adjusted_offset;
|
2007-11-12 15:16:41 +00:00
|
|
|
h->filesize = size;
|
|
|
|
h->available = 0;
|
|
|
|
h->widx = buf_widx;
|
|
|
|
}
|
2007-11-11 12:29:37 +00:00
|
|
|
|
|
|
|
if (type == TYPE_CUESHEET) {
|
2007-10-27 04:49:04 +00:00
|
|
|
h->fd = fd;
|
|
|
|
/* Immediately start buffering those */
|
2008-07-18 23:42:47 +00:00
|
|
|
LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", h->id);
|
2007-10-25 21:27:45 +00:00
|
|
|
queue_send(&buffering_queue, Q_BUFFER_HANDLE, h->id);
|
2007-10-27 04:49:04 +00:00
|
|
|
} else {
|
|
|
|
/* Other types will get buffered in the course of normal operations */
|
|
|
|
h->fd = -1;
|
|
|
|
close(fd);
|
2008-02-12 23:15:59 +00:00
|
|
|
|
|
|
|
/* Inform the buffering thread that we added a handle */
|
|
|
|
LOGFQUEUE("buffering > Q_HANDLE_ADDED %d", h->id);
|
|
|
|
queue_post(&buffering_queue, Q_HANDLE_ADDED, h->id);
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logf("bufopen: new hdl %d", h->id);
|
|
|
|
return h->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
2007-10-27 04:49:04 +00:00
|
|
|
struct memory_handle *h = add_handle(size, false, true);
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2007-10-27 04:49:04 +00:00
|
|
|
if (!h)
|
2007-10-29 14:15:59 +00:00
|
|
|
return ERR_BUFFER_FULL;
|
2007-10-25 21:27:45 +00:00
|
|
|
|
|
|
|
if (src) {
|
|
|
|
if (type == TYPE_ID3 && size == sizeof(struct mp3entry)) {
|
|
|
|
/* specially take care of struct mp3entry */
|
|
|
|
copy_mp3entry((struct mp3entry *)&buffer[buf_widx],
|
2008-03-26 08:57:25 +00:00
|
|
|
(const struct mp3entry *)src);
|
2007-10-25 21:27:45 +00:00
|
|
|
} else {
|
|
|
|
memcpy(&buffer[buf_widx], src, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
h->fd = -1;
|
|
|
|
*h->path = 0;
|
|
|
|
h->filesize = size;
|
|
|
|
h->filerem = 0;
|
|
|
|
h->offset = 0;
|
|
|
|
h->ridx = buf_widx;
|
|
|
|
h->widx = buf_widx + size; /* this is safe because the data doesn't wrap */
|
|
|
|
h->data = buf_widx;
|
|
|
|
h->available = size;
|
|
|
|
h->type = type;
|
|
|
|
|
|
|
|
buf_widx += size; /* safe too */
|
|
|
|
|
|
|
|
logf("bufalloc: new hdl %d", h->id);
|
|
|
|
return h->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set reading index in handle (relatively to the start of the file).
|
|
|
|
Access before the available data will trigger a rebuffer.
|
|
|
|
Return 0 for success and < 0 for failure:
|
|
|
|
-1 if the handle wasn't found
|
|
|
|
-2 if the new requested position was beyond the end of the file
|
|
|
|
*/
|
2008-03-28 12:51:33 +00:00
|
|
|
int bufseek(int handle_id, size_t newpos)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
|
|
|
struct memory_handle *h = find_handle(handle_id);
|
|
|
|
if (!h)
|
2007-10-29 14:15:59 +00:00
|
|
|
return ERR_HANDLE_NOT_FOUND;
|
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
|
|
|
}
|
|
|
|
else if (newpos < h->offset || h->offset + h->available < newpos) {
|
|
|
|
/* access before or after buffered data. A rebuffer is needed. */
|
|
|
|
rebuffer_handle(handle_id, newpos);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance the reading index in a handle (relatively to its current position).
|
|
|
|
Return 0 for success and < 0 for failure */
|
2008-03-28 12:51:33 +00:00
|
|
|
int bufadvance(int handle_id, off_t offset)
|
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
|
|
|
|
2010-02-12 13:12:59 +00:00
|
|
|
size_t newpos = h->offset + ringbuf_sub(h->ridx, h->data) + offset;
|
2007-10-25 21:27:45 +00:00
|
|
|
return bufseek(handle_id, newpos);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
if (!h)
|
|
|
|
return NULL;
|
|
|
|
|
2010-02-12 13:12:59 +00:00
|
|
|
size_t avail = ringbuf_sub(h->widx, h->ridx);
|
2007-10-25 21:27:45 +00:00
|
|
|
|
2007-11-02 19:13:03 +00:00
|
|
|
if (avail == 0 && h->filerem == 0)
|
2007-11-08 18:27:19 +00:00
|
|
|
{
|
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
|
|
|
|
2007-11-08 15:34:23 +00:00
|
|
|
if (*size == 0 || *size > avail + h->filerem)
|
|
|
|
*size = avail + h->filerem;
|
2007-11-06 16:49:30 +00:00
|
|
|
|
2007-11-08 15:52:10 +00:00
|
|
|
if (guardbuf_limit && h->type == TYPE_PACKET_AUDIO && *size > GUARD_BUFSIZE)
|
2007-11-06 16:49:30 +00:00
|
|
|
{
|
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 */
|
|
|
|
*size = MIN(*size, 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
|
|
|
}
|
|
|
|
|
2007-11-08 15:34:23 +00:00
|
|
|
if (h->filerem > 0 && avail < *size)
|
2007-11-02 19:13:03 +00:00
|
|
|
{
|
|
|
|
/* Data isn't ready. Request buffering */
|
2007-11-08 18:59:22 +00:00
|
|
|
buf_request_buffer_handle(handle_id);
|
2007-11-02 19:13:03 +00:00
|
|
|
/* Wait for the data to be ready */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
sleep(1);
|
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);
|
2007-11-08 16:06:24 +00:00
|
|
|
if (!h)
|
|
|
|
return NULL;
|
2010-02-12 13:12:59 +00:00
|
|
|
avail = ringbuf_sub(h->widx, h->ridx);
|
2007-11-02 19:13:03 +00:00
|
|
|
}
|
2007-11-08 15:34:23 +00:00
|
|
|
while (h->filerem > 0 && avail < *size);
|
2007-11-02 19:13:03 +00:00
|
|
|
}
|
|
|
|
|
2007-11-08 15:34:23 +00:00
|
|
|
*size = MIN(*size,avail);
|
|
|
|
return h;
|
2007-11-06 16:49:30 +00:00
|
|
|
}
|
2007-10-25 21:27:45 +00:00
|
|
|
|
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;
|
|
|
|
|
2008-03-28 11:24:24 +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);
|
2007-10-25 21:27:45 +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
|
|
|
|
2008-03-28 11:24:24 +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;
|
|
|
|
/* 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
|
|
|
|
|
|
|
if (tidx + size > buffer_len)
|
|
|
|
{
|
|
|
|
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_get_offset
|
|
|
|
buf_handle_offset
|
|
|
|
buf_request_buffer_handle
|
|
|
|
buf_set_base_handle
|
|
|
|
buf_used
|
2007-11-26 21:13:08 +00:00
|
|
|
register_buffering_callback
|
|
|
|
unregister_buffering_callback
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Get a handle offset from a pointer */
|
2008-03-28 12:51:33 +00:00
|
|
|
ssize_t buf_get_offset(int handle_id, void *ptr)
|
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 (size_t)ptr - (size_t)&buffer[h->ridx];
|
|
|
|
}
|
|
|
|
|
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_request_buffer_handle(int handle_id)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2007-11-05 17:51:55 +00:00
|
|
|
LOGFQUEUE("buffering >| Q_START_FILL %d",handle_id);
|
|
|
|
queue_send(&buffering_queue, Q_START_FILL, handle_id);
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|
|
|
|
|
2008-03-28 12:51:33 +00:00
|
|
|
void buf_set_base_handle(int handle_id)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2007-11-05 15:24:21 +00:00
|
|
|
LOGFQUEUE("buffering > Q_BASE_HANDLE %d", handle_id);
|
2007-10-25 21:27:45 +00:00
|
|
|
queue_post(&buffering_queue, Q_BASE_HANDLE, handle_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-10-25 21:27:45 +00:00
|
|
|
void buffering_thread(void)
|
|
|
|
{
|
2007-11-03 06:21:32 +00:00
|
|
|
bool filling = false;
|
2007-10-25 21:27:45 +00:00
|
|
|
struct queue_event ev;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2008-03-29 17:40:04 +00:00
|
|
|
if (!filling) {
|
2008-03-29 17:28:30 +00:00
|
|
|
cancel_cpu_boost();
|
2008-03-29 17:40:04 +00:00
|
|
|
}
|
2008-03-29 17:28:30 +00:00
|
|
|
|
|
|
|
queue_wait_w_tmo(&buffering_queue, &ev, filling ? 5 : HZ/2);
|
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-03 21:57:27 +00:00
|
|
|
/* Call buffer callbacks here because this is one of two ways
|
|
|
|
* to begin a full buffer fill */
|
2008-10-16 10:38:03 +00:00
|
|
|
send_event(BUFFER_EVENT_BUFFER_LOW, 0);
|
2007-11-05 15:24:21 +00:00
|
|
|
shrink_buffer();
|
2007-11-05 03:11:58 +00:00
|
|
|
queue_reply(&buffering_queue, 1);
|
2007-11-05 17:51:55 +00:00
|
|
|
filling |= buffer_handle((int)ev.data);
|
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);
|
2007-11-05 17:51:55 +00:00
|
|
|
buffer_handle((int)ev.data);
|
2007-10-25 21:27:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Q_RESET_HANDLE:
|
2008-07-18 23:42:47 +00:00
|
|
|
LOGFQUEUE("buffering < Q_RESET_HANDLE %d", (int)ev.data);
|
2007-10-25 21:27:45 +00:00
|
|
|
queue_reply(&buffering_queue, 1);
|
|
|
|
reset_handle((int)ev.data);
|
|
|
|
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 Q_BASE_HANDLE:
|
2008-07-18 23:42:47 +00:00
|
|
|
LOGFQUEUE("buffering < Q_BASE_HANDLE %d", (int)ev.data);
|
2007-10-25 21:27:45 +00:00
|
|
|
base_handle_id = (int)ev.data;
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
case SYS_USB_CONNECTED:
|
|
|
|
LOGFQUEUE("buffering < SYS_USB_CONNECTED");
|
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
usb_wait_for_disconnect(&buffering_queue);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case SYS_TIMEOUT:
|
|
|
|
LOGFQUEUE_SYS_TIMEOUT("buffering < SYS_TIMEOUT");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
update_data_counters();
|
|
|
|
|
|
|
|
/* If the buffer is low, call the callbacks to get new data */
|
2007-11-03 03:46:22 +00:00
|
|
|
if (num_handles > 0 && data_counters.useful <= conf_watermark)
|
2008-10-16 10:38:03 +00:00
|
|
|
send_event(BUFFER_EVENT_BUFFER_LOW, 0);
|
2007-10-25 21:27:45 +00:00
|
|
|
|
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 */
|
2007-10-25 21:27:45 +00:00
|
|
|
#if MEM > 8
|
|
|
|
/* If the disk is spinning, take advantage by filling the buffer */
|
2008-11-01 16:14:28 +00:00
|
|
|
else if (storage_disk_is_active() && queue_empty(&buffering_queue))
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
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
|
|
|
|
|
|
|
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();
|
|
|
|
update_data_counters();
|
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
|
|
|
|
|
2007-11-03 17:55:45 +00:00
|
|
|
if (queue_empty(&buffering_queue)) {
|
|
|
|
if (filling) {
|
|
|
|
if (data_counters.remaining > 0 && BUF_USED < buffer_len)
|
|
|
|
filling = fill_buffer();
|
2008-07-02 21:50:42 +00:00
|
|
|
else if (data_counters.remaining == 0)
|
|
|
|
filling = false;
|
2007-11-03 17:55:45 +00:00
|
|
|
}
|
|
|
|
else if (ev.id == SYS_TIMEOUT)
|
2007-10-25 21:27:45 +00:00
|
|
|
{
|
2007-11-03 17:55:45 +00:00
|
|
|
if (data_counters.remaining > 0 &&
|
2007-11-05 03:11:58 +00:00
|
|
|
data_counters.useful <= conf_watermark) {
|
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);
|
2009-05-15 00:14:38 +00:00
|
|
|
mutex_init(&llist_mod_mutex);
|
2008-03-25 02:34:12 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
/* This behavior not safe atm */
|
|
|
|
mutex_set_preempt(&llist_mutex, false);
|
2009-05-15 00:14:38 +00:00
|
|
|
mutex_set_preempt(&llist_mod_mutex, false);
|
2008-03-25 02:34:12 +00:00
|
|
|
#endif
|
2007-10-27 04:16:41 +00:00
|
|
|
|
|
|
|
conf_watermark = BUFFERING_DEFAULT_WATERMARK;
|
|
|
|
|
2007-10-27 04:19:17 +00:00
|
|
|
queue_init(&buffering_queue, true);
|
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
|
|
|
{
|
|
|
|
if (!buf || !buflen)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
buffer = buf;
|
2010-02-01 17:16:39 +00:00
|
|
|
/* Preserve alignment when wrapping around */
|
|
|
|
buffer_len = buflen & ~STORAGE_ALIGN_MASK;
|
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
|
|
|
|
|
|
|
/* Set the high watermark as 75% full...or 25% empty :) */
|
|
|
|
#if MEM > 8
|
|
|
|
high_watermark = 3*buflen / 4;
|
|
|
|
#endif
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
update_data_counters();
|
|
|
|
dbgdata->num_handles = num_handles;
|
|
|
|
dbgdata->data_rem = data_counters.remaining;
|
|
|
|
dbgdata->wasted_space = data_counters.wasted;
|
|
|
|
dbgdata->buffered_data = data_counters.buffered;
|
|
|
|
dbgdata->useful_data = data_counters.useful;
|
2009-02-23 22:55:48 +00:00
|
|
|
dbgdata->watermark = conf_watermark;
|
2007-10-25 21:27:45 +00:00
|
|
|
}
|