File: MPEGring.cpp

package info (click to toggle)
smpeg 0.4.4-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,892 kB
  • ctags: 1,445
  • sloc: cpp: 14,948; sh: 8,962; ansic: 2,323; asm: 542; makefile: 162
file content (259 lines) | stat: -rw-r--r-- 6,630 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
    SMPEG - SDL MPEG Player Library
    Copyright (C) 1999  Loki Entertainment Software

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>

#include "SDL_timer.h"

#include "MPEGring.h"


MPEG_ring:: MPEG_ring( Uint32 size, Uint32 count )
{
    Uint32 tSize;

    /* Set up the 'ring' pointer for all the old C code */
    ring = this;

    tSize = (size + sizeof(Uint32)) * count;
    if( tSize )
    {
        ring->begin = (Uint8 *) malloc( tSize );
        ring->timestamps = (double *) malloc( sizeof(double)*count );
    }
    else
        ring->begin = 0;

    if( ring->begin && count )
    {
        ring->end   = ring->begin + tSize;
        ring->read  = ring->begin;
        ring->write = ring->begin;
        ring->timestamp_read  = timestamps;
        ring->timestamp_write = timestamps;
        ring->bufSize  = size;
        
        ring->readwait = SDL_CreateSemaphore(0);
        ring->writewait = SDL_CreateSemaphore(count);
    }
    else
    {
        ring->end   = 0;
        ring->read  = 0;
        ring->write = 0;
        ring->bufSize  = 0;

        ring->readwait = 0;
    }

    if ( ring->begin && ring->readwait && ring->writewait ) {
        ring->active = 1;
    }
}

/* Release any waiting threads on the ring so they can be cleaned up.
   The ring isn't valid after this call, so when threads are done you
   should call MPRing_sdelete() on the ring.
 */
void
MPEG_ring:: ReleaseThreads( void )
{
    /* Let the threads know that the ring is now inactive */
    ring->active = 0;

    if ( ring->readwait ) {
        while ( SDL_SemValue(ring->readwait) == 0 ) {
            SDL_SemPost(ring->readwait);
        }
    }
    if ( ring->writewait ) {
        while ( SDL_SemValue(ring->writewait) == 0 ) {
            SDL_SemPost(ring->writewait);
        }
    }
}


MPEG_ring:: ~MPEG_ring( void )
{
    if( ring )
    {
        /* Free up the semaphores */
        ReleaseThreads();

	/* Destroy the semaphores */
	if( ring->readwait )
	{
	    SDL_DestroySemaphore( ring->readwait );
	    ring->readwait = 0;
	}
	if( ring->writewait )
	{
	    SDL_DestroySemaphore( ring->writewait );
	    ring->writewait = 0;
	}

        /* Free data buffer */
        if ( ring->begin ) {
            free( ring->begin );
            free( ring->timestamps );
            ring->begin = 0;
            ring->timestamps = 0;
        }
    }
}

/*
  Returns free buffer of ring->bufSize to be filled with data.
  Zero is returned if there is no free buffer.
*/

Uint8 *
MPEG_ring:: NextWriteBuffer( void )
{
    Uint8 *buffer;

    buffer = 0;
    if ( ring->active ) {
	//printf("Waiting for write buffer (%d available)\n", SDL_SemValue(ring->writewait));
        SDL_SemWait(ring->writewait);
	//printf("Finished waiting for write buffer\n");
	if ( ring->active ) {
            buffer = ring->write + sizeof(Uint32);
        }
    }
    return buffer;
}


/*
  Call this when the buffer returned from MPRing_nextWriteBuffer() has
  been filled.  The passed length must not be larger than ring->bufSize.
*/

void
MPEG_ring:: WriteDone( Uint32 len, double timestamp)
{
    if ( ring->active ) {
#ifdef NO_GRIFF_MODS
        assert(len <= ring->bufSize);
#else
	if ( len > ring->bufSize )
            len = ring->bufSize;
#endif
        *((Uint32*) ring->write) = len;

        ring->write += ring->bufSize + sizeof(Uint32);
        *(ring->timestamp_write++) = timestamp;
        if( ring->write >= ring->end )
        {
            ring->write = ring->begin;
            ring->timestamp_write = ring->timestamps;
        }
//printf("Finished write buffer of %u bytes, making available for reads (%d+1 available for reads)\n", len, SDL_SemValue(ring->readwait));
        SDL_SemPost(ring->readwait);
    }
}


/*
  Returns the number of bytes in the next ring buffer and sets the buffer
  pointer to this buffer.  If there is no buffer ready then the buffer
  pointer is not changed and zero is returned.
*/

Uint32
MPEG_ring:: NextReadBuffer( Uint8** buffer )
{
    Uint32 size;

    size = 0;
    if ( ring->active ) {
        /* Wait for a buffer to become available */
//printf("Waiting for read buffer (%d available)\n", SDL_SemValue(ring->readwait));
        SDL_SemWait(ring->readwait);
//printf("Finished waiting for read buffer\n");
	if ( ring->active ) {
            size = *((Uint32*) ring->read);
            *buffer = ring->read + sizeof(Uint32);
        }
    }
    return size;
}

/*
  Call this when you have used some of the buffer previously returned by
  MPRing_nextReadBuffer(), and want to update it so the rest of the data
  is returned with the next call to MPRing_nextReadBuffer().
*/

double
MPEG_ring:: ReadTimeStamp(void)
{
  if(ring->active)
    return *ring->timestamp_read;
  return(0);
}

void
MPEG_ring:: ReadSome( Uint32 used )
{
    Uint8 *data;
    Uint32 oldlen;
    Uint32 newlen;

    if ( ring->active ) {
        data = ring->read + sizeof(Uint32);
        oldlen = *((Uint32*) ring->read);
        newlen = oldlen - used;
        memmove(data, data+used, newlen);
        *((Uint32*) ring->read) = newlen;
//printf("Reusing read buffer (%d+1 available)\n", SDL_SemValue(ring->readwait));
        SDL_SemPost(ring->readwait);
    }
}

/*
  Call this when the buffer returned from MPRing_nextReadBuffer() is no
  longer needed.  This assumes there is only one read thread and one write
  thread for a particular ring buffer.
*/

void
MPEG_ring:: ReadDone( void )
{
    if ( ring->active ) {
        ring->read += ring->bufSize + sizeof(Uint32);
        ring->timestamp_read++;
        if( ring->read >= ring->end )
        {
            ring->read = ring->begin;
            ring->timestamp_read = ring->timestamps;
        }
//printf("Finished read buffer, making available for writes (%d+1 available for writes)\n", SDL_SemValue(ring->writewait));
        SDL_SemPost(ring->writewait);
    }
}


/* EOF */