A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 152 lines 5.5 kB view raw
1/*************************************************************************** 2 * __________ __ ___. 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 7 * \/ \/ \/ \/ \/ 8 * $Id$ 9 * 10 * AV disk buffer declarations 11 * 12 * Copyright (c) 2007 Michael Sevakis 13 * 14 * This program is free software; you can redistribute it and/or 15 * modify it under the terms of the GNU General Public License 16 * as published by the Free Software Foundation; either version 2 17 * of the License, or (at your option) any later version. 18 * 19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 20 * KIND, either express or implied. 21 * 22 ****************************************************************************/ 23#ifndef DISK_BUF_H 24#define DISK_BUF_H 25 26#ifndef OFF_T_MAX 27#define OFF_T_MAX (~((off_t)1 << (sizeof (off_t)*8 - 1))) 28#endif 29 30#ifndef OFF_T_MIN 31#define OFF_T_MIN ((off_t)1 << (sizeof (off_t)*8 - 1)) 32#endif 33 34#define DISK_BUF_PAGE_SHIFT 15 /* 32KB cache lines */ 35#define DISK_BUF_PAGE_SIZE (1 << DISK_BUF_PAGE_SHIFT) 36#define DISK_BUF_PAGE_MASK (DISK_BUF_PAGE_SIZE-1) 37 38enum 39{ 40 DISK_BUF_NOTIFY_ERROR = -1, 41 DISK_BUF_NOTIFY_NULL = 0, 42 DISK_BUF_NOTIFY_OK, 43 DISK_BUF_NOTIFY_TIMEDOUT, 44 DISK_BUF_NOTIFY_PROCESS_EVENT, 45 DISK_BUF_NOTIFY_REGISTERED, 46}; 47 48/** Macros to map file offsets to cached data **/ 49 50/* Returns a cache tag given a file offset */ 51#define MAP_OFFSET_TO_TAG(o) \ 52 ((o) >> DISK_BUF_PAGE_SHIFT) 53 54/* Returns the cache page number given a file offset */ 55#define MAP_OFFSET_TO_PAGE(o) \ 56 (MAP_OFFSET_TO_TAG(o) % disk_buf.pgcount) 57 58/* Returns the buffer offset given a file offset */ 59#define MAP_OFFSET_TO_BUFFER(o) \ 60 (MAP_OFFSET_TO_PAGE(o) * DISK_BUF_PAGE_SIZE) 61 62struct dbuf_range 63{ 64 uint32_t tag_start; 65 uint32_t tag_end; 66 int pg_start; 67}; 68 69#define DISK_BUF_L2_CACHE_SHIFT 6 70#define DISK_BUF_L2_CACHE_SIZE (1 << DISK_BUF_L2_CACHE_SHIFT) 71#define DISK_BUF_L2_CACHE_MASK (DISK_BUF_L2_CACHE_SIZE-1) 72 73struct dbuf_l2_cache 74{ 75 off_t addr; /* L2 file offset */ 76 size_t size; /* Real size */ 77 uint8_t data[DISK_BUF_L2_CACHE_SIZE*2]; /* Local data and guard */ 78}; 79 80void dbuf_l2_init(struct dbuf_l2_cache *l2_p); 81 82/* This object is an extension of the stream manager and handles some 83 * playback events as well as buffering */ 84struct disk_buf 85{ 86 unsigned int thread; 87 struct event_queue *q; 88 uint8_t *start; /* Start pointer */ 89 uint8_t *end; /* End of buffer pointer less MPEG_GUARDBUF_SIZE. The 90 guard space is used to wrap data at the buffer start to 91 pass continuous data packets */ 92 uint8_t *tail; /* Location of last data + 1 filled into the buffer */ 93 ssize_t size; /* The buffer length _not_ including the guard space (end-start) */ 94 int pgcount; /* Total number of available cached pages */ 95 uint32_t *cache; /* Pointer to cache structure - allocated on buffer */ 96 int in_file; /* File being read */ 97 ssize_t filesize; /* Size of file in_file in bytes */ 98 int file_pages; /* Number of pages in file (rounded up) */ 99 off_t offset; /* Current position (random access) */ 100 off_t win_left; /* Left edge of buffer window (streaming) */ 101 off_t win_right; /* Right edge of buffer window (streaming) */ 102 uint32_t time_last; /* Last time watermark was checked */ 103 off_t pos_last; /* Last position at watermark check time */ 104 ssize_t low_wm; /* The low watermark for automatic rebuffering */ 105 int status; /* Status as stream */ 106 int state; /* Current thread state */ 107 bool need_seek; /* Need to seek because a read was not contiguous */ 108}; 109 110extern struct disk_buf disk_buf SHAREDBSS_ATTR; 111 112struct stream_hdr; 113bool disk_buf_is_data_ready(struct stream_hdr *sh, ssize_t margin); 114 115bool disk_buf_init(void); 116void disk_buf_exit(void); 117 118static inline int disk_buf_status(void) 119 { return disk_buf.status; } 120 121int disk_buf_open(const char *filename); 122void disk_buf_close(void); 123ssize_t _disk_buf_getbuffer(size_t size, void **pp, void **pwrap, 124 size_t *sizewrap); 125#define disk_buf_getbuffer(size, pp, pwrap, sizewrap) \ 126 _disk_buf_getbuffer((size), PUN_PTR(void **, (pp)), \ 127 PUN_PTR(void **, (pwrap)), (sizewrap)) 128 129ssize_t _disk_buf_getbuffer_l2(struct dbuf_l2_cache *l2, 130 size_t size, void **pp); 131#define disk_buf_getbuffer_l2(l2, size, pp) \ 132 _disk_buf_getbuffer_l2((l2), (size), PUN_PTR(void **, (pp))) 133 134ssize_t disk_buf_read(void *buffer, size_t size); 135ssize_t disk_buf_lseek(off_t offset, int whence); 136 137static inline off_t disk_buf_ftell(void) 138 { return disk_buf.offset; } 139 140static inline ssize_t disk_buf_filesize(void) 141 { return disk_buf.filesize; } 142 143ssize_t disk_buf_prepare_streaming(off_t pos, size_t len); 144ssize_t disk_buf_set_streaming_window(off_t left, off_t right); 145void * disk_buf_offset2ptr(off_t offset); 146int disk_buf_check_streaming_window(off_t left, off_t right); 147 148intptr_t disk_buf_send_msg(long id, intptr_t data); 149void disk_buf_post_msg(long id, intptr_t data); 150void disk_buf_reply_msg(intptr_t retval); 151 152#endif /* DISK_BUF_H */