// // 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]; // 生成随机 streamId:stream + 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:@"\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