Skip to content

Commit 30e222d

Browse files
committed
调整pitch调节时机
Change-Id: I187d9a3d76dbd5249271ba4b73b68f3f5d174608
1 parent 7142917 commit 30e222d

File tree

7 files changed

+349
-12
lines changed

7 files changed

+349
-12
lines changed

Source/LinkSDKDemo/Video/P2P/Controller/TIoTDemoPreviewDeviceVC.m

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#import "TIoTXp2pInfoModel.h"
2626
#import <AVFoundation/AVFoundation.h>
2727
#import "ReachabilityManager.h"
28+
#import "TIoTSessionManager.h"
2829

2930
static CGFloat const kPadding = 16;
3031
static NSString *const kPreviewDeviceCellID = @"kPreviewDeviceCellID";
@@ -107,7 +108,7 @@ - (void)viewDidLoad {
107108
// [TIoTCoreXP2PBridge sharedInstance].logEnable = NO;
108109
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
109110

110-
self.qualityString = quality_standard;
111+
self.qualityString = quality_high;
111112
self.screenRect = [UIApplication sharedApplication].delegate.window.frame;
112113

113114
if (self.isNVR == NO) {
@@ -214,6 +215,8 @@ - (void)dealloc{
214215
[[NSNotificationCenter defaultCenter]removeObserver:self];
215216
if (self.isNVR == NO) {
216217
[[TIoTCoreXP2PBridge sharedInstance] stopService:self.deviceName?:@""];
218+
219+
[[TIoTSessionManager sharedInstance] resetToCachedAudioSession];
217220
}
218221

219222
printf("debugdeinit---%s,%s,%d", __FILE__, __FUNCTION__, __LINE__);
@@ -254,21 +257,28 @@ - (void)getDeviceStatusWithType:(NSString *)singleType qualityType:(NSString *)q
254257
channel = [NSString stringWithFormat:@"channel=%d",channelNum.intValue];
255258
}
256259

257-
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
258-
[[AVAudioSession sharedInstance] setActive:YES error:nil];
259-
260260
// [[TIoTCoreXP2PBridge sharedInstance] sendVoiceToServer:weakSelf.deviceName?:@"" channel:channel];
261+
[[TIoTSessionManager sharedInstance] resumeRTCAudioSession];
262+
263+
static int tt_pitch = -6;
261264
TIoTCoreAudioConfig *audio_config = [TIoTCoreAudioConfig new];
265+
audio_config.refreshSession = YES;
262266
audio_config.sampleRate = TIoTAVCaptionFLVAudio_8;
263267
audio_config.channels = 1;
264268
audio_config.isEchoCancel = YES;
265-
audio_config.pitch = -6; //声音会变粗一点
269+
audio_config.pitch = tt_pitch; // -6声音会变粗一点; 6声音会变细一点
266270

267271
TIoTCoreVideoConfig *video_config = [TIoTCoreVideoConfig new];
268272
video_config.localView = nil;
269273
video_config.videoPosition = AVCaptureDevicePositionFront;
270274

271275
[[TIoTCoreXP2PBridge sharedInstance] sendVoiceToServer:weakSelf.deviceName?:@"" channel:channel audioConfig:audio_config videoConfig:video_config];
276+
277+
if(tt_pitch == 6){
278+
tt_pitch = -6;
279+
}else {
280+
tt_pitch = 6;
281+
}
272282
}
273283

274284
}else {
@@ -654,6 +664,8 @@ - (void)clickTalkback:(UIButton *)button {
654664
}else {
655665
self.talkbackIcon.image = [UIImage imageNamed:@"talkback_unselect"];
656666
[[TIoTCoreXP2PBridge sharedInstance] stopVoiceToServer];
667+
668+
[[TIoTSessionManager sharedInstance] resetToCachedAudioSession];
657669
}
658670

659671
button.selected = !button.selected;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// TIoTCoreASessionManager.h
3+
// TIoTLinkVideo
4+
//
5+
// Created by eagleychen on 2022/11/2.
6+
//
7+
8+
#import <Foundation/Foundation.h>
9+
10+
NS_ASSUME_NONNULL_BEGIN
11+
12+
@interface TIoTSessionManager : NSObject
13+
+ (instancetype)sharedInstance;
14+
//需要录音时,AudioSession的设置代码如下
15+
- (void)resumeRTCAudioSession;
16+
17+
//功能结束时重置audioSession,重置到缓存的audioSession设置
18+
- (void)resetToCachedAudioSession;
19+
20+
- (NSString *)description;
21+
22+
@end
23+
24+
NS_ASSUME_NONNULL_END
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
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

Comments
 (0)