1/* 2 * Copyright 2018 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11#import "RTCDisplayLinkTimer.h" 12 13#import <UIKit/UIKit.h> 14 15@implementation RTCDisplayLinkTimer { 16 CADisplayLink *_displayLink; 17 void (^_timerHandler)(void); 18} 19 20- (instancetype)initWithTimerHandler:(void (^)(void))timerHandler { 21 NSParameterAssert(timerHandler); 22 if (self = [super init]) { 23 _timerHandler = timerHandler; 24 _displayLink = 25 [CADisplayLink displayLinkWithTarget:self 26 selector:@selector(displayLinkDidFire:)]; 27 _displayLink.paused = YES; 28#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_10_0 29 _displayLink.preferredFramesPerSecond = 30; 30#else 31 [_displayLink setFrameInterval:2]; 32#endif 33 [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] 34 forMode:NSRunLoopCommonModes]; 35 } 36 return self; 37} 38 39- (void)dealloc { 40 [self invalidate]; 41} 42 43- (BOOL)isPaused { 44 return _displayLink.paused; 45} 46 47- (void)setIsPaused:(BOOL)isPaused { 48 _displayLink.paused = isPaused; 49} 50 51- (void)invalidate { 52 [_displayLink invalidate]; 53} 54 55- (void)displayLinkDidFire:(CADisplayLink *)displayLink { 56 _timerHandler(); 57} 58 59@end 60