-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathFFTFrameQueue.m
More file actions
131 lines (111 loc) · 2.35 KB
/
FFTFrameQueue.m
File metadata and controls
131 lines (111 loc) · 2.35 KB
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
//
// FFTFrameQueue.m
// FFmpegTutorial-macOS
//
// Created by qianlongxu on 2022/7/27.
// Copyright © 2022 Matt Reach's Awesome FFmpeg Tutotial. All rights reserved.
//
#import "FFTFrameQueue.h"
#import <libavutil/frame.h>
#import <libavutil/rational.h>
@implementation FFFrameItem
- (instancetype)initWithAVFrame:(AVFrame *)frame
{
self = [super init];
if (self) {
self.frame = av_frame_alloc();
av_frame_ref(self.frame, frame);
}
return self;
}
- (void)dealloc
{
av_frame_unref(_frame);
av_frame_free(&_frame);
}
@end
@interface FFTFrameQueue ()
@property (nonatomic, assign, readwrite) int capacity;
@property (nonatomic, strong) NSMutableArray *queue;
@property (nonatomic, strong) NSRecursiveLock *lock;
@property (atomic, assign) BOOL canceled;
@property (nonatomic, strong) FFFrameItem *lastFrame;
@end
@implementation FFTFrameQueue
- (void)dealloc
{
}
- (instancetype)initWithCapacity:(int)capacity
{
self = [super init];
if (self) {
self.capacity = capacity;
self.queue = [NSMutableArray array];
self.lock = [[NSRecursiveLock alloc] init];
}
return self;
}
- (void)cancel
{
self.canceled = YES;
}
- (BOOL)isCanceled
{
return self.canceled;
}
- (void)push:(FFFrameItem *)item
{
if (self.canceled) {
return;
}
int count;
[self.lock lock];
while (!self.canceled && [self.queue count] >= self.capacity) {
[self.lock unlock];
usleep(30*1000);
[self.lock lock];
}
[self.queue addObject:item];
[self.queue count];
[self.lock unlock];
}
- (void)pop
{
if (!self.canceled) {
[self.lock lock];
if ([self.queue count] > 0) {
self.lastFrame = [self.queue objectAtIndex:0];
[self.queue removeObjectAtIndex:0];
}
[self.lock unlock];
}
}
- (int)count
{
[self.lock lock];
int size = (int)[self.queue count];
[self.lock unlock];
return size;
}
- (FFFrameItem *)peekLast
{
return self.lastFrame;
}
- (FFFrameItem *)peek
{
[self.lock lock];
FFFrameItem *item = [self.queue firstObject];
[self.lock unlock];
return item;
}
- (FFFrameItem *)peekNext
{
FFFrameItem *item = nil;
[self.lock lock];
if ([self.queue count] > 1) {
item = [self.queue objectAtIndex:1];
}
[self.lock unlock];
return item;
}
@end