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 | #ifndef _BUFFERING_H_ |
| 23 | #define _BUFFERING_H_ |
| 24 | |
| 25 | #include <sys/types.h> |
| 26 | #include <stdbool.h> |
Boris Gjenero | fdc29d0 | 2011-12-19 20:12:52 +0000 | [diff] [blame] | 27 | #include "config.h" |
Jonathan Gordon | 71898e5 | 2008-10-16 10:38:03 +0000 | [diff] [blame] | 28 | #include "appevents.h" |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 29 | |
| 30 | |
| 31 | enum data_type { |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 32 | TYPE_UNKNOWN = 0, /* invalid type indicator */ |
| 33 | TYPE_ID3, |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 34 | TYPE_CODEC, |
Brandon Low | dcca586 | 2007-11-02 14:06:48 +0000 | [diff] [blame] | 35 | TYPE_PACKET_AUDIO, |
| 36 | TYPE_ATOMIC_AUDIO, |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 37 | TYPE_CUESHEET, |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 38 | TYPE_BITMAP, |
Michael Sevakis | c1a01be | 2017-12-08 13:01:25 -0500 | [diff] [blame] | 39 | TYPE_RAW_ATOMIC, |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
Nicolas Pennequin | 7807279 | 2007-10-28 15:54:10 +0000 | [diff] [blame] | 42 | /* Error return values */ |
Nicolas Pennequin | d400e23 | 2007-10-29 14:15:59 +0000 | [diff] [blame] | 43 | #define ERR_HANDLE_NOT_FOUND -1 |
| 44 | #define ERR_BUFFER_FULL -2 |
| 45 | #define ERR_INVALID_VALUE -3 |
| 46 | #define ERR_FILE_ERROR -4 |
Brandon Low | 3386dd7 | 2007-11-28 04:58:16 +0000 | [diff] [blame] | 47 | #define ERR_HANDLE_NOT_DONE -5 |
Thomas Martitz | 86cab2e | 2011-02-09 20:27:23 +0000 | [diff] [blame] | 48 | #define ERR_UNSUPPORTED_TYPE -6 |
Michael Sevakis | 3661581 | 2013-08-26 16:49:53 -0400 | [diff] [blame] | 49 | #define ERR_WRONG_THREAD -7 |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 50 | |
| 51 | /* Initialise the buffering subsystem */ |
Boris Gjenero | fdc29d0 | 2011-12-19 20:12:52 +0000 | [diff] [blame] | 52 | void buffering_init(void) INIT_ATTR; |
Brandon Low | 14b6f43 | 2007-10-27 04:16:41 +0000 | [diff] [blame] | 53 | |
| 54 | /* Reset the buffering system */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 55 | bool buffering_reset(char *buf, size_t buflen); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 56 | |
| 57 | |
| 58 | /*************************************************************************** |
| 59 | * MAIN BUFFERING API CALLS |
| 60 | * ======================== |
| 61 | * |
| 62 | * bufopen : Reserve space in the buffer for a given file |
| 63 | * bufalloc : Open a new handle from data that needs to be copied from memory |
| 64 | * bufclose : Close an open handle |
| 65 | * bufseek : Set handle reading index, relatively to the start of the file |
| 66 | * bufadvance: Move handle reading index, relatively to current position |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 67 | * bufftell : Return the handle's file read position |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 68 | * bufread : Copy data from a handle to a buffer |
| 69 | * bufgetdata: Obtain a pointer for linear access to a "size" amount of data |
Brandon Low | 3386dd7 | 2007-11-28 04:58:16 +0000 | [diff] [blame] | 70 | * bufgettail: Out-of-band get the last size bytes of a handle. |
| 71 | * bufcuttail: Out-of-band remove the trailing 'size' bytes of a handle. |
Nicolas Pennequin | b838a62 | 2007-11-02 19:13:03 +0000 | [diff] [blame] | 72 | * |
| 73 | * NOTE: bufread and bufgetdata will block the caller until the requested |
| 74 | * amount of data is ready (unless EOF is reached). |
Brandon Low | 3386dd7 | 2007-11-28 04:58:16 +0000 | [diff] [blame] | 75 | * NOTE: Tail operations are only legal when the end of the file is buffered. |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 76 | ****************************************************************************/ |
| 77 | |
Michael Sevakis | dfff938 | 2017-12-17 16:12:10 -0500 | [diff] [blame] | 78 | int bufopen(const char *file, off_t offset, enum data_type type, |
Thomas Martitz | e9c1018 | 2009-10-16 19:14:41 +0000 | [diff] [blame] | 79 | void *user_data); |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 80 | int bufalloc(const void *src, size_t size, enum data_type type); |
| 81 | bool bufclose(int handle_id); |
| 82 | int bufseek(int handle_id, size_t newpos); |
| 83 | int bufadvance(int handle_id, off_t offset); |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 84 | off_t bufftell(int handle_id); |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 85 | ssize_t bufread(int handle_id, size_t size, void *dest); |
| 86 | ssize_t bufgetdata(int handle_id, size_t size, void **data); |
| 87 | ssize_t bufgettail(int handle_id, size_t size, void **data); |
| 88 | ssize_t bufcuttail(int handle_id, size_t size); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 89 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 90 | /*************************************************************************** |
| 91 | * SECONDARY FUNCTIONS |
| 92 | * =================== |
| 93 | * |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 94 | * buf_handle_data_type: return the handle's data type |
| 95 | * buf_is_handle: is the handle valid? |
| 96 | * buf_pin_handle: Disallow/allow handle movement. Handle may still be removed. |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 97 | * buf_handle_offset: Get the offset of the first buffered byte from the file |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 98 | * buf_set_base_handle: Tell the buffering thread which handle is currently read |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 99 | * buf_length: Total size of ringbuffer |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 100 | * buf_used: Total amount of buffer space used (including allocated space) |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 101 | * buf_back_off_storage: tell buffering thread to take it easy |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 102 | ****************************************************************************/ |
| 103 | |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 104 | bool buf_is_handle(int handle_id); |
Michael Sevakis | dfff938 | 2017-12-17 16:12:10 -0500 | [diff] [blame] | 105 | int buf_handle_data_type(int handle_id); |
| 106 | off_t buf_filesize(int handle_id); |
| 107 | off_t buf_handle_offset(int handle_id); |
| 108 | off_t buf_handle_remaining(int handle_id); |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 109 | bool buf_pin_handle(int handle_id, bool pin); |
| 110 | bool buf_signal_handle(int handle_id, bool signal); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 111 | |
Michael Sevakis | dfff938 | 2017-12-17 16:12:10 -0500 | [diff] [blame] | 112 | size_t buf_length(void); |
| 113 | size_t buf_used(void); |
| 114 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 115 | /* Settings */ |
Michael Sevakis | dfff938 | 2017-12-17 16:12:10 -0500 | [diff] [blame] | 116 | void buf_set_base_handle(int handle_id); |
Brandon Low | 3379440 | 2007-11-05 17:48:21 +0000 | [diff] [blame] | 117 | void buf_set_watermark(size_t bytes); |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 118 | size_t buf_get_watermark(void); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 119 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 120 | /* Debugging */ |
| 121 | struct buffering_debug { |
| 122 | int num_handles; |
| 123 | size_t buffered_data; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 124 | size_t data_rem; |
| 125 | size_t useful_data; |
Thomas Martitz | fcbfef8 | 2009-02-23 22:55:48 +0000 | [diff] [blame] | 126 | size_t watermark; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 127 | }; |
| 128 | void buffering_get_debugdata(struct buffering_debug *dbgdata); |
| 129 | |
| 130 | #endif |