Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2007 Nicolas Pennequin |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
| 22 | #include "config.h" |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <ctype.h> |
| 27 | #include "buffering.h" |
| 28 | |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 29 | #include "storage.h" |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 30 | #include "system.h" |
| 31 | #include "thread.h" |
| 32 | #include "file.h" |
| 33 | #include "panic.h" |
| 34 | #include "memory.h" |
| 35 | #include "lcd.h" |
| 36 | #include "font.h" |
| 37 | #include "button.h" |
| 38 | #include "kernel.h" |
| 39 | #include "tree.h" |
| 40 | #include "debug.h" |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 41 | #include "settings.h" |
| 42 | #include "codecs.h" |
| 43 | #include "audio.h" |
| 44 | #include "mp3_playback.h" |
| 45 | #include "usb.h" |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 46 | #include "screens.h" |
| 47 | #include "playlist.h" |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 48 | #include "pcmbuf.h" |
Jonathan Gordon | 71898e5 | 2008-10-16 10:38:03 +0000 | [diff] [blame] | 49 | #include "appevents.h" |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 50 | #include "metadata.h" |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 51 | #ifdef HAVE_ALBUMART |
| 52 | #include "albumart.h" |
Andrew Mahone | 54e6eb3 | 2009-05-01 23:31:43 +0000 | [diff] [blame] | 53 | #include "jpeg_load.h" |
Thomas Martitz | e9c1018 | 2009-10-16 19:14:41 +0000 | [diff] [blame] | 54 | #include "bmp.h" |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 55 | #endif |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 56 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 57 | #define GUARD_BUFSIZE (32*1024) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 58 | |
| 59 | /* Define LOGF_ENABLE to enable logf output in this file */ |
Nicolas Pennequin | 86b7c2b | 2007-11-05 23:46:45 +0000 | [diff] [blame] | 60 | /*#define LOGF_ENABLE*/ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 61 | #include "logf.h" |
| 62 | |
| 63 | /* macros to enable logf for queues |
| 64 | logging on SYS_TIMEOUT can be disabled */ |
| 65 | #ifdef SIMULATOR |
| 66 | /* Define this for logf output of all queuing except SYS_TIMEOUT */ |
| 67 | #define BUFFERING_LOGQUEUES |
| 68 | /* Define this to logf SYS_TIMEOUT messages */ |
| 69 | /* #define BUFFERING_LOGQUEUES_SYS_TIMEOUT */ |
| 70 | #endif |
| 71 | |
| 72 | #ifdef BUFFERING_LOGQUEUES |
| 73 | #define LOGFQUEUE logf |
| 74 | #else |
| 75 | #define LOGFQUEUE(...) |
| 76 | #endif |
| 77 | |
| 78 | #ifdef BUFFERING_LOGQUEUES_SYS_TIMEOUT |
| 79 | #define LOGFQUEUE_SYS_TIMEOUT logf |
| 80 | #else |
| 81 | #define LOGFQUEUE_SYS_TIMEOUT(...) |
| 82 | #endif |
| 83 | |
| 84 | /* default point to start buffer refill */ |
Björn Stenberg | 6427d12 | 2009-01-10 21:10:56 +0000 | [diff] [blame] | 85 | #define BUFFERING_DEFAULT_WATERMARK (1024*128) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 86 | /* amount of data to read in one read() call */ |
Michael Sevakis | 9120c85 | 2008-03-29 20:52:56 +0000 | [diff] [blame] | 87 | #define BUFFERING_DEFAULT_FILECHUNK (1024*32) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 88 | |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 89 | #define BUF_HANDLE_MASK 0x7FFFFFFF |
| 90 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 91 | |
Brandon Low | 404c6fb | 2007-10-27 01:37:33 +0000 | [diff] [blame] | 92 | /* assert(sizeof(struct memory_handle)%4==0) */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 93 | struct memory_handle { |
Brandon Low | 9784f6b | 2007-11-03 22:06:56 +0000 | [diff] [blame] | 94 | int id; /* A unique ID for the handle */ |
Brandon Low | 404c6fb | 2007-10-27 01:37:33 +0000 | [diff] [blame] | 95 | enum data_type type; /* Type of data buffered with this handle */ |
| 96 | char path[MAX_PATH]; /* Path if data originated in a file */ |
| 97 | int fd; /* File descriptor to path (-1 if closed) */ |
Torne Wuff | c4e051b | 2010-02-01 17:16:39 +0000 | [diff] [blame] | 98 | size_t start; /* Start index of the handle's data buffer, |
| 99 | for use by reset_handle. */ |
| 100 | size_t data; /* Start index of the handle's data */ |
Brandon Low | 404c6fb | 2007-10-27 01:37:33 +0000 | [diff] [blame] | 101 | volatile size_t ridx; /* Read pointer, relative to the main buffer */ |
| 102 | size_t widx; /* Write pointer */ |
| 103 | size_t filesize; /* File total length */ |
| 104 | size_t filerem; /* Remaining bytes of file NOT in buffer */ |
| 105 | volatile size_t available; /* Available bytes to read from buffer */ |
| 106 | size_t offset; /* Offset at which we started reading the file */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 107 | struct memory_handle *next; |
| 108 | }; |
Brandon Low | 404c6fb | 2007-10-27 01:37:33 +0000 | [diff] [blame] | 109 | /* invariant: filesize == offset + available + filerem */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 110 | |
| 111 | static char *buffer; |
| 112 | static char *guard_buffer; |
| 113 | |
| 114 | static size_t buffer_len; |
| 115 | |
| 116 | static volatile size_t buf_widx; /* current writing position */ |
| 117 | static volatile size_t buf_ridx; /* current reading position */ |
| 118 | /* buf_*idx are values relative to the buffer, not real pointers. */ |
| 119 | |
| 120 | /* Configuration */ |
| 121 | static size_t conf_watermark = 0; /* Level to trigger filebuf fill */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 122 | #if MEM > 8 |
| 123 | static size_t high_watermark = 0; /* High watermark for rebuffer */ |
| 124 | #endif |
| 125 | |
| 126 | /* current memory handle in the linked list. NULL when the list is empty. */ |
| 127 | static struct memory_handle *cur_handle; |
| 128 | /* first memory handle in the linked list. NULL when the list is empty. */ |
| 129 | static struct memory_handle *first_handle; |
| 130 | |
| 131 | static int num_handles; /* number of handles in the list */ |
| 132 | |
Brandon Low | 9784f6b | 2007-11-03 22:06:56 +0000 | [diff] [blame] | 133 | static int base_handle_id; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 134 | |
| 135 | static struct mutex llist_mutex; |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 136 | static struct mutex llist_mod_mutex; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 137 | |
| 138 | /* Handle cache (makes find_handle faster). |
Brandon Low | 404c6fb | 2007-10-27 01:37:33 +0000 | [diff] [blame] | 139 | This is global so that move_handle and rm_handle can invalidate it. */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 140 | static struct memory_handle *cached_handle = NULL; |
| 141 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 142 | static struct { |
| 143 | size_t remaining; /* Amount of data needing to be buffered */ |
| 144 | size_t wasted; /* Amount of space available for freeing */ |
| 145 | size_t buffered; /* Amount of data currently in the buffer */ |
| 146 | size_t useful; /* Amount of data still useful to the user */ |
| 147 | } data_counters; |
| 148 | |
| 149 | |
| 150 | /* Messages available to communicate with the buffering thread */ |
| 151 | enum { |
Brandon Low | 3379440 | 2007-11-05 17:48:21 +0000 | [diff] [blame] | 152 | Q_BUFFER_HANDLE = 1, /* Request buffering of a handle, this should not be |
| 153 | used in a low buffer situation. */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 154 | Q_RESET_HANDLE, /* (internal) Request resetting of a handle to its |
| 155 | offset (the offset has to be set beforehand) */ |
| 156 | Q_CLOSE_HANDLE, /* Request closing a handle */ |
| 157 | Q_BASE_HANDLE, /* Set the reference handle for buf_useful_data */ |
| 158 | |
| 159 | /* Configuration: */ |
Brandon Low | 86830b6 | 2007-11-05 17:51:55 +0000 | [diff] [blame] | 160 | Q_START_FILL, /* Request that the buffering thread initiate a buffer |
Brandon Low | 47eb569 | 2007-11-05 03:11:58 +0000 | [diff] [blame] | 161 | fill at its earliest convenience */ |
Nicolas Pennequin | 483c402 | 2008-02-12 23:15:59 +0000 | [diff] [blame] | 162 | Q_HANDLE_ADDED, /* Inform the buffering thread that a handle was added, |
| 163 | (which means the disk is spinning) */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | /* Buffering thread */ |
Steve Bavin | 73f9863 | 2008-03-26 08:57:25 +0000 | [diff] [blame] | 167 | static void buffering_thread(void); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 168 | static long buffering_stack[(DEFAULT_STACK_SIZE + 0x2000)/sizeof(long)]; |
| 169 | static const char buffering_thread_name[] = "buffering"; |
Michael Sevakis | 8cfbd36 | 2008-12-10 08:57:10 +0000 | [diff] [blame] | 170 | static unsigned int buffering_thread_id = 0; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 171 | static struct event_queue buffering_queue; |
| 172 | static struct queue_sender_list buffering_queue_sender_list; |
| 173 | |
| 174 | |
Nicolas Pennequin | e24454f | 2007-11-26 21:13:08 +0000 | [diff] [blame] | 175 | |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 176 | /* Ring buffer helper functions */ |
Thomas Martitz | abb3dd4 | 2010-02-20 15:13:53 +0000 | [diff] [blame] | 177 | |
| 178 | static inline uintptr_t ringbuf_offset(const void *ptr) |
| 179 | { |
| 180 | return (uintptr_t)(ptr - (void*)buffer); |
| 181 | } |
| 182 | |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 183 | /* Buffer pointer (p) plus value (v), wrapped if necessary */ |
| 184 | static inline uintptr_t ringbuf_add(uintptr_t p, size_t v) |
| 185 | { |
| 186 | uintptr_t res = p + v; |
| 187 | if (res >= buffer_len) |
| 188 | res -= buffer_len; /* wrap if necssary */ |
| 189 | return res; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /* Buffer pointer (p) minus value (v), wrapped if necessary */ |
| 194 | static inline uintptr_t ringbuf_sub(uintptr_t p, size_t v) |
| 195 | { |
| 196 | uintptr_t res = p; |
| 197 | if (p < v) |
| 198 | res += buffer_len; /* wrap */ |
| 199 | |
| 200 | return res - v; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /* How far value (v) plus buffer pointer (p1) will cross buffer pointer (p2) */ |
| 205 | static inline ssize_t ringbuf_add_cross(uintptr_t p1, size_t v, uintptr_t p2) |
| 206 | { |
| 207 | ssize_t res = p1 + v - p2; |
| 208 | if (p1 >= p2) /* wrap if necessary */ |
| 209 | res -= buffer_len; |
| 210 | |
| 211 | return res; |
| 212 | } |
| 213 | |
| 214 | /* Bytes available in the buffer */ |
| 215 | #define BUF_USED ringbuf_sub(buf_widx, buf_ridx) |
| 216 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 217 | /* |
| 218 | LINKED LIST MANAGEMENT |
| 219 | ====================== |
| 220 | |
| 221 | add_handle : Add a handle to the list |
| 222 | rm_handle : Remove a handle from the list |
| 223 | find_handle : Get a handle pointer from an ID |
| 224 | move_handle : Move a handle in the buffer (with or without its data) |
| 225 | |
| 226 | These functions only handle the linked list structure. They don't touch the |
| 227 | contents of the struct memory_handle headers. They also change the buf_*idx |
| 228 | pointers when necessary and manage the handle IDs. |
| 229 | |
| 230 | The first and current (== last) handle are kept track of. |
| 231 | A new handle is added at buf_widx and becomes the current one. |
| 232 | buf_widx always points to the current writing position for the current handle |
| 233 | buf_ridx always points to the location of the first handle. |
| 234 | buf_ridx == buf_widx means the buffer is empty. |
| 235 | */ |
| 236 | |
| 237 | |
| 238 | /* Add a new handle to the linked list and return it. It will have become the |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 239 | new current handle. |
| 240 | data_size must contain the size of what will be in the handle. |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 241 | can_wrap tells us whether this type of data may wrap on buffer |
| 242 | alloc_all tells us if we must immediately be able to allocate data_size |
Brandon Low | 94b133a | 2007-10-28 19:19:54 +0000 | [diff] [blame] | 243 | returns a valid memory handle if all conditions for allocation are met. |
| 244 | NULL if there memory_handle itself cannot be allocated or if the |
Brandon Low | 151b7c9 | 2007-10-30 15:09:52 +0000 | [diff] [blame] | 245 | data_size cannot be allocated and alloc_all is set. This function's |
| 246 | only potential side effect is to allocate space for the cur_handle |
| 247 | if it returns NULL. |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 248 | */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 249 | static struct memory_handle *add_handle(size_t data_size, bool can_wrap, |
| 250 | bool alloc_all) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 251 | { |
Brandon Low | 9784f6b | 2007-11-03 22:06:56 +0000 | [diff] [blame] | 252 | /* gives each handle a unique id */ |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 253 | static int cur_handle_id = 0; |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 254 | size_t shift; |
Brandon Low | 151b7c9 | 2007-10-30 15:09:52 +0000 | [diff] [blame] | 255 | size_t new_widx; |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 256 | size_t len; |
| 257 | int overlap; |
| 258 | |
Brandon Low | a042c72 | 2007-11-03 02:54:34 +0000 | [diff] [blame] | 259 | if (num_handles >= BUF_MAX_HANDLES) |
| 260 | return NULL; |
| 261 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 262 | mutex_lock(&llist_mutex); |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 263 | mutex_lock(&llist_mod_mutex); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 264 | |
Nicolas Pennequin | 4ff2f9f | 2007-10-30 14:11:03 +0000 | [diff] [blame] | 265 | if (cur_handle && cur_handle->filerem > 0) { |
| 266 | /* the current handle hasn't finished buffering. We can only add |
| 267 | a new one if there is already enough free space to finish |
| 268 | the buffering. */ |
| 269 | size_t req = cur_handle->filerem + sizeof(struct memory_handle); |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 270 | if (ringbuf_add_cross(cur_handle->widx, req, buf_ridx) >= 0) { |
Nicolas Pennequin | 4ff2f9f | 2007-10-30 14:11:03 +0000 | [diff] [blame] | 271 | /* Not enough space */ |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 272 | mutex_unlock(&llist_mod_mutex); |
Nicolas Pennequin | 4ff2f9f | 2007-10-30 14:11:03 +0000 | [diff] [blame] | 273 | mutex_unlock(&llist_mutex); |
| 274 | return NULL; |
| 275 | } else { |
| 276 | /* Allocate the remainder of the space for the current handle */ |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 277 | buf_widx = ringbuf_add(cur_handle->widx, cur_handle->filerem); |
Nicolas Pennequin | 4ff2f9f | 2007-10-30 14:11:03 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 280 | |
Brandon Low | 151b7c9 | 2007-10-30 15:09:52 +0000 | [diff] [blame] | 281 | /* align to 4 bytes up */ |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 282 | new_widx = ringbuf_add(buf_widx, 3) & ~3; |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 283 | |
| 284 | len = data_size + sizeof(struct memory_handle); |
| 285 | |
| 286 | /* First, will the handle wrap? */ |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 287 | /* If the handle would wrap, move to the beginning of the buffer, |
Antonius Hellmann | 0055f13 | 2009-02-22 10:12:34 +0000 | [diff] [blame] | 288 | * or if the data must not but would wrap, move it to the beginning */ |
| 289 | if( (new_widx + sizeof(struct memory_handle) > buffer_len) || |
| 290 | (!can_wrap && (new_widx + len > buffer_len)) ) { |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 291 | new_widx = 0; |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Brandon Low | 151b7c9 | 2007-10-30 15:09:52 +0000 | [diff] [blame] | 294 | /* How far we shifted buf_widx to align things, must be < buffer_len */ |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 295 | shift = ringbuf_sub(new_widx, buf_widx); |
Nicolas Pennequin | 7807279 | 2007-10-28 15:54:10 +0000 | [diff] [blame] | 296 | |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 297 | /* How much space are we short in the actual ring buffer? */ |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 298 | overlap = ringbuf_add_cross(buf_widx, shift + len, buf_ridx); |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 299 | if (overlap >= 0 && (alloc_all || (unsigned)overlap > data_size)) { |
| 300 | /* Not enough space for required allocations */ |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 301 | mutex_unlock(&llist_mod_mutex); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 302 | mutex_unlock(&llist_mutex); |
| 303 | return NULL; |
| 304 | } |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 305 | |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 306 | /* There is enough space for the required data, advance the buf_widx and |
| 307 | * initialize the struct */ |
| 308 | buf_widx = new_widx; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 309 | |
| 310 | struct memory_handle *new_handle = |
| 311 | (struct memory_handle *)(&buffer[buf_widx]); |
| 312 | |
| 313 | /* only advance the buffer write index of the size of the struct */ |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 314 | buf_widx = ringbuf_add(buf_widx, sizeof(struct memory_handle)); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 315 | |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 316 | new_handle->id = cur_handle_id; |
Brandon Low | 9784f6b | 2007-11-03 22:06:56 +0000 | [diff] [blame] | 317 | /* Wrap signed int is safe and 0 doesn't happen */ |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 318 | cur_handle_id = (cur_handle_id + 1) & BUF_HANDLE_MASK; |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 319 | new_handle->next = NULL; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 320 | num_handles++; |
| 321 | |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 322 | if (!first_handle) |
| 323 | /* the new handle is the first one */ |
| 324 | first_handle = new_handle; |
| 325 | |
| 326 | if (cur_handle) |
| 327 | cur_handle->next = new_handle; |
| 328 | |
| 329 | cur_handle = new_handle; |
| 330 | |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 331 | mutex_unlock(&llist_mod_mutex); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 332 | mutex_unlock(&llist_mutex); |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 333 | return new_handle; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* Delete a given memory handle from the linked list |
| 337 | and return true for success. Nothing is actually erased from memory. */ |
Brandon Low | 404c6fb | 2007-10-27 01:37:33 +0000 | [diff] [blame] | 338 | static bool rm_handle(const struct memory_handle *h) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 339 | { |
Brandon Low | 404c6fb | 2007-10-27 01:37:33 +0000 | [diff] [blame] | 340 | if (h == NULL) |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 341 | return true; |
Brandon Low | 404c6fb | 2007-10-27 01:37:33 +0000 | [diff] [blame] | 342 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 343 | mutex_lock(&llist_mutex); |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 344 | mutex_lock(&llist_mod_mutex); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 345 | |
| 346 | if (h == first_handle) { |
| 347 | first_handle = h->next; |
| 348 | if (h == cur_handle) { |
| 349 | /* h was the first and last handle: the buffer is now empty */ |
| 350 | cur_handle = NULL; |
Brandon Low | 404c6fb | 2007-10-27 01:37:33 +0000 | [diff] [blame] | 351 | buf_ridx = buf_widx = 0; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 352 | } else { |
| 353 | /* update buf_ridx to point to the new first handle */ |
Thomas Martitz | abb3dd4 | 2010-02-20 15:13:53 +0000 | [diff] [blame] | 354 | buf_ridx = (size_t)ringbuf_offset(first_handle); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 355 | } |
| 356 | } else { |
| 357 | struct memory_handle *m = first_handle; |
Nicolas Pennequin | e24454f | 2007-11-26 21:13:08 +0000 | [diff] [blame] | 358 | /* Find the previous handle */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 359 | while (m && m->next != h) { |
| 360 | m = m->next; |
| 361 | } |
Brandon Low | 404c6fb | 2007-10-27 01:37:33 +0000 | [diff] [blame] | 362 | if (m && m->next == h) { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 363 | m->next = h->next; |
| 364 | if (h == cur_handle) { |
| 365 | cur_handle = m; |
| 366 | buf_widx = cur_handle->widx; |
| 367 | } |
| 368 | } else { |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 369 | mutex_unlock(&llist_mod_mutex); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 370 | mutex_unlock(&llist_mutex); |
| 371 | return false; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /* Invalidate the cache to prevent it from keeping the old location of h */ |
| 376 | if (h == cached_handle) |
| 377 | cached_handle = NULL; |
| 378 | |
| 379 | num_handles--; |
| 380 | |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 381 | mutex_unlock(&llist_mod_mutex); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 382 | mutex_unlock(&llist_mutex); |
| 383 | return true; |
| 384 | } |
| 385 | |
| 386 | /* Return a pointer to the memory handle of given ID. |
| 387 | NULL if the handle wasn't found */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 388 | static struct memory_handle *find_handle(int handle_id) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 389 | { |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 390 | if (handle_id < 0) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 391 | return NULL; |
| 392 | |
| 393 | mutex_lock(&llist_mutex); |
| 394 | |
| 395 | /* simple caching because most of the time the requested handle |
| 396 | will either be the same as the last, or the one after the last */ |
| 397 | if (cached_handle) |
| 398 | { |
| 399 | if (cached_handle->id == handle_id) { |
| 400 | mutex_unlock(&llist_mutex); |
| 401 | return cached_handle; |
| 402 | } else if (cached_handle->next && |
| 403 | (cached_handle->next->id == handle_id)) { |
| 404 | cached_handle = cached_handle->next; |
| 405 | mutex_unlock(&llist_mutex); |
| 406 | return cached_handle; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | struct memory_handle *m = first_handle; |
| 411 | while (m && m->id != handle_id) { |
| 412 | m = m->next; |
| 413 | } |
| 414 | /* This condition can only be reached with !m or m->id == handle_id */ |
Brandon Low | 404c6fb | 2007-10-27 01:37:33 +0000 | [diff] [blame] | 415 | if (m) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 416 | cached_handle = m; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 417 | |
| 418 | mutex_unlock(&llist_mutex); |
| 419 | return m; |
| 420 | } |
| 421 | |
Brandon Low | 4feab10 | 2007-10-28 20:18:59 +0000 | [diff] [blame] | 422 | /* Move a memory handle and data_size of its data delta bytes along the buffer. |
| 423 | delta maximum bytes available to move the handle. If the move is performed |
| 424 | it is set to the actual distance moved. |
| 425 | data_size is the amount of data to move along with the struct. |
Yoshihisa Uchida | 9c13b6e | 2010-05-24 10:49:36 +0000 | [diff] [blame] | 426 | returns true if the move is successful and false if the handle is NULL, |
| 427 | the move would be less than the size of a memory_handle after |
| 428 | correcting for wraps or if the handle is not found in the linked |
| 429 | list for adjustment. This function has no side effects if false |
| 430 | is returned. */ |
Brandon Low | dcca586 | 2007-11-02 14:06:48 +0000 | [diff] [blame] | 431 | static bool move_handle(struct memory_handle **h, size_t *delta, |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 432 | size_t data_size, bool can_wrap) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 433 | { |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 434 | struct memory_handle *dest; |
Brandon Low | 483dca9 | 2007-10-29 16:48:16 +0000 | [diff] [blame] | 435 | const struct memory_handle *src; |
Michael Giacomelli | ecd9bcf | 2009-11-21 17:00:38 +0000 | [diff] [blame] | 436 | int32_t *here; |
| 437 | int32_t *there; |
| 438 | int32_t *end; |
| 439 | int32_t *begin; |
Thomas Martitz | abb3dd4 | 2010-02-20 15:13:53 +0000 | [diff] [blame] | 440 | size_t final_delta = *delta, size_to_move, n; |
| 441 | uintptr_t oldpos, newpos; |
| 442 | intptr_t overlap, overlap_old; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 443 | |
Brandon Low | 483dca9 | 2007-10-29 16:48:16 +0000 | [diff] [blame] | 444 | if (h == NULL || (src = *h) == NULL) |
| 445 | return false; |
Brandon Low | 4feab10 | 2007-10-28 20:18:59 +0000 | [diff] [blame] | 446 | |
| 447 | size_to_move = sizeof(struct memory_handle) + data_size; |
| 448 | |
| 449 | /* Align to four bytes, down */ |
Brandon Low | 483dca9 | 2007-10-29 16:48:16 +0000 | [diff] [blame] | 450 | final_delta &= ~3; |
| 451 | if (final_delta < sizeof(struct memory_handle)) { |
Brandon Low | 4feab10 | 2007-10-28 20:18:59 +0000 | [diff] [blame] | 452 | /* It's not legal to move less than the size of the struct */ |
Brandon Low | 483dca9 | 2007-10-29 16:48:16 +0000 | [diff] [blame] | 453 | return false; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 454 | } |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 455 | |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 456 | mutex_lock(&llist_mutex); |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 457 | mutex_lock(&llist_mod_mutex); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 458 | |
Thomas Martitz | abb3dd4 | 2010-02-20 15:13:53 +0000 | [diff] [blame] | 459 | oldpos = ringbuf_offset(src); |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 460 | newpos = ringbuf_add(oldpos, final_delta); |
| 461 | overlap = ringbuf_add_cross(newpos, size_to_move, buffer_len - 1); |
| 462 | overlap_old = ringbuf_add_cross(oldpos, size_to_move, buffer_len -1); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 463 | |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 464 | if (overlap > 0) { |
Brandon Low | 4feab10 | 2007-10-28 20:18:59 +0000 | [diff] [blame] | 465 | /* Some part of the struct + data would wrap, maybe ok */ |
Brandon Low | dcca586 | 2007-11-02 14:06:48 +0000 | [diff] [blame] | 466 | size_t correction = 0; |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 467 | /* If the overlap lands inside the memory_handle */ |
Michael Giacomelli | ecd9bcf | 2009-11-21 17:00:38 +0000 | [diff] [blame] | 468 | if (!can_wrap) { |
Brandon Low | 4feab10 | 2007-10-28 20:18:59 +0000 | [diff] [blame] | 469 | /* Otherwise the overlap falls in the data area and must all be |
| 470 | * backed out. This may become conditional if ever we move |
| 471 | * data that is allowed to wrap (ie audio) */ |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 472 | correction = overlap; |
Thomas Martitz | abb3dd4 | 2010-02-20 15:13:53 +0000 | [diff] [blame] | 473 | } else if ((uintptr_t)overlap > data_size) { |
Michael Giacomelli | ecd9bcf | 2009-11-21 17:00:38 +0000 | [diff] [blame] | 474 | /* Correct the position and real delta to prevent the struct from |
| 475 | * wrapping, this guarantees an aligned delta, I think */ |
| 476 | correction = overlap - data_size; |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 477 | } |
Brandon Low | dcca586 | 2007-11-02 14:06:48 +0000 | [diff] [blame] | 478 | if (correction) { |
Michael Giacomelli | ecd9bcf | 2009-11-21 17:00:38 +0000 | [diff] [blame] | 479 | /* Align correction to four bytes up */ |
Rafaël Carré | f620103 | 2009-11-22 13:51:25 +0000 | [diff] [blame] | 480 | correction = (correction + 3) & ~3; |
Brandon Low | dcca586 | 2007-11-02 14:06:48 +0000 | [diff] [blame] | 481 | if (final_delta < correction + sizeof(struct memory_handle)) { |
| 482 | /* Delta cannot end up less than the size of the struct */ |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 483 | mutex_unlock(&llist_mod_mutex); |
Brandon Low | dcca586 | 2007-11-02 14:06:48 +0000 | [diff] [blame] | 484 | mutex_unlock(&llist_mutex); |
| 485 | return false; |
| 486 | } |
Brandon Low | dcca586 | 2007-11-02 14:06:48 +0000 | [diff] [blame] | 487 | newpos -= correction; |
| 488 | overlap -= correction;/* Used below to know how to split the data */ |
| 489 | final_delta -= correction; |
| 490 | } |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 491 | } |
Nicolas Pennequin | 7807279 | 2007-10-28 15:54:10 +0000 | [diff] [blame] | 492 | |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 493 | dest = (struct memory_handle *)(&buffer[newpos]); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 494 | |
Brandon Low | 483dca9 | 2007-10-29 16:48:16 +0000 | [diff] [blame] | 495 | if (src == first_handle) { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 496 | first_handle = dest; |
| 497 | buf_ridx = newpos; |
| 498 | } else { |
| 499 | struct memory_handle *m = first_handle; |
Brandon Low | 483dca9 | 2007-10-29 16:48:16 +0000 | [diff] [blame] | 500 | while (m && m->next != src) { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 501 | m = m->next; |
| 502 | } |
Brandon Low | 483dca9 | 2007-10-29 16:48:16 +0000 | [diff] [blame] | 503 | if (m && m->next == src) { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 504 | m->next = dest; |
| 505 | } else { |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 506 | mutex_unlock(&llist_mod_mutex); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 507 | mutex_unlock(&llist_mutex); |
Brandon Low | 483dca9 | 2007-10-29 16:48:16 +0000 | [diff] [blame] | 508 | return false; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
Brandon Low | 4feab10 | 2007-10-28 20:18:59 +0000 | [diff] [blame] | 512 | |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 513 | /* Update the cache to prevent it from keeping the old location of h */ |
Brandon Low | 483dca9 | 2007-10-29 16:48:16 +0000 | [diff] [blame] | 514 | if (src == cached_handle) |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 515 | cached_handle = dest; |
| 516 | |
| 517 | /* the cur_handle pointer might need updating */ |
Brandon Low | 483dca9 | 2007-10-29 16:48:16 +0000 | [diff] [blame] | 518 | if (src == cur_handle) |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 519 | cur_handle = dest; |
| 520 | |
Michael Giacomelli | ecd9bcf | 2009-11-21 17:00:38 +0000 | [diff] [blame] | 521 | |
Rafaël Carré | f620103 | 2009-11-22 13:51:25 +0000 | [diff] [blame] | 522 | /* Copying routine takes into account that the handles have a |
Michael Giacomelli | ecd9bcf | 2009-11-21 17:00:38 +0000 | [diff] [blame] | 523 | * distance between each other which is a multiple of four. Faster 2 word |
| 524 | * copy may be ok but do this for safety and because wrapped copies should |
Rafaël Carré | f620103 | 2009-11-22 13:51:25 +0000 | [diff] [blame] | 525 | * be fairly uncommon */ |
Michael Giacomelli | ecd9bcf | 2009-11-21 17:00:38 +0000 | [diff] [blame] | 526 | |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 527 | here = (int32_t *)((ringbuf_add(oldpos, size_to_move - 1) & ~3)+ (intptr_t)buffer); |
| 528 | there =(int32_t *)((ringbuf_add(newpos, size_to_move - 1) & ~3)+ (intptr_t)buffer); |
Michael Giacomelli | ecd9bcf | 2009-11-21 17:00:38 +0000 | [diff] [blame] | 529 | end = (int32_t *)(( intptr_t)buffer + buffer_len - 4); |
| 530 | begin =(int32_t *)buffer; |
Rafaël Carré | f620103 | 2009-11-22 13:51:25 +0000 | [diff] [blame] | 531 | |
Michael Giacomelli | ecd9bcf | 2009-11-21 17:00:38 +0000 | [diff] [blame] | 532 | n = (size_to_move & ~3)/4; |
| 533 | |
Rafaël Carré | f620103 | 2009-11-22 13:51:25 +0000 | [diff] [blame] | 534 | if ( overlap_old > 0 || overlap > 0 ) { |
| 535 | /* Old or moved handle wraps */ |
| 536 | while (n--) { |
| 537 | if (here < begin) |
| 538 | here = end; |
| 539 | if (there < begin) |
| 540 | there = end; |
| 541 | *there-- = *here--; |
| 542 | } |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 543 | } else { |
Michael Giacomelli | ecd9bcf | 2009-11-21 17:00:38 +0000 | [diff] [blame] | 544 | /* both handles do not wrap */ |
| 545 | memmove(dest,src,size_to_move); |
Rafaël Carré | f620103 | 2009-11-22 13:51:25 +0000 | [diff] [blame] | 546 | } |
Michael Giacomelli | ecd9bcf | 2009-11-21 17:00:38 +0000 | [diff] [blame] | 547 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 548 | |
Brandon Low | 483dca9 | 2007-10-29 16:48:16 +0000 | [diff] [blame] | 549 | /* Update the caller with the new location of h and the distance moved */ |
| 550 | *h = dest; |
| 551 | *delta = final_delta; |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 552 | mutex_unlock(&llist_mod_mutex); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 553 | mutex_unlock(&llist_mutex); |
Yoshihisa Uchida | 9c13b6e | 2010-05-24 10:49:36 +0000 | [diff] [blame] | 554 | return true; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | |
| 558 | /* |
| 559 | BUFFER SPACE MANAGEMENT |
| 560 | ======================= |
| 561 | |
Nicolas Pennequin | 0c7b26d | 2007-11-05 21:11:54 +0000 | [diff] [blame] | 562 | update_data_counters: Updates the values in data_counters |
| 563 | buffer_is_low : Returns true if the amount of useful data in the buffer is low |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 564 | buffer_handle : Buffer data for a handle |
Nicolas Pennequin | 0c7b26d | 2007-11-05 21:11:54 +0000 | [diff] [blame] | 565 | reset_handle : Reset write position and data buffer of a handle to its offset |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 566 | rebuffer_handle : Seek to a nonbuffered part of a handle by rebuffering the data |
| 567 | shrink_handle : Free buffer space by moving a handle |
| 568 | fill_buffer : Call buffer_handle for all handles that have data to buffer |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 569 | |
| 570 | These functions are used by the buffering thread to manage buffer space. |
| 571 | */ |
| 572 | |
Nicolas Pennequin | 0c7b26d | 2007-11-05 21:11:54 +0000 | [diff] [blame] | 573 | static void update_data_counters(void) |
| 574 | { |
| 575 | struct memory_handle *m = find_handle(base_handle_id); |
| 576 | bool is_useful = m==NULL; |
| 577 | |
| 578 | size_t buffered = 0; |
| 579 | size_t wasted = 0; |
| 580 | size_t remaining = 0; |
| 581 | size_t useful = 0; |
| 582 | |
Nicolas Pennequin | f7e0e6b | 2008-05-13 20:51:06 +0000 | [diff] [blame] | 583 | mutex_lock(&llist_mutex); |
| 584 | |
Nicolas Pennequin | 0c7b26d | 2007-11-05 21:11:54 +0000 | [diff] [blame] | 585 | m = first_handle; |
| 586 | while (m) { |
| 587 | buffered += m->available; |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 588 | wasted += ringbuf_sub(m->ridx, m->data); |
Nicolas Pennequin | 0c7b26d | 2007-11-05 21:11:54 +0000 | [diff] [blame] | 589 | remaining += m->filerem; |
| 590 | |
| 591 | if (m->id == base_handle_id) |
| 592 | is_useful = true; |
| 593 | |
| 594 | if (is_useful) |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 595 | useful += ringbuf_sub(m->widx, m->ridx); |
Nicolas Pennequin | 0c7b26d | 2007-11-05 21:11:54 +0000 | [diff] [blame] | 596 | |
| 597 | m = m->next; |
| 598 | } |
| 599 | |
Nicolas Pennequin | f7e0e6b | 2008-05-13 20:51:06 +0000 | [diff] [blame] | 600 | mutex_unlock(&llist_mutex); |
| 601 | |
Nicolas Pennequin | 0c7b26d | 2007-11-05 21:11:54 +0000 | [diff] [blame] | 602 | data_counters.buffered = buffered; |
| 603 | data_counters.wasted = wasted; |
| 604 | data_counters.remaining = remaining; |
| 605 | data_counters.useful = useful; |
| 606 | } |
| 607 | |
| 608 | static inline bool buffer_is_low(void) |
| 609 | { |
| 610 | update_data_counters(); |
Björn Stenberg | 6427d12 | 2009-01-10 21:10:56 +0000 | [diff] [blame] | 611 | return data_counters.useful < (conf_watermark / 2); |
Nicolas Pennequin | 0c7b26d | 2007-11-05 21:11:54 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Brandon Low | 60d4e7c | 2007-11-03 17:55:45 +0000 | [diff] [blame] | 614 | /* Buffer data for the given handle. |
| 615 | Return whether or not the buffering should continue explicitly. */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 616 | static bool buffer_handle(int handle_id) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 617 | { |
| 618 | logf("buffer_handle(%d)", handle_id); |
| 619 | struct memory_handle *h = find_handle(handle_id); |
Thomas Martitz | df79ac2 | 2010-02-18 15:38:30 +0000 | [diff] [blame] | 620 | bool stop = false; |
| 621 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 622 | if (!h) |
Brandon Low | 7b74dd7 | 2007-11-03 21:57:27 +0000 | [diff] [blame] | 623 | return true; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 624 | |
| 625 | if (h->filerem == 0) { |
| 626 | /* nothing left to buffer */ |
Brandon Low | 7b74dd7 | 2007-11-03 21:57:27 +0000 | [diff] [blame] | 627 | return true; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | if (h->fd < 0) /* file closed, reopen */ |
| 631 | { |
| 632 | if (*h->path) |
| 633 | h->fd = open(h->path, O_RDONLY); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 634 | |
| 635 | if (h->fd < 0) |
Brandon Low | ebc981b | 2007-11-04 05:57:48 +0000 | [diff] [blame] | 636 | { |
| 637 | /* could not open the file, truncate it where it is */ |
| 638 | h->filesize -= h->filerem; |
| 639 | h->filerem = 0; |
Brandon Low | 7b74dd7 | 2007-11-03 21:57:27 +0000 | [diff] [blame] | 640 | return true; |
Brandon Low | ebc981b | 2007-11-04 05:57:48 +0000 | [diff] [blame] | 641 | } |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 642 | |
| 643 | if (h->offset) |
| 644 | lseek(h->fd, h->offset, SEEK_SET); |
| 645 | } |
| 646 | |
| 647 | trigger_cpu_boost(); |
| 648 | |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 649 | if (h->type == TYPE_ID3) |
| 650 | { |
Nicolas Pennequin | de026dc | 2008-04-16 16:18:05 +0000 | [diff] [blame] | 651 | if (!get_metadata((struct mp3entry *)(buffer + h->data), h->fd, h->path)) |
| 652 | { |
| 653 | /* metadata parsing failed: clear the buffer. */ |
| 654 | memset(buffer + h->data, 0, sizeof(struct mp3entry)); |
| 655 | } |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 656 | close(h->fd); |
| 657 | h->fd = -1; |
| 658 | h->filerem = 0; |
| 659 | h->available = sizeof(struct mp3entry); |
| 660 | h->widx += sizeof(struct mp3entry); |
Jonathan Gordon | 71898e5 | 2008-10-16 10:38:03 +0000 | [diff] [blame] | 661 | send_event(BUFFER_EVENT_FINISHED, &h->id); |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 662 | return true; |
| 663 | } |
| 664 | |
Thomas Martitz | df79ac2 | 2010-02-18 15:38:30 +0000 | [diff] [blame] | 665 | while (h->filerem > 0 && !stop) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 666 | { |
| 667 | /* max amount to copy */ |
Brandon Low | 3379440 | 2007-11-05 17:48:21 +0000 | [diff] [blame] | 668 | size_t copy_n = MIN( MIN(h->filerem, BUFFERING_DEFAULT_FILECHUNK), |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 669 | buffer_len - h->widx); |
| 670 | |
Thomas Martitz | df79ac2 | 2010-02-18 15:38:30 +0000 | [diff] [blame] | 671 | ssize_t overlap; |
Thomas Martitz | abb3dd4 | 2010-02-20 15:13:53 +0000 | [diff] [blame] | 672 | uintptr_t next_handle = ringbuf_offset(h->next); |
Thomas Martitz | df79ac2 | 2010-02-18 15:38:30 +0000 | [diff] [blame] | 673 | |
Brandon Low | ff9cdb4 | 2007-11-01 05:12:55 +0000 | [diff] [blame] | 674 | /* stop copying if it would overwrite the reading position */ |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 675 | if (ringbuf_add_cross(h->widx, copy_n, buf_ridx) >= 0) |
Brandon Low | ebc981b | 2007-11-04 05:57:48 +0000 | [diff] [blame] | 676 | return false; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 677 | |
Thomas Martitz | df79ac2 | 2010-02-18 15:38:30 +0000 | [diff] [blame] | 678 | /* FIXME: This would overwrite the next handle |
| 679 | * If this is true, then there's a handle even though we have still |
| 680 | * data to buffer. This should NEVER EVER happen! (but it does :( ) */ |
| 681 | if (h->next && (overlap |
| 682 | = ringbuf_add_cross(h->widx, copy_n, next_handle)) > 0) |
| 683 | { |
| 684 | /* stop buffering data for now and post-pone buffering the rest */ |
| 685 | stop = true; |
| 686 | DEBUGF( "%s(): Preventing handle corruption: h1.id:%d h2.id:%d" |
Jeffrey Goode | 6ef04a7 | 2010-05-15 03:34:31 +0000 | [diff] [blame] | 687 | " copy_n:%lu overlap:%ld h1.filerem:%lu\n", __func__, |
| 688 | h->id, h->next->id, (unsigned long)copy_n, (long)overlap, |
Thomas Martitz | 4c78eca | 2010-02-18 17:43:55 +0000 | [diff] [blame] | 689 | (unsigned long)h->filerem); |
Thomas Martitz | df79ac2 | 2010-02-18 15:38:30 +0000 | [diff] [blame] | 690 | copy_n -= overlap; |
| 691 | } |
Brandon Low | ff9cdb4 | 2007-11-01 05:12:55 +0000 | [diff] [blame] | 692 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 693 | /* rc is the actual amount read */ |
| 694 | int rc = read(h->fd, &buffer[h->widx], copy_n); |
| 695 | |
| 696 | if (rc < 0) |
| 697 | { |
Brandon Low | 60d4e7c | 2007-11-03 17:55:45 +0000 | [diff] [blame] | 698 | /* Some kind of filesystem error, maybe recoverable if not codec */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 699 | if (h->type == TYPE_CODEC) { |
| 700 | logf("Partial codec"); |
| 701 | break; |
| 702 | } |
| 703 | |
| 704 | DEBUGF("File ended %ld bytes early\n", (long)h->filerem); |
| 705 | h->filesize -= h->filerem; |
| 706 | h->filerem = 0; |
| 707 | break; |
| 708 | } |
| 709 | |
| 710 | /* Advance buffer */ |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 711 | h->widx = ringbuf_add(h->widx, rc); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 712 | if (h == cur_handle) |
| 713 | buf_widx = h->widx; |
| 714 | h->available += rc; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 715 | h->filerem -= rc; |
| 716 | |
Nicolas Pennequin | 0c7b26d | 2007-11-05 21:11:54 +0000 | [diff] [blame] | 717 | /* If this is a large file, see if we need to break or give the codec |
Brandon Low | aabd688 | 2007-11-04 02:40:24 +0000 | [diff] [blame] | 718 | * more time */ |
Michael Sevakis | 398d9fd | 2007-11-20 22:45:46 +0000 | [diff] [blame] | 719 | if (h->type == TYPE_PACKET_AUDIO && |
| 720 | pcmbuf_is_lowdata() && !buffer_is_low()) |
| 721 | { |
| 722 | sleep(1); |
| 723 | } |
| 724 | else |
| 725 | { |
| 726 | yield(); |
| 727 | } |
| 728 | |
| 729 | if (!queue_empty(&buffering_queue)) |
Nicolas Pennequin | 0c7b26d | 2007-11-05 21:11:54 +0000 | [diff] [blame] | 730 | break; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | if (h->filerem == 0) { |
| 734 | /* finished buffering the file */ |
| 735 | close(h->fd); |
| 736 | h->fd = -1; |
Jonathan Gordon | 71898e5 | 2008-10-16 10:38:03 +0000 | [diff] [blame] | 737 | send_event(BUFFER_EVENT_FINISHED, &h->id); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Thomas Martitz | df79ac2 | 2010-02-18 15:38:30 +0000 | [diff] [blame] | 740 | return !stop; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | /* Reset writing position and data buffer of a handle to its current offset. |
| 744 | Use this after having set the new offset to use. */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 745 | static void reset_handle(int handle_id) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 746 | { |
Torne Wuff | c4e051b | 2010-02-01 17:16:39 +0000 | [diff] [blame] | 747 | size_t alignment_pad; |
| 748 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 749 | logf("reset_handle(%d)", handle_id); |
| 750 | |
| 751 | struct memory_handle *h = find_handle(handle_id); |
| 752 | if (!h) |
| 753 | return; |
| 754 | |
Torne Wuff | c4e051b | 2010-02-01 17:16:39 +0000 | [diff] [blame] | 755 | /* Align to desired storage alignment */ |
Rafaël Carré | a8d1690 | 2010-03-25 23:01:56 +0000 | [diff] [blame] | 756 | alignment_pad = STORAGE_OVERLAP(h->offset - (size_t)(&buffer[h->start])); |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 757 | h->ridx = h->widx = h->data = ringbuf_add(h->start, alignment_pad); |
Torne Wuff | c4e051b | 2010-02-01 17:16:39 +0000 | [diff] [blame] | 758 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 759 | if (h == cur_handle) |
| 760 | buf_widx = h->widx; |
| 761 | h->available = 0; |
| 762 | h->filerem = h->filesize - h->offset; |
| 763 | |
| 764 | if (h->fd >= 0) { |
| 765 | lseek(h->fd, h->offset, SEEK_SET); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | /* Seek to a nonbuffered part of a handle by rebuffering the data. */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 770 | static void rebuffer_handle(int handle_id, size_t newpos) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 771 | { |
| 772 | struct memory_handle *h = find_handle(handle_id); |
| 773 | if (!h) |
| 774 | return; |
| 775 | |
Nicolas Pennequin | e24454f | 2007-11-26 21:13:08 +0000 | [diff] [blame] | 776 | /* When seeking foward off of the buffer, if it is a short seek don't |
| 777 | rebuffer the whole track, just read enough to satisfy */ |
Brandon Low | 76f9bfa | 2007-11-05 17:50:51 +0000 | [diff] [blame] | 778 | if (newpos > h->offset && newpos - h->offset < BUFFERING_DEFAULT_FILECHUNK) |
| 779 | { |
Nicolas Pennequin | cf36957 | 2008-07-18 23:42:47 +0000 | [diff] [blame] | 780 | LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", handle_id); |
Brandon Low | 76f9bfa | 2007-11-05 17:50:51 +0000 | [diff] [blame] | 781 | queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id); |
| 782 | h->ridx = h->data + newpos; |
| 783 | return; |
| 784 | } |
Nicolas Pennequin | e24454f | 2007-11-26 21:13:08 +0000 | [diff] [blame] | 785 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 786 | h->offset = newpos; |
| 787 | |
Nicolas Pennequin | e24454f | 2007-11-26 21:13:08 +0000 | [diff] [blame] | 788 | /* Reset the handle to its new offset */ |
Nicolas Pennequin | cf36957 | 2008-07-18 23:42:47 +0000 | [diff] [blame] | 789 | LOGFQUEUE("buffering >| Q_RESET_HANDLE %d", handle_id); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 790 | queue_send(&buffering_queue, Q_RESET_HANDLE, handle_id); |
| 791 | |
Thomas Martitz | abb3dd4 | 2010-02-20 15:13:53 +0000 | [diff] [blame] | 792 | uintptr_t next = ringbuf_offset(h->next); |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 793 | if (ringbuf_sub(next, h->data) < h->filesize - newpos) |
Nicolas Pennequin | e24454f | 2007-11-26 21:13:08 +0000 | [diff] [blame] | 794 | { |
Nicolas Pennequin | 774fd19 | 2007-11-26 23:51:36 +0000 | [diff] [blame] | 795 | /* There isn't enough space to rebuffer all of the track from its new |
| 796 | offset, so we ask the user to free some */ |
Thomas Martitz | df79ac2 | 2010-02-18 15:38:30 +0000 | [diff] [blame] | 797 | DEBUGF("%s(): space is needed\n", __func__); |
Jonathan Gordon | 71898e5 | 2008-10-16 10:38:03 +0000 | [diff] [blame] | 798 | send_event(BUFFER_EVENT_REBUFFER, &handle_id); |
Nicolas Pennequin | e24454f | 2007-11-26 21:13:08 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /* Now we ask for a rebuffer */ |
Nicolas Pennequin | cf36957 | 2008-07-18 23:42:47 +0000 | [diff] [blame] | 802 | LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", handle_id); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 803 | queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 806 | static bool close_handle(int handle_id) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 807 | { |
| 808 | struct memory_handle *h = find_handle(handle_id); |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 809 | |
| 810 | /* If the handle is not found, it is closed */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 811 | if (!h) |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 812 | return true; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 813 | |
| 814 | if (h->fd >= 0) { |
| 815 | close(h->fd); |
| 816 | h->fd = -1; |
| 817 | } |
| 818 | |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 819 | /* rm_handle returns true unless the handle somehow persists after exit */ |
| 820 | return rm_handle(h); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /* Free buffer space by moving the handle struct right before the useful |
| 824 | part of its data buffer or by moving all the data. */ |
Brandon Low | a042c72 | 2007-11-03 02:54:34 +0000 | [diff] [blame] | 825 | static void shrink_handle(struct memory_handle *h) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 826 | { |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 827 | size_t delta; |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 828 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 829 | if (!h) |
| 830 | return; |
| 831 | |
Brandon Low | dcca586 | 2007-11-02 14:06:48 +0000 | [diff] [blame] | 832 | if (h->next && h->filerem == 0 && |
| 833 | (h->type == TYPE_ID3 || h->type == TYPE_CUESHEET || |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 834 | h->type == TYPE_BITMAP || h->type == TYPE_CODEC || |
Brandon Low | dcca586 | 2007-11-02 14:06:48 +0000 | [diff] [blame] | 835 | h->type == TYPE_ATOMIC_AUDIO)) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 836 | { |
| 837 | /* metadata handle: we can move all of it */ |
Thomas Martitz | abb3dd4 | 2010-02-20 15:13:53 +0000 | [diff] [blame] | 838 | uintptr_t handle_distance = |
| 839 | ringbuf_sub(ringbuf_offset(h->next), h->data); |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 840 | delta = handle_distance - h->available; |
| 841 | |
| 842 | /* The value of delta might change for alignment reasons */ |
Brandon Low | dcca586 | 2007-11-02 14:06:48 +0000 | [diff] [blame] | 843 | if (!move_handle(&h, &delta, h->available, h->type==TYPE_CODEC)) |
Brandon Low | 483dca9 | 2007-10-29 16:48:16 +0000 | [diff] [blame] | 844 | return; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 845 | |
| 846 | size_t olddata = h->data; |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 847 | h->data = ringbuf_add(h->data, delta); |
| 848 | h->ridx = ringbuf_add(h->ridx, delta); |
| 849 | h->widx = ringbuf_add(h->widx, delta); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 850 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 851 | if (h->type == TYPE_ID3 && h->filesize == sizeof(struct mp3entry)) { |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 852 | /* when moving an mp3entry we need to readjust its pointers. */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 853 | adjust_mp3entry((struct mp3entry *)&buffer[h->data], |
| 854 | (void *)&buffer[h->data], |
Steve Bavin | 73f9863 | 2008-03-26 08:57:25 +0000 | [diff] [blame] | 855 | (const void *)&buffer[olddata]); |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 856 | } else if (h->type == TYPE_BITMAP) { |
| 857 | /* adjust the bitmap's pointer */ |
| 858 | struct bitmap *bmp = (struct bitmap *)&buffer[h->data]; |
| 859 | bmp->data = &buffer[h->data + sizeof(struct bitmap)]; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | else |
| 863 | { |
| 864 | /* only move the handle struct */ |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 865 | delta = ringbuf_sub(h->ridx, h->data); |
Brandon Low | dcca586 | 2007-11-02 14:06:48 +0000 | [diff] [blame] | 866 | if (!move_handle(&h, &delta, 0, true)) |
Brandon Low | 483dca9 | 2007-10-29 16:48:16 +0000 | [diff] [blame] | 867 | return; |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 868 | |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 869 | h->data = ringbuf_add(h->data, delta); |
| 870 | h->start = ringbuf_add(h->start, delta); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 871 | h->available -= delta; |
| 872 | h->offset += delta; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | /* Fill the buffer by buffering as much data as possible for handles that still |
Brandon Low | 11a3661 | 2007-11-03 06:21:32 +0000 | [diff] [blame] | 877 | have data left to buffer |
| 878 | Return whether or not to continue filling after this */ |
| 879 | static bool fill_buffer(void) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 880 | { |
| 881 | logf("fill_buffer()"); |
Magnus Holmgren | 7c84ede | 2008-07-11 12:44:27 +0000 | [diff] [blame] | 882 | struct memory_handle *m; |
| 883 | shrink_handle(first_handle); |
| 884 | m = first_handle; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 885 | while (queue_empty(&buffering_queue) && m) { |
| 886 | if (m->filerem > 0) { |
Brandon Low | 60d4e7c | 2007-11-03 17:55:45 +0000 | [diff] [blame] | 887 | if (!buffer_handle(m->id)) { |
| 888 | m = NULL; |
| 889 | break; |
| 890 | } |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 891 | } |
| 892 | m = m->next; |
| 893 | } |
| 894 | |
Brandon Low | 11a3661 | 2007-11-03 06:21:32 +0000 | [diff] [blame] | 895 | if (m) { |
| 896 | return true; |
| 897 | } |
| 898 | else |
| 899 | { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 900 | /* only spin the disk down if the filling wasn't interrupted by an |
| 901 | event arriving in the queue. */ |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 902 | storage_sleep(); |
Brandon Low | 11a3661 | 2007-11-03 06:21:32 +0000 | [diff] [blame] | 903 | return false; |
| 904 | } |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Nicolas Pennequin | a384fb6 | 2007-11-11 13:15:36 +0000 | [diff] [blame] | 907 | #ifdef HAVE_ALBUMART |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 908 | /* Given a file descriptor to a bitmap file, write the bitmap data to the |
| 909 | buffer, with a struct bitmap and the actual data immediately following. |
| 910 | Return value is the total size (struct + data). */ |
Thomas Martitz | e9c1018 | 2009-10-16 19:14:41 +0000 | [diff] [blame] | 911 | static int load_image(int fd, const char *path, struct dim *dim) |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 912 | { |
| 913 | int rc; |
| 914 | struct bitmap *bmp = (struct bitmap *)&buffer[buf_widx]; |
Thomas Martitz | e9c1018 | 2009-10-16 19:14:41 +0000 | [diff] [blame] | 915 | |
| 916 | /* get the desired image size */ |
| 917 | bmp->width = dim->width, bmp->height = dim->height; |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 918 | /* FIXME: alignment may be needed for the data buffer. */ |
| 919 | bmp->data = &buffer[buf_widx + sizeof(struct bitmap)]; |
Andrew Mahone | 00d6cfd | 2009-05-26 20:26:05 +0000 | [diff] [blame] | 920 | #ifndef HAVE_JPEG |
| 921 | (void) path; |
| 922 | #endif |
Nicolas Pennequin | a384fb6 | 2007-11-11 13:15:36 +0000 | [diff] [blame] | 923 | #if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1) |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 924 | bmp->maskdata = NULL; |
Nicolas Pennequin | a384fb6 | 2007-11-11 13:15:36 +0000 | [diff] [blame] | 925 | #endif |
| 926 | |
Andrew Mahone | 781421a | 2008-12-09 23:07:59 +0000 | [diff] [blame] | 927 | int free = (int)MIN(buffer_len - BUF_USED, buffer_len - buf_widx) |
| 928 | - sizeof(struct bitmap); |
| 929 | |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 930 | #ifdef HAVE_JPEG |
| 931 | int pathlen = strlen(path); |
Andrew Mahone | 54e6eb3 | 2009-05-01 23:31:43 +0000 | [diff] [blame] | 932 | if (strcmp(path + pathlen - 4, ".bmp")) |
| 933 | rc = read_jpeg_fd(fd, bmp, free, FORMAT_NATIVE|FORMAT_DITHER| |
| 934 | FORMAT_RESIZE|FORMAT_KEEP_ASPECT, NULL); |
| 935 | else |
Andrew Mahone | 20f76d6 | 2009-05-04 15:46:41 +0000 | [diff] [blame] | 936 | #endif |
Andrew Mahone | 54e6eb3 | 2009-05-01 23:31:43 +0000 | [diff] [blame] | 937 | rc = read_bmp_fd(fd, bmp, free, FORMAT_NATIVE|FORMAT_DITHER| |
| 938 | FORMAT_RESIZE|FORMAT_KEEP_ASPECT, NULL); |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 939 | return rc + (rc > 0 ? sizeof(struct bitmap) : 0); |
| 940 | } |
| 941 | #endif |
| 942 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 943 | |
| 944 | /* |
| 945 | MAIN BUFFERING API CALLS |
| 946 | ======================== |
| 947 | |
| 948 | bufopen : Request the opening of a new handle for a file |
| 949 | bufalloc : Open a new handle for data other than a file. |
| 950 | bufclose : Close an open handle |
| 951 | bufseek : Set the read pointer in a handle |
| 952 | bufadvance : Move the read pointer in a handle |
| 953 | bufread : Copy data from a handle into a given buffer |
| 954 | bufgetdata : Give a pointer to the handle's data |
| 955 | |
| 956 | These functions are exported, to allow interaction with the buffer. |
| 957 | They take care of the content of the structs, and rely on the linked list |
| 958 | management functions for all the actual handle management work. |
| 959 | */ |
| 960 | |
| 961 | |
| 962 | /* Reserve space in the buffer for a file. |
| 963 | filename: name of the file to open |
| 964 | offset: offset at which to start buffering the file, useful when the first |
| 965 | (offset-1) bytes of the file aren't needed. |
Thomas Martitz | e9c1018 | 2009-10-16 19:14:41 +0000 | [diff] [blame] | 966 | type: one of the data types supported (audio, image, cuesheet, others |
| 967 | user_data: user data passed possibly passed in subcalls specific to a |
| 968 | data_type (only used for image (albumart) buffering so far ) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 969 | return value: <0 if the file cannot be opened, or one file already |
| 970 | queued to be opened, otherwise the handle for the file in the buffer |
| 971 | */ |
Thomas Martitz | e9c1018 | 2009-10-16 19:14:41 +0000 | [diff] [blame] | 972 | int bufopen(const char *file, size_t offset, enum data_type type, |
| 973 | void *user_data) |
Nicolas Pennequin | 4fd2774 | 2008-03-29 14:09:14 +0000 | [diff] [blame] | 974 | { |
Thomas Martitz | e9c1018 | 2009-10-16 19:14:41 +0000 | [diff] [blame] | 975 | #ifndef HAVE_ALBUMART |
| 976 | /* currently only used for aa loading */ |
| 977 | (void)user_data; |
| 978 | #endif |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 979 | if (type == TYPE_ID3) |
| 980 | { |
| 981 | /* ID3 case: allocate space, init the handle and return. */ |
| 982 | |
| 983 | struct memory_handle *h = add_handle(sizeof(struct mp3entry), false, true); |
| 984 | if (!h) |
| 985 | return ERR_BUFFER_FULL; |
| 986 | |
| 987 | h->fd = -1; |
| 988 | h->filesize = sizeof(struct mp3entry); |
| 989 | h->filerem = sizeof(struct mp3entry); |
| 990 | h->offset = 0; |
| 991 | h->data = buf_widx; |
| 992 | h->ridx = buf_widx; |
| 993 | h->widx = buf_widx; |
| 994 | h->available = 0; |
| 995 | h->type = type; |
Nils Wallménius | 3d4701a | 2009-07-14 13:57:45 +0000 | [diff] [blame] | 996 | strlcpy(h->path, file, MAX_PATH); |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 997 | |
| 998 | buf_widx += sizeof(struct mp3entry); /* safe because the handle |
| 999 | can't wrap */ |
Nicolas Pennequin | 3625be4 | 2008-12-02 21:07:12 +0000 | [diff] [blame] | 1000 | |
| 1001 | /* Inform the buffering thread that we added a handle */ |
| 1002 | LOGFQUEUE("buffering > Q_HANDLE_ADDED %d", h->id); |
| 1003 | queue_post(&buffering_queue, Q_HANDLE_ADDED, h->id); |
| 1004 | |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1005 | return h->id; |
| 1006 | } |
| 1007 | |
| 1008 | /* Other cases: there is a little more work. */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1009 | int fd = open(file, O_RDONLY); |
| 1010 | if (fd < 0) |
Nicolas Pennequin | d400e23 | 2007-10-29 14:15:59 +0000 | [diff] [blame] | 1011 | return ERR_FILE_ERROR; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1012 | |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 1013 | size_t size = filesize(fd); |
Brandon Low | dcca586 | 2007-11-02 14:06:48 +0000 | [diff] [blame] | 1014 | bool can_wrap = type==TYPE_PACKET_AUDIO || type==TYPE_CODEC; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1015 | |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1016 | size_t adjusted_offset = offset; |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1017 | if (adjusted_offset > size) |
| 1018 | adjusted_offset = 0; |
Nicolas Pennequin | 659fe5a | 2008-01-08 23:48:51 +0000 | [diff] [blame] | 1019 | |
Torne Wuff | c4e051b | 2010-02-01 17:16:39 +0000 | [diff] [blame] | 1020 | /* Reserve extra space because alignment can move data forward */ |
Rafaël Carré | a8d1690 | 2010-03-25 23:01:56 +0000 | [diff] [blame] | 1021 | size_t padded_size = STORAGE_PAD(size-adjusted_offset); |
| 1022 | struct memory_handle *h = add_handle(padded_size, can_wrap, false); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1023 | if (!h) |
| 1024 | { |
Thomas Martitz | df79ac2 | 2010-02-18 15:38:30 +0000 | [diff] [blame] | 1025 | DEBUGF("%s(): failed to add handle\n", __func__); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1026 | close(fd); |
Nicolas Pennequin | d400e23 | 2007-10-29 14:15:59 +0000 | [diff] [blame] | 1027 | return ERR_BUFFER_FULL; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Nils Wallménius | 3d4701a | 2009-07-14 13:57:45 +0000 | [diff] [blame] | 1030 | strlcpy(h->path, file, MAX_PATH); |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1031 | h->offset = adjusted_offset; |
Torne Wuff | c4e051b | 2010-02-01 17:16:39 +0000 | [diff] [blame] | 1032 | |
| 1033 | /* Don't bother to storage align bitmaps because they are not |
| 1034 | * loaded directly into the buffer. |
| 1035 | */ |
| 1036 | if (type != TYPE_BITMAP) |
| 1037 | { |
| 1038 | size_t alignment_pad; |
| 1039 | |
| 1040 | /* Remember where data area starts, for use by reset_handle */ |
| 1041 | h->start = buf_widx; |
| 1042 | |
| 1043 | /* Align to desired storage alignment */ |
Rafaël Carré | a8d1690 | 2010-03-25 23:01:56 +0000 | [diff] [blame] | 1044 | alignment_pad = STORAGE_OVERLAP(adjusted_offset - (size_t)(&buffer[buf_widx])); |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 1045 | buf_widx = ringbuf_add(buf_widx, alignment_pad); |
Torne Wuff | c4e051b | 2010-02-01 17:16:39 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1048 | h->ridx = buf_widx; |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 1049 | h->widx = buf_widx; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1050 | h->data = buf_widx; |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 1051 | h->available = 0; |
| 1052 | h->filerem = 0; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1053 | h->type = type; |
| 1054 | |
Nicolas Pennequin | 5e0e239 | 2007-11-11 13:28:24 +0000 | [diff] [blame] | 1055 | #ifdef HAVE_ALBUMART |
Nicolas Pennequin | 87e5b11 | 2007-11-12 15:16:41 +0000 | [diff] [blame] | 1056 | if (type == TYPE_BITMAP) |
| 1057 | { |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 1058 | /* Bitmap file: we load the data instead of the file */ |
Nicolas Pennequin | 87e5b11 | 2007-11-12 15:16:41 +0000 | [diff] [blame] | 1059 | int rc; |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 1060 | mutex_lock(&llist_mod_mutex); /* Lock because load_bitmap yields */ |
Thomas Martitz | e9c1018 | 2009-10-16 19:14:41 +0000 | [diff] [blame] | 1061 | rc = load_image(fd, file, (struct dim*)user_data); |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 1062 | mutex_unlock(&llist_mod_mutex); |
Nicolas Pennequin | 87e5b11 | 2007-11-12 15:16:41 +0000 | [diff] [blame] | 1063 | if (rc <= 0) |
| 1064 | { |
| 1065 | rm_handle(h); |
| 1066 | close(fd); |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 1067 | return ERR_FILE_ERROR; |
Nicolas Pennequin | 87e5b11 | 2007-11-12 15:16:41 +0000 | [diff] [blame] | 1068 | } |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 1069 | h->filerem = 0; |
Nicolas Pennequin | 87e5b11 | 2007-11-12 15:16:41 +0000 | [diff] [blame] | 1070 | h->filesize = rc; |
| 1071 | h->available = rc; |
| 1072 | h->widx = buf_widx + rc; /* safe because the data doesn't wrap */ |
| 1073 | buf_widx += rc; /* safe too */ |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 1074 | } |
Nicolas Pennequin | 87e5b11 | 2007-11-12 15:16:41 +0000 | [diff] [blame] | 1075 | else |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 1076 | #endif |
Nicolas Pennequin | 87e5b11 | 2007-11-12 15:16:41 +0000 | [diff] [blame] | 1077 | { |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1078 | h->filerem = size - adjusted_offset; |
Nicolas Pennequin | 87e5b11 | 2007-11-12 15:16:41 +0000 | [diff] [blame] | 1079 | h->filesize = size; |
| 1080 | h->available = 0; |
| 1081 | h->widx = buf_widx; |
| 1082 | } |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 1083 | |
| 1084 | if (type == TYPE_CUESHEET) { |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 1085 | h->fd = fd; |
| 1086 | /* Immediately start buffering those */ |
Nicolas Pennequin | cf36957 | 2008-07-18 23:42:47 +0000 | [diff] [blame] | 1087 | LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", h->id); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1088 | queue_send(&buffering_queue, Q_BUFFER_HANDLE, h->id); |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 1089 | } else { |
| 1090 | /* Other types will get buffered in the course of normal operations */ |
| 1091 | h->fd = -1; |
| 1092 | close(fd); |
Nicolas Pennequin | 483c402 | 2008-02-12 23:15:59 +0000 | [diff] [blame] | 1093 | |
| 1094 | /* Inform the buffering thread that we added a handle */ |
| 1095 | LOGFQUEUE("buffering > Q_HANDLE_ADDED %d", h->id); |
| 1096 | queue_post(&buffering_queue, Q_HANDLE_ADDED, h->id); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | logf("bufopen: new hdl %d", h->id); |
| 1100 | return h->id; |
| 1101 | } |
| 1102 | |
| 1103 | /* Open a new handle from data that needs to be copied from memory. |
| 1104 | src is the source buffer from which to copy data. It can be NULL to simply |
| 1105 | reserve buffer space. |
| 1106 | size is the requested size. The call will only be successful if the |
| 1107 | requested amount of data can entirely fit in the buffer without wrapping. |
| 1108 | Return value is the handle id for success or <0 for failure. |
| 1109 | */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1110 | int bufalloc(const void *src, size_t size, enum data_type type) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1111 | { |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 1112 | struct memory_handle *h = add_handle(size, false, true); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1113 | |
Brandon Low | 18c9aba | 2007-10-27 04:49:04 +0000 | [diff] [blame] | 1114 | if (!h) |
Nicolas Pennequin | d400e23 | 2007-10-29 14:15:59 +0000 | [diff] [blame] | 1115 | return ERR_BUFFER_FULL; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1116 | |
| 1117 | if (src) { |
| 1118 | if (type == TYPE_ID3 && size == sizeof(struct mp3entry)) { |
| 1119 | /* specially take care of struct mp3entry */ |
| 1120 | copy_mp3entry((struct mp3entry *)&buffer[buf_widx], |
Steve Bavin | 73f9863 | 2008-03-26 08:57:25 +0000 | [diff] [blame] | 1121 | (const struct mp3entry *)src); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1122 | } else { |
| 1123 | memcpy(&buffer[buf_widx], src, size); |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | h->fd = -1; |
| 1128 | *h->path = 0; |
| 1129 | h->filesize = size; |
| 1130 | h->filerem = 0; |
| 1131 | h->offset = 0; |
| 1132 | h->ridx = buf_widx; |
| 1133 | h->widx = buf_widx + size; /* this is safe because the data doesn't wrap */ |
| 1134 | h->data = buf_widx; |
| 1135 | h->available = size; |
| 1136 | h->type = type; |
| 1137 | |
| 1138 | buf_widx += size; /* safe too */ |
| 1139 | |
| 1140 | logf("bufalloc: new hdl %d", h->id); |
| 1141 | return h->id; |
| 1142 | } |
| 1143 | |
| 1144 | /* Close the handle. Return true for success and false for failure */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1145 | bool bufclose(int handle_id) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1146 | { |
| 1147 | logf("bufclose(%d)", handle_id); |
| 1148 | |
Nicolas Pennequin | d08131a | 2007-10-27 01:25:47 +0000 | [diff] [blame] | 1149 | LOGFQUEUE("buffering >| Q_CLOSE_HANDLE %d", handle_id); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1150 | return queue_send(&buffering_queue, Q_CLOSE_HANDLE, handle_id); |
| 1151 | } |
| 1152 | |
| 1153 | /* Set reading index in handle (relatively to the start of the file). |
| 1154 | Access before the available data will trigger a rebuffer. |
| 1155 | Return 0 for success and < 0 for failure: |
| 1156 | -1 if the handle wasn't found |
| 1157 | -2 if the new requested position was beyond the end of the file |
| 1158 | */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1159 | int bufseek(int handle_id, size_t newpos) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1160 | { |
| 1161 | struct memory_handle *h = find_handle(handle_id); |
| 1162 | if (!h) |
Nicolas Pennequin | d400e23 | 2007-10-29 14:15:59 +0000 | [diff] [blame] | 1163 | return ERR_HANDLE_NOT_FOUND; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1164 | |
| 1165 | if (newpos > h->filesize) { |
| 1166 | /* access beyond the end of the file */ |
Nicolas Pennequin | d400e23 | 2007-10-29 14:15:59 +0000 | [diff] [blame] | 1167 | return ERR_INVALID_VALUE; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1168 | } |
| 1169 | else if (newpos < h->offset || h->offset + h->available < newpos) { |
| 1170 | /* access before or after buffered data. A rebuffer is needed. */ |
| 1171 | rebuffer_handle(handle_id, newpos); |
| 1172 | } |
| 1173 | else { |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 1174 | h->ridx = ringbuf_add(h->data, newpos - h->offset); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1175 | } |
| 1176 | return 0; |
| 1177 | } |
| 1178 | |
| 1179 | /* Advance the reading index in a handle (relatively to its current position). |
| 1180 | Return 0 for success and < 0 for failure */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1181 | int bufadvance(int handle_id, off_t offset) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1182 | { |
Brandon Low | 404c6fb | 2007-10-27 01:37:33 +0000 | [diff] [blame] | 1183 | const struct memory_handle *h = find_handle(handle_id); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1184 | if (!h) |
Nicolas Pennequin | d400e23 | 2007-10-29 14:15:59 +0000 | [diff] [blame] | 1185 | return ERR_HANDLE_NOT_FOUND; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1186 | |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 1187 | size_t newpos = h->offset + ringbuf_sub(h->ridx, h->data) + offset; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1188 | return bufseek(handle_id, newpos); |
| 1189 | } |
| 1190 | |
Brandon Low | 9821cce | 2007-11-06 16:49:30 +0000 | [diff] [blame] | 1191 | /* Used by bufread and bufgetdata to prepare the buffer and retrieve the |
| 1192 | * actual amount of data available for reading. This function explicitly |
| 1193 | * does not check the validity of the input handle. It does do range checks |
| 1194 | * on size and returns a valid (and explicit) amount of data for reading */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1195 | static struct memory_handle *prep_bufdata(int handle_id, size_t *size, |
| 1196 | bool guardbuf_limit) |
Brandon Low | 9821cce | 2007-11-06 16:49:30 +0000 | [diff] [blame] | 1197 | { |
Brandon Low | 6e8ee40 | 2007-11-08 15:34:23 +0000 | [diff] [blame] | 1198 | struct memory_handle *h = find_handle(handle_id); |
| 1199 | if (!h) |
| 1200 | return NULL; |
| 1201 | |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 1202 | size_t avail = ringbuf_sub(h->widx, h->ridx); |
Brandon Low | 9821cce | 2007-11-06 16:49:30 +0000 | [diff] [blame] | 1203 | |
| 1204 | if (avail == 0 && h->filerem == 0) |
Nicolas Pennequin | b50473f | 2007-11-08 18:27:19 +0000 | [diff] [blame] | 1205 | { |
Brandon Low | 9821cce | 2007-11-06 16:49:30 +0000 | [diff] [blame] | 1206 | /* File is finished reading */ |
Nicolas Pennequin | b50473f | 2007-11-08 18:27:19 +0000 | [diff] [blame] | 1207 | *size = 0; |
| 1208 | return h; |
| 1209 | } |
Brandon Low | 9821cce | 2007-11-06 16:49:30 +0000 | [diff] [blame] | 1210 | |
Brandon Low | 6e8ee40 | 2007-11-08 15:34:23 +0000 | [diff] [blame] | 1211 | if (*size == 0 || *size > avail + h->filerem) |
| 1212 | *size = avail + h->filerem; |
Brandon Low | 9821cce | 2007-11-06 16:49:30 +0000 | [diff] [blame] | 1213 | |
Brandon Low | 1db4243 | 2007-11-08 15:52:10 +0000 | [diff] [blame] | 1214 | if (guardbuf_limit && h->type == TYPE_PACKET_AUDIO && *size > GUARD_BUFSIZE) |
Brandon Low | 9821cce | 2007-11-06 16:49:30 +0000 | [diff] [blame] | 1215 | { |
Brandon Low | 1db4243 | 2007-11-08 15:52:10 +0000 | [diff] [blame] | 1216 | logf("data request > guardbuf"); |
| 1217 | /* If more than the size of the guardbuf is requested and this is a |
| 1218 | * bufgetdata, limit to guard_bufsize over the end of the buffer */ |
| 1219 | *size = MIN(*size, buffer_len - h->ridx + GUARD_BUFSIZE); |
Nicolas Pennequin | ca4771b | 2007-11-08 16:12:28 +0000 | [diff] [blame] | 1220 | /* this ensures *size <= buffer_len - h->ridx + GUARD_BUFSIZE */ |
Brandon Low | 9821cce | 2007-11-06 16:49:30 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Brandon Low | 6e8ee40 | 2007-11-08 15:34:23 +0000 | [diff] [blame] | 1223 | if (h->filerem > 0 && avail < *size) |
Brandon Low | 9821cce | 2007-11-06 16:49:30 +0000 | [diff] [blame] | 1224 | { |
| 1225 | /* Data isn't ready. Request buffering */ |
Brandon Low | dc0f497 | 2007-11-08 18:59:22 +0000 | [diff] [blame] | 1226 | buf_request_buffer_handle(handle_id); |
Brandon Low | 9821cce | 2007-11-06 16:49:30 +0000 | [diff] [blame] | 1227 | /* Wait for the data to be ready */ |
| 1228 | do |
| 1229 | { |
| 1230 | sleep(1); |
Brandon Low | 551db40 | 2007-11-08 16:06:24 +0000 | [diff] [blame] | 1231 | /* it is not safe for a non-buffering thread to sleep while |
| 1232 | * holding a handle */ |
Brandon Low | 6e8ee40 | 2007-11-08 15:34:23 +0000 | [diff] [blame] | 1233 | h = find_handle(handle_id); |
Brandon Low | 551db40 | 2007-11-08 16:06:24 +0000 | [diff] [blame] | 1234 | if (!h) |
| 1235 | return NULL; |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 1236 | avail = ringbuf_sub(h->widx, h->ridx); |
Brandon Low | 9821cce | 2007-11-06 16:49:30 +0000 | [diff] [blame] | 1237 | } |
Brandon Low | 6e8ee40 | 2007-11-08 15:34:23 +0000 | [diff] [blame] | 1238 | while (h->filerem > 0 && avail < *size); |
Brandon Low | 9821cce | 2007-11-06 16:49:30 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Brandon Low | 6e8ee40 | 2007-11-08 15:34:23 +0000 | [diff] [blame] | 1241 | *size = MIN(*size,avail); |
| 1242 | return h; |
Brandon Low | 9821cce | 2007-11-06 16:49:30 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1245 | /* Copy data from the given handle to the dest buffer. |
Nicolas Pennequin | b838a62 | 2007-11-02 19:13:03 +0000 | [diff] [blame] | 1246 | Return the number of bytes copied or < 0 for failure (handle not found). |
| 1247 | The caller is blocked until the requested amount of data is available. |
| 1248 | */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1249 | ssize_t bufread(int handle_id, size_t size, void *dest) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1250 | { |
Nicolas Pennequin | 4fd2774 | 2008-03-29 14:09:14 +0000 | [diff] [blame] | 1251 | const struct memory_handle *h; |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1252 | size_t adjusted_size = size; |
Brandon Low | 6e8ee40 | 2007-11-08 15:34:23 +0000 | [diff] [blame] | 1253 | |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1254 | h = prep_bufdata(handle_id, &adjusted_size, false); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1255 | if (!h) |
Nicolas Pennequin | d400e23 | 2007-10-29 14:15:59 +0000 | [diff] [blame] | 1256 | return ERR_HANDLE_NOT_FOUND; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1257 | |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1258 | if (h->ridx + adjusted_size > buffer_len) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1259 | { |
| 1260 | /* the data wraps around the end of the buffer */ |
| 1261 | size_t read = buffer_len - h->ridx; |
| 1262 | memcpy(dest, &buffer[h->ridx], read); |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1263 | memcpy(dest+read, buffer, adjusted_size - read); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1264 | } |
| 1265 | else |
| 1266 | { |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1267 | memcpy(dest, &buffer[h->ridx], adjusted_size); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1270 | return adjusted_size; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | /* Update the "data" pointer to make the handle's data available to the caller. |
Nicolas Pennequin | b838a62 | 2007-11-02 19:13:03 +0000 | [diff] [blame] | 1274 | Return the length of the available linear data or < 0 for failure (handle |
| 1275 | not found). |
| 1276 | The caller is blocked until the requested amount of data is available. |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1277 | size is the amount of linear data requested. it can be 0 to get as |
| 1278 | much as possible. |
Nicolas Pennequin | b838a62 | 2007-11-02 19:13:03 +0000 | [diff] [blame] | 1279 | The guard buffer may be used to provide the requested size. This means it's |
| 1280 | unsafe to request more than the size of the guard buffer. |
| 1281 | */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1282 | ssize_t bufgetdata(int handle_id, size_t size, void **data) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1283 | { |
Brandon Low | 6e8ee40 | 2007-11-08 15:34:23 +0000 | [diff] [blame] | 1284 | const struct memory_handle *h; |
Nicolas Pennequin | 4fd2774 | 2008-03-29 14:09:14 +0000 | [diff] [blame] | 1285 | size_t adjusted_size = size; |
Brandon Low | 6e8ee40 | 2007-11-08 15:34:23 +0000 | [diff] [blame] | 1286 | |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1287 | h = prep_bufdata(handle_id, &adjusted_size, true); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1288 | if (!h) |
Nicolas Pennequin | d400e23 | 2007-10-29 14:15:59 +0000 | [diff] [blame] | 1289 | return ERR_HANDLE_NOT_FOUND; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1290 | |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1291 | if (h->ridx + adjusted_size > buffer_len) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1292 | { |
| 1293 | /* the data wraps around the end of the buffer : |
| 1294 | use the guard buffer to provide the requested amount of data. */ |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1295 | size_t copy_n = h->ridx + adjusted_size - buffer_len; |
| 1296 | /* prep_bufdata ensures adjusted_size <= buffer_len - h->ridx + GUARD_BUFSIZE, |
Nicolas Pennequin | ca4771b | 2007-11-08 16:12:28 +0000 | [diff] [blame] | 1297 | so copy_n <= GUARD_BUFSIZE */ |
Steve Bavin | 73f9863 | 2008-03-26 08:57:25 +0000 | [diff] [blame] | 1298 | memcpy(guard_buffer, (const unsigned char *)buffer, copy_n); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Nicolas Pennequin | ecec940 | 2007-12-16 01:38:56 +0000 | [diff] [blame] | 1301 | if (data) |
| 1302 | *data = &buffer[h->ridx]; |
| 1303 | |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1304 | return adjusted_size; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1307 | ssize_t bufgettail(int handle_id, size_t size, void **data) |
Brandon Low | 3386dd7 | 2007-11-28 04:58:16 +0000 | [diff] [blame] | 1308 | { |
| 1309 | size_t tidx; |
| 1310 | |
| 1311 | const struct memory_handle *h; |
Nicolas Pennequin | ecec940 | 2007-12-16 01:38:56 +0000 | [diff] [blame] | 1312 | |
Brandon Low | 3386dd7 | 2007-11-28 04:58:16 +0000 | [diff] [blame] | 1313 | h = find_handle(handle_id); |
| 1314 | |
| 1315 | if (!h) |
| 1316 | return ERR_HANDLE_NOT_FOUND; |
| 1317 | |
| 1318 | if (h->filerem) |
| 1319 | return ERR_HANDLE_NOT_DONE; |
| 1320 | |
| 1321 | /* We don't support tail requests of > guardbuf_size, for simplicity */ |
| 1322 | if (size > GUARD_BUFSIZE) |
| 1323 | return ERR_INVALID_VALUE; |
| 1324 | |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 1325 | tidx = ringbuf_sub(h->widx, size); |
Brandon Low | 3386dd7 | 2007-11-28 04:58:16 +0000 | [diff] [blame] | 1326 | |
| 1327 | if (tidx + size > buffer_len) |
| 1328 | { |
| 1329 | size_t copy_n = tidx + size - buffer_len; |
Steve Bavin | 73f9863 | 2008-03-26 08:57:25 +0000 | [diff] [blame] | 1330 | memcpy(guard_buffer, (const unsigned char *)buffer, copy_n); |
Brandon Low | 3386dd7 | 2007-11-28 04:58:16 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | *data = &buffer[tidx]; |
| 1334 | return size; |
| 1335 | } |
| 1336 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1337 | ssize_t bufcuttail(int handle_id, size_t size) |
Brandon Low | 3386dd7 | 2007-11-28 04:58:16 +0000 | [diff] [blame] | 1338 | { |
Nicolas Pennequin | 4fd2774 | 2008-03-29 14:09:14 +0000 | [diff] [blame] | 1339 | struct memory_handle *h; |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1340 | size_t adjusted_size = size; |
Nicolas Pennequin | ecec940 | 2007-12-16 01:38:56 +0000 | [diff] [blame] | 1341 | |
Brandon Low | 3386dd7 | 2007-11-28 04:58:16 +0000 | [diff] [blame] | 1342 | h = find_handle(handle_id); |
| 1343 | |
| 1344 | if (!h) |
| 1345 | return ERR_HANDLE_NOT_FOUND; |
| 1346 | |
| 1347 | if (h->filerem) |
| 1348 | return ERR_HANDLE_NOT_DONE; |
| 1349 | |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1350 | if (h->available < adjusted_size) |
| 1351 | adjusted_size = h->available; |
Brandon Low | 3386dd7 | 2007-11-28 04:58:16 +0000 | [diff] [blame] | 1352 | |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1353 | h->available -= adjusted_size; |
| 1354 | h->filesize -= adjusted_size; |
Thomas Martitz | b11c819 | 2010-02-12 13:12:59 +0000 | [diff] [blame] | 1355 | h->widx = ringbuf_sub(h->widx, adjusted_size); |
Steve Bavin | 73f9863 | 2008-03-26 08:57:25 +0000 | [diff] [blame] | 1356 | if (h == cur_handle) |
Brandon Low | e959c5a | 2007-11-28 16:39:58 +0000 | [diff] [blame] | 1357 | buf_widx = h->widx; |
Steve Bavin | 73f9863 | 2008-03-26 08:57:25 +0000 | [diff] [blame] | 1358 | |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 1359 | return adjusted_size; |
Brandon Low | 3386dd7 | 2007-11-28 04:58:16 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1363 | /* |
| 1364 | SECONDARY EXPORTED FUNCTIONS |
| 1365 | ============================ |
| 1366 | |
| 1367 | buf_get_offset |
| 1368 | buf_handle_offset |
| 1369 | buf_request_buffer_handle |
| 1370 | buf_set_base_handle |
| 1371 | buf_used |
Nicolas Pennequin | e24454f | 2007-11-26 21:13:08 +0000 | [diff] [blame] | 1372 | register_buffering_callback |
| 1373 | unregister_buffering_callback |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1374 | |
| 1375 | These functions are exported, to allow interaction with the buffer. |
| 1376 | They take care of the content of the structs, and rely on the linked list |
| 1377 | management functions for all the actual handle management work. |
| 1378 | */ |
| 1379 | |
| 1380 | /* Get a handle offset from a pointer */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1381 | ssize_t buf_get_offset(int handle_id, void *ptr) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1382 | { |
Brandon Low | 404c6fb | 2007-10-27 01:37:33 +0000 | [diff] [blame] | 1383 | const struct memory_handle *h = find_handle(handle_id); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1384 | if (!h) |
Nicolas Pennequin | d400e23 | 2007-10-29 14:15:59 +0000 | [diff] [blame] | 1385 | return ERR_HANDLE_NOT_FOUND; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1386 | |
| 1387 | return (size_t)ptr - (size_t)&buffer[h->ridx]; |
| 1388 | } |
| 1389 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1390 | ssize_t buf_handle_offset(int handle_id) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1391 | { |
Brandon Low | 404c6fb | 2007-10-27 01:37:33 +0000 | [diff] [blame] | 1392 | const struct memory_handle *h = find_handle(handle_id); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1393 | if (!h) |
Nicolas Pennequin | d400e23 | 2007-10-29 14:15:59 +0000 | [diff] [blame] | 1394 | return ERR_HANDLE_NOT_FOUND; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1395 | return h->offset; |
| 1396 | } |
| 1397 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1398 | void buf_request_buffer_handle(int handle_id) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1399 | { |
Brandon Low | 86830b6 | 2007-11-05 17:51:55 +0000 | [diff] [blame] | 1400 | LOGFQUEUE("buffering >| Q_START_FILL %d",handle_id); |
| 1401 | queue_send(&buffering_queue, Q_START_FILL, handle_id); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1404 | void buf_set_base_handle(int handle_id) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1405 | { |
Brandon Low | 555a764 | 2007-11-05 15:24:21 +0000 | [diff] [blame] | 1406 | LOGFQUEUE("buffering > Q_BASE_HANDLE %d", handle_id); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1407 | queue_post(&buffering_queue, Q_BASE_HANDLE, handle_id); |
| 1408 | } |
| 1409 | |
| 1410 | /* Return the amount of buffer space used */ |
| 1411 | size_t buf_used(void) |
| 1412 | { |
| 1413 | return BUF_USED; |
| 1414 | } |
| 1415 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1416 | void buf_set_watermark(size_t bytes) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1417 | { |
Björn Stenberg | 6427d12 | 2009-01-10 21:10:56 +0000 | [diff] [blame] | 1418 | conf_watermark = bytes; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Nicolas Pennequin | 4fd2774 | 2008-03-29 14:09:14 +0000 | [diff] [blame] | 1421 | static void shrink_buffer_inner(struct memory_handle *h) |
Steve Bavin | 73f9863 | 2008-03-26 08:57:25 +0000 | [diff] [blame] | 1422 | { |
Brandon Low | a042c72 | 2007-11-03 02:54:34 +0000 | [diff] [blame] | 1423 | if (h == NULL) |
| 1424 | return; |
| 1425 | |
Brandon Low | 555a764 | 2007-11-05 15:24:21 +0000 | [diff] [blame] | 1426 | shrink_buffer_inner(h->next); |
Brandon Low | a042c72 | 2007-11-03 02:54:34 +0000 | [diff] [blame] | 1427 | |
| 1428 | shrink_handle(h); |
Brandon Low | 483dca9 | 2007-10-29 16:48:16 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Nicolas Pennequin | 4fd2774 | 2008-03-29 14:09:14 +0000 | [diff] [blame] | 1431 | static void shrink_buffer(void) |
Steve Bavin | 73f9863 | 2008-03-26 08:57:25 +0000 | [diff] [blame] | 1432 | { |
Brandon Low | 555a764 | 2007-11-05 15:24:21 +0000 | [diff] [blame] | 1433 | logf("shrink_buffer()"); |
| 1434 | shrink_buffer_inner(first_handle); |
| 1435 | } |
| 1436 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1437 | void buffering_thread(void) |
| 1438 | { |
Brandon Low | 11a3661 | 2007-11-03 06:21:32 +0000 | [diff] [blame] | 1439 | bool filling = false; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1440 | struct queue_event ev; |
| 1441 | |
| 1442 | while (true) |
| 1443 | { |
Nicolas Pennequin | 12b6c84 | 2008-03-29 17:40:04 +0000 | [diff] [blame] | 1444 | if (!filling) { |
Nicolas Pennequin | 732df38 | 2008-03-29 17:28:30 +0000 | [diff] [blame] | 1445 | cancel_cpu_boost(); |
Nicolas Pennequin | 12b6c84 | 2008-03-29 17:40:04 +0000 | [diff] [blame] | 1446 | } |
Nicolas Pennequin | 732df38 | 2008-03-29 17:28:30 +0000 | [diff] [blame] | 1447 | |
| 1448 | queue_wait_w_tmo(&buffering_queue, &ev, filling ? 5 : HZ/2); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1449 | |
| 1450 | switch (ev.id) |
| 1451 | { |
Brandon Low | 86830b6 | 2007-11-05 17:51:55 +0000 | [diff] [blame] | 1452 | case Q_START_FILL: |
Nicolas Pennequin | cf36957 | 2008-07-18 23:42:47 +0000 | [diff] [blame] | 1453 | LOGFQUEUE("buffering < Q_START_FILL %d", (int)ev.data); |
Brandon Low | 7b74dd7 | 2007-11-03 21:57:27 +0000 | [diff] [blame] | 1454 | /* Call buffer callbacks here because this is one of two ways |
| 1455 | * to begin a full buffer fill */ |
Jonathan Gordon | 71898e5 | 2008-10-16 10:38:03 +0000 | [diff] [blame] | 1456 | send_event(BUFFER_EVENT_BUFFER_LOW, 0); |
Brandon Low | 555a764 | 2007-11-05 15:24:21 +0000 | [diff] [blame] | 1457 | shrink_buffer(); |
Brandon Low | 47eb569 | 2007-11-05 03:11:58 +0000 | [diff] [blame] | 1458 | queue_reply(&buffering_queue, 1); |
Brandon Low | 86830b6 | 2007-11-05 17:51:55 +0000 | [diff] [blame] | 1459 | filling |= buffer_handle((int)ev.data); |
Brandon Low | 47eb569 | 2007-11-05 03:11:58 +0000 | [diff] [blame] | 1460 | break; |
Brandon Low | 555a764 | 2007-11-05 15:24:21 +0000 | [diff] [blame] | 1461 | |
Brandon Low | 47eb569 | 2007-11-05 03:11:58 +0000 | [diff] [blame] | 1462 | case Q_BUFFER_HANDLE: |
Nicolas Pennequin | cf36957 | 2008-07-18 23:42:47 +0000 | [diff] [blame] | 1463 | LOGFQUEUE("buffering < Q_BUFFER_HANDLE %d", (int)ev.data); |
Brandon Low | 47eb569 | 2007-11-05 03:11:58 +0000 | [diff] [blame] | 1464 | queue_reply(&buffering_queue, 1); |
Brandon Low | 86830b6 | 2007-11-05 17:51:55 +0000 | [diff] [blame] | 1465 | buffer_handle((int)ev.data); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1466 | break; |
| 1467 | |
| 1468 | case Q_RESET_HANDLE: |
Nicolas Pennequin | cf36957 | 2008-07-18 23:42:47 +0000 | [diff] [blame] | 1469 | LOGFQUEUE("buffering < Q_RESET_HANDLE %d", (int)ev.data); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1470 | queue_reply(&buffering_queue, 1); |
| 1471 | reset_handle((int)ev.data); |
| 1472 | break; |
| 1473 | |
| 1474 | case Q_CLOSE_HANDLE: |
Nicolas Pennequin | cf36957 | 2008-07-18 23:42:47 +0000 | [diff] [blame] | 1475 | LOGFQUEUE("buffering < Q_CLOSE_HANDLE %d", (int)ev.data); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1476 | queue_reply(&buffering_queue, close_handle((int)ev.data)); |
| 1477 | break; |
| 1478 | |
Nicolas Pennequin | 483c402 | 2008-02-12 23:15:59 +0000 | [diff] [blame] | 1479 | case Q_HANDLE_ADDED: |
| 1480 | LOGFQUEUE("buffering < Q_HANDLE_ADDED %d", (int)ev.data); |
| 1481 | /* A handle was added: the disk is spinning, so we can fill */ |
| 1482 | filling = true; |
| 1483 | break; |
| 1484 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1485 | case Q_BASE_HANDLE: |
Nicolas Pennequin | cf36957 | 2008-07-18 23:42:47 +0000 | [diff] [blame] | 1486 | LOGFQUEUE("buffering < Q_BASE_HANDLE %d", (int)ev.data); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1487 | base_handle_id = (int)ev.data; |
| 1488 | break; |
| 1489 | |
Thomas Martitz | 35e8b14 | 2010-06-21 16:53:00 +0000 | [diff] [blame] | 1490 | #if (CONFIG_PLATFORM & PLATFORM_NATIVE) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1491 | case SYS_USB_CONNECTED: |
| 1492 | LOGFQUEUE("buffering < SYS_USB_CONNECTED"); |
| 1493 | usb_acknowledge(SYS_USB_CONNECTED_ACK); |
| 1494 | usb_wait_for_disconnect(&buffering_queue); |
| 1495 | break; |
| 1496 | #endif |
| 1497 | |
| 1498 | case SYS_TIMEOUT: |
| 1499 | LOGFQUEUE_SYS_TIMEOUT("buffering < SYS_TIMEOUT"); |
| 1500 | break; |
| 1501 | } |
| 1502 | |
| 1503 | update_data_counters(); |
| 1504 | |
| 1505 | /* If the buffer is low, call the callbacks to get new data */ |
Brandon Low | dc58c3d | 2007-11-03 03:46:22 +0000 | [diff] [blame] | 1506 | if (num_handles > 0 && data_counters.useful <= conf_watermark) |
Jonathan Gordon | 71898e5 | 2008-10-16 10:38:03 +0000 | [diff] [blame] | 1507 | send_event(BUFFER_EVENT_BUFFER_LOW, 0); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1508 | |
Brandon Low | 1c36a66 | 2007-11-04 00:23:22 +0000 | [diff] [blame] | 1509 | #if 0 |
| 1510 | /* TODO: This needs to be fixed to use the idle callback, disable it |
| 1511 | * for simplicity until its done right */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1512 | #if MEM > 8 |
| 1513 | /* If the disk is spinning, take advantage by filling the buffer */ |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 1514 | else if (storage_disk_is_active() && queue_empty(&buffering_queue)) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1515 | { |
Brandon Low | dc58c3d | 2007-11-03 03:46:22 +0000 | [diff] [blame] | 1516 | if (num_handles > 0 && data_counters.useful <= high_watermark) |
Jonathan Gordon | 71898e5 | 2008-10-16 10:38:03 +0000 | [diff] [blame] | 1517 | send_event(BUFFER_EVENT_BUFFER_LOW, 0); |
Brandon Low | 11a3661 | 2007-11-03 06:21:32 +0000 | [diff] [blame] | 1518 | |
| 1519 | if (data_counters.remaining > 0 && BUF_USED <= high_watermark) |
| 1520 | { |
Brandon Low | 60d4e7c | 2007-11-03 17:55:45 +0000 | [diff] [blame] | 1521 | /* This is a new fill, shrink the buffer up first */ |
| 1522 | if (!filling) |
Brandon Low | 555a764 | 2007-11-05 15:24:21 +0000 | [diff] [blame] | 1523 | shrink_buffer(); |
Brandon Low | 11a3661 | 2007-11-03 06:21:32 +0000 | [diff] [blame] | 1524 | filling = fill_buffer(); |
| 1525 | update_data_counters(); |
Nicolas Pennequin | 09bce70 | 2007-10-30 17:24:31 +0000 | [diff] [blame] | 1526 | } |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1527 | } |
| 1528 | #endif |
Brandon Low | 1c36a66 | 2007-11-04 00:23:22 +0000 | [diff] [blame] | 1529 | #endif |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1530 | |
Brandon Low | 60d4e7c | 2007-11-03 17:55:45 +0000 | [diff] [blame] | 1531 | if (queue_empty(&buffering_queue)) { |
| 1532 | if (filling) { |
| 1533 | if (data_counters.remaining > 0 && BUF_USED < buffer_len) |
| 1534 | filling = fill_buffer(); |
Nicolas Pennequin | a2191b9 | 2008-07-02 21:50:42 +0000 | [diff] [blame] | 1535 | else if (data_counters.remaining == 0) |
| 1536 | filling = false; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1537 | } |
Brandon Low | 60d4e7c | 2007-11-03 17:55:45 +0000 | [diff] [blame] | 1538 | else if (ev.id == SYS_TIMEOUT) |
| 1539 | { |
| 1540 | if (data_counters.remaining > 0 && |
Brandon Low | 47eb569 | 2007-11-05 03:11:58 +0000 | [diff] [blame] | 1541 | data_counters.useful <= conf_watermark) { |
Brandon Low | 555a764 | 2007-11-05 15:24:21 +0000 | [diff] [blame] | 1542 | shrink_buffer(); |
Brandon Low | 60d4e7c | 2007-11-03 17:55:45 +0000 | [diff] [blame] | 1543 | filling = fill_buffer(); |
Brandon Low | 47eb569 | 2007-11-05 03:11:58 +0000 | [diff] [blame] | 1544 | } |
Brandon Low | 60d4e7c | 2007-11-03 17:55:45 +0000 | [diff] [blame] | 1545 | } |
Brandon Low | 11a3661 | 2007-11-03 06:21:32 +0000 | [diff] [blame] | 1546 | } |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1547 | } |
| 1548 | } |
| 1549 | |
Nicolas Pennequin | 4fd2774 | 2008-03-29 14:09:14 +0000 | [diff] [blame] | 1550 | void buffering_init(void) |
Steve Bavin | 73f9863 | 2008-03-26 08:57:25 +0000 | [diff] [blame] | 1551 | { |
Brandon Low | 14b6f43 | 2007-10-27 04:16:41 +0000 | [diff] [blame] | 1552 | mutex_init(&llist_mutex); |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 1553 | mutex_init(&llist_mod_mutex); |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 1554 | #ifdef HAVE_PRIORITY_SCHEDULING |
| 1555 | /* This behavior not safe atm */ |
| 1556 | mutex_set_preempt(&llist_mutex, false); |
Andrew Mahone | 216424a | 2009-05-15 00:14:38 +0000 | [diff] [blame] | 1557 | mutex_set_preempt(&llist_mod_mutex, false); |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 1558 | #endif |
Brandon Low | 14b6f43 | 2007-10-27 04:16:41 +0000 | [diff] [blame] | 1559 | |
Brandon Low | 14b6f43 | 2007-10-27 04:16:41 +0000 | [diff] [blame] | 1560 | conf_watermark = BUFFERING_DEFAULT_WATERMARK; |
| 1561 | |
Brandon Low | 2c1e828 | 2007-10-27 04:19:17 +0000 | [diff] [blame] | 1562 | queue_init(&buffering_queue, true); |
Michael Sevakis | 8cfbd36 | 2008-12-10 08:57:10 +0000 | [diff] [blame] | 1563 | buffering_thread_id = create_thread( buffering_thread, buffering_stack, |
Brandon Low | 7104ad5 | 2007-10-27 04:29:46 +0000 | [diff] [blame] | 1564 | sizeof(buffering_stack), CREATE_THREAD_FROZEN, |
Michael Sevakis | 8a6fd3f | 2008-03-29 23:21:19 +0000 | [diff] [blame] | 1565 | buffering_thread_name IF_PRIO(, PRIORITY_BUFFERING) |
Brandon Low | 14b6f43 | 2007-10-27 04:16:41 +0000 | [diff] [blame] | 1566 | IF_COP(, CPU)); |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 1567 | |
| 1568 | queue_enable_queue_send(&buffering_queue, &buffering_queue_sender_list, |
Michael Sevakis | 8cfbd36 | 2008-12-10 08:57:10 +0000 | [diff] [blame] | 1569 | buffering_thread_id); |
Brandon Low | 14b6f43 | 2007-10-27 04:16:41 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1572 | /* Initialise the buffering subsystem */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1573 | bool buffering_reset(char *buf, size_t buflen) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1574 | { |
| 1575 | if (!buf || !buflen) |
| 1576 | return false; |
| 1577 | |
| 1578 | buffer = buf; |
Rafaël Carré | 2494afc | 2010-06-23 04:34:18 +0000 | [diff] [blame] | 1579 | buffer_len = buflen; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1580 | guard_buffer = buf + buflen; |
| 1581 | |
| 1582 | buf_widx = 0; |
| 1583 | buf_ridx = 0; |
| 1584 | |
| 1585 | first_handle = NULL; |
Nicolas Pennequin | d08131a | 2007-10-27 01:25:47 +0000 | [diff] [blame] | 1586 | cur_handle = NULL; |
| 1587 | cached_handle = NULL; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1588 | num_handles = 0; |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 1589 | base_handle_id = -1; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1590 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1591 | /* Set the high watermark as 75% full...or 25% empty :) */ |
| 1592 | #if MEM > 8 |
| 1593 | high_watermark = 3*buflen / 4; |
| 1594 | #endif |
| 1595 | |
Michael Sevakis | 8cfbd36 | 2008-12-10 08:57:10 +0000 | [diff] [blame] | 1596 | thread_thaw(buffering_thread_id); |
Brandon Low | 7104ad5 | 2007-10-27 04:29:46 +0000 | [diff] [blame] | 1597 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1598 | return true; |
| 1599 | } |
| 1600 | |
| 1601 | void buffering_get_debugdata(struct buffering_debug *dbgdata) |
| 1602 | { |
| 1603 | update_data_counters(); |
| 1604 | dbgdata->num_handles = num_handles; |
| 1605 | dbgdata->data_rem = data_counters.remaining; |
| 1606 | dbgdata->wasted_space = data_counters.wasted; |
| 1607 | dbgdata->buffered_data = data_counters.buffered; |
| 1608 | dbgdata->useful_data = data_counters.useful; |
Thomas Martitz | fcbfef8 | 2009-02-23 22:55:48 +0000 | [diff] [blame] | 1609 | dbgdata->watermark = conf_watermark; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1610 | } |