forked from SteveMaddison/cabrio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.c
91 lines (71 loc) · 1.68 KB
/
packet.c
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
#include <SDL/SDL_mutex.h>
#include "packet.h"
void packet_queue_init( struct packet_queue *q ) {
memset( q, 0, sizeof(struct packet_queue) );
q->mutex = SDL_CreateMutex();
q->cond = SDL_CreateCond();
}
int packet_queue_put( struct packet_queue *q, AVPacket *p ) {
AVPacketList *packet;
if( av_dup_packet( p ) < 0 ) {
fprintf( stderr, "Warning: Couldn't allocate packet in queue (av_dup_packet)\n" );
return -1;
}
packet = av_malloc( sizeof(AVPacketList) );
if( !packet ) {
fprintf( stderr, "Warning: Couldn't allocate packet in queue (av_malloc)\n" );
return -1;
}
packet->pkt = *p;
packet->next = NULL;
SDL_LockMutex( q->mutex );
if( q->last )
q->last->next = packet;
else
q->first = packet;
q->last = packet;
q->packets++;
q->size += packet->pkt.size;
SDL_CondSignal( q->cond );
SDL_UnlockMutex( q->mutex );
return 0;
}
int packet_queue_get( struct packet_queue *q, AVPacket *p, int block ) {
AVPacketList *packet;
int ret;
SDL_LockMutex( q->mutex );
packet = q->first;
if( packet ) {
q->first = packet->next;
if( !q->first )
q->last = NULL;
q->packets--;
q->size -= packet->pkt.size;
*p = packet->pkt;
av_free( packet );
ret = 1;
}
else if( !block ) {
ret = 0;
}
else {
SDL_CondWait( q->cond, q->mutex );
}
SDL_UnlockMutex( q->mutex );
return ret;
}
void packet_queue_flush( struct packet_queue *q ) {
AVPacketList *packet, *tmp;
SDL_LockMutex(q->mutex);
for( packet = q->first; packet != NULL; packet = tmp ) {
tmp = packet->next;
av_free_packet( &packet->pkt );
av_freep( &packet );
}
q->last = NULL;
q->first = NULL;
q->packets = 0;
q->size = 0;
SDL_CondSignal( q->cond );
SDL_UnlockMutex(q->mutex);
}