UIView+VAP.m 29 KB

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