You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
7.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2015 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. #import "ARDVideoCallView.h"
  11. #import <AVFoundation/AVFoundation.h>
  12. #import <WebRTC/RTCEAGLVideoView.h>
  13. #if defined(RTC_SUPPORTS_METAL)
  14. #import <WebRTC/RTCMTLVideoView.h>
  15. #endif
  16. #import "UIImage+ARDUtilities.h"
  17. static CGFloat const kButtonPadding = 16;
  18. static CGFloat const kButtonSize = 48;
  19. static CGFloat const kLocalVideoViewSize = 120;
  20. static CGFloat const kLocalVideoViewPadding = 8;
  21. static CGFloat const kStatusBarHeight = 20;
  22. @interface ARDVideoCallView () <RTCVideoViewDelegate>
  23. @end
  24. @implementation ARDVideoCallView {
  25. UIButton *_routeChangeButton;
  26. UIButton *_cameraSwitchButton;
  27. UIButton *_hangupButton;
  28. CGSize _remoteVideoSize;
  29. }
  30. @synthesize statusLabel = _statusLabel;
  31. @synthesize localVideoView = _localVideoView;
  32. @synthesize remoteVideoView = _remoteVideoView;
  33. @synthesize statsView = _statsView;
  34. @synthesize delegate = _delegate;
  35. - (instancetype)initWithFrame:(CGRect)frame {
  36. if (self = [super initWithFrame:frame]) {
  37. #if defined(RTC_SUPPORTS_METAL)
  38. _remoteVideoView = [[RTCMTLVideoView alloc] initWithFrame:CGRectZero];
  39. #else
  40. RTCEAGLVideoView *remoteView = [[RTCEAGLVideoView alloc] initWithFrame:CGRectZero];
  41. remoteView.delegate = self;
  42. _remoteVideoView = remoteView;
  43. #endif
  44. [self addSubview:_remoteVideoView];
  45. _localVideoView = [[RTCCameraPreviewView alloc] initWithFrame:CGRectZero];
  46. [self addSubview:_localVideoView];
  47. _statsView = [[ARDStatsView alloc] initWithFrame:CGRectZero];
  48. _statsView.hidden = YES;
  49. [self addSubview:_statsView];
  50. _routeChangeButton = [UIButton buttonWithType:UIButtonTypeCustom];
  51. _routeChangeButton.backgroundColor = [UIColor whiteColor];
  52. _routeChangeButton.layer.cornerRadius = kButtonSize / 2;
  53. _routeChangeButton.layer.masksToBounds = YES;
  54. UIImage *image = [UIImage imageNamed:@"ic_surround_sound_black_24dp.png"];
  55. [_routeChangeButton setImage:image forState:UIControlStateNormal];
  56. [_routeChangeButton addTarget:self
  57. action:@selector(onRouteChange:)
  58. forControlEvents:UIControlEventTouchUpInside];
  59. [self addSubview:_routeChangeButton];
  60. // TODO(tkchin): don't display this if we can't actually do camera switch.
  61. _cameraSwitchButton = [UIButton buttonWithType:UIButtonTypeCustom];
  62. _cameraSwitchButton.backgroundColor = [UIColor whiteColor];
  63. _cameraSwitchButton.layer.cornerRadius = kButtonSize / 2;
  64. _cameraSwitchButton.layer.masksToBounds = YES;
  65. image = [UIImage imageNamed:@"ic_switch_video_black_24dp.png"];
  66. [_cameraSwitchButton setImage:image forState:UIControlStateNormal];
  67. [_cameraSwitchButton addTarget:self
  68. action:@selector(onCameraSwitch:)
  69. forControlEvents:UIControlEventTouchUpInside];
  70. [self addSubview:_cameraSwitchButton];
  71. _hangupButton = [UIButton buttonWithType:UIButtonTypeCustom];
  72. _hangupButton.backgroundColor = [UIColor redColor];
  73. _hangupButton.layer.cornerRadius = kButtonSize / 2;
  74. _hangupButton.layer.masksToBounds = YES;
  75. image = [UIImage imageForName:@"ic_call_end_black_24dp.png"
  76. color:[UIColor whiteColor]];
  77. [_hangupButton setImage:image forState:UIControlStateNormal];
  78. [_hangupButton addTarget:self
  79. action:@selector(onHangup:)
  80. forControlEvents:UIControlEventTouchUpInside];
  81. [self addSubview:_hangupButton];
  82. _statusLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  83. _statusLabel.font = [UIFont fontWithName:@"Roboto" size:16];
  84. _statusLabel.textColor = [UIColor whiteColor];
  85. [self addSubview:_statusLabel];
  86. UITapGestureRecognizer *tapRecognizer =
  87. [[UITapGestureRecognizer alloc]
  88. initWithTarget:self
  89. action:@selector(didTripleTap:)];
  90. tapRecognizer.numberOfTapsRequired = 3;
  91. [self addGestureRecognizer:tapRecognizer];
  92. }
  93. return self;
  94. }
  95. - (void)layoutSubviews {
  96. CGRect bounds = self.bounds;
  97. if (_remoteVideoSize.width > 0 && _remoteVideoSize.height > 0) {
  98. // Aspect fill remote video into bounds.
  99. CGRect remoteVideoFrame =
  100. AVMakeRectWithAspectRatioInsideRect(_remoteVideoSize, bounds);
  101. CGFloat scale = 1;
  102. if (remoteVideoFrame.size.width > remoteVideoFrame.size.height) {
  103. // Scale by height.
  104. scale = bounds.size.height / remoteVideoFrame.size.height;
  105. } else {
  106. // Scale by width.
  107. scale = bounds.size.width / remoteVideoFrame.size.width;
  108. }
  109. remoteVideoFrame.size.height *= scale;
  110. remoteVideoFrame.size.width *= scale;
  111. _remoteVideoView.frame = remoteVideoFrame;
  112. _remoteVideoView.center =
  113. CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
  114. } else {
  115. _remoteVideoView.frame = bounds;
  116. }
  117. // Aspect fit local video view into a square box.
  118. CGRect localVideoFrame =
  119. CGRectMake(0, 0, kLocalVideoViewSize, kLocalVideoViewSize);
  120. // Place the view in the bottom right.
  121. localVideoFrame.origin.x = CGRectGetMaxX(bounds)
  122. - localVideoFrame.size.width - kLocalVideoViewPadding;
  123. localVideoFrame.origin.y = CGRectGetMaxY(bounds)
  124. - localVideoFrame.size.height - kLocalVideoViewPadding;
  125. _localVideoView.frame = localVideoFrame;
  126. // Place stats at the top.
  127. CGSize statsSize = [_statsView sizeThatFits:bounds.size];
  128. _statsView.frame = CGRectMake(CGRectGetMinX(bounds),
  129. CGRectGetMinY(bounds) + kStatusBarHeight,
  130. statsSize.width, statsSize.height);
  131. // Place hangup button in the bottom left.
  132. _hangupButton.frame =
  133. CGRectMake(CGRectGetMinX(bounds) + kButtonPadding,
  134. CGRectGetMaxY(bounds) - kButtonPadding -
  135. kButtonSize,
  136. kButtonSize,
  137. kButtonSize);
  138. // Place button to the right of hangup button.
  139. CGRect cameraSwitchFrame = _hangupButton.frame;
  140. cameraSwitchFrame.origin.x =
  141. CGRectGetMaxX(cameraSwitchFrame) + kButtonPadding;
  142. _cameraSwitchButton.frame = cameraSwitchFrame;
  143. // Place route button to the right of camera button.
  144. CGRect routeChangeFrame = _cameraSwitchButton.frame;
  145. routeChangeFrame.origin.x =
  146. CGRectGetMaxX(routeChangeFrame) + kButtonPadding;
  147. _routeChangeButton.frame = routeChangeFrame;
  148. [_statusLabel sizeToFit];
  149. _statusLabel.center =
  150. CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
  151. }
  152. #pragma mark - RTCVideoViewDelegate
  153. - (void)videoView:(id<RTCVideoRenderer>)videoView didChangeVideoSize:(CGSize)size {
  154. if (videoView == _remoteVideoView) {
  155. _remoteVideoSize = size;
  156. }
  157. [self setNeedsLayout];
  158. }
  159. #pragma mark - Private
  160. - (void)onCameraSwitch:(id)sender {
  161. [_delegate videoCallViewDidSwitchCamera:self];
  162. }
  163. - (void)onRouteChange:(id)sender {
  164. [_delegate videoCallViewDidChangeRoute:self];
  165. }
  166. - (void)onHangup:(id)sender {
  167. [_delegate videoCallViewDidHangup:self];
  168. }
  169. - (void)didTripleTap:(UITapGestureRecognizer *)recognizer {
  170. [_delegate videoCallViewDidEnableStats:self];
  171. }
  172. @end