//
//  XPSDKUpstreamVideoConnection.h
//  XPMobileSDK
//
//  Created by Aleksandra Spirovska on 11/29/17.
//  Copyright © 2017 Milestone Systems A/S. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "XPSDKVideoConnection.h"
#import "XPSDKLocationData.h"
#import <UIKit/UIKit.h>

@protocol XPSDKUpstreamVideoConnectionDelegate;

@interface XPSDKUpstreamVideoConnection : XPSDKVideoConnection {
    NSMutableURLRequest * request;
    
    unsigned int frameCounter;
    float fps;
}

@property (nonatomic) BOOL readyForModeData;
@property (nonatomic, assign) CGFloat compression;
@property (nonatomic, weak) id<XPSDKUpstreamVideoConnectionDelegate> upstreamEventsDelegate;

@property (nonatomic, strong) XPSDKLocationData* location;

@property (nonatomic, readonly) BOOL locationSupportedByServer;
@property (nonatomic, strong) NSDate * fpsStartTime;
@property (nonatomic) NSUInteger videoDeviceIndex;
@property (nonatomic) BOOL automatic;

/*!
Designated initializer. Instances should only ne created by the main connection!
@param mainConnection the main connection from which the video connection is derived.
@param vid Video ID
*/
- (id) initFromControlConnection:(XPSDKConnection *) mainConnection
                         videoID:(XPSDKVideoID) vid
       locationSupportedByServer:(BOOL) locSupported;

-(void) open;
-(XPSDKVideoConnectionMethod) method;

- (void) uploadFromBuffer: (CVImageBufferRef) imageBuffer;
- (void) enableLocation: (BOOL) enabled;

@end


@protocol XPSDKUpstreamVideoConnectionDelegate <NSObject>

@required
/*!
 Called in case an error has occured during the upload
 @param upstreamVideoConnection The upstream connection instance
 @param error Brief description of the error. Most likely the error code will be an HTTP status code. You can also check the underlying error, if it exists for more information
 
 Note that this method will ALWAYS be called on the main thread!
 */
- (void) upstreamVideoConnection:(XPSDKUpstreamVideoConnection *) upstreamVideoConnection receivedError: (NSError *) error;

@optional
/*!
 Called on each image upload.
 @param upstreamVideoConnection Pointer to the upstream instance
 @param compression Compression of the image (0..1)
 @param byteLength Size of the image in bytes
 @param fps Average frames per second
 
 WARNING: This method will most likely be called on a thread, different from the main one!
 */
- (void) upstreamVideoConnection:(XPSDKUpstreamVideoConnection *) upstreamVideoConnection uploadedFrameWithCompression: (float) compression size: (NSUInteger) byteLength fps: (NSUInteger) fps;

@end

