-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathVideoScreen.m
369 lines (322 loc) · 10.6 KB
/
VideoScreen.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//
// PEAGLView.m
// FFmpegPlayTest
//
// Created by Jack on 11/1/12.
// Copyright (c) 2012 Jack. All rights reserved.
//
#import "VideoScreen.h"
#import "RendererFactory.h"
#import <MediaPlayer/MediaPlayer.h>
#import <QuartzCore/CADisplayLink.h>
//#import "RendererProtocol.h"
//#import "ES1Renderer.h"
//#import "ES2Renderer.h"
@interface VideoScreen () <RendererDelegate>
{
id _displayLink;
NSTimer* _animationTimer;
BOOL _useES1;
id<Renderer> _renderer;
NSTimeInterval _startTime;
NSTimeInterval _currentTime;
VideoPicture* _curPicture;
NSInteger _frameCount;
}
@end
//------------------------------------------------------------------------
@implementation VideoScreen
@synthesize source=_source;
@synthesize supportDisplayLink=_supportDisplayLink;
@synthesize frameInterval=_frameInterval;
@synthesize state=_state;
@synthesize delegate=_delegate;
+ (Class)layerClass
{
return [CAEAGLLayer class];
}
#pragma mark Initialize
//------------------------------------------------------------------------
- (id) initWithSource: (id<VideoScreenSource>) source
delegate: (id<VideoScreenDelegate>) delegate
scalingMode: (NSInteger) scalingMode
{
self = [self initWithSource: source delegate:delegate];
if (self) {
[self setScalingMode:scalingMode];
}
return self;
}
//------------------------------------------------------------------------
- (id) initWithSource: (id<VideoScreenSource>) source
delegate:(id<VideoScreenDelegate>)delegate
{
self = [super init];
if (self) {
_delegate = delegate;
_source = source;
_state = kScreenStateUnknown;
_supportDisplayLink = NO;
_displayLink = nil;
_animationTimer = nil;
_frameInterval = 1;
_scalingMode = MPMovieScalingModeAspectFit;
_frameCount = 0;
if(![self initRenderer])
return nil;
NSString *reqSysVer = @"3.1";
NSString *curSysVer = [[UIDevice currentDevice] systemVersion];
if ([curSysVer compare: reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
_supportDisplayLink = YES;
}
_state = kScreenStateStopped;
}
return self;
}
//------------------------------------------------------------------------
- (void) initEAGLlayer
{
CAEAGLLayer* eaglLayer = (CAEAGLLayer*) self.layer;
eaglLayer.opaque = TRUE;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking,
kEAGLColorFormatRGB565, kEAGLDrawablePropertyColorFormat,nil];
}
//------------------------------------------------------------------------
/**
Renderer need two type of information to init
(1) Description of input video:
+ size of video frame : for scaling calculation
+ Pixel format: for textture buffer generation
input video information can be obtained from _source
(2) Description of view port where to render video pictures
+ Size of view port
+ Layer object : to generate backing colorBuffer
+ Scaling mode
Size and layer object is the layer object of [self],
Scaling mode is set by client object ( MoviePlayerController)
*/
- (BOOL) initRenderer
{
BOOL ret = YES;
[self initEAGLlayer];
_renderer = [RendererFactory createRendererWithDelegate: self
videoSource:_source
screen: self];
if (!_renderer) {
NSLog(@"Can not create renderer");
ret = NO;
goto finish;
}
finish:
if (!ret && _renderer)
_renderer = nil;
return ret;
}
//---------------------------------------------------------------------
#pragma mark UIView methods
- (void)layoutSubviews
{
if ([_renderer prepareTexture]) {
//[self updateScreen:_displayLink];
}
// CAEAGLLayer changeds, so we need to update renderer's
// setting and texture here.
}
//---------------------------------------------------------------------
- (void) willMoveToSuperview:(UIView *)newSuperview
{
}
//---------------------------------------------------------------------
- (void) didMoveToSuperview
{
if ([_delegate respondsToSelector:@selector(screenDidShow:)]) {
[_delegate screenDidShow:self];
}
}
//---------------------------------------------------------------------
#pragma mark Utilities methods
- (void) setCurrentPicture: (VideoPicture*) picture
{
}
- (NSInteger) screenScalingMode
{
switch (_scalingMode) {
case MPMovieScalingModeNone:
return kRendererScaleModeNon;
break;
case MPMovieScalingModeAspectFit:
return kRendererScaleModeAspectFit;
break;
case MPMovieScalingModeAspectFill:
return kRendererScaleModeAspectFill;
break;
case MPMovieScalingModeFill:
return kRendererScaleModeScale;
break;
default:
return kRendererScaleModeDefault;
break;
}
}
//---------------------------------------------------------------------
#pragma mark Screen Properties
- (NSInteger) frameInterval
{
return _frameInterval;
}
//---------------------------------------------------------------------
- (void) setFrameInterval:(NSInteger)frameInterval
{
if (frameInterval > 1) {
_frameInterval = frameInterval;
// TODO: Consider paused status
if ([self isRuning]) {
[self stop];
[self start];
}
}
}
//---------------------------------------------------------------------
#pragma mark RendererDelegate
- (void) finishFrameByRenderer:(id<Renderer>)renderer
{
[_source finishFrameForScreen:self];
_curPicture = nil;
}
//---------------------------------------------------------------------
#pragma mark Display Video
/**
TODO:
This method is called at displayLink refresh rate to update texture,
so this function should do the following:
(1) Get current clock time of video screen
(2) Invoke source methods to get video frame, given the obtained clock time
(3) Set new received picture as current picture (release the old one)
(4) Invoke renderer to render the received picture
Q: Do we need to be notified that renderer finished rendering the frame?
A: No, be cause render to frame is running in the same thread as this one
(we use current run loop when we create the displaylink), so this method
exit only when renderer finish its job rendering the frame.
*/
- (void) updateScreen:(id)sender
{
// TODO: need re-enable this
// if ((_state != kScreenStateRunning) && (_state)) {
// return;
// }
// CADisplayLink* displayLink = (CADisplayLink*) sender;
//*
// static double lastLastPts = 0;
// static double lastPts = 0;
// lastLastPts = lastPts;
// lastPts = [displayLink timestamp];
// if (displayLink) {
// NSLog(@"Time between last two timestamp: %f", lastPts - lastLastPts);
// //sleep(arc4random_uniform(2));
// }
//*/
//
// NSLog(@"Updating video screen");
_curPicture = [_source getPictureForScreen: self
screenClock: [self currentTimeSinceStart ]];
if (_curPicture) {
//usleep(15000);
[_renderer render:_curPicture];
_frameCount++;
//NSLog(@"Frame rate: %.2f", _frameCount/[self currentTimeSinceStart]);
}
}
//---------------------------------------------------------------------
#pragma mark Controllable protocol
//---------------------------------------------------------------------
- (void) pause
{
if (_state == kScreenStateRunning) {
[_displayLink setPaused:YES];
_state = kScreenStatePaused;
NSLog(@"Video Screen Paused");
}
}
- (void) resume
{
if (_state == kScreenStatePaused) {
[_displayLink setPaused:NO];
_state = kScreenStateRunning;
NSLog(@"Video Screen Resumed");
}
}
//---------------------------------------------------------------------
- (BOOL) isReady
{
return _state != kScreenStateUnknown;
}
//---------------------------------------------------------------------
- (void) start
{
// If screen is initialized but not running
if((_state != kScreenStateRunning) && (_state != kScreenStatePaused))
{
// Start screen refresh by displaylink or nstimer
if (_supportDisplayLink) {
_displayLink = [CADisplayLink displayLinkWithTarget:self
selector:@selector(updateScreen:)];
[_displayLink setFrameInterval: _frameInterval];
//[_displayLink setFrameInterval: 60];
[_displayLink addToRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
}else{
_animationTimer = [NSTimer scheduledTimerWithTimeInterval:((1.0 / 60.0) * (double) _frameInterval)
target:self
selector:@selector(updateScreen:)
userInfo:nil
repeats:YES];
}
_startTime = CACurrentMediaTime();
// Set running flag ON
_state = kScreenStateRunning;
NSLog(@"Video Screen Started at: %f", _startTime);
}
}
//---------------------------------------------------------------------
- (void) stop
{
if ((_state == kScreenStateRunning) || (_state == kScreenStatePaused)) {
if (_supportDisplayLink) {
[_displayLink invalidate];
_displayLink = nil;
}else{
[_animationTimer invalidate];
_animationTimer = nil;
}
_state = kScreenStateStopped;
NSLog(@"Video Screen Stopped");
}
}
//---------------------------------------------------------------------
- (BOOL) isRuning
{
return (_state == kScreenStateRunning);
}
//---------------------------------------------------------------------
#pragma mark VideoScreen Protocol
@synthesize scalingMode=_scalingMode;
- (id<EAGLDrawable>) viewPort
{
return ((CAEAGLLayer*) self.layer);
}
//---------------------------------------------------------------------
#pragma mark Clock protocol
- (NSTimeInterval) startTime
{
return _startTime;
}
//---------------------------------------------------------------------
/**
Current time of video screen is the time of last display frame.
*/
- (NSTimeInterval) currentTimeSinceStart
{
return CACurrentMediaTime() - _startTime;
}
@end