// Copyright © 2022 BytePlusRTC All rights reserved.
// SPDX-License-Identifier: MIT

//
//  VodVideoProcessor.m
//  FeedShareDemo
//
//  Created by ByteDance on 2022/8/8.
//

#import "VodVideoProcessor.h"
#import "RTCHeader.h"

@interface VodVideoProcessor ()

@property (nonatomic, weak) ByteRTCVideo *rtcKit;
@property (nonatomic, weak) NSDictionary *options;
@property (nonatomic, assign) NSInteger time;
@property (nonatomic, assign) int fps;

@end

@implementation VodVideoProcessor

- (instancetype)initWithRTCKit:(ByteRTCVideo *)rtcKit options:(NSDictionary *)options {
    if (self = [super init]) {
        self.rtcKit = rtcKit;
        self.options = options;
        self.fps = [[options valueForKey:@"fps"] intValue];
        self.time = 0;
        [rtcKit setVideoSourceType:ByteRTCVideoSourceTypeExternal WithStreamIndex:ByteRTCStreamIndexScreen];
    }
    return self;
}

- (void)processVideo:(CVPixelBufferRef)frame timestamp:(int64_t)timestamp {
    if (_fps < 1) {
        if (timestamp > 0) {
            _fps = 1000 / (timestamp - 0);
            NSLog(@"detect video fps=%d", _fps);
        }
    }
    int res = [self.rtcKit pushScreenVideoFrame:frame time:CMTimeMake(self.time, self.fps) rotation:0];
    if (res != 0) {
        NSLog(@"bytertc pushScreenVideoFrame fail return '%d'", res);
    }
    self.time += 1;
}

- (void)releaseVideo {
    NSLog(@"video release");
}

@end
