forked from wjhwsh/VideoPlayer-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVPacketQueue.m
182 lines (154 loc) · 4.06 KB
/
AVPacketQueue.m
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
//
// AVPacketQueue.m
// FFmpegPlayTest
//
// Created by Jack on 11/12/12.
// Copyright (c) 2012 Jack. All rights reserved.
//
#import "AVPacketQueue.h"
//-----------------------------------------------------------------
@implementation AVPacketQueue
@synthesize count=_count;
@synthesize dataSize=_dataSize;
- (id) initWithSize:(NSInteger)size
{
self = [super init];
if (self) {
// Init condition
_condition = [[NSCondition alloc] init];
_firstPacket = nil;
_lastPacket = nil;
_count = 0;
_dataSize = 0;
_queueSize = size;
}
return self;
}
//-----------------------------------------------------------------
- (void) dealloc
{
[self flush];
[super dealloc];
}
//-----------------------------------------------------------------
- (int) popPacket: (AVPacket*) outPacket
blocked: (BOOL) blocked
{
NSLog(@"Poping packet...");
AVPacketList *pktList;
int ret = 0;
[_condition lock];
for(;;) {
// TODO: Handle quit signal
/*
if(global_video_state->quit) {
ret = -1;
break;
}
//*/
pktList = _firstPacket;
if (pktList) {
_firstPacket = pktList->next;
if (!_firstPacket)
_lastPacket = NULL;
_count--;
_dataSize -= pktList->pkt.size;
*outPacket = pktList->pkt;
av_free(pktList);
ret = 1;
[_condition signal];
break;
} else if (!blocked) {
NSLog(@"No pkt popped");
ret = 0;
break;
} else {
NSLog(@"Waiting to pop packet...");
[_condition wait];
}
}
[_condition unlock];
//SDL_UnlockMutex(q->mutex);
return ret;
}
//-----------------------------------------------------------------
/*
blocked variable is unused since writing in queue doesnt wait for
space in queue. See TOTO in header file.
*/
- (int) pushPacket: (AVPacket*) inPacket
blocked: (BOOL) blocked
{
NSLog(@"Pushing packet...");
//*
AVPacketList *pktList;
// TODO: Handle quit signal and flush pkt
/*
if(global_video_state->quit) {
ret = -1;
break;
}
if(pkt != &flush_pkt && av_dup_packet(pkt) < 0) {
return -1;
}
//*/
int ret = 0;
[_condition lock];
for(;;){
if(_dataSize > _queueSize){
if (blocked) {
NSLog(@"Waiting to push packet...");
[_condition wait];
}else{
NSLog(@"No pkt pushed.");
break;
}
}else{
if(av_dup_packet(inPacket) < 0) {
NSLog(@"Invalid packet");
ret = -1;
break;
}
pktList = av_malloc(sizeof(AVPacketList));
if (!pktList) {
NSLog(@"Unable to allocate mem for pkt");
ret = -1;
break;
}
pktList->pkt = *inPacket;
pktList->next = NULL;
//SDL_LockMutex(q->mutex);
if (!_lastPacket)
_firstPacket = pktList;
else
_lastPacket->next = pktList;
_lastPacket = pktList;
_count++;
_dataSize += pktList->pkt.size;
NSLog(@"Packet is pushed");
[_condition signal];
break;
}
}
[_condition unlock];
return ret;
}
//-----------------------------------------------------------------
- (void) flush
{
AVPacketList *pkt, *pkt1;
//SDL_LockMutex(q->mutex);
[_condition lock];
for(pkt = _firstPacket; pkt != NULL; pkt = pkt1) {
pkt1 = pkt->next;
av_free_packet(&pkt->pkt);
av_freep(&pkt);
}
_lastPacket = NULL;
_firstPacket = NULL;
_count = 0;
_dataSize = 0;
//SDL_UnlockMutex(q->mutex);
[_condition unlock];
}
@end