UIView+VAP.m 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. // UIView+VAP.m
  2. // Tencent is pleased to support the open source community by making vap available.
  3. //
  4. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  5. //
  6. // Licensed under the MIT License (the "License"); you may not use this file except in
  7. // compliance with the License. You may obtain a copy of the License at
  8. //
  9. // http://opensource.org/licenses/MIT
  10. //
  11. // Unless required by applicable law or agreed to in writing, software distributed under the License is
  12. // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  13. // either express or implied. See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #import <UIKit/UIKit.h>
  16. #import <objc/runtime.h>
  17. #import "UIView+VAP.h"
  18. #import "QGAnimatedImageDecodeManager.h"
  19. #import "QGMP4HWDFileInfo.h"
  20. #import "QGMP4FrameHWDecoder.h"
  21. #import "QGBaseAnimatedImageFrame+Displaying.h"
  22. #import "QGHWDMP4OpenGLView.h"
  23. #import "QGVAPWeakProxy.h"
  24. #import "NSNotificationCenter+VAPThreadSafe.h"
  25. #import "QGHWDMP4OpenGLView.h"
  26. #import "QGMP4FrameHWDecoder.h"
  27. #import "QGMP4AnimatedImageFrame.h"
  28. #import "QGMP4FrameHWDecoder.h"
  29. #import "QGHWDMetalView.h"
  30. #import "QGVAPMetalView.h"
  31. #import "QGBaseAnimatedImageFrame+Displaying.h"
  32. #import "QGVAPConfigManager.h"
  33. #import "QGHWDMetalRenderer.h"
  34. #import "UIGestureRecognizer+VAPUtil.h"
  35. NSInteger const kQGHWDMP4DefaultFPS = 20;
  36. NSInteger const kQGHWDMP4MinFPS = 1;
  37. NSInteger const QGHWDMP4MaxFPS = 60;
  38. NSInteger const VapMaxCompatibleVersion = 2;
  39. @interface UIView () <QGAnimatedImageDecoderDelegate,QGHWDMP4OpenGLViewDelegate, QGHWDMetelViewDelegate, QGVAPMetalViewDelegate, QGVAPConfigDelegate>
  40. @property (nonatomic, assign) QGHWDTextureBlendMode hwd_blendMode; //alpha通道混合模式
  41. @property (nonatomic, strong) QGMP4AnimatedImageFrame *hwd_currentFrameInstance; //store the frame value
  42. @property (nonatomic, strong) QGMP4HWDFileInfo *hwd_fileInfo; //MP4文件信息
  43. @property (nonatomic, strong) QGAnimatedImageDecodeManager *hwd_decodeManager; //解码逻辑
  44. @property (nonatomic, strong) QGAnimatedImageDecodeConfig *hwd_decodeConfig; //线程数与buffer数
  45. @property (nonatomic, strong) NSOperationQueue *hwd_callbackQueue; //回调执行队列
  46. @property (nonatomic, assign) BOOL hwd_onPause; //标记是否暂停中
  47. @property (nonatomic, assign) BOOL hwd_onSeek; //正在seek当中,此时继续播放会导致时序混乱
  48. @property (nonatomic, strong) QGHWDMP4OpenGLView *hwd_openGLView; //opengl绘制图层
  49. @property (nonatomic, strong) QGHWDMetalView *hwd_metalView; //metal绘制图层
  50. @property (nonatomic, strong) QGVAPMetalView *vap_metalView; //vap格式mp4渲染图层
  51. @property (nonatomic, assign) BOOL hwd_isFinish; //标记是否结束
  52. @property (nonatomic, assign) NSInteger hwd_repeatCount; //播放次数;-1 表示无限循环
  53. @property (nonatomic, strong) QGVAPConfigManager *hwd_configManager; //额外的配置信息
  54. @property (nonatomic, strong) dispatch_queue_t vap_renderQueue; //播放队列
  55. @end
  56. @implementation UIView (VAP)
  57. #pragma mark - private methods
  58. - (void)hwd_registerNotification {
  59. [[NSNotificationCenter defaultCenter] hwd_addSafeObserver:self selector:@selector(hwd_didReceiveEnterBackgroundNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];
  60. [[NSNotificationCenter defaultCenter] hwd_addSafeObserver:self selector:@selector(hwd_didReceiveWillEnterForegroundNotification:) name:UIApplicationWillEnterForegroundNotification object:nil];
  61. [[NSNotificationCenter defaultCenter] hwd_addSafeObserver:self selector:@selector(hwd_didReceiveSeekStartNotification:) name:kQGVAPDecoderSeekStart object:nil];
  62. [[NSNotificationCenter defaultCenter] hwd_addSafeObserver:self selector:@selector(hwd_didReceiveSeekFinishNotification:) name:kQGVAPDecoderSeekFinish object:nil];
  63. }
  64. - (void)hwd_didReceiveEnterBackgroundNotification:(NSNotification *)notification {
  65. switch (self.hwd_enterBackgroundOP) {
  66. case HWDMP4EBOperationTypePauseAndResume:
  67. [self pauseHWDMP4];
  68. break;
  69. case HWDMP4EBOperationTypeDoNothing:
  70. break;
  71. default:
  72. [self stopHWDMP4];
  73. }
  74. }
  75. - (void)hwd_didReceiveWillEnterForegroundNotification:(NSNotification *)notification {
  76. switch (self.hwd_enterBackgroundOP) {
  77. case HWDMP4EBOperationTypePauseAndResume:
  78. [self resumeHWDMP4];
  79. break;
  80. default:
  81. break;
  82. }
  83. }
  84. - (void)hwd_didReceiveSeekStartNotification:(NSNotification *)notification {
  85. if ([self.hwd_decodeManager containsThisDeocder:notification.object]) {
  86. self.hwd_onSeek = YES;
  87. }
  88. }
  89. - (void)hwd_didReceiveSeekFinishNotification:(NSNotification *)notification {
  90. if ([self.hwd_decodeManager containsThisDeocder:notification.object]) {
  91. self.hwd_onSeek = NO;
  92. }
  93. }
  94. //结束播放
  95. - (void)hwd_stopHWDMP4 {
  96. VAP_Info(kQGVAPModuleCommon, @"hwd stop playing");
  97. self.hwd_repeatCount = 0;
  98. if (self.hwd_isFinish) {
  99. VAP_Info(kQGVAPModuleCommon, @"isFinish already set");
  100. return ;
  101. }
  102. self.hwd_isFinish = YES;
  103. self.hwd_onPause = YES;
  104. if (self.hwd_openGLView) {
  105. self.hwd_openGLView.pause = YES;
  106. if ([EAGLContext currentContext] != self.hwd_openGLView.glContext) {
  107. [EAGLContext setCurrentContext:self.hwd_openGLView.glContext];
  108. }
  109. [self.hwd_openGLView dispose];
  110. glFinish();
  111. }
  112. if (self.hwd_metalView) {
  113. [self.hwd_metalView dispose];
  114. }
  115. if (self.vap_metalView) {
  116. [self.vap_metalView dispose];
  117. }
  118. [self.hwd_callbackQueue addOperationWithBlock:^{
  119. //此处必须延迟释放,避免野指针
  120. if ([self.hwd_Delegate respondsToSelector:@selector(viewDidStopPlayMP4:view:)]) {
  121. [self.hwd_Delegate viewDidStopPlayMP4:self.hwd_currentFrame.frameIndex view:self];
  122. }
  123. }];
  124. self.hwd_decodeManager = nil;
  125. self.hwd_decodeConfig = nil;
  126. self.hwd_currentFrameInstance = nil;
  127. self.hwd_fileInfo = nil;
  128. [EAGLContext setCurrentContext:nil];
  129. }
  130. //播放完成
  131. - (void)hwd_didFinishDisplay {
  132. VAP_Info(kQGVAPModuleCommon, @"hwd didFinishDisplay");
  133. [self.hwd_callbackQueue addOperationWithBlock:^{
  134. //此处必须延迟释放,避免野指针
  135. if ([self.hwd_Delegate respondsToSelector:@selector(viewDidFinishPlayMP4:view:)]) {
  136. [self.hwd_Delegate viewDidFinishPlayMP4:self.hwd_currentFrame.frameIndex+1 view:self];
  137. }
  138. }];
  139. NSInteger currentCount = self.hwd_repeatCount;
  140. if (currentCount == -1 || currentCount-- > 0) {
  141. //continuing
  142. VAP_Info(kQGVAPModuleCommon, @"continue to display. currentCount:%@", @(currentCount));
  143. [self p_playHWDMP4:self.hwd_fileInfo.filePath
  144. fps:self.hwd_fps
  145. blendMode:self.hwd_blendMode
  146. repeatCount:currentCount
  147. delegate:self.hwd_Delegate];
  148. return ;
  149. }
  150. [self hwd_stopHWDMP4];
  151. }
  152. - (void)hwd_loadMetalViewIfNeed:(QGHWDTextureBlendMode)mode {
  153. if (self.hwd_renderByOpenGL) {
  154. return ;
  155. }
  156. //use vap metal
  157. if (self.useVapMetalView) {
  158. if (self.vap_metalView) {
  159. self.vap_metalView.commonInfo = self.hwd_configManager.model.info;
  160. return ;
  161. }
  162. QGVAPMetalView *vapMetalView = [[QGVAPMetalView alloc] initWithFrame:self.bounds];
  163. vapMetalView.commonInfo = self.hwd_configManager.model.info;
  164. vapMetalView.maskInfo = self.vap_maskInfo;
  165. vapMetalView.delegate = self;
  166. [self addSubview:vapMetalView];
  167. vapMetalView.translatesAutoresizingMaskIntoConstraints = false;
  168. NSDictionary *views = @{@"vapMetalView": vapMetalView};
  169. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[vapMetalView]|" options:0 metrics:nil views:views]];
  170. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[vapMetalView]|" options:0 metrics:nil views:views]];
  171. self.vap_metalView = vapMetalView;
  172. [self hwd_registerNotification];
  173. return ;
  174. }
  175. //use hwd metal
  176. if (self.hwd_metalView) {
  177. self.hwd_metalView.blendMode = mode;
  178. return ;
  179. }
  180. QGHWDMetalView *metalView = [[QGHWDMetalView alloc] initWithFrame:self.bounds blendMode:mode];
  181. if (!metalView) {
  182. VAP_Event(kQGVAPModuleCommon, @"metal view is nil!");
  183. return ;
  184. }
  185. metalView.blendMode = mode;
  186. metalView.delegate = self;
  187. [self addSubview:metalView];
  188. metalView.translatesAutoresizingMaskIntoConstraints = false;
  189. NSDictionary *views = @{@"metalView": metalView};
  190. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[metalView]|" options:0 metrics:nil views:views]];
  191. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[metalView]|" options:0 metrics:nil views:views]];
  192. self.hwd_metalView = metalView;
  193. [self hwd_registerNotification];
  194. }
  195. - (void)hwd_loadMetalDataIfNeed {
  196. [self.hwd_configManager loadMTLTextures:kQGHWDMetalRendererDevice];//加载所需的纹理数据
  197. [self.hwd_configManager loadMTLBuffers:kQGHWDMetalRendererDevice];//加载所需的buffer
  198. }
  199. - (void)hwd_loadOpenglViewIfNeed:(QGHWDTextureBlendMode)mode {
  200. if (!self.hwd_renderByOpenGL) {
  201. return ;
  202. }
  203. if (self.hwd_openGLView) {
  204. self.hwd_openGLView.blendMode = mode;
  205. self.hwd_openGLView.pause = NO;
  206. VAP_Info(kQGVAPModuleCommon, @"quit loading openglView for already loaded.");
  207. return ;
  208. }
  209. QGHWDMP4OpenGLView *openGLView = [[QGHWDMP4OpenGLView alloc] initWithFrame:self.bounds];
  210. openGLView.displayDelegate = self;
  211. openGLView.blendMode = mode;
  212. [self addSubview:openGLView];
  213. openGLView.userInteractionEnabled = NO;
  214. [openGLView setupGL];
  215. self.hwd_openGLView = openGLView;
  216. NSDictionary *views = @{@"openGLView": openGLView};
  217. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[openGLView]|" options:0 metrics:nil views:views]];
  218. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[openGLView]|" options:0 metrics:nil views:views]];
  219. [self hwd_registerNotification];
  220. }
  221. //fps策略:优先使用调用者指定的fps;若不合法则使用mp4中的数据;若还是不合法则使用默认18
  222. - (NSTimeInterval)hwd_appropriateDurationForFrame:(QGMP4AnimatedImageFrame *)frame {
  223. NSInteger fps = self.hwd_fps;
  224. if (fps < kQGHWDMP4MinFPS || fps > QGHWDMP4MaxFPS) {
  225. if (frame.defaultFps >= kQGHWDMP4MinFPS && frame.defaultFps <= QGHWDMP4MaxFPS) {
  226. fps = frame.defaultFps;
  227. }else {
  228. fps = kQGHWDMP4DefaultFPS;
  229. }
  230. }
  231. return 1000/(double)fps;
  232. }
  233. #pragma mark - main
  234. /**
  235. 见playHWDMP4:blendMode:repeatCount:delegate:
  236. 播放一遍,alpha数据在左边,不需要回调
  237. */
  238. - (void)playHWDMp4:(NSString *)filePath {
  239. [self playHWDMP4:filePath delegate:nil];
  240. }
  241. /**
  242. 见playHWDMP4:blendMode:repeatCount:delegate:
  243. 播放一遍,alpha数据在左边,设置回调
  244. */
  245. - (void)playHWDMP4:(NSString *)filePath delegate:(id<HWDMP4PlayDelegate>)delegate {
  246. [self p_playHWDMP4:filePath fps:0 blendMode:QGHWDTextureBlendMode_AlphaLeft repeatCount:0 delegate:delegate];
  247. }
  248. /**
  249. 见playHWDMP4:blendMode:repeatCount:delegate:
  250. alpha数据在左边
  251. */
  252. - (void)playHWDMP4:(NSString *)filePath repeatCount:(NSInteger)repeatCount delegate:(id<HWDMP4PlayDelegate>)delegate {
  253. [self p_playHWDMP4:filePath fps:0 blendMode:QGHWDTextureBlendMode_AlphaLeft repeatCount:repeatCount delegate:delegate];
  254. }
  255. - (void)p_playHWDMP4:(NSString *)filePath
  256. fps:(NSInteger)fps
  257. blendMode:(QGHWDTextureBlendMode)mode
  258. repeatCount:(NSInteger)repeatCount
  259. delegate:(id<HWDMP4PlayDelegate>)delegate {
  260. VAP_Info(kQGVAPModuleCommon, @"try to display mp4:%@ blendMode:%@ fps:%@ repeatCount:%@", filePath, @(mode), @(fps), @(repeatCount));
  261. NSAssert([NSThread isMainThread], @"HWDMP4 needs to be accessed on the main thread.");
  262. //filePath check
  263. if (!filePath || filePath.length == 0) {
  264. VAP_Error(kQGVAPModuleCommon, @"playHWDMP4 error! has no filePath!");
  265. return ;
  266. }
  267. NSFileManager *fileMgr = [NSFileManager defaultManager];
  268. if (![fileMgr fileExistsAtPath:filePath]) {
  269. VAP_Error(kQGVAPModuleCommon, @"playHWDMP4 error! fileNotExistsAtPath filePath:%#", filePath);
  270. return ;
  271. }
  272. self.hwd_isFinish = NO;
  273. self.hwd_blendMode = mode;
  274. self.hwd_fps = fps;
  275. self.hwd_repeatCount = repeatCount;
  276. self.hwd_Delegate = delegate;
  277. if (self.hwd_Delegate && !self.hwd_callbackQueue) {
  278. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  279. queue.maxConcurrentOperationCount = 1;
  280. self.hwd_callbackQueue = queue;
  281. }
  282. //mp4 info
  283. QGMP4HWDFileInfo *fileInfo = [[QGMP4HWDFileInfo alloc] init];
  284. fileInfo.filePath = filePath;
  285. fileInfo.mp4Parser = [[QGMP4ParserProxy alloc] initWithFilePath:fileInfo.filePath];
  286. [fileInfo.mp4Parser parse];
  287. self.hwd_fileInfo = fileInfo;
  288. //config manager
  289. QGVAPConfigManager *configManager = [[QGVAPConfigManager alloc] initWith:fileInfo];
  290. configManager.delegate = self;
  291. self.hwd_configManager = configManager;
  292. if (configManager.model.info.version > VapMaxCompatibleVersion) {
  293. VAP_Error(kQGVAPModuleCommon, @"playHWDMP4 error! not compatible vap version:%@!", @(configManager.model.info.version));
  294. [self stopHWDMP4];
  295. return ;
  296. }
  297. //reset
  298. self.hwd_currentFrameInstance = nil;
  299. self.hwd_decodeManager = nil;
  300. self.hwd_onPause = NO;
  301. if (!self.hwd_decodeConfig) {
  302. self.hwd_decodeConfig = [QGAnimatedImageDecodeConfig defaultConfig];
  303. }
  304. //OpenGLView
  305. [self hwd_loadOpenglViewIfNeed:mode];
  306. //metalView
  307. [self hwd_loadMetalViewIfNeed:mode];
  308. if ([[UIDevice currentDevice] hwd_isSimulator]) {
  309. VAP_Error(kQGVAPModuleCommon, @"playHWDMP4 error! not allowed in Simulator!");
  310. [self stopHWDMP4];
  311. return ;
  312. }
  313. if (!self.vap_renderQueue) {
  314. self.vap_renderQueue = dispatch_queue_create("com.qgame.vap.render", DISPATCH_QUEUE_SERIAL);
  315. }
  316. self.hwd_decodeManager = [[QGAnimatedImageDecodeManager alloc] initWith:self.hwd_fileInfo config:self.hwd_decodeConfig delegate:self];
  317. [self.hwd_configManager loadConfigResources]; //必须按先加载必要资源才能播放 - onVAPConfigResourcesLoaded
  318. }
  319. #pragma mark - play run
  320. - (void)hwd_renderVideoRun {
  321. static NSTimeInterval durationForWaitingFrame = 16/1000.0;
  322. static NSTimeInterval minimumDurationForLoop = 1/1000.0;
  323. __block NSTimeInterval lastRenderingInterval = 0;
  324. __block NSTimeInterval lastRenderingDuration = 0;
  325. dispatch_async(self.vap_renderQueue, ^{
  326. if (self.hwd_onPause || self.hwd_isFinish) {
  327. return ;
  328. }
  329. //不能将self.hwd_onPause判断加到while语句中!会导致releasepool不断上涨
  330. while (YES) {
  331. @autoreleasepool {
  332. if (self.hwd_isFinish) {
  333. break ;
  334. }
  335. if (self.hwd_onPause || self.hwd_onSeek) {
  336. lastRenderingInterval = NSDate.timeIntervalSinceReferenceDate;
  337. [NSThread sleepForTimeInterval:durationForWaitingFrame];
  338. continue;
  339. }
  340. __block QGMP4AnimatedImageFrame *nextFrame = nil;
  341. dispatch_sync(dispatch_get_main_queue(), ^{
  342. nextFrame = [self hwd_displayNext];
  343. });
  344. NSTimeInterval duration = nextFrame.duration/1000.0;
  345. if (duration == 0) {
  346. duration = durationForWaitingFrame;
  347. }
  348. NSTimeInterval currentTimeInterval = NSDate.timeIntervalSinceReferenceDate;
  349. if (nextFrame && nextFrame.frameIndex != 0) {
  350. duration -= ((currentTimeInterval-lastRenderingInterval) - lastRenderingDuration); //追回时间
  351. }
  352. duration = MAX(minimumDurationForLoop, duration);
  353. lastRenderingInterval = currentTimeInterval;
  354. lastRenderingDuration = duration;
  355. [NSThread sleepForTimeInterval:duration];
  356. }
  357. }
  358. });
  359. }
  360. - (QGMP4AnimatedImageFrame *)hwd_displayNext {
  361. if (self.hwd_onPause || self.hwd_isFinish) {
  362. return nil;
  363. }
  364. NSInteger nextIndex = self.hwd_currentFrame.frameIndex + 1;
  365. if (!self.hwd_currentFrame) {
  366. nextIndex = 0;
  367. }
  368. QGMP4AnimatedImageFrame *nextFrame = (QGMP4AnimatedImageFrame *)[self.hwd_decodeManager consumeDecodedFrame:nextIndex];
  369. //没取到预期的帧
  370. if (!nextFrame || nextFrame.frameIndex != nextIndex || ![nextFrame isKindOfClass:[QGMP4AnimatedImageFrame class]]) {
  371. return nil;
  372. }
  373. //音频播放
  374. if (nextIndex == 0) {
  375. [self.hwd_decodeManager tryToStartAudioPlay];
  376. }
  377. nextFrame.duration = [self hwd_appropriateDurationForFrame:nextFrame];
  378. //VAP_Debug(kQGVAPModuleCommon, @"display frame:%@, has frameBuffer:%@",@(nextIndex),@(nextFrame.pixelBuffer != nil));
  379. if (self.hwd_renderByOpenGL) {
  380. [self.hwd_openGLView displayPixelBuffer:nextFrame.pixelBuffer];
  381. } else if (self.useVapMetalView) {
  382. NSArray<QGVAPMergedInfo *> *mergeInfos = self.hwd_configManager.model.mergedConfig[@(nextFrame.frameIndex)];
  383. [self.vap_metalView display:nextFrame.pixelBuffer mergeInfos:mergeInfos];
  384. } else {
  385. [self.hwd_metalView display:nextFrame.pixelBuffer];
  386. }
  387. self.hwd_currentFrameInstance = nextFrame;
  388. [self.hwd_callbackQueue addOperationWithBlock:^{
  389. if (nextIndex == 0 && [self.hwd_Delegate respondsToSelector:@selector(viewDidStartPlayMP4:)]) {
  390. [self.hwd_Delegate viewDidStartPlayMP4:self];
  391. }
  392. //此处必须延迟释放,避免野指针
  393. if ([self.hwd_Delegate respondsToSelector:@selector(viewDidPlayMP4AtFrame:view:)]) {
  394. [self.hwd_Delegate viewDidPlayMP4AtFrame:self.hwd_currentFrame view:self];
  395. }
  396. }];
  397. return nextFrame;
  398. }
  399. //结束播放
  400. - (void)stopHWDMP4 {
  401. [self hwd_stopHWDMP4];
  402. }
  403. - (void)pauseHWDMP4 {
  404. VAP_Info(kQGVAPModuleCommon, @"pauseHWDMP4");
  405. self.hwd_onPause = YES;
  406. // pause回调stop会导致一般使用场景将view移除,无法resume,因此暂时去掉该回调触发
  407. // [self.hwd_callbackQueue addOperationWithBlock:^{
  408. // //此处必须延迟释放,避免野指针
  409. // if ([self.hwd_Delegate respondsToSelector:@selector(viewDidStopPlayMP4:view:)]) {
  410. // [self.hwd_Delegate viewDidStopPlayMP4:self.hwd_currentFrame.frameIndex view:self];
  411. // }
  412. // }];
  413. }
  414. - (void)resumeHWDMP4 {
  415. VAP_Info(kQGVAPModuleCommon, @"resumeHWDMP4");
  416. self.hwd_onPause = NO;
  417. self.hwd_openGLView.pause = NO;
  418. }
  419. + (void)registerHWDLog:(QGVAPLoggerFunc)logger {
  420. [QGVAPLogger registerExternalLog:logger];
  421. }
  422. #pragma mark - delegate
  423. #pragma clang diagnostic push
  424. #pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
  425. //decoder
  426. - (Class)decoderClassForManager:(QGAnimatedImageDecodeManager *)manager {
  427. return [QGMP4FrameHWDecoder class];
  428. }
  429. - (void)decoderDidFinishDecode:(QGBaseDecoder *)decoder {
  430. VAP_Info(kQGVAPModuleCommon, @"decoderDidFinishDecode.");
  431. [self hwd_didFinishDisplay];
  432. }
  433. - (void)decoderDidFailDecode:(QGBaseDecoder *)decoder error:(NSError *)error{
  434. VAP_Error(kQGVAPModuleCommon, @"decoderDidFailDecode:%@", error);
  435. [self hwd_stopHWDMP4];
  436. [self.hwd_callbackQueue addOperationWithBlock:^{
  437. //此处必须延迟释放,避免野指针
  438. if ([self.hwd_Delegate respondsToSelector:@selector(viewDidFailPlayMP4:)]) {
  439. [self.hwd_Delegate viewDidFailPlayMP4:error];
  440. }
  441. }];
  442. }
  443. //opengl
  444. - (void)onViewUnavailableStatus {
  445. VAP_Error(kQGVAPModuleCommon, @"onViewUnavailableStatus");
  446. [self hwd_stopHWDMP4];
  447. }
  448. //metal
  449. - (void)onMetalViewUnavailable {
  450. VAP_Error(kQGVAPModuleCommon, @"onMetalViewUnavailable");
  451. [self stopHWDMP4];
  452. }
  453. //config resources loaded
  454. - (void)onVAPConfigResourcesLoaded:(QGVAPConfigModel *)config error:(NSError *)error {
  455. [self hwd_loadMetalDataIfNeed];
  456. if ([self.hwd_Delegate respondsToSelector:@selector(shouldStartPlayMP4:config:)]) {
  457. BOOL shouldStart = [self.hwd_Delegate shouldStartPlayMP4:self config:self.hwd_configManager.model];
  458. if (!shouldStart) {
  459. VAP_Event(kQGVAPModuleCommon, @"shouldStartPlayMP4 return no!");
  460. [self hwd_stopHWDMP4];
  461. return ;
  462. }
  463. }
  464. [self hwd_renderVideoRun];
  465. }
  466. - (NSString *)vap_contentForTag:(NSString *)tag resource:(QGVAPSourceInfo *)info {
  467. if ([self.hwd_Delegate respondsToSelector:@selector(contentForVapTag:resource:)]) {
  468. return [self.hwd_Delegate contentForVapTag:tag resource:info];
  469. }
  470. return nil;
  471. }
  472. - (void)vap_loadImageWithURL:(NSString *)urlStr context:(NSDictionary *)context completion:(VAPImageCompletionBlock)completionBlock {
  473. if ([self.hwd_Delegate respondsToSelector:@selector(loadVapImageWithURL:context:completion:)]) {
  474. [self.hwd_Delegate loadVapImageWithURL:urlStr context:context completion:completionBlock];
  475. } else if (completionBlock) {
  476. NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:-1 userInfo:@{@"msg" : @"hwd_Delegate doesn't responds to selector loadVapImageWithURL:context:completion:"}];
  477. completionBlock(nil, error, nil);
  478. }
  479. }
  480. #pragma clang diagnostic pop
  481. #pragma mark - setters&getters
  482. - (BOOL)useVapMetalView {
  483. return self.hwd_configManager.hasValidConfig;
  484. }
  485. - (QGMP4AnimatedImageFrame *)hwd_currentFrame {
  486. return self.hwd_currentFrameInstance;
  487. }
  488. - (id<HWDMP4PlayDelegate>)hwd_Delegate {
  489. return objc_getAssociatedObject(self, @"MP4PlayDelegate");
  490. }
  491. - (void)setHwd_Delegate:(id<HWDMP4PlayDelegate>)MP4PlayDelegate {
  492. //解决循环播放问题,本身已经是一个weakproxy对象,就不再处理
  493. id weakDelegate = MP4PlayDelegate;
  494. if (![MP4PlayDelegate isKindOfClass:[QGVAPWeakProxy class]]) {
  495. weakDelegate = [QGVAPWeakProxy proxyWithTarget:MP4PlayDelegate];
  496. }
  497. return objc_setAssociatedObject(self, @"MP4PlayDelegate", weakDelegate, OBJC_ASSOCIATION_RETAIN);
  498. }
  499. //category methods
  500. HWDSYNTH_DYNAMIC_PROPERTY_CTYPE(hwd_onPause, setHwd_onPause, BOOL)
  501. HWDSYNTH_DYNAMIC_PROPERTY_CTYPE(hwd_onSeek, setHwd_onSeek, BOOL)
  502. HWDSYNTH_DYNAMIC_PROPERTY_CTYPE(hwd_enterBackgroundOP, setHwd_enterBackgroundOP, HWDMP4EBOperationType)
  503. HWDSYNTH_DYNAMIC_PROPERTY_CTYPE(hwd_renderByOpenGL, setHwd_renderByOpenGL, BOOL)
  504. HWDSYNTH_DYNAMIC_PROPERTY_CTYPE(hwd_isFinish, setHwd_isFinish, BOOL)
  505. HWDSYNTH_DYNAMIC_PROPERTY_CTYPE(hwd_fps, setHwd_fps, NSInteger)
  506. HWDSYNTH_DYNAMIC_PROPERTY_CTYPE(hwd_blendMode, setHwd_blendMode, NSInteger)
  507. HWDSYNTH_DYNAMIC_PROPERTY_CTYPE(hwd_repeatCount, setHwd_repeatCount, NSInteger)
  508. HWDSYNTH_DYNAMIC_PROPERTY_OBJECT(hwd_currentFrameInstance, setHwd_currentFrameInstance, OBJC_ASSOCIATION_RETAIN)
  509. HWDSYNTH_DYNAMIC_PROPERTY_OBJECT(hwd_MP4FilePath, setHwd_MP4FilePath, OBJC_ASSOCIATION_RETAIN)
  510. HWDSYNTH_DYNAMIC_PROPERTY_OBJECT(hwd_decodeManager, setHwd_decodeManager, OBJC_ASSOCIATION_RETAIN)
  511. HWDSYNTH_DYNAMIC_PROPERTY_OBJECT(hwd_fileInfo, setHwd_fileInfo, OBJC_ASSOCIATION_RETAIN)
  512. HWDSYNTH_DYNAMIC_PROPERTY_OBJECT(hwd_decodeConfig, setHwd_decodeConfig, OBJC_ASSOCIATION_RETAIN)
  513. HWDSYNTH_DYNAMIC_PROPERTY_OBJECT(hwd_callbackQueue, setHwd_callbackQueue, OBJC_ASSOCIATION_RETAIN)
  514. HWDSYNTH_DYNAMIC_PROPERTY_OBJECT(hwd_openGLView, setHwd_openGLView, OBJC_ASSOCIATION_RETAIN)
  515. HWDSYNTH_DYNAMIC_PROPERTY_OBJECT(hwd_metalView, setHwd_metalView, OBJC_ASSOCIATION_RETAIN)
  516. HWDSYNTH_DYNAMIC_PROPERTY_OBJECT(vap_metalView, setVap_metalView, OBJC_ASSOCIATION_RETAIN)
  517. HWDSYNTH_DYNAMIC_PROPERTY_OBJECT(hwd_attachmentsModel, setHwd_attachmentsModel, OBJC_ASSOCIATION_RETAIN)
  518. HWDSYNTH_DYNAMIC_PROPERTY_OBJECT(hwd_configManager, setHwd_configManager, OBJC_ASSOCIATION_RETAIN)
  519. HWDSYNTH_DYNAMIC_PROPERTY_OBJECT(vap_renderQueue, setVap_renderQueue, OBJC_ASSOCIATION_RETAIN)
  520. @end
  521. /// vap 增加手势识别的能力
  522. @implementation UIView (VAPGesture)
  523. /// 手势识别通用接口
  524. /// @param gestureRecognizer 需要的手势识别器
  525. /// @param handler 手势识别事件回调,按照gestureRecognizer回调时机回调
  526. /// @note 例:[mp4View addVapGesture:[UILongPressGestureRecognizer new] callback:^(UIGestureRecognizer *gestureRecognizer, BOOL insideSource,QGVAPSourceDisplayItem *source) { NSLog(@"long press"); }];
  527. - (void)addVapGesture:(UIGestureRecognizer *)gestureRecognizer callback:(VAPGestureEventBlock)handler {
  528. if (!gestureRecognizer) {
  529. VAP_Event(kQGVAPModuleCommon, @"addVapTapGesture with empty gestureRecognizer!");
  530. return ;
  531. }
  532. if (!handler) {
  533. VAP_Event(kQGVAPModuleCommon, @"addVapTapGesture with empty handler!");
  534. return ;
  535. }
  536. __weak __typeof(self) weakSelf = self;
  537. [gestureRecognizer addVapActionBlock:^(UITapGestureRecognizer *sender) {
  538. QGVAPSourceDisplayItem *diplaySource = [weakSelf displayingSourceAt:[sender locationInView:weakSelf]];
  539. if (diplaySource) {
  540. handler(sender, YES, diplaySource);
  541. } else {
  542. handler(sender, NO, nil);
  543. }
  544. }];
  545. [self addGestureRecognizer:gestureRecognizer];
  546. }
  547. /// 增加点击的手势识别
  548. /// @param handler 点击事件回调
  549. - (void)addVapTapGesture:(VAPGestureEventBlock)handler {
  550. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init];
  551. [self addVapGesture:tapGesture callback:handler];
  552. }
  553. /// 获取当前视图中point位置最近的一个source,没有的话返回nil
  554. /// @param point 当前view坐标系下的某一个位置
  555. - (QGVAPSourceDisplayItem *)displayingSourceAt:(CGPoint)point {
  556. NSArray<QGVAPMergedInfo *> *mergeInfos = self.hwd_configManager.model.mergedConfig[@(self.hwd_currentFrame.frameIndex)];
  557. mergeInfos = [mergeInfos sortedArrayUsingComparator:^NSComparisonResult(QGVAPMergedInfo *obj1, QGVAPMergedInfo *obj2) {
  558. return [@(obj2.renderIndex) compare:@(obj1.renderIndex)];
  559. }];
  560. CGSize renderingPixelSize = self.hwd_configManager.model.info.size;
  561. if (renderingPixelSize.width <= 0 || renderingPixelSize.height <= 0) {
  562. return nil;
  563. }
  564. __block QGVAPMergedInfo *targetMergeInfo = nil;
  565. __block CGRect targetSourceFrame = CGRectZero;
  566. CGSize viewSize = self.frame.size;
  567. CGFloat xRatio = viewSize.width / renderingPixelSize.width;
  568. CGFloat yRatio = viewSize.height / renderingPixelSize.height;
  569. [mergeInfos enumerateObjectsUsingBlock:^(QGVAPMergedInfo * mergeInfo, NSUInteger idx, BOOL * _Nonnull stop) {
  570. CGRect sourceRenderingRect = mergeInfo.renderRect;
  571. CGRect sourceRenderingFrame = CGRectMake(CGRectGetMinX(sourceRenderingRect) * xRatio, CGRectGetMinY(sourceRenderingRect) * yRatio, CGRectGetWidth(sourceRenderingRect) * xRatio, CGRectGetHeight(sourceRenderingRect) * yRatio);
  572. BOOL inside = CGRectContainsPoint(sourceRenderingFrame, point);
  573. if (inside) {
  574. targetMergeInfo = mergeInfo;
  575. targetSourceFrame = sourceRenderingFrame;
  576. *stop = YES;
  577. }
  578. }];
  579. if (!targetMergeInfo) {
  580. return nil;
  581. }
  582. QGVAPSourceDisplayItem *diplayItem = [QGVAPSourceDisplayItem new];
  583. diplayItem.sourceInfo = targetMergeInfo.source;
  584. diplayItem.frame = targetSourceFrame;
  585. return diplayItem;
  586. }
  587. @end
  588. @implementation UIView (VAPMask)
  589. - (void)setVap_maskInfo:(QGVAPMaskInfo *)vap_maskInfo {
  590. objc_setAssociatedObject(self, @"VAPMaskInfo", vap_maskInfo, OBJC_ASSOCIATION_RETAIN);
  591. [self.vap_metalView setMaskInfo:vap_maskInfo];
  592. }
  593. - (QGVAPMaskInfo *)vap_maskInfo {
  594. return objc_getAssociatedObject(self, @"VAPMaskInfo");
  595. }
  596. @end
  597. @implementation UIView (MP4HWDDeprecated)
  598. /**
  599. 见playHWDMP4:blendMode:repeatCount:delegate:
  600. @param fps 每一帧播放时优先使用这个值确定帧展示时长;若不合法则使用mp4中的数据;若还是不合法则使用默认18
  601. 播放一遍,alpha数据在左边,设置回调
  602. */
  603. - (void)playHWDMP4:(NSString *)filePath fps:(NSInteger)fps delegate:(id<HWDMP4PlayDelegate>)delegate {
  604. [self p_playHWDMP4:filePath fps:fps blendMode:QGHWDTextureBlendMode_AlphaLeft repeatCount:0 delegate:delegate];
  605. }
  606. /**
  607. 见playHWDMP4:blendMode:repeatCount:delegate:
  608. @param fps 每一帧播放时优先使用这个值确定帧展示时长;若不合法则使用mp4中的数据;若还是不合法则使用默认18
  609. alpha数据在左边
  610. */
  611. - (void)playHWDMP4:(NSString *)filePath fps:(NSInteger)fps repeatCount:(NSInteger)repeatCount delegate:(id<HWDMP4PlayDelegate>)delegate {
  612. [self p_playHWDMP4:filePath fps:fps blendMode:QGHWDTextureBlendMode_AlphaLeft repeatCount:repeatCount delegate:delegate];
  613. }
  614. /**
  615. 见playHWDMP4:blendMode:repeatCount:delegate:
  616. 播放一遍
  617. */
  618. - (void)playHWDMP4:(NSString *)filePath blendMode:(QGHWDTextureBlendMode)mode delegate:(id<HWDMP4PlayDelegate>)delegate {
  619. [self p_playHWDMP4:filePath fps:0 blendMode:mode repeatCount:0 delegate:delegate];
  620. }
  621. /**
  622. 利用GPU解码并播放mp4-h.264素材,在模拟器中会直接失败无法播放。
  623. @param filePath mp4s素材本地地址
  624. @param mode 确定素材中alpha通道数据位置,默认QGHWDTextureBlendMode_AlphaLeft
  625. @param repeatCount 重复播放次数,若repeatCount==n, 则播放n+1次;若repeatCount==-1,则循环播放.
  626. @param delegate 播放回调,⚠️注意:不在主线程回调
  627. @note 素材文件需要按照规范生成
  628. */
  629. - (void)playHWDMP4:(NSString *)filePath blendMode:(QGHWDTextureBlendMode)mode repeatCount:(NSInteger)repeatCount delegate:(id<HWDMP4PlayDelegate>)delegate {
  630. [self p_playHWDMP4:filePath fps:0 blendMode:mode repeatCount:repeatCount delegate:delegate];
  631. }
  632. /**
  633. 见playHWDMP4:blendMode:repeatCount:delegate:
  634. @param fps 每一帧播放时优先使用这个值确定帧展示时长;若不合法则使用mp4中的数据;若还是不合法则使用默认18
  635. 播放一遍
  636. */
  637. - (void)playHWDMP4:(NSString *)filePath fps:(NSInteger)fps blendMode:(QGHWDTextureBlendMode)mode delegate:(id<HWDMP4PlayDelegate>)delegate {
  638. [self p_playHWDMP4:filePath fps:fps blendMode:mode repeatCount:0 delegate:delegate];
  639. }
  640. /**
  641. 见playHWDMP4:blendMode:repeatCount:delegate:
  642. @param fps 每一帧播放时优先使用这个值确定帧展示时长;若不合法则使用mp4中的数据;若还是不合法则使用默认18
  643. */
  644. - (void)playHWDMP4:(NSString *)filePath fps:(NSInteger)fps blendMode:(QGHWDTextureBlendMode)mode repeatCount:(NSInteger)repeatCount delegate:(id<HWDMP4PlayDelegate>)delegate {
  645. [self p_playHWDMP4:filePath fps:fps blendMode:mode repeatCount:repeatCount delegate:delegate];
  646. }
  647. @end