File: MPEGstream.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 (348 lines) | stat: -rw-r--r-- 7,077 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
    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.
*/

/* The generic MPEG stream class */

#include <string.h>

#include "MPEG.h"
#include "MPEGstream.h"
#include "video/video.h"

/* This is the limit of the quantity of pre-read data */
#define MAX_QUEUE (256 * 1024)

MPEGstream::MPEGstream(MPEGsystem * System, Uint8 Streamid)
{
  system = System;
  streamid = Streamid;
  br = new MPEGlist();
  cleareof = true;

  data = 0;
  stop = 0;
  pos = 0;
  
  preread_size = 0;
  enabled = true;
  mutex = SDL_CreateMutex();
}

MPEGstream::~MPEGstream()
{
  MPEGlist * newbr;

  SDL_DestroyMutex(mutex);

  /* Free the list */
  for(newbr = br; newbr->Prev(); newbr = newbr->Prev());

  while(newbr->Next())
  {
    newbr = newbr->Next();
    delete newbr->Prev();
  }
  delete newbr;
}

void
MPEGstream::reset_stream()
{
  MPEGlist * newbr;

  SDL_mutexP(mutex);
  /* Seek the first buffer */
  for(newbr = br; newbr->Prev(); newbr = newbr->Prev());
  
  /* Free buffers  */
  while(newbr->Next())
  {
    newbr = newbr->Next();
    delete newbr->Prev();
  } 
  delete newbr;

  br = new MPEGlist();
  cleareof = true;
  data = 0;
  stop = 0;
  pos = 0;
  preread_size = 0;
  SDL_mutexV(mutex);
}

void
MPEGstream::rewind_stream()
{
  /* Note that this will rewind all streams, and other streams than this one */
  /* will finish reading their prebuffured data (they are not reseted) */
  /* This should works because there are always sequence start codes or */
  /* audio start codes at the beginning of the streams */
  /* Of course, this won't work on network streams */

  /* Restart the system */
  system->Rewind();
}

bool
MPEGstream:: next_system_buffer(void)
{
  bool has_data = true;

  /* No more buffer ? */
  while(has_data && !br->Next())
  {
    SDL_mutexV(mutex);
    system->RequestBuffer();
    has_data = system->Wait();
    SDL_mutexP(mutex);
  }

  if ( has_data && (br->Size() || cleareof) ) {
    cleareof = false;
    br = br->Next();
    preread_size -= br->Size();
  }
  return(has_data);
}

bool
MPEGstream:: next_packet(bool recurse, bool update_timestamp)
{
  SDL_mutexP(mutex);

  /* Unlock current buffer */
  br->Unlock();

  /* Check for the end of stream mark */
  next_system_buffer();
  if(eof())
  {
    /* Report eof */
    SDL_mutexV(mutex);
    return(false);
  }

  /* Lock the buffer */
  br->Lock();

  /* Make sure that we have read buffers in advance if possible */
  if(preread_size < MAX_QUEUE)
    system->RequestBuffer();
  
  /* Update stream datas */
  data = (Uint8 *) br->Buffer();
  stop = data + br->Size();
  if(update_timestamp){
    timestamp = br->TimeStamp;
    timestamp_pos = pos;
  }
  SDL_mutexV(mutex);

  return(true);
}

MPEGstream_marker *
MPEGstream:: new_marker(int offset)
{
    MPEGstream_marker * marker;

    SDL_mutexP(mutex);
    /* We can't mark past the end of the stream */
    if ( eof() ) {
      SDL_mutexV(mutex);
      return(0);
    }

    /* It may be possible to seek in the data stream, but punt for now */
    if ( ((data+offset) < br->Buffer()) || ((data+offset) > stop) ) {
        SDL_mutexV(mutex);
        return(0);
    }

    /* Set up the mark */
    marker = new MPEGstream_marker;
    marker->marked_buffer = br;
    marker->marked_data = data+offset;
    marker->marked_stop = stop;

    /* Lock the new buffer */
    marker->marked_buffer->Lock();

    SDL_mutexV(mutex);

    return(marker);
}

bool
MPEGstream:: seek_marker(MPEGstream_marker const * marker)
{
    SDL_mutexP(mutex);

    if ( marker ) {
        /* Release current buffer */
        if(br->IsLocked())
	{
	  br->Unlock();
       	  marker->marked_buffer->Lock();
	}

        /* Reset the data positions */
	br = marker->marked_buffer;
        data = marker->marked_data;
        stop = marker->marked_stop;
    }

    SDL_mutexV(mutex);

    return(marker != 0);
}

void
MPEGstream:: delete_marker(MPEGstream_marker *marker)
{
    if( marker && marker->marked_buffer)
    {
      marker->marked_buffer->Unlock();
      delete marker;
    }
}

Uint32
MPEGstream:: copy_data(Uint8 *area, Sint32 size, bool short_read)
{
    Uint32 copied = 0;
    bool timestamped = false;

    while ( (size > 0) && !eof()) {
        Uint32 len;

        /* Get new data if necessary */
        if ( data == stop ) {
            /* try to use the timestamp of the first packet */
            if ( ! next_packet(true, (timestamp == -1) || !timestamped) ) {
                break;
            }
	    timestamped = true;
        }

	SDL_mutexP(mutex);

        /* Copy as much as we need */
        if ( size <= (Sint32)(stop-data) ) {
            len = size;
        } else {
            len = (stop-data);
        }

        memcpy(area, data, len);

        area += len;
        data += len;
        size -= len;
        copied += len;
	pos += len;

        /* Allow 32-bit aligned short reads? */
        if ( ((copied%4) == 0) && short_read ) {
            break;
        }

	SDL_mutexV(mutex);

    }

    return(copied);
}

int MPEGstream::copy_byte(void)
{
  /* Get new data if necessary */
  if ( data == stop ) {
    if ( ! next_packet() ) {
      return (-1);
    }
  }
  pos ++;
  return(*data++);
}

bool MPEGstream::eof() const
{
  return(!br->Size());
}

void MPEGstream::insert_packet(Uint8 * Data, Uint32 Size, double timestamp)
{
  MPEGlist * newbr;

  /* Discard all packets if not enabled */
  if(!enabled) return;

  SDL_mutexP(mutex);

  preread_size += Size;

  /* Seek the last buffer */
  for(newbr = br; newbr->Next(); newbr = newbr->Next());

  /* Position ourselves at the end of the stream */
  newbr = newbr->Alloc(Size);
  if ( Size ) {
    memcpy(newbr->Buffer(), Data, Size);
  }
  newbr->TimeStamp = timestamp;

  SDL_mutexV(mutex);
  garbage_collect();
}

/* - Check for unused buffers and free them - */
void MPEGstream::garbage_collect(void)
{
  MPEGlist * newbr;

  SDL_mutexP(mutex);  

  br->Lock();

  /* First of all seek the first buffer */
  for(newbr = br; newbr->Prev(); newbr = newbr->Prev());

  /* Now free buffers until we find a locked buffer */
  while(newbr->Next() && !newbr->IsLocked())
  {
    newbr = newbr->Next();
    delete newbr->Prev();
  }

  br->Unlock();

  SDL_mutexV(mutex);
}

void MPEGstream::enable(bool toggle)
{
  enabled = toggle;
}

double MPEGstream::time()
{
  return(br->TimeStamp);
}