|
| 1 | +// |
| 2 | +// TIoTSessionManager.m |
| 3 | +// TIoTLinkVideo |
| 4 | +// |
| 5 | +// Created by eagleychen on 2022/11/2. |
| 6 | +// |
| 7 | + |
| 8 | +#import "TIoTSessionManager.h" |
| 9 | +#import <AVFoundation/AVFoundation.h> |
| 10 | + |
| 11 | +static AVAudioSessionCategory cachedCategory = nil; |
| 12 | +static AVAudioSessionCategoryOptions cachedCategoryOptions = 0; |
| 13 | + |
| 14 | +@interface TIoTSessionManager () |
| 15 | +@property(nonatomic, assign) AVAudioSessionPortOverride portOverride; |
| 16 | +@property (nonatomic,weak) AVAudioSession *session; |
| 17 | +@end |
| 18 | + |
| 19 | +@implementation TIoTSessionManager |
| 20 | + |
| 21 | ++ (instancetype)sharedInstance { |
| 22 | + static dispatch_once_t onceToken; |
| 23 | + static TIoTSessionManager *sharedInstance = nil; |
| 24 | + dispatch_once(&onceToken, ^{ |
| 25 | + sharedInstance = [[self alloc] init]; |
| 26 | + }); |
| 27 | + return sharedInstance; |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +- (instancetype)init { |
| 32 | + if (self = [super init]) { |
| 33 | + |
| 34 | + NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; |
| 35 | + [center addObserver:self |
| 36 | + selector:@selector(handleInterruptionNotification:) |
| 37 | + name:AVAudioSessionInterruptionNotification |
| 38 | + object:nil]; |
| 39 | + [center addObserver:self |
| 40 | + selector:@selector(handleRouteChangeNotification:) |
| 41 | + name:AVAudioSessionRouteChangeNotification |
| 42 | + object:nil]; |
| 43 | + [center addObserver:self |
| 44 | + selector:@selector(handleMediaServicesWereLost:) |
| 45 | + name:AVAudioSessionMediaServicesWereLostNotification |
| 46 | + object:nil]; |
| 47 | + [center addObserver:self |
| 48 | + selector:@selector(handleMediaServicesWereReset:) |
| 49 | + name:AVAudioSessionMediaServicesWereResetNotification |
| 50 | + object:nil]; |
| 51 | + // Posted on the main thread when the primary audio from other applications |
| 52 | + // starts and stops. Foreground applications may use this notification as a |
| 53 | + // hint to enable or disable audio that is secondary. |
| 54 | + [center addObserver:self |
| 55 | + selector:@selector(handleSilenceSecondaryAudioHintNotification:) |
| 56 | + name:AVAudioSessionSilenceSecondaryAudioHintNotification |
| 57 | + object:nil]; |
| 58 | + // Also track foreground event in order to deal with interruption ended situation. |
| 59 | + [center addObserver:self |
| 60 | + selector:@selector(handleApplicationDidBecomeActive:) |
| 61 | + name:UIApplicationDidBecomeActiveNotification |
| 62 | + object:nil]; |
| 63 | + |
| 64 | + NSLog(@"RTC_OBJC_TYPE(RTCAudioSession) (%p): init.", self); |
| 65 | + } |
| 66 | + return self; |
| 67 | +} |
| 68 | + |
| 69 | +//更改audioSession前缓存RTC当下的设置 |
| 70 | +- (void)cacheCurrentAudioSession { |
| 71 | + NSLog(@"cache-->%@",[self description]); |
| 72 | + |
| 73 | + @synchronized (self) { |
| 74 | + cachedCategory = [AVAudioSession sharedInstance].category; |
| 75 | + cachedCategoryOptions = [AVAudioSession sharedInstance].categoryOptions; |
| 76 | + |
| 77 | + NSLog(@"cacheCategory==>%@,cacheOptions==>%d", cachedCategory, cachedCategoryOptions); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +//需要录音时,AudioSession的设置代码如下: |
| 83 | +- (void)resumeRTCAudioSession { |
| 84 | + self.session = [AVAudioSession sharedInstance]; |
| 85 | + if (self.session.category != AVAudioSessionCategoryPlayAndRecord) { |
| 86 | + [self cacheCurrentAudioSession]; //先缓存之前的 |
| 87 | + |
| 88 | + NSLog(@"resumeToCachegory===>%@, categoryOption==>%ld",cachedCategory, cachedCategoryOptions); |
| 89 | +// NSError *error; |
| 90 | + |
| 91 | +// [self.session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]; |
| 92 | +// [self.session setMode:AVAudioSessionModeVideoChat error:nil]; |
| 93 | +// [self.session setActive:YES error:&error]; |
| 94 | + |
| 95 | + AVAudioSessionCategoryOptions categoryOptions = AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionMixWithOthers; |
| 96 | + if (@available(iOS 10.0, *)) { |
| 97 | + categoryOptions |= AVAudioSessionCategoryOptionAllowBluetooth; |
| 98 | + } |
| 99 | + [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:categoryOptions error:nil]; |
| 100 | + [[AVAudioSession sharedInstance] setActive:YES error:nil]; |
| 101 | + } |
| 102 | +} |
| 103 | +//功能结束时重置audioSession,重置到缓存的audioSession设置 |
| 104 | +- (void)resetToCachedAudioSession { |
| 105 | + if (!cachedCategory) { |
| 106 | + return; |
| 107 | + } |
| 108 | + BOOL needResetAudioSession = ![[AVAudioSession sharedInstance].category isEqualToString:cachedCategory] || [AVAudioSession sharedInstance].categoryOptions != cachedCategoryOptions; |
| 109 | + NSLog(@"resetToCachegory===>%@, categoryOption==>%ld, needrest==%d",cachedCategory, cachedCategoryOptions, needResetAudioSession); |
| 110 | + if (needResetAudioSession) { |
| 111 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 112 | +// dispatch_async(dispatch_get_global_queue(0, 0), ^{ |
| 113 | + [[AVAudioSession sharedInstance] setCategory:cachedCategory withOptions:cachedCategoryOptions error:nil]; |
| 114 | + [[AVAudioSession sharedInstance] setActive:YES error:nil]; |
| 115 | + @synchronized (self) { |
| 116 | + cachedCategory = nil; |
| 117 | + cachedCategoryOptions = 0; |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +- (NSString *)description { |
| 124 | + NSString *format = @"RTC_OBJC_TYPE(RTCAudioSession): {\n" |
| 125 | + " category: %@\n" |
| 126 | + " categoryOptions: %ld\n" |
| 127 | + " mode: %@\n" |
| 128 | + " isActive: %d\n" |
| 129 | + " sampleRate: %.2f\n" |
| 130 | + " IOBufferDuration: %f\n" |
| 131 | + " outputNumberOfChannels: %ld\n" |
| 132 | + " inputNumberOfChannels: %ld\n" |
| 133 | + " outputLatency: %f\n" |
| 134 | + " inputLatency: %f\n" |
| 135 | + " outputVolume: %f\n" |
| 136 | + "}"; |
| 137 | + |
| 138 | + AVAudioSession *session = [AVAudioSession sharedInstance]; |
| 139 | + AVAudioSessionCategory category = session.category; |
| 140 | + AVAudioSessionCategoryOptions categoryOptions = session.categoryOptions; |
| 141 | + AVAudioSessionMode mode = session.mode; |
| 142 | + BOOL isActive = YES; |
| 143 | + double sampleRate = session.sampleRate; |
| 144 | + double preferredSampleRate = session.preferredSampleRate; |
| 145 | + NSTimeInterval IOBufferDuration = session.IOBufferDuration; |
| 146 | + NSInteger outputNumberOfChannels = session.outputNumberOfChannels; |
| 147 | + NSInteger inputNumberOfChannels = session.inputNumberOfChannels; |
| 148 | + NSTimeInterval outputLatency = session.outputLatency; |
| 149 | + NSTimeInterval inputLatency = session.inputLatency; |
| 150 | + float outputVolume = session.outputVolume; |
| 151 | + AVAudioSessionRouteDescription *currentRoute = session.currentRoute; |
| 152 | + NSLog(@"currentRout===>%@",currentRoute); |
| 153 | + NSString *description = [NSString stringWithFormat:format, |
| 154 | + category, (long)categoryOptions, mode, |
| 155 | + isActive, sampleRate, IOBufferDuration, |
| 156 | + outputNumberOfChannels, inputNumberOfChannels, |
| 157 | + outputLatency, inputLatency, outputVolume]; |
| 158 | + |
| 159 | + return description; |
| 160 | +} |
| 161 | + |
| 162 | + |
| 163 | +#pragma mark - Notifications |
| 164 | + |
| 165 | +- (void)handleInterruptionNotification:(NSNotification *)notification { |
| 166 | + NSNumber* typeNumber = |
| 167 | + notification.userInfo[AVAudioSessionInterruptionTypeKey]; |
| 168 | + AVAudioSessionInterruptionType type = |
| 169 | + (AVAudioSessionInterruptionType)typeNumber.unsignedIntegerValue; |
| 170 | + switch (type) { |
| 171 | + case AVAudioSessionInterruptionTypeBegan: |
| 172 | + NSLog(@"Audio session interruption began."); |
| 173 | + // self.isActive = NO; |
| 174 | + // self.isInterrupted = YES; |
| 175 | + // [self notifyDidBeginInterruption]; |
| 176 | + break; |
| 177 | + case AVAudioSessionInterruptionTypeEnded: { |
| 178 | + NSLog(@"Audio session interruption ended."); |
| 179 | + // self.isInterrupted = NO; |
| 180 | + // [self updateAudioSessionAfterEvent]; |
| 181 | + NSNumber *optionsNumber = notification.userInfo[AVAudioSessionInterruptionOptionKey]; |
| 182 | + AVAudioSessionInterruptionOptions options = optionsNumber.unsignedIntegerValue; |
| 183 | + BOOL shouldResume = options & AVAudioSessionInterruptionOptionShouldResume; |
| 184 | + // [self notifyDidEndInterruptionWithShouldResumeSession:shouldResume]; |
| 185 | + break; |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +- (void)handleRouteChangeNotification:(NSNotification *)notification { |
| 191 | + // Get reason for current route change. |
| 192 | + NSNumber* reasonNumber = |
| 193 | + notification.userInfo[AVAudioSessionRouteChangeReasonKey]; |
| 194 | + AVAudioSessionRouteChangeReason reason = |
| 195 | + (AVAudioSessionRouteChangeReason)reasonNumber.unsignedIntegerValue; |
| 196 | + NSLog(@"Audio route changed:"); |
| 197 | + switch (reason) { |
| 198 | + case AVAudioSessionRouteChangeReasonUnknown: |
| 199 | + NSLog(@"Audio route changed: ReasonUnknown"); |
| 200 | + break; |
| 201 | + case AVAudioSessionRouteChangeReasonNewDeviceAvailable: |
| 202 | + NSLog(@"Audio route changed: NewDeviceAvailable"); |
| 203 | + break; |
| 204 | + case AVAudioSessionRouteChangeReasonOldDeviceUnavailable: |
| 205 | + NSLog(@"Audio route changed: OldDeviceUnavailable"); |
| 206 | + break; |
| 207 | + case AVAudioSessionRouteChangeReasonCategoryChange: |
| 208 | + NSLog(@"Audio route changed: CategoryChange to :%@", |
| 209 | + [AVAudioSession sharedInstance].category); |
| 210 | + break; |
| 211 | + case AVAudioSessionRouteChangeReasonOverride: |
| 212 | + NSLog(@"Audio route changed: Override"); |
| 213 | + break; |
| 214 | + case AVAudioSessionRouteChangeReasonWakeFromSleep: |
| 215 | + NSLog(@"Audio route changed: WakeFromSleep"); |
| 216 | + break; |
| 217 | + case AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory: |
| 218 | + NSLog(@"Audio route changed: NoSuitableRouteForCategory"); |
| 219 | + break; |
| 220 | + case AVAudioSessionRouteChangeReasonRouteConfigurationChange: |
| 221 | + NSLog(@"Audio route changed: RouteConfigurationChange"); |
| 222 | + break; |
| 223 | + } |
| 224 | + AVAudioSessionRouteDescription* previousRoute = |
| 225 | + notification.userInfo[AVAudioSessionRouteChangePreviousRouteKey]; |
| 226 | + // Log previous route configuration. |
| 227 | + NSLog(@"Previous route: %@\nCurrent route:%@", |
| 228 | + previousRoute, [AVAudioSession sharedInstance].currentRoute); |
| 229 | + |
| 230 | +// [self.session setActive:YES error:nil]; |
| 231 | + [[NSNotificationCenter defaultCenter] postNotificationName:@"testAudioSession" object:nil]; |
| 232 | +} |
| 233 | + |
| 234 | +- (void)handleMediaServicesWereLost:(NSNotification *)notification { |
| 235 | + NSLog(@"Media services were lost."); |
| 236 | +// [self updateAudioSessionAfterEvent]; |
| 237 | +// [self notifyMediaServicesWereLost]; |
| 238 | +} |
| 239 | + |
| 240 | +- (void)handleMediaServicesWereReset:(NSNotification *)notification { |
| 241 | + NSLog(@"Media services were reset."); |
| 242 | +// [self updateAudioSessionAfterEvent]; |
| 243 | +// [self notifyMediaServicesWereReset]; |
| 244 | +} |
| 245 | + |
| 246 | +- (void)handleSilenceSecondaryAudioHintNotification:(NSNotification *)notification { |
| 247 | + // TODO(henrika): just adding logs here for now until we know if we are ever |
| 248 | + // see this notification and might be affected by it or if further actions |
| 249 | + // are required. |
| 250 | + NSNumber *typeNumber = |
| 251 | + notification.userInfo[AVAudioSessionSilenceSecondaryAudioHintTypeKey]; |
| 252 | + AVAudioSessionSilenceSecondaryAudioHintType type = |
| 253 | + (AVAudioSessionSilenceSecondaryAudioHintType)typeNumber.unsignedIntegerValue; |
| 254 | + switch (type) { |
| 255 | + case AVAudioSessionSilenceSecondaryAudioHintTypeBegin: |
| 256 | + NSLog(@"Another application's primary audio has started."); |
| 257 | + break; |
| 258 | + case AVAudioSessionSilenceSecondaryAudioHintTypeEnd: |
| 259 | + NSLog(@"Another application's primary audio has stopped."); |
| 260 | + break; |
| 261 | + } |
| 262 | +} |
| 263 | + |
| 264 | +- (void)handleApplicationDidBecomeActive:(NSNotification *)notification { |
| 265 | + BOOL isInterrupted = YES; //self.isInterrupted; |
| 266 | + NSLog(@"Application became active after an interruption. Treating as interruption " |
| 267 | + "end. isInterrupted changed from %d to 0.", |
| 268 | + isInterrupted); |
| 269 | +// if (isInterrupted) { |
| 270 | +// self.isInterrupted = NO; |
| 271 | +// [self updateAudioSessionAfterEvent]; |
| 272 | +// } |
| 273 | + // Always treat application becoming active as an interruption end event. |
| 274 | +// [self notifyDidEndInterruptionWithShouldResumeSession:YES]; |
| 275 | +} |
| 276 | +@end |
0 commit comments