//
//  NXTransportSession.h
//  NXSdwan
//
//  Created by 徐家骏 on 2019/5/7.
//  Copyright © 2019 徐家骏. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <NXSdwan/NXGlobalDefs.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSInteger, NXTransportSessionType) {
/**
 *  实时通话数据的传输
 */
NXTransportSessionTypeRealtime              = 0,
/**
 *  大文件请求下载传输
 */
NXTransportSessionTypeRequestForDownloading = 1,
/**
 *  大文件存储转发异步传输
 */
NXTransportSessionTypeStoreAndForward       = 2,
};

typedef NS_ENUM(NSInteger, NXTransportSessionDataCategory) {
    /**
     *  数据类别audio
     */
    NXTransportSessionDataCategoryAudio  = 0,
    /**
     *  数据类别视频
     */
    NXTransportSessionDataCategoryVideo  = 1,
    /**
     *  数据类别其他
     */
    NXTransportSessionDataCategoryOther  = 2,
};

typedef NS_ENUM(uint32_t, NXTransportSessionBandwidthLimitType) {
    /**
     *  标清
     */
    NXTransportSessionBandwidthLimitStandard    = 800,
    /**
     *  高清
     */
    NXTransportSessionBandwidthLimitHD          = 2000,
    /**
     *  超清
     */
    NXTransportSessionBandwidthLimit4k          = 8000,
};

@class NXTransportSession;

@protocol NXTransportSessionAudioDelegate <NSObject>
- (void)transportSession:(NXTransportSession *)tSession didReceiveAudioData:(NSData *)data andContext:(NSData *)dataContext withSeqId:(int16_t)seqid from:(int64_t)from;
- (int)transportSession:(NXTransportSession *)tSession requestJitterBufferAudioRepeatFactorOf:(int64_t)uid;
- (NSSet *)transportSessionGetAudioAcceptList:(NXTransportSession *)tSession;
@end

@protocol NXTransportSessionVideoDelegate <NSObject>
- (void)transportSession:(NXTransportSession *)tSession didReceiveVideoData:(NSData *)data andContext:(NSData *)dataContext withSeqId:(int16_t)seqid from:(int64_t)from;
- (void)transportSession:(NXTransportSession *)tSession askForIFrameFrom:(int64_t)from isThumb:(BOOL)isThumb;
- (BOOL)transportSession:(NXTransportSession *)tSession requestUIInfoViewPosX:(int *)x Y:(int *)y width:(int *)width height:(int *)height of:(int64_t)uid;
- (BOOL)transportSession:(NXTransportSession *)tSession needTempVideoRequestOf:(int64_t)uid;
- (void)transportSession:(NXTransportSession *)tSession stopAcceptVideoForLowBandwidth:(BOOL)stopped;
@end

@protocol  NXTransportSessionDataDelegate <NSObject>
- (void)transportSession:(NXTransportSession *)tSession didReceiveData:(NSData *)data andContext:(NSData *)dataContext withSeqId:(int16_t)seqid from:(int64_t)from;
@end

@protocol NXTransportSessionDelegate <NSObject>
- (void)transportSession:(NXTransportSession *)tSession onEvent:(NXDeviceStatusEvent)event from:(int64_t)from;
@end

@interface NXTransportSession : NSObject

@property (nonatomic) NXTransportSessionType  sessionType;
@property (nonatomic) int64_t                  sessionID;
@property (nonatomic, strong) NSMutableArray  *relayList;
@property (nonatomic)         int64_t          vuid;
@property (nonatomic)         int              p2pEnabledRatio;
@property (nonatomic, weak)  id<NXTransportSessionDelegate>      delegate;
@property (nonatomic, weak)  id<NXTransportSessionAudioDelegate> audioDelegate;
@property (nonatomic, weak)  id<NXTransportSessionVideoDelegate> videoDelegate;
@property (nonatomic, weak)  id<NXTransportSessionDataDelegate>  dataDelegate;
@property (nonatomic, strong)   NSOrderedSet     *activeParticipants;
@property (nonatomic)         uint32_t            bandwidthUpperLimit;

@property (atomic)             BOOL          appCameraOn;
@property (atomic)             BOOL          appForeground;   //在前台
@property (atomic)             BOOL          appInterrupted;  //被系统电话打断状态
@property (atomic)             BOOL          appMuted; //mute状态
@property (atomic)             BOOL          appAudioInterrupted; //音频被中断状态

- (instancetype)initWithType:(NXTransportSessionType)type SessionId:(int64_t)sessionId Vuid:(int64_t)vuid andRelayList:(NSArray *)relayList p2pEnabledRatio:(int)ratio;
- (void)close;
- (int)sendAudioData:(NSData *)data andContext:(NSData *)dataContext withSeqId:(int16_t)seqid;
//- (void)sendAudioDataCBR:(NSData *)data withSeqId:(int16_t)seqid;
- (void)sendVideoData:(NSData *)data andContext:(NSData *)dataContext withSeqId:(int16_t)seqid isIFrame:(BOOL)isIFrame isThumb:(BOOL)isThumb;
- (void)sendData:(NSData *)data andContext:(NSData *)dataContext withSeqId:(int16_t)seqid;
- (void)sendData:(NSData *)data andContext:(NSData *)dataContext withSeqId:(int16_t)seqid to:(int64_t)uid; //数据有单点发送需求
- (void)askForIFrameFrom:(int64_t)from;
- (int)suggestedBitrate;
- (int)suggestedThumbBitrate;
- (int)suggestedAudioBitrate;
- (BOOL)networkReachable;
- (float)maxPermitBitrateExceeded;
- (int)repairRateJump;
- (int)meRTT;
- (int)downRTT;
- (void)downRTT:(void (^)(int64_t, int))block;
- (int)avgRTT;
- (int)bandwidthDown;
- (int)bandwidthUp;
- (int)trafficIn;
- (int)trafficOut;
- (int)jitterCount;
- (int)lossRateDown;
- (int)lossRateUp;
- (int)uiAsBigViewCount;
- (float)uiMaxViewScreenRatio;
- (void)setInitNet:(int)net;
- (double)lastReceiveTimestamp;
- (double)lastReceiveNotIncludeNoopTimestamp;
- (int)repairRate;
- (void)reportUIChange;
- (NSArray *)relayPriority;
- (int)relayPriorityConfidenceLevel;
- (void)changeRelay:(NSDictionary *)newRelay;
- (struct NXNetworkInfo)networkInfo:(int64_t)uid;
- (void)reportNetworkInfo:(int)netStatus :(int)jitterBufferLoss;
- (BOOL)noReceivedOver3s;
- (BOOL)noReceivedWarning;
- (int)liveAccumulateTime;

- (int64_t)trafficInRelay;
- (int64_t)trafficInP2p;
- (int64_t)trafficOutRelay;
- (int64_t)trafficOutP2p;

@end

NS_ASSUME_NONNULL_END
