PlayerView.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. //
  2. // PlayerView.m
  3. // VclustersGemini
  4. //
  5. // Created by APPLE on 2019/6/13.
  6. // Copyright © 2019 APPLE. All rights reserved.
  7. //
  8. #import "PlayerView.h"
  9. #import "UIView+View.h"
  10. @interface PlayerView ()
  11. <UIGestureRecognizerDelegate,
  12. ShowImageViewDelegate>{
  13. EAGLContext *eagContext;
  14. CIContext *ciContext;
  15. CIFilter *filter;
  16. //触控指令视图,用于计算触控位置坐标
  17. ShowImageView *touchCommondView;
  18. UIView *bottomContrView;
  19. UIButton *mueBtn;
  20. UIButton *homeBtn;
  21. UIButton *backBtn;
  22. }
  23. @end
  24. @implementation PlayerView
  25. @synthesize showImageView;
  26. @synthesize glkshowImageView;
  27. @synthesize controlBtn;
  28. @synthesize delegate;
  29. @synthesize isLan;
  30. - (id)initWithFrame:(CGRect)frame{
  31. self = [super initWithFrame:frame];
  32. if (self){
  33. [self setBackgroundColor:[UIColor blackColor]];
  34. [self initOpenGL];
  35. [self drawView:frame];
  36. }
  37. return self;
  38. }
  39. - (void)initOpenGL{
  40. eagContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  41. ciContext = [CIContext contextWithEAGLContext:eagContext
  42. options:@{kCIContextWorkingColorSpace:[NSNull null]}];
  43. filter = [CIFilter filterWithName:@"CISepiaTone"];
  44. }
  45. - (void)glkShowImage:(CIImage *)ciImage{
  46. [filter setValue:ciImage forKey:kCIInputImageKey];
  47. [filter setValue:@(0) forKey:kCIInputIntensityKey];
  48. //开始渲染
  49. [ciContext drawImage:[filter valueForKey:kCIOutputImageKey]
  50. inRect:CGRectMake(0, 0, glkshowImageView.drawableWidth, glkshowImageView.drawableHeight)
  51. fromRect:[ciImage extent]];
  52. //[self->glkshowImageView display];
  53. }
  54. - (void)showIMage
  55. {
  56. ;
  57. }
  58. - (void)drawView:(CGRect)frame{
  59. self.frame = frame;
  60. touchCommondView = [[ShowImageView alloc] init];
  61. [touchCommondView setBackgroundColor: [UIColor clearColor]];
  62. [touchCommondView setUserInteractionEnabled:YES];
  63. touchCommondView.multipleTouchEnabled = YES;
  64. touchCommondView.delegate = self;
  65. [self addSubview:touchCommondView];
  66. [touchCommondView mas_makeConstraints:^(MASConstraintMaker *make) {
  67. make.left.mas_equalTo(0.f);
  68. make.height.mas_equalTo(frame.size.height);
  69. make.right.mas_equalTo(0.f);
  70. make.top.mas_equalTo(0.f);
  71. }];
  72. /*播放视图*/
  73. showImageView = [[ShowImageView alloc] init];
  74. [showImageView setBackgroundColor: [UIColor blackColor]];
  75. [showImageView setUserInteractionEnabled:NO];
  76. [showImageView setBackgroundColor:[UIColor blackColor]];
  77. glkshowImageView = [[GLKView alloc] initWithFrame:self.frame context:eagContext];
  78. [glkshowImageView setBackgroundColor:[UIColor blackColor]];
  79. [glkshowImageView bindDrawable];
  80. [glkshowImageView setUserInteractionEnabled:NO];
  81. [touchCommondView addSubview:showImageView];
  82. [showImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  83. make.left.mas_equalTo(0.f);
  84. make.right.mas_equalTo(0.f);
  85. make.bottom.mas_equalTo(0.f);
  86. make.top.mas_equalTo(0.f);
  87. }];
  88. [touchCommondView addSubview:glkshowImageView];
  89. [glkshowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  90. make.left.mas_equalTo(0.f);
  91. make.right.mas_equalTo(0.f);
  92. make.bottom.mas_equalTo(0.f);
  93. make.top.mas_equalTo(0.f);
  94. }];
  95. /*控制按钮*/
  96. UIImage *driftBtnImage = [UIImage imageNamed:@"you_icon"];
  97. controlBtn = [[UIButton alloc] init];
  98. [controlBtn setBackgroundColor:[UIColor clearColor]];
  99. [controlBtn addTarget:self
  100. action:@selector(controlBtnPressed:)
  101. forControlEvents:(UIControlEventTouchUpInside)];
  102. UIPanGestureRecognizer *gester = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureCallback:)];
  103. [controlBtn addGestureRecognizer:gester];
  104. gester.delegate = self;
  105. gester = nil;
  106. [controlBtn setImage:driftBtnImage forState:(UIControlStateNormal)];
  107. [controlBtn addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(controlBtnLongPress:)]];
  108. /*下方控制器*/
  109. bottomContrView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height - 47.f, frame.size.width, 47.f)];
  110. [bottomContrView setBackgroundColor:[UIColor blackColor]];
  111. [self addSubview:bottomContrView];
  112. [bottomContrView mas_makeConstraints:^(MASConstraintMaker *make) {
  113. make.right.mas_equalTo(0);
  114. make.height.mas_equalTo(47.f);
  115. make.left.mas_equalTo(0.f);
  116. make.bottom.mas_equalTo(0.f);
  117. }];
  118. [bottomContrView setHidden:YES];
  119. [self initAddSubViewForBottomContrView];
  120. [self setPoMas_makeWithImageRate:frame.size.width/frame.size.height isOpenVirtual:NO isUsageMode:NO];
  121. }
  122. - (void)initAddSubViewForBottomContrView{
  123. /*第一个app菜单*/
  124. UIImage *mueBtnImage = [UIImage imageNamed:@"houtai_icon"];
  125. mueBtn = [[UIButton alloc] init];
  126. [mueBtn setBackgroundColor:[UIColor clearColor]];
  127. [mueBtn addTarget:self
  128. action:@selector(mueBtnPressed:)
  129. forControlEvents:(UIControlEventTouchUpInside)];
  130. [mueBtn setImage:mueBtnImage forState:(UIControlStateNormal)];
  131. [mueBtn setImageEdgeInsets:(UIEdgeInsetsMake(6.f, 6.f, 6.f, 6.f))];
  132. [bottomContrView addSubview:mueBtn];
  133. /*第二个home键*/
  134. UIImage *homeBtnImage = [UIImage imageNamed:@"home_icon"];
  135. homeBtn = [[UIButton alloc] init];
  136. [homeBtn setBackgroundColor:[UIColor clearColor]];
  137. [homeBtn addTarget:self
  138. action:@selector(homeBtnPressed:)
  139. forControlEvents:(UIControlEventTouchUpInside)];
  140. [homeBtn setImage:homeBtnImage forState:(UIControlStateNormal)];
  141. [homeBtn setImageEdgeInsets:(UIEdgeInsetsMake(6.f, 6.f, 6.f, 6.f))];
  142. [bottomContrView addSubview:homeBtn];
  143. /*第三个返回键*/
  144. UIImage *backBtnImage = [UIImage imageNamed:@"fanhui_you_icon"];
  145. backBtn = [[UIButton alloc] init];
  146. [backBtn setBackgroundColor:[UIColor clearColor]];
  147. [backBtn addTarget:self
  148. action:@selector(backBtnPressed:)
  149. forControlEvents:(UIControlEventTouchUpInside)];
  150. [backBtn setImage:backBtnImage forState:(UIControlStateNormal)];
  151. [backBtn setImageEdgeInsets:(UIEdgeInsetsMake(6.f, 6.f, 6.f, 6.f))];
  152. [bottomContrView addSubview:backBtn];
  153. }
  154. - (void)mueBtnPressed:(id)sender{
  155. if ([delegate respondsToSelector:@selector(mueBtnBePressed:)]) {
  156. [delegate mueBtnBePressed:sender];
  157. }
  158. }
  159. - (void)homeBtnPressed:(id)sender{
  160. if ([delegate respondsToSelector:@selector(homeBtnBePressed:)]) {
  161. [delegate homeBtnBePressed:sender];
  162. }
  163. }
  164. - (void)backBtnPressed:(id)sender{
  165. if ([delegate respondsToSelector:@selector(backBtnBePressed:)]) {
  166. [delegate backBtnBePressed:sender];
  167. }
  168. }
  169. - (void)initPoMas_makeWithImageRate:(BOOL)isOpenVirtual isUsageMode:(BOOL)isUsageMode{
  170. [self setPoMas_makeWithImageRate:self.frame.size.width/self.frame.size.height isOpenVirtual:isOpenVirtual isUsageMode:isUsageMode];
  171. }
  172. - (void)setPoMas_makeWithImageRate:(CGFloat)w_h_rate isOpenVirtual:(BOOL)isOpenVirtual isUsageMode:(BOOL)isUsageMode{
  173. /*播放视图*/
  174. // touchCommondView.isLan = NO;
  175. [showImageView setBackgroundColor:[UIColor blackColor]];
  176. [touchCommondView mas_remakeConstraints:^(MASConstraintMaker *make) {
  177. CGFloat scr_W = SCREEN_W;
  178. CGFloat scr_H = SCREEN_H;
  179. if (SCREEN_W > SCREEN_H){
  180. scr_W = SCREEN_H;
  181. scr_H = SCREEN_W;
  182. }
  183. if ((scr_H - scr_W/w_h_rate - 40.f)/2.f < 0){
  184. make.height.mas_equalTo(scr_H - 40.f);
  185. make.width.mas_equalTo((scr_H - 40.f)*w_h_rate);
  186. make.centerX.equalTo(self.mas_centerX);
  187. make.top.mas_equalTo(0);
  188. }
  189. else{
  190. make.left.mas_equalTo(0.f);
  191. make.height.mas_equalTo(scr_W/w_h_rate);
  192. make.right.mas_equalTo(0.f);
  193. make.top.mas_equalTo((scr_H - scr_W/w_h_rate - 40.f)/2.f);
  194. }
  195. }];
  196. [showImageView setTransform:CGAffineTransformMakeRotation(0)];
  197. [glkshowImageView setTransform:CGAffineTransformMakeRotation(0)];
  198. [showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  199. make.left.equalTo(touchCommondView.mas_left);
  200. make.right.equalTo(touchCommondView.mas_right);
  201. make.top.equalTo(touchCommondView.mas_top);
  202. make.bottom.equalTo(touchCommondView.mas_bottom);
  203. }];
  204. [self.glkshowImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  205. make.left.equalTo(touchCommondView.mas_left);
  206. make.right.equalTo(touchCommondView.mas_right);
  207. make.top.equalTo(touchCommondView.mas_top);
  208. make.bottom.equalTo(touchCommondView.mas_bottom);
  209. }];
  210. [bottomContrView setHidden:NO];
  211. [bottomContrView mas_remakeConstraints:^(MASConstraintMaker *make) {
  212. make.height.mas_equalTo(40.f);
  213. make.right.equalTo(touchCommondView.mas_right);
  214. make.top.equalTo(touchCommondView.mas_bottom);
  215. make.left.equalTo(touchCommondView.mas_left);
  216. }];
  217. [mueBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  218. make.left.mas_equalTo(41.f);
  219. make.width.and.height.mas_equalTo(40.f);
  220. make.top.mas_equalTo(0);
  221. }];
  222. [homeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  223. make.centerX.equalTo(bottomContrView.mas_centerX);
  224. make.width.and.height.mas_equalTo(40.f);
  225. make.top.mas_equalTo(0);
  226. }];
  227. UIImage *backBtnImage = [UIImage imageNamed:@"fanhui_you_icon"];
  228. // UIImage *backBtnImage = [UIImage imageNamed:@"fanhui_icon"];
  229. [backBtn setImage:backBtnImage forState:(UIControlStateNormal)];
  230. [backBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  231. make.right.mas_equalTo(-41.f);
  232. make.width.and.height.mas_equalTo(40.f);
  233. make.top.mas_equalTo(0);
  234. }];
  235. }
  236. /**
  237. * @brief 设置横屏模式UI布局
  238. *
  239. * @param w_h_rate 宽高比
  240. *
  241. * @param isOpenVirtual 是否有打开底部虚拟按键
  242. *
  243. * @param isUsageMode 是否为专业模式
  244. *
  245. */
  246. - (void)setLanMas_makeWithImageRate:(CGFloat)w_h_rate isOpenVirtual:(BOOL)isOpenVirtual isUsageMode:(BOOL)isUsageMode{
  247. /*需要考虑专业模式*/
  248. /*暂时只考虑非专业模式无虚拟键布局*/
  249. CGFloat h_forPhoneTop = 0;
  250. if (IPHONE_X){
  251. h_forPhoneTop = 44.f;
  252. }
  253. CGFloat des_W = SCREEN_W<SCREEN_H?SCREEN_W:SCREEN_H;/*实际较小的数字*/
  254. CGFloat des_H = (SCREEN_W>SCREEN_H?SCREEN_W:SCREEN_H) - h_forPhoneTop;/*实际较大的数字*/
  255. // touchCommondView.isLan = YES;
  256. [touchCommondView mas_remakeConstraints:^(MASConstraintMaker *make) {
  257. make.bottom.mas_equalTo(0.f);
  258. make.width.mas_equalTo(des_W*w_h_rate);
  259. make.left.mas_equalTo(h_forPhoneTop + (des_H - des_W*w_h_rate)/2.f);
  260. make.top.mas_equalTo(0);
  261. }];
  262. CGFloat des_H1 = des_W*w_h_rate;/*实际较大的数字*/
  263. CGFloat des_W1 = des_W;/*实际较小的数字*/
  264. if (isUsageMode){
  265. des_W1 = des_W-44.f;
  266. des_H1 = (des_W-44.f)*w_h_rate;
  267. }
  268. [showImageView setTransform:CGAffineTransformMakeRotation(-M_PI/2)];
  269. [glkshowImageView setTransform:CGAffineTransformMakeRotation(-M_PI/2)];
  270. [showImageView setBackgroundColor:[UIColor blackColor]];
  271. [showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  272. make.height.mas_equalTo(des_H1);
  273. make.width.mas_equalTo((des_W1));
  274. make.left.mas_equalTo((des_H1 - des_W1)/2.f);
  275. make.top.mas_equalTo((des_W1 - des_H1)/2.f);
  276. }];
  277. [self.glkshowImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  278. /*动态处理宽高*/
  279. make.height.mas_equalTo(des_H1);
  280. make.width.mas_equalTo((des_W1));
  281. make.left.mas_equalTo((des_H1 - des_W1)/2.f);
  282. make.top.mas_equalTo((des_W1 - des_H1)/2.f);
  283. }];
  284. }
  285. - (void)controlBtnPressed:(id)sender{
  286. if ([delegate respondsToSelector:@selector(useInfoBtnBePressed:)]) {
  287. [delegate useInfoBtnBePressed:sender];
  288. }
  289. }
  290. static int sendCount = 0;
  291. #pragma -mark showImageViewDelegate
  292. - (void)showImageViewtouchesBeganCommondStr:(NSString *)commondStr{
  293. if ([delegate respondsToSelector:@selector(playerShowImageViewtouchesStr:)]){
  294. sendCount = 1;
  295. [delegate playerShowImageViewtouchesStr:commondStr];
  296. HLog(@"\n-----showImageViewtouchesBeganCommondStr-----");
  297. }
  298. }
  299. - (void)showImageViewtouchesEndedCommondStr:(NSString *)commondStr{
  300. if ([delegate respondsToSelector:@selector(playerShowImageViewtouchesStr:)]){
  301. HLog(@"\n-----showImageViewtouchesEndedCommondStr-----");
  302. sendCount = 0;
  303. [delegate playerShowImageViewtouchesStr:commondStr];
  304. }
  305. }
  306. - (void)showImageViewtouchesMovedCommondStr:(NSString *)commondStr{
  307. if ([delegate respondsToSelector:@selector(playerShowImageViewtouchesStr:)]){
  308. HLog(@"\n-----showImageViewtouchesMovedCommondStr-----");
  309. if (sendCount%2 == 0){
  310. [delegate playerShowImageViewtouchesStr:commondStr];
  311. }
  312. sendCount++;
  313. }
  314. }
  315. - (void)showImageViewtouchesCancelledCommondStr:(NSString *)commondStr{
  316. if ([delegate respondsToSelector:@selector(playerShowImageViewtouchesStr:)]){
  317. HLog(@"\n-----showImageViewtouchesCancelledCommondStr-----");
  318. sendCount = 0;
  319. [delegate playerShowImageViewtouchesStr:commondStr];
  320. }
  321. }
  322. - (void)controlBtnLongPress:(UILongPressGestureRecognizer *)longPressGesture {
  323. switch (longPressGesture.state) {
  324. case UIGestureRecognizerStateBegan:{
  325. // HLog(@"云手机推流界面 控制按钮长按手势开始");
  326. [[NSNotificationCenter defaultCenter] postNotificationName:PlayerViewControlBtnTouchBeganNotification object:nil userInfo:nil];
  327. break;
  328. case UIGestureRecognizerStateEnded:{
  329. // HLog(@"云手机推流界面 控制按钮长按手势结束");
  330. [[NSNotificationCenter defaultCenter] postNotificationName:PlayerViewControlBtnTouchEndNotification object:nil userInfo:nil];
  331. break;
  332. }
  333. default:
  334. break;
  335. }
  336. }
  337. }
  338. static CGRect startFrame;
  339. - (void)panGestureCallback:(UIPanGestureRecognizer *)panGesture{
  340. self.controlBtn.alpha = 1;
  341. switch (panGesture.state) {
  342. case UIGestureRecognizerStateBegan:{
  343. startFrame = controlBtn.frame;
  344. // HLog(@"云手机推流界面 控制按钮拖动手势开始");
  345. [[NSNotificationCenter defaultCenter] postNotificationName:PlayerViewControlBtnTouchBeganNotification object:nil userInfo:nil];
  346. break;
  347. }
  348. case UIGestureRecognizerStateChanged:{
  349. CGPoint translatedPoint = [panGesture translationInView:self];/*KXLY*/
  350. CGRect frame = controlBtn.frame;
  351. frame.origin.x = translatedPoint.x + startFrame.origin.x;
  352. frame.origin.y = translatedPoint.y + startFrame.origin.y;
  353. controlBtn.frame = frame;
  354. // HLog(@"\n--(%f,%f)--controlBtn.frame--X-%f",translatedPoint.x,translatedPoint.x,controlBtn.frame.origin.x);
  355. break;
  356. }
  357. case UIGestureRecognizerStateEnded:
  358. // HLog(@"云手机推流界面 控制按钮拖动手势结束");
  359. [[NSNotificationCenter defaultCenter] postNotificationName:PlayerViewControlBtnTouchEndNotification object:nil userInfo:nil];
  360. /*防止控制器移出屏幕*/
  361. CGRect frame = controlBtn.frame;
  362. if (frame.origin.x < 10.f/2.f){
  363. frame.origin.x = 10.f/2.f;
  364. }
  365. if (frame.origin.y < 10.f/2.f + H_STATE_BAR){
  366. frame.origin.y = 10.f/2.f + H_STATE_BAR;
  367. }
  368. if (frame.size.width + frame.origin.x + 10.f/2.f > self.width){
  369. frame.origin.x = self.width - 10.f/2.f - frame.size.width;
  370. }
  371. if (frame.size.height + frame.origin.y + 10.f/2.f > self.height){
  372. frame.origin.y = self.height - 10.f/2.f - frame.size.height;
  373. }
  374. controlBtn.frame = frame;
  375. break;
  376. case UIGestureRecognizerStateCancelled: {
  377. /*防止控制器移出屏幕*/
  378. CGRect frame = controlBtn.frame;
  379. if (frame.origin.x < 10.f/2.f){
  380. frame.origin.x = 10.f/2.f;
  381. }
  382. if (frame.origin.y < 10.f/2.f + H_STATE_BAR){
  383. frame.origin.y = 10.f/2.f + H_STATE_BAR;
  384. }
  385. if (frame.size.width + frame.origin.x + 10.f/2.f > self.width){
  386. frame.origin.x = self.width - 10.f/2.f - frame.size.width;
  387. }
  388. if (frame.size.height + frame.origin.y + 10.f/2.f > self.height){
  389. frame.origin.y = self.height - 10.f/2.f - frame.size.height;
  390. }
  391. controlBtn.frame = frame;
  392. break;
  393. }
  394. default:
  395. break;
  396. }
  397. }
  398. - (void)setIsLan:(BOOL)isLan{
  399. self->isLan = isLan;
  400. /*设置视图方向*/
  401. showImageView.isLan = isLan;
  402. touchCommondView.isLan = isLan;
  403. }
  404. @end