WebRtcPlayerViewController.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // WebRtcPlayerViewController.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/4/29.
  6. //
  7. #import "WebRtcPlayerViewController.h"
  8. #import "AppDelegate.h"
  9. @interface WebRtcPlayerViewController ()
  10. @property (nonatomic, assign) BOOL canRate;
  11. @property (nonatomic, assign) BOOL isLan;
  12. @end
  13. @implementation WebRtcPlayerViewController {
  14. //ARDFileCaptureController *_fileCaptureController NS_AVAILABLE_IOS(10);
  15. webRtcVideoCallView *_videoCallView;
  16. NSURL *_url;
  17. NSString* _roomName;
  18. NSInteger _direct;
  19. NSInteger _fmt;
  20. NSInteger _vsize;
  21. NSInteger _fps;
  22. NSInteger _bitrate;
  23. }
  24. @synthesize delegate=_delegate;
  25. - (instancetype)initForRoom:(NSURL *)url
  26. roomName:(NSString*)roomName
  27. direct:(NSInteger)direct
  28. fmt:(NSInteger)fmt
  29. vsize:(NSInteger)vsize
  30. fps:(NSInteger)fps
  31. bitrate:(NSInteger)bitrate
  32. delegate:(id<WebRtcPlayerViewControllerDelegate>)delegate {
  33. if (self = [super init]) {
  34. self->_url=url;
  35. self->_roomName=roomName;
  36. self->_direct=direct;
  37. self->_fmt=fmt;
  38. self->_vsize=vsize;
  39. self->_fps=fps;
  40. self->_bitrate=bitrate;
  41. self.delegate=delegate;
  42. [self customLoadView];
  43. [self addNSNotification];
  44. }
  45. return self;
  46. }
  47. - (void)customLoadView {
  48. _canRate = NO;
  49. _isLan = NO;
  50. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  51. app.isInPlayer = YES;
  52. _videoCallView = [[webRtcVideoCallView alloc] initForRoom:_url
  53. roomName:_roomName
  54. direct:_direct
  55. fmt:_fmt
  56. vsize:_vsize
  57. fps:_fps
  58. bitrate:_bitrate
  59. delegate:self];
  60. self.view = _videoCallView;
  61. //[self.view addSubview:_videoCallView];
  62. // _videoCallView.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H);
  63. // [_videoCallView mas_makeConstraints:^(MASConstraintMaker *make) {
  64. // make.left.mas_equalTo(0);
  65. // make.right.mas_equalTo(0);
  66. // make.top.mas_equalTo(100);
  67. // make.bottom.mas_equalTo(200);
  68. // }];
  69. }
  70. - (void)videoCallViewDidHangup:(webRtcVideoCallView *)view{
  71. [_delegate viewControllerDidFinish:self];
  72. }
  73. -(void)onFrameResolutionChangedFromPeerName:(NSString*)peerName videoWidth:(int)videoWidth videoHeight:(int)videoHeight rotation:(int)rotation {
  74. HLog(@"横屏状态:%d",rotation);
  75. if (rotation == 0) {
  76. self.isLan = NO;
  77. [horizontalScreenTool setCurScreenIsHorizontaltype:NO];
  78. } else {
  79. self.isLan = YES;
  80. [horizontalScreenTool setCurScreenIsHorizontaltype:YES];
  81. }
  82. if (@available(iOS 16,*)){
  83. [self setNeedsUpdateOfSupportedInterfaceOrientations];
  84. }
  85. }
  86. #pragma mark - 屏幕旋转相关
  87. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  88. return UIInterfaceOrientationPortrait;
  89. }
  90. - (BOOL)shouldAutorotate {
  91. if (_canRate)/*画面发现变化 状态变为YES*/{
  92. _canRate = NO;/*关键点 只能旋转一次*/
  93. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  94. app.allowAutoRotate = NO;
  95. return YES;
  96. }
  97. //是否允许转屏
  98. return NO;/*平时禁止*/
  99. }
  100. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  101. if (@available(iOS 16,*)){
  102. if (_isLan){
  103. return UIInterfaceOrientationMaskLandscapeLeft;
  104. }
  105. return UIInterfaceOrientationMaskPortrait;
  106. }
  107. return UIInterfaceOrientationMaskAll;
  108. }
  109. - (void)viewDidLoad
  110. {
  111. [super viewDidLoad];
  112. self.toolBar.hidden = YES;
  113. self.navigationBar.hidden = YES;
  114. }
  115. - (void)viewWillAppear:(BOOL)animated
  116. {
  117. [super viewWillAppear:animated];
  118. // 屏幕常亮
  119. [UIApplication sharedApplication].idleTimerDisabled = YES;
  120. [[UIApplication sharedApplication] setStatusBarHidden:YES];
  121. }
  122. - (void)viewWillDisappear:(BOOL)animated {
  123. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  124. app.isInPlayer = NO;
  125. app.allowAutoRotate = NO;
  126. [horizontalScreenTool setCurScreenIsHorizontaltype:NO];
  127. [super viewWillDisappear:animated];
  128. // 屏幕常亮
  129. [UIApplication sharedApplication].idleTimerDisabled = NO;
  130. [[UIApplication sharedApplication] setStatusBarHidden:NO];
  131. }
  132. - (void)addNSNotification
  133. {
  134. [[AVAudioSession sharedInstance] addObserver:self forKeyPath:@"outputVolume" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:(void *)[AVAudioSession sharedInstance]];
  135. }
  136. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
  137. if(context == (__bridge void *)[AVAudioSession sharedInstance])
  138. {
  139. float newValue = [[change objectForKey:@"new"] floatValue];
  140. float oldValue = [[change objectForKey:@"old"] floatValue];
  141. if (newValue > oldValue)
  142. {
  143. HLog(@"\n-----音量增加");
  144. [_videoCallView volumeUp];
  145. }
  146. else
  147. {
  148. HLog(@"\n-----音量降低");
  149. [_videoCallView volumeDown];
  150. }
  151. }
  152. }
  153. @end