//
//  RCLiveSteamManager.h
//  RCLiveDemo
//
//  Created by My Apple  on 2021/11/11.
//

#import <Foundation/Foundation.h>
#include <CoreVideo/CVPixelBuffer.h>

NS_ASSUME_NONNULL_BEGIN

typedef enum EventState {
    EventStateUnKnown = -1L,
    EventStateLandscape = 0L,
    EventStateVerticalScreen,
    EventStateTurnOnAudioThrough,
    EventStateTurnOffAudioThrough,
    EventStateTurnOnVideoThrough,
    EventStateTurnOffVideoThrough,
    EventStateScreenChange,
    EventStateTurnOnVideoFront,
    EventStateUploadFileOnLine,
    EventStateUploadFileOffLine,
    EventStateTurnOnVideoDontNeedToServer,
    EventStateTurnOnVideoFrontDontNeedToServer
} EventState;

typedef enum ConnectState {
    ConnectStateDestroy = 0L,
    ConnectStateConnecting,
    ConnectStateConnected,
    ConnectStateRendering,
    ConnectStateClosing,
    ConnectStateClosed,
    ConnectStateAbnormalDisconnect,
    ConnectStateVfailed,
    ConnectStatePicPushing,
    ConnectStatePicPushed,
    ConnectStatePicPushErr,
    ConnectStateMultiLogin,
    ConnectStateVerifyErr,
    ConnectStateNetRequestIFrame
} ConnectState;

typedef enum VirtualDataType {
    VirtualDataTypeVideo = 1L,
    VirtualDataTypeAudio,
    VirtualDataTypePicture = 9L
} VirtualDataType;

typedef enum VirtualRecordState {
    VirtualRecordStateTurnOnAudioThrough = 2L,
    VirtualRecordStateTurnOffAudioThrough,
    VirtualRecordStateTurnOnVideoThrough,
    VirtualRecordStateTurnOffVideoThrough,
    VirtualRecordStateAudioThroughPause,
    VirtualRecordStateVideoThroughPause
} VirtualRecordState;

typedef enum ConnectChannelType {
    ConnectChannelTypeUpload = 0L,
    ConnectChannelTypeUdp = 1L,
    ConnectChannelTypeTcp,
    ConnectChannelTypeKcp,
    ConnectChannelTypeUdpH265,
    ConnectChannelTypeTcpH265,
    ConnectChannelTypeWebsocket,
    ConnectChannelTypeWebsocketH265
} ConnectChannelType;

typedef enum VideoDecodeType {
    VideoDecodeTypeSdk,
    VideoDecodeTypeNative
} VideoDecodeType;

typedef enum AudioDecodeType {
    AudioDecodeTypeSdk,
    AudioDecodeTypeNative
} AudioDecodeType;

typedef enum VirtualThroughModel {
    VirtualThroughModelOnline = 1L,
    VirtualThroughModelOffline
} VirtualThroughModel;

typedef NS_CLOSED_ENUM(NSInteger, RCCloudPhoneConnectType) {
    RCCloudPhoneConnectType_udp = 0L,
    RCCloudPhoneConnectType_websocket
};

typedef NS_CLOSED_ENUM(NSInteger, RCCloudPhoneVideoQuality) {
    RCCloudPhoneVideoQuality_Default = 0L, //h264
    RCCloudPhoneVideoQuality_High          //h265
};

typedef struct {
    int loss_rate_ratio; //丢包率 = loss_rate_ratio / 256 * 100
    int min_rtt;         //传输延时
    int send_fps;        //帧率
    int send_bytes;      //码率
    int current_estimator_rate;   //当前带宽
    int send_delay_ms;   //发送耗时
    int encode_delay_ms; //编号耗时
    int support_resolution_level;
} NetEstimator;

typedef NSInteger RCStateInt;

@class RCLiveSteamManager;
@protocol RCLiveSteamManagerDelegate <NSObject>

- (void)liveStreamManagerOutPut:(RCLiveSteamManager *)manager didOutPutVideoPixelBuffer:(CVPixelBufferRef)pixelBuffer;
- (void)liveStreamManagerOutPut:(RCLiveSteamManager *)manager didOutPutAudioData:(NSData *)pcmData;

- (void)liveStreamManager:(RCLiveSteamManager *)manager connectState:(ConnectState)state;
- (void)liveStreamManager:(RCLiveSteamManager *)manager didReceiveEvent:(EventState)event;
- (void)liveStreamManager:(RCLiveSteamManager *)manager didReceiveMessage:(NSString *)message;
- (void)liveStreamManager:(RCLiveSteamManager *)manager didReceiNetEstimator:(NetEstimator)estimator;
- (void)liveStreamManager:(RCLiveSteamManager *)manager didReceiveJitterDelay:(int)jitterDelayMs;
@end


@interface RCLiveStreamConfigure : NSObject

@property (nonatomic, strong) NSString *ipAdress;
@property (nonatomic, assign) NSInteger port;
@property (nonatomic, assign) RCCloudPhoneConnectType connectType;
@property (nonatomic, assign) RCCloudPhoneVideoQuality videoQuality;
 
@end


@interface RCLiveSteamManager : NSObject

@property (nonatomic, weak) id <RCLiveSteamManagerDelegate> delegate;

- (RCStateInt)rc_openWithConfigure:(RCLiveStreamConfigure *)configure verify:(nullable NSString *)verify;

- (RCStateInt)rc_openWithIp:(nonnull NSString *)ip
                    port:(NSInteger)port
                connectType:(RCCloudPhoneConnectType)connectType
            videoQuality:(RCCloudPhoneVideoQuality)videoQuality
                     verify:(nullable NSString *)verify
               streamAESKey:(nullable NSString *)aesKey;

- (void)rc_requestIDFrame;

- (void)rc_disconnect;

- (void)rc_release;

- (void)rc_sendTransportData:(const char *)buffer length:(int)length dataType:(VirtualDataType)type;

- (void)rc_sendTransportRecordState:(VirtualRecordState)state;

- (void)rc_setMediaTransportModel:(VirtualThroughModel)model;

- (void)rc_sendJsonData:(const char *)data;

- (void)rc_setDecodeVideoDelay:(int)delay;

@end

NS_ASSUME_NONNULL_END
