Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3 | pmbaty | 1 | /* |
2 | SMPEG - SDL MPEG Player Library |
||
3 | Copyright (C) 1999 Loki Entertainment Software |
||
4 | |||
5 | This library is free software; you can redistribute it and/or |
||
6 | modify it under the terms of the GNU Library General Public |
||
7 | License as published by the Free Software Foundation; either |
||
8 | version 2 of the License, or (at your option) any later version. |
||
9 | |||
10 | This library is distributed in the hope that it will be useful, |
||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||
13 | Library General Public License for more details. |
||
14 | |||
15 | You should have received a copy of the GNU Library General Public |
||
16 | License along with this library; if not, write to the Free |
||
17 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
||
18 | */ |
||
19 | |||
20 | /* A ring-buffer class for multi-threaded applications. |
||
21 | This assumes a single reader and a single writer, with blocking reads. |
||
22 | */ |
||
23 | |||
24 | #ifndef _MPEGRING_H |
||
25 | #define _MPEGRING_H |
||
26 | |||
27 | #include "SDL_types.h" |
||
28 | #include "SDL_thread.h" |
||
29 | |||
30 | class MPEG_ring { |
||
31 | public: |
||
32 | /* Create a ring with 'count' buffers, each 'size' bytes long */ |
||
33 | MPEG_ring(Uint32 size, Uint32 count = 16); |
||
34 | |||
35 | /* Release any waiting threads on the ring so they can be cleaned up. |
||
36 | The ring isn't valid after this call, so when threads are done you |
||
37 | should call MPRing_sdelete() on the ring. |
||
38 | */ |
||
39 | void ReleaseThreads(void); |
||
40 | |||
41 | /* Destroy a ring after all threads are no longer using it */ |
||
42 | virtual ~MPEG_ring(); |
||
43 | |||
44 | /* Returns the maximum size of each buffer */ |
||
45 | Uint32 BufferSize( void ) { |
||
46 | return(bufSize); |
||
47 | } |
||
48 | /* Returns how many buffers have available data */ |
||
49 | int BuffersWritten(void) { |
||
50 | return(SDL_SemValue(ring->readwait)); |
||
51 | } |
||
52 | |||
53 | /* Reserve a buffer for writing in the ring */ |
||
54 | Uint8 *NextWriteBuffer( void ); |
||
55 | |||
56 | /* Release a buffer, written to in the ring */ |
||
57 | void WriteDone( Uint32 len, double timestamp=-1 ); |
||
58 | |||
59 | /* Reserve a buffer for reading in the ring */ |
||
60 | Uint32 NextReadBuffer( Uint8** buffer ); |
||
61 | |||
62 | /* Read the timestamp of the current buffer */ |
||
63 | double ReadTimeStamp(void); |
||
64 | |||
65 | /* Release a buffer having read some of it */ |
||
66 | void ReadSome( Uint32 used ); |
||
67 | |||
68 | /* Release a buffer having read all of it */ |
||
69 | void ReadDone( void ); |
||
70 | |||
71 | protected: |
||
72 | MPEG_ring *ring; /* Converted from C code, an alias for 'this' */ |
||
73 | |||
74 | /* read only */ |
||
75 | Uint32 bufSize; |
||
76 | |||
77 | /* private */ |
||
78 | Uint8 *begin; |
||
79 | Uint8 *end; |
||
80 | |||
81 | double *timestamps; |
||
82 | double *timestamp_read; |
||
83 | double *timestamp_write; |
||
84 | |||
85 | Uint8 *read; |
||
86 | Uint8 *write; |
||
87 | |||
88 | /* For read/write synchronization */ |
||
89 | int active; |
||
90 | SDL_semaphore *readwait; |
||
91 | SDL_semaphore *writewait; |
||
92 | }; |
||
93 | |||
94 | #endif /* _MPEGRING_H */ |