xref: /haiku/src/kits/media/ChunkCache.h (revision 95bac3fda53a4cb21880712d7b43f8c21db32a2e)
1 /*
2  * Copyright (c) 2004, Marcus Overhagen
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  *  * Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23  * OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #ifndef _CHUNK_CACHE_H
26 #define _CHUNK_CACHE_H
27 
28 #include <MediaDefs.h>
29 
30 namespace BPrivate {
31 namespace media {
32 
33 
34 struct chunk_info
35 {
36 	chunk_info *	next;
37 	void *			buffer;
38 	int32			sizeUsed;
39 	int32			sizeMax;
40 	media_header	mediaHeader;
41 	status_t		err;
42 };
43 
44 
45 class ChunkCache
46 {
47 public:
48 					ChunkCache();
49 					~ChunkCache();
50 
51 	void			MakeEmpty();
52 	bool			NeedsRefill();
53 
54 	status_t		GetNextChunk(void **chunkBuffer, int32 *chunkSize, media_header *mediaHeader);
55 	void			PutNextChunk(void *chunkBuffer, int32 chunkSize, const media_header &mediaHeader, status_t err);
56 
57 private:
58 	enum { CHUNK_COUNT = 5 };
59 
60 	chunk_info *	fNextPut;
61 	chunk_info *	fNextGet;
62 	chunk_info		fChunkInfos[CHUNK_COUNT];
63 
64 	sem_id			fGetWaitSem;
65 	int32			fEmptyChunkCount;
66 	int32			fReadyChunkCount;
67 	int32			fNeedsRefill;
68 
69 	BLocker *		fLocker;
70 };
71 
72 
73 }; // namespace media
74 }; // namespace BPrivate
75 
76 using namespace BPrivate::media;
77 
78 #endif
79