1616#import " AudioQueuePlay.h"
1717#import " TCGDemoAudioCapturor.h"
1818#import < CoreMotion/CoreMotion.h>
19+ #import " video_capture/TCGCameraVideoCapturer.h"
1920
2021@interface TCGDemoGamePlayVC () <TcrSessionObserver, TCGDemoTextFieldDelegate, CustomDataChannelObserver, TCGDemoSettingViewDelegate, TCRLogDelegate,
2122 VideoSink, AudioSink, TcrRenderViewObserver, TCGDemoMultiSettingViewDelegate, UIGestureRecognizerDelegate>
@@ -50,8 +51,12 @@ @interface TCGDemoGamePlayVC () <TcrSessionObserver, TCGDemoTextFieldDelegate, C
5051@property (nonatomic , assign ) BOOL isFirstRender;
5152@property (nonatomic , assign ) BOOL isMobile;
5253@property (strong , nonatomic ) CMMotionManager *motionManager;
53-
54-
54+ @property (nonatomic , strong ) TCGCameraVideoCapturer *videoCapturer;
55+ @property (nonatomic , assign ) BOOL isFrontCamera;
56+ @property (nonatomic , assign ) int captureWidth;
57+ @property (nonatomic , assign ) int captureHeight;
58+ @property (nonatomic , assign ) int captureFps;
59+ @property (nonatomic , assign ) BOOL enableSendCustomVideo;
5560@end
5661
5762@implementation TCGDemoGamePlayVC
@@ -67,6 +72,25 @@ - (instancetype)initWithPlay:(TcrSession *)play remoteSession:(NSString *)remote
6772 self.isFirstRender = NO ;
6873 self.remoteSession = remoteSession;
6974 self.loadingView = (TCGDemoLoadingView *)loadingView;
75+ self.captureWidth = 720 ;
76+ self.captureHeight = 1280 ;
77+ self.captureFps = 20 ;
78+ [self .session setTcrSessionObserver: self ];
79+ }
80+ return self;
81+ }
82+
83+ - (instancetype )initWithPlay : (TcrSession *)play remoteSession : (NSString *)remoteSession loadingView : (UIView *)loadingView captureWidth : (int )captureWidth captureHeight : (int )captureHeight captureFps : (int )captureFps {
84+ self = [super init ];
85+ if (self) {
86+ self.session = play;
87+ self.isFirstRender = NO ;
88+ self.remoteSession = remoteSession;
89+ self.loadingView = (TCGDemoLoadingView *)loadingView;
90+ self.captureWidth = captureWidth;
91+ self.captureHeight = captureHeight;
92+ self.captureFps = captureFps;
93+ self.enableSendCustomVideo = true ;
7094 [self .session setTcrSessionObserver: self ];
7195 }
7296 return self;
@@ -349,6 +373,7 @@ - (void)stopGame {
349373 [self .debugLabTimer invalidate ];
350374 [self .renderView removeFromSuperview ];
351375 [self .audioPlayer stop ];
376+ [self .videoCapturer stopCapture ];
352377 [self .session releaseSession ];
353378 if (self.gameStopBlk ) {
354379 self.gameStopBlk ();
@@ -460,11 +485,25 @@ - (void)onEnableLocalAudio:(BOOL)enable {
460485}
461486
462487- (void )onEnableLocalVideo : (BOOL )enable {
488+ if (_enableSendCustomVideo) {
489+ if (enable) {
490+ AVCaptureDevice *device = [self selectDevice ];
491+ AVCaptureDeviceFormat *format = [self selectFormatForDevice: device];
492+ if (_videoCapturer == nil ) {
493+ _videoCapturer = [[TCGCameraVideoCapturer alloc ] initWithTcrSession: self .session];
494+ }
495+ [_videoCapturer startCaptureWithDevice: device format: format fps: _captureFps];
496+
497+ } else {
498+ [_videoCapturer stopCapture ];
499+ }
500+ }
501+
463502 [self .session setEnableLocalVideo: enable];
464503}
465504
466505- (void )onSwitchCamera : (BOOL )isFrontCamera {
467- [self .session setLocalVideoProfile: 1280 height: 720 fps: 30 minBitrate: 1000 maxBitrate: 5000 isFrontCamera: isFrontCamera];
506+ [self .session setLocalVideoProfile: _captureWidth height: _captureHeight fps: _captureFps minBitrate: 1000 maxBitrate: 5000 isFrontCamera: isFrontCamera];
468507}
469508
470509- (void )onRotateView {
@@ -583,6 +622,7 @@ - (void)onEvent:(TcrEvent)event eventData:(id)eventData {
583622 break ;
584623 case CLIENT_STATS:
585624 info = (NSDictionary *)eventData;
625+ NSLog (@" ApiTest CLIENT_STATS: %@ " , info);
586626 [self updateDebugInfo: eventData];
587627 break ;
588628 case CLIENT_IDLE:
@@ -605,8 +645,9 @@ - (void)onEvent:(TcrEvent)event eventData:(id)eventData {
605645 if ([status isEqualToString: @" close" ]) {
606646 [self onEnableLocalVideo: false ];
607647 } else {
648+ _isFrontCamera = [status isEqualToString: @" open_front" ];
608649 [self onEnableLocalVideo: true ];
609- [self .session setLocalVideoProfile: 720 height: 1280 fps: 20 minBitrate: 1000 maxBitrate: 15000 isFrontCamera: [status isEqualToString: @" open_front " ] ? YES : NO ];
650+ [self .session setLocalVideoProfile: _captureWidth height: _captureHeight fps: _captureFps minBitrate: 1000 maxBitrate: 15000 isFrontCamera: _isFrontCamera ];
610651 }
611652 break ;
612653 }
@@ -726,6 +767,42 @@ - (void)onsetMicMute:(nonnull NSString *)userid enable:(BOOL)index {
726767 NSLog (@" 禁言结果:%d " , retCode);
727768 }];
728769}
770+
771+ - (AVCaptureDevice *)selectDevice {
772+ AVCaptureDevicePosition position = _isFrontCamera ? AVCaptureDevicePositionFront : AVCaptureDevicePositionBack;
773+ NSArray <AVCaptureDevice *> *captureDevices;
774+ if (@available (iOS 10.0 , *)) {
775+ AVCaptureDeviceDiscoverySession *session =
776+ [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes: @[AVCaptureDeviceTypeBuiltInWideAngleCamera] mediaType: AVMediaTypeVideo
777+ position: AVCaptureDevicePositionUnspecified];
778+ captureDevices = session.devices ;
779+ } else {
780+ captureDevices = [AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo];
781+ }
782+ for (AVCaptureDevice *device in captureDevices) {
783+ if (device.position == position) {
784+ return device;
785+ }
786+ }
787+ return captureDevices[0 ];
788+ }
789+ - (AVCaptureDeviceFormat *)selectFormatForDevice : (AVCaptureDevice *)device {
790+ NSArray <AVCaptureDeviceFormat *> *formats = device.formats ;
791+ AVCaptureDeviceFormat *selectedFormat = nil ;
792+ int currentDiff = INT_MAX;
793+ for (AVCaptureDeviceFormat *format in formats) {
794+ CMVideoDimensions dimension = CMVideoFormatDescriptionGetDimensions (format.formatDescription );
795+ FourCharCode pixelFormat = CMFormatDescriptionGetMediaSubType (format.formatDescription );
796+ int diff = abs (_captureWidth - dimension.width ) + abs (_captureHeight - dimension.height );
797+ if (diff <= currentDiff) {
798+ selectedFormat = format;
799+ currentDiff = diff;
800+ } else if (_videoCapturer != nil && diff == currentDiff && pixelFormat == [_videoCapturer preferredOutputPixelFormat ]) {
801+ selectedFormat = format;
802+ }
803+ }
804+ return selectedFormat;
805+ }
729806
730807#pragma mark - 手势操作
731808- (void )addEdgeSwipeGestures {
0 commit comments