initial commit

This commit is contained in:
Caleb
2026-03-01 15:59:27 +08:00
commit a9e97d56cb
1426 changed files with 172367 additions and 0 deletions

View File

@@ -0,0 +1,167 @@
//
// AVVideoConfiguration.m
// AVDemo
//
#import "AVVideoConfiguration.h"
@implementation AVVideoConfiguration
#pragma mark - Initialization
- (instancetype)init {
if (self = [super init]) {
//
_nickname = @"";
_streamId = @"";
_codec = AVVideoCodeH264;
}
return self;
}
#pragma mark - Factory Methods
+ (instancetype)defaultConfiguration {
AVVideoConfiguration *config = [[AVVideoConfiguration alloc] init];
//
NSInteger randomNum = arc4random_uniform(1000);
config.nickname = [NSString stringWithFormat:@"test%03ld", (long)randomNum];
// streamIdstream + 3
NSInteger streamRandomNum = 100 + arc4random_uniform(900); // 100-999
config.streamId = [NSString stringWithFormat:@"%ld", (long)streamRandomNum];
config.codec = AVVideoCodeH264;
// 720p
[config setResolution:AVVideoResolution360p];
//
config.videoFrameRate = 20;
config.videoMinFrameRate = 15;
config.videoMaxKeyframeInterval = 60; // 2x fps
// kbps bps
config.videoBitRate = 2000 * 1000; // 2000 kbps
config.videoMinBitRate = 500 * 1000; // 500 kbps
return config;
}
+ (instancetype)configurationWithResolution:(AVVideoResolution)resolution {
AVVideoConfiguration *config = [self defaultConfiguration];
[config setResolution:resolution];
return config;
}
#pragma mark - Resolution Management
- (void)setResolution:(AVVideoResolution)resolution {
// 使
[self setCaptureResolution:resolution outputResolution:resolution];
}
- (void)setCaptureResolution:(AVVideoResolution)captureResolution
outputResolution:(AVVideoResolution)outputResolution {
// videoSize
CGSize outputSize = AVVideoResolutionSize(outputResolution);
self.videoSize = outputSize;
//
[self adjustBitrateForResolution:outputResolution];
}
- (void)adjustBitrateForResolution:(AVVideoResolution)resolution {
NSUInteger maxBitrate, minBitrate;
switch (resolution) {
case AVVideoResolution360p:
maxBitrate = 600;
minBitrate = 300;
break;
case AVVideoResolution480p:
maxBitrate = 900;
minBitrate = 400;
break;
case AVVideoResolution540p:
maxBitrate = 1500;
minBitrate = 500;
break;
case AVVideoResolution720p:
maxBitrate = 2000;
minBitrate = 800;
break;
}
self.videoBitRate = maxBitrate * 1000; // Convert kbps to bps
self.videoMinBitRate = minBitrate * 1000; // Convert kbps to bps
}
- (AVVideoResolution)currentResolution {
//
return [self currentOutputResolution];
}
- (AVVideoResolution)currentOutputResolution {
// videoSize
CGSize size = self.videoSize;
//
CGFloat width = MAX(size.width, size.height);
CGFloat height = MIN(size.width, size.height);
if (width == 640 && height == 360) {
return AVVideoResolution360p;
} else if (width == 854 && height == 480) {
return AVVideoResolution480p;
} else if (width == 960 && height == 540) {
return AVVideoResolution540p;
} else if (width == 1280 && height == 720) {
return AVVideoResolution720p;
}
// 720p
return AVVideoResolution720p;
}
#pragma mark - Convenience Accessors
- (NSInteger)maxBitrateKbps {
return self.videoBitRate / 1000;
}
- (NSInteger)minBitrateKbps {
return self.videoMinBitRate / 1000;
}
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone {
// copy
AVVideoConfiguration *copy = [super copyWithZone:zone];
//
copy.nickname = [self.nickname copy];
copy.streamId = [self.streamId copy];
copy.codec = self.codec;
return copy;
}
#pragma mark - Description
- (NSString *)description {
NSMutableString *desc = @"".mutableCopy;
[desc appendFormat:@"<AVVideoConfiguration: %p>\n", self];
[desc appendFormat:@" nickname: %@\n", self.nickname];
[desc appendFormat:@" streamId: %@\n", self.streamId];
[desc appendFormat:@" codec: %@\n", AVVideoCodecString(self.codec)];
[desc appendFormat:@" videoSize: %@\n", NSStringFromCGSize(self.videoSize)];
[desc appendFormat:@" videoFrameRate: %lu\n", (unsigned long)self.videoFrameRate];
[desc appendFormat:@" videoBitRate: %lu kbps\n", (unsigned long)(self.videoBitRate / 1000)];
[desc appendFormat:@" videoMinBitRate: %lu kbps", (unsigned long)(self.videoMinBitRate / 1000)];
return desc;
}
@end