//
//  NXVideoEngine.h
//  NXCall
//
//  Created by 徐家骏 on 2019/4/24.
//  Copyright © 2019 徐家骏. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <NXSdwan/NXSdwan.h>
#import "NXGlobalDefs.h"  //不知道为啥在pch里面有了，这儿还要这句才行。。。
#import "NXVideoFrameContext.h"
#import "NXGLView.h"

NS_ASSUME_NONNULL_BEGIN

@protocol NXVideoEngineDelegate <NSObject>

- (void)onRemoteVideoFrameReady:(CVImageBufferRef)image withContext:(NXVideoFrameContext *)frameContext from:(int64_t)from;
- (void)onLocalVideoFrameReady:(CVImageBufferRef)image withContext:(NXVideoFrameContext *)frameContext;
- (void)onStopAcceptVideo:(BOOL)stopped;

@end

@interface NXVideoEngine : NSObject<NXTransportSessionVideoDelegate>

@property (nonatomic, weak)  id<NXVideoEngineDelegate>    delegate;
@property (nonatomic, strong, nullable) NXTransportSession *transportSession;  //CALL MANAGER里面会持有，这儿weak即可，以后engine改为非单例时，可以在dealloc中释放 //后改为strong，因为会引起crash
@property (nonatomic, strong)         NSOrderedSet        *activeParticipants;
@property (nonatomic)   NXCallMode      mode;
@property (atomic)      BOOL             isAppStateBackground;
/// 图像是否自动跟随设备自动旋转。默认 YES。
@property (nonatomic, getter = isAutoRotate) BOOL autoRotate;

+ (instancetype)sharedInstance;
- (void)StartPreview;
- (void)Start;
- (void)Stop;

- (NXVideoCameraPosition) getCameraPosition;
- (void) setCameraPosition:(NXVideoCameraPosition)position;
- (uint32_t) getVideoMaxBitrate;
- (void) setVideoMaxBitrate:(uint32_t)bitrate;
- (int32_t) getVideoFramerate;
- (void) setVideoFramerate:(int32_t)framerate;
- (NXCallVideoPreset) getVideoPreset;
- (void) setVideoPreset:(NXCallVideoPreset)preset;
- (void) setVideoPreferredMode:(NXCallVideoPreferredMode) preferredMode;
- (void) setCameraOn:(BOOL)on;
- (BOOL) isCameraOn;
- (void) setVideoCodecH264Preferred:(BOOL)h264Preferred;
- (BOOL) isH264Preferred;
- (BOOL) isGreenCurtainOn;
- (void) setGreenCurtain:(BOOL)on;
- (void) setGreenCurtainBackground:(UIImage *)image;

- (void)registerGLView:(NXGLView *)glview forUid:(int64_t)uid; //这个是给NXGlView用的，用来建立view和uid的联系
- (void)unRegisterGLView:(NXGLView *)glview forUid:(int64_t)uid;
- (void)registerGLView:(NXGLView *)glview forTempUid:(int64_t)uid; //这个是给切换视频流后，新流没来之前临时拉取原视频流。
- (void)unRegisterGLView:(NXGLView *)glview forTempUid:(int64_t)uid;
- (UIDeviceOrientation) currentDeviceOrientation;

//用于视频画面的水平、垂直镜像及透视变换
#pragma mark - perspective transform
- (BOOL)isMirrorHorizontalUid:(int64_t)uid;
- (BOOL)isMirrorVerticalUid:(int64_t)uid;
- (void)setMirrorHorizontal:(BOOL)mirrored uid:(int64_t)uid;
- (void)setMirrorVertical:(BOOL)mirrored uid:(int64_t)uid;
- (BOOL)getBaseRect:(CGFloat *)x0 :(CGFloat *)y0 :(CGFloat *)x1 :(CGFloat *)y1 :(CGFloat *)x2 :(CGFloat *)y2 :(CGFloat *)x3 :(CGFloat *)y3 uid:(int64_t)uid autoDetection:(BOOL)enabled;
- (void)setBaseRect:(CGFloat)x0 :(CGFloat)y0 :(CGFloat)x1 :(CGFloat)y1 :(CGFloat)x2 :(CGFloat)y2 :(CGFloat)x3 :(CGFloat)y3 uid:(int64_t)uid;
- (void)getPerspectiveRect:(CGFloat *)x0 :(CGFloat *)y0 :(CGFloat *)x1 :(CGFloat *)y1 :(CGFloat *)x2 :(CGFloat *)y2 :(CGFloat *)x3 :(CGFloat *)y3 uid:(int64_t)uid;
- (void)setPerspectiveRect:(CGFloat)x0 :(CGFloat)y0 :(CGFloat)x1 :(CGFloat)y1 :(CGFloat)x2 :(CGFloat)y2 :(CGFloat)x3 :(CGFloat)y3 uid:(int64_t)uid;
- (void)autoPerspectiveTransform:(int64_t)uid;
- (void)confirmVideoTransformUid:(int64_t)uid;
- (void)cancelVideoTransformUid:(int64_t)uid;
- (void)resetVideoTransformUid:(int64_t)uid;

- (void)sendVideoTransformCommandUid:(int64_t)uid op:(NSString *)op;
- (void)onReceivedVideoTransformCommand:(NSData *)data from:(int64_t)from;
@end

NS_ASSUME_NONNULL_END
