138 lines
5.0 KiB
Objective-C
138 lines
5.0 KiB
Objective-C
//
|
||
// AVConfigManager.m
|
||
// AVDemo
|
||
//
|
||
|
||
#import "AVConfigManager.h"
|
||
|
||
static NSString * const kAVConfigStreamId = @"kAVConfigStreamId";
|
||
static NSString * const kAVConfigNickname = @"AVConfigNickname";
|
||
static NSString * const kAVConfigCodec = @"AVConfigCodec";
|
||
static NSString * const kAVConfigResolution = @"AVConfigResolution";
|
||
static NSString * const kAVConfigFPS = @"AVConfigFPS";
|
||
static NSString * const kAVConfigMaxBitrate = @"AVConfigMaxBitrate";
|
||
static NSString * const kAVConfigMinBitrate = @"AVConfigMinBitrate";
|
||
static NSString * const kAVConfigCallChannelId = @"AVConfigCallChannelId";
|
||
static NSString * const kAVConfigConferenceChannelId = @"AVConfigConferenceChannelId";
|
||
|
||
NSString * const AVConfigDidChangeNotification = @"AVConfigDidChangeNotification";
|
||
|
||
@implementation AVConfigManager
|
||
|
||
+ (instancetype)sharedManager {
|
||
static AVConfigManager *manager = nil;
|
||
static dispatch_once_t onceToken;
|
||
dispatch_once(&onceToken, ^{
|
||
manager = [[AVConfigManager alloc] init];
|
||
[manager loadConfig];
|
||
});
|
||
return manager;
|
||
}
|
||
|
||
- (instancetype)init {
|
||
if (self = [super init]) {
|
||
_globalConfig = [AVVideoConfiguration defaultConfiguration];
|
||
}
|
||
return self;
|
||
}
|
||
|
||
- (void)loadConfig {
|
||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||
|
||
NSString *nickname = [defaults objectForKey:kAVConfigNickname];
|
||
if (nickname) {
|
||
self.globalConfig.nickname = nickname;
|
||
}
|
||
|
||
if ([defaults objectForKey:kAVConfigCodec]) {
|
||
self.globalConfig.codec = [defaults integerForKey:kAVConfigCodec];
|
||
}
|
||
|
||
if ([defaults objectForKey:kAVConfigResolution]) {
|
||
AVVideoResolution resolution = [defaults integerForKey:kAVConfigResolution];
|
||
[self.globalConfig setResolution:resolution];
|
||
}
|
||
|
||
if ([defaults objectForKey:kAVConfigFPS]) {
|
||
self.globalConfig.videoFrameRate = [defaults integerForKey:kAVConfigFPS];
|
||
self.globalConfig.videoMaxKeyframeInterval = self.globalConfig.videoFrameRate * 2;
|
||
}
|
||
|
||
if ([defaults objectForKey:kAVConfigMaxBitrate]) {
|
||
NSInteger maxBitrate = [defaults integerForKey:kAVConfigMaxBitrate];
|
||
self.globalConfig.videoBitRate = maxBitrate * 1000; // Convert kbps to bps
|
||
}
|
||
|
||
if ([defaults objectForKey:kAVConfigMinBitrate]) {
|
||
NSInteger minBitrate = [defaults integerForKey:kAVConfigMinBitrate];
|
||
self.globalConfig.videoMinBitRate = minBitrate * 1000; // Convert kbps to bps
|
||
}
|
||
|
||
NSString *streamId = [defaults objectForKey:kAVConfigStreamId];
|
||
if (streamId) {
|
||
self.globalConfig.streamId = streamId;
|
||
}
|
||
}
|
||
|
||
- (void)saveConfig {
|
||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||
[defaults setObject:self.globalConfig.streamId forKey:kAVConfigStreamId];
|
||
[defaults setObject:self.globalConfig.nickname forKey:kAVConfigNickname];
|
||
[defaults setInteger:self.globalConfig.codec forKey:kAVConfigCodec];
|
||
[defaults setInteger:[self.globalConfig currentResolution] forKey:kAVConfigResolution];
|
||
[defaults setInteger:self.globalConfig.videoFrameRate forKey:kAVConfigFPS];
|
||
[defaults setInteger:(self.globalConfig.videoBitRate / 1000) forKey:kAVConfigMaxBitrate]; // Convert bps to kbps
|
||
[defaults setInteger:(self.globalConfig.videoMinBitRate / 1000) forKey:kAVConfigMinBitrate]; // Convert bps to kbps
|
||
|
||
[defaults synchronize];
|
||
|
||
[[NSNotificationCenter defaultCenter] postNotificationName:AVConfigDidChangeNotification object:nil];
|
||
}
|
||
|
||
+ (NSString *)generateRandomNickname {
|
||
NSInteger randomNum = arc4random_uniform(1000);
|
||
return [NSString stringWithFormat:@"test%03ld", (long)randomNum];
|
||
}
|
||
|
||
#pragma mark - Channel ID Management
|
||
|
||
- (void)saveCallChannelId:(NSString *)channelId {
|
||
if (channelId && channelId.length > 0) {
|
||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||
[defaults setObject:channelId forKey:kAVConfigCallChannelId];
|
||
[defaults synchronize];
|
||
}
|
||
}
|
||
|
||
- (NSString *)loadCallChannelId {
|
||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||
NSString *channelId = [defaults objectForKey:kAVConfigCallChannelId];
|
||
// 如果没有保存过,返回3位随机数字
|
||
if (!channelId) {
|
||
NSInteger randomNum = arc4random_uniform(900) + 100; // 生成 100-999 的随机数
|
||
channelId = [NSString stringWithFormat:@"%ld", (long)randomNum];
|
||
}
|
||
return channelId;
|
||
}
|
||
|
||
- (void)saveConferenceChannelId:(NSString *)channelId {
|
||
if (channelId && channelId.length > 0) {
|
||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||
[defaults setObject:channelId forKey:kAVConfigConferenceChannelId];
|
||
[defaults synchronize];
|
||
}
|
||
}
|
||
|
||
- (NSString *)loadConferenceChannelId {
|
||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||
NSString *channelId = [defaults objectForKey:kAVConfigConferenceChannelId];
|
||
// 如果没有保存过,返回3位随机数字
|
||
if (!channelId) {
|
||
NSInteger randomNum = arc4random_uniform(900) + 100; // 生成 100-999 的随机数
|
||
channelId = [NSString stringWithFormat:@"%ld", (long)randomNum];
|
||
}
|
||
return channelId;
|
||
}
|
||
|
||
@end
|