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.

160 lines
5.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2018 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 "NADViewController.h"
  11. #import "sdk/objc/base/RTCVideoRenderer.h"
  12. #import "sdk/objc/components/capturer/RTCCameraVideoCapturer.h"
  13. #if defined(RTC_SUPPORTS_METAL)
  14. #import "sdk/objc/components/renderer/metal/RTCMTLVideoView.h" // nogncheck
  15. #endif
  16. #import "sdk/objc/components/renderer/opengl/RTCEAGLVideoView.h"
  17. #import "sdk/objc/helpers/RTCCameraPreviewView.h"
  18. #include <memory>
  19. #include "examples/objcnativeapi/objc/objc_call_client.h"
  20. @interface NADViewController ()
  21. @property(nonatomic) RTCCameraVideoCapturer *capturer;
  22. @property(nonatomic) RTCCameraPreviewView *localVideoView;
  23. @property(nonatomic) __kindof UIView<RTCVideoRenderer> *remoteVideoView;
  24. @property(nonatomic) UIButton *callButton;
  25. @property(nonatomic) UIButton *hangUpButton;
  26. @end
  27. @implementation NADViewController {
  28. std::unique_ptr<webrtc_examples::ObjCCallClient> _call_client;
  29. UIView *_view;
  30. }
  31. @synthesize capturer = _capturer;
  32. @synthesize localVideoView = _localVideoView;
  33. @synthesize remoteVideoView = _remoteVideoView;
  34. @synthesize callButton = _callButton;
  35. @synthesize hangUpButton = _hangUpButton;
  36. #pragma mark - View controller lifecycle
  37. - (void)loadView {
  38. _view = [[UIView alloc] initWithFrame:CGRectZero];
  39. #if defined(RTC_SUPPORTS_METAL)
  40. _remoteVideoView = [[RTCMTLVideoView alloc] initWithFrame:CGRectZero];
  41. #else
  42. _remoteVideoView = [[RTCEAGLVideoView alloc] initWithFrame:CGRectZero];
  43. #endif
  44. _remoteVideoView.translatesAutoresizingMaskIntoConstraints = NO;
  45. [_view addSubview:_remoteVideoView];
  46. _localVideoView = [[RTCCameraPreviewView alloc] initWithFrame:CGRectZero];
  47. _localVideoView.translatesAutoresizingMaskIntoConstraints = NO;
  48. [_view addSubview:_localVideoView];
  49. _callButton = [UIButton buttonWithType:UIButtonTypeSystem];
  50. _callButton.translatesAutoresizingMaskIntoConstraints = NO;
  51. [_callButton setTitle:@"Call" forState:UIControlStateNormal];
  52. [_callButton addTarget:self action:@selector(call:) forControlEvents:UIControlEventTouchUpInside];
  53. [_view addSubview:_callButton];
  54. _hangUpButton = [UIButton buttonWithType:UIButtonTypeSystem];
  55. _hangUpButton.translatesAutoresizingMaskIntoConstraints = NO;
  56. [_hangUpButton setTitle:@"Hang up" forState:UIControlStateNormal];
  57. [_hangUpButton addTarget:self
  58. action:@selector(hangUp:)
  59. forControlEvents:UIControlEventTouchUpInside];
  60. [_view addSubview:_hangUpButton];
  61. UILayoutGuide *margin = _view.layoutMarginsGuide;
  62. [_remoteVideoView.leadingAnchor constraintEqualToAnchor:margin.leadingAnchor].active = YES;
  63. [_remoteVideoView.topAnchor constraintEqualToAnchor:margin.topAnchor].active = YES;
  64. [_remoteVideoView.trailingAnchor constraintEqualToAnchor:margin.trailingAnchor].active = YES;
  65. [_remoteVideoView.bottomAnchor constraintEqualToAnchor:margin.bottomAnchor].active = YES;
  66. [_localVideoView.leadingAnchor constraintEqualToAnchor:margin.leadingAnchor constant:8.0].active =
  67. YES;
  68. [_localVideoView.topAnchor constraintEqualToAnchor:margin.topAnchor constant:8.0].active = YES;
  69. [_localVideoView.widthAnchor constraintEqualToConstant:60].active = YES;
  70. [_localVideoView.heightAnchor constraintEqualToConstant:60].active = YES;
  71. [_callButton.leadingAnchor constraintEqualToAnchor:margin.leadingAnchor constant:8.0].active =
  72. YES;
  73. [_callButton.bottomAnchor constraintEqualToAnchor:margin.bottomAnchor constant:8.0].active = YES;
  74. [_callButton.widthAnchor constraintEqualToConstant:100].active = YES;
  75. [_callButton.heightAnchor constraintEqualToConstant:40].active = YES;
  76. [_hangUpButton.trailingAnchor constraintEqualToAnchor:margin.trailingAnchor constant:8.0].active =
  77. YES;
  78. [_hangUpButton.bottomAnchor constraintEqualToAnchor:margin.bottomAnchor constant:8.0].active =
  79. YES;
  80. [_hangUpButton.widthAnchor constraintEqualToConstant:100].active = YES;
  81. [_hangUpButton.heightAnchor constraintEqualToConstant:40].active = YES;
  82. self.view = _view;
  83. }
  84. - (void)viewDidLoad {
  85. [super viewDidLoad];
  86. self.capturer = [[RTCCameraVideoCapturer alloc] init];
  87. self.localVideoView.captureSession = self.capturer.captureSession;
  88. _call_client.reset(new webrtc_examples::ObjCCallClient());
  89. // Start capturer.
  90. AVCaptureDevice *selectedDevice = nil;
  91. NSArray<AVCaptureDevice *> *captureDevices = [RTCCameraVideoCapturer captureDevices];
  92. for (AVCaptureDevice *device in captureDevices) {
  93. if (device.position == AVCaptureDevicePositionFront) {
  94. selectedDevice = device;
  95. break;
  96. }
  97. }
  98. AVCaptureDeviceFormat *selectedFormat = nil;
  99. int targetWidth = 640;
  100. int targetHeight = 480;
  101. int currentDiff = INT_MAX;
  102. NSArray<AVCaptureDeviceFormat *> *formats =
  103. [RTCCameraVideoCapturer supportedFormatsForDevice:selectedDevice];
  104. for (AVCaptureDeviceFormat *format in formats) {
  105. CMVideoDimensions dimension = CMVideoFormatDescriptionGetDimensions(format.formatDescription);
  106. FourCharCode pixelFormat = CMFormatDescriptionGetMediaSubType(format.formatDescription);
  107. int diff = abs(targetWidth - dimension.width) + abs(targetHeight - dimension.height);
  108. if (diff < currentDiff) {
  109. selectedFormat = format;
  110. currentDiff = diff;
  111. } else if (diff == currentDiff && pixelFormat == [_capturer preferredOutputPixelFormat]) {
  112. selectedFormat = format;
  113. }
  114. }
  115. [self.capturer startCaptureWithDevice:selectedDevice format:selectedFormat fps:30];
  116. }
  117. - (void)didReceiveMemoryWarning {
  118. [super didReceiveMemoryWarning];
  119. // Dispose of any resources that can be recreated.
  120. }
  121. #pragma mark - Actions
  122. - (IBAction)call:(id)sender {
  123. _call_client->Call(self.capturer, self.remoteVideoView);
  124. }
  125. - (IBAction)hangUp:(id)sender {
  126. _call_client->Hangup();
  127. }
  128. @end