处理崩溃

This commit is contained in:
2026-01-30 21:38:58 +08:00
parent cc82396195
commit d2f582b7f8
5 changed files with 71 additions and 64 deletions

View File

@@ -32,7 +32,7 @@
} }
- (void)dealloc { - (void)dealloc {
[self cancel]; [self cancelInternal];
} }
#pragma mark - Public Methods #pragma mark - Public Methods

View File

@@ -52,8 +52,9 @@ static NSString *const kDeepgramStreamingManagerErrorDomain =
_audioCapture = [[AudioCaptureManager alloc] init]; _audioCapture = [[AudioCaptureManager alloc] init];
_audioCapture.delegate = self; _audioCapture.delegate = self;
_client = [[DeepgramWebSocketClient alloc] init]; ///
_client.delegate = self; // _client = [[DeepgramWebSocketClient alloc] init];
// _client.delegate = self;
_serverURL = @"wss://api.deepgram.com/v1/listen"; _serverURL = @"wss://api.deepgram.com/v1/listen";
_encoding = @"linear16"; _encoding = @"linear16";
@@ -81,7 +82,7 @@ static NSString *const kDeepgramStreamingManagerErrorDomain =
- (void)dealloc { - (void)dealloc {
[self removeNotifications]; [self removeNotifications];
[self disconnect]; [self disconnectInternal];
} }
- (void)start { - (void)start {
@@ -203,21 +204,25 @@ static NSString *const kDeepgramStreamingManagerErrorDomain =
- (void)disconnect { - (void)disconnect {
dispatch_async(self.stateQueue, ^{ dispatch_async(self.stateQueue, ^{
if (self.streaming) { [self disconnectInternal];
[self.audioCapture stopCapture];
self.streaming = NO;
}
[self.pendingFrames removeAllObjects];
self.pendingStart = NO;
self.keepConnection = NO;
self.shouldReconnectOnForeground = NO;
[self.client disableAudioSending];
[self stopKeepAlive];
[self.client disconnect];
[self.audioSession deactivateSession];
}); });
} }
- (void)disconnectInternal {
if (self.streaming) {
[self.audioCapture stopCapture];
self.streaming = NO;
}
[self.pendingFrames removeAllObjects];
self.pendingStart = NO;
self.keepConnection = NO;
self.shouldReconnectOnForeground = NO;
[self.client disableAudioSending];
[self stopKeepAlive];
[self.client disconnect];
[self.audioSession deactivateSession];
}
#pragma mark - AudioCaptureManagerDelegate #pragma mark - AudioCaptureManagerDelegate
- (void)audioCaptureManagerDidOutputPCMFrame:(NSData *)pcmFrame { - (void)audioCaptureManagerDidOutputPCMFrame:(NSData *)pcmFrame {

View File

@@ -40,7 +40,7 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
} }
- (void)dealloc { - (void)dealloc {
[self disconnect]; [self disconnectInternal];
} }
#pragma mark - Public Methods #pragma mark - Public Methods
@@ -124,14 +124,14 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
- (void)finish { - (void)finish {
NSLog(@"[DeepgramWebSocketClient] Sending CloseStream"); NSLog(@"[DeepgramWebSocketClient] Sending CloseStream");
[self sendJSON:@{ @"type" : @"CloseStream" }]; [self sendJSON:@{@"type" : @"CloseStream"}];
} }
- (void)sendKeepAlive { - (void)sendKeepAlive {
if (!self.connected || !self.webSocketTask) { if (!self.connected || !self.webSocketTask) {
return; return;
} }
[self sendJSON:@{ @"type" : @"KeepAlive" }]; [self sendJSON:@{@"type" : @"KeepAlive"}];
} }
- (void)enableAudioSending { - (void)enableAudioSending {
@@ -165,24 +165,24 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
[self upsertQueryItemWithName:@"model" value:self.model items:items]; [self upsertQueryItemWithName:@"model" value:self.model items:items];
[self upsertQueryItemWithName:@"language" value:self.language items:items]; [self upsertQueryItemWithName:@"language" value:self.language items:items];
[self upsertQueryItemWithName:@"punctuate" [self
value:(self.punctuate ? @"true" : @"false") upsertQueryItemWithName:@"punctuate"
items:items]; value:(self.punctuate ? @"true" : @"false")items:items];
[self upsertQueryItemWithName:@"smart_format" [self upsertQueryItemWithName:@"smart_format"
value:(self.smartFormat ? @"true" : @"false") value:(self.smartFormat ? @"true" : @"false")items
items:items]; :items];
[self upsertQueryItemWithName:@"interim_results" [self upsertQueryItemWithName:@"interim_results"
value:(self.interimResults ? @"true" : @"false") value:(self.interimResults ? @"true" : @"false")items
items:items]; :items];
[self upsertQueryItemWithName:@"encoding" value:self.encoding items:items]; [self upsertQueryItemWithName:@"encoding" value:self.encoding items:items];
[self upsertQueryItemWithName:@"sample_rate" [self upsertQueryItemWithName:@"sample_rate"
value:[NSString stringWithFormat:@"%.0f", value:[NSString
self.sampleRate] stringWithFormat:@"%.0f", self.sampleRate]
items:items]; items:items];
[self upsertQueryItemWithName:@"channels" [self upsertQueryItemWithName:@"channels"
value:[NSString stringWithFormat:@"%d", self.channels] value:[NSString stringWithFormat:@"%d", self.channels]
items:items]; items:items];
components.queryItems = items; components.queryItems = items;
return components.URL; return components.URL;
@@ -220,9 +220,8 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
return; return;
} }
NSString *jsonString = NSString *jsonString = [[NSString alloc] initWithData:jsonData
[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
encoding:NSUTF8StringEncoding];
if (!jsonString) { if (!jsonString) {
[self reportErrorWithMessage:@"Failed to encode JSON message"]; [self reportErrorWithMessage:@"Failed to encode JSON message"];
return; return;
@@ -231,13 +230,12 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
dispatch_async(self.networkQueue, ^{ dispatch_async(self.networkQueue, ^{
NSURLSessionWebSocketMessage *message = NSURLSessionWebSocketMessage *message =
[[NSURLSessionWebSocketMessage alloc] initWithString:jsonString]; [[NSURLSessionWebSocketMessage alloc] initWithString:jsonString];
[self.webSocketTask [self.webSocketTask sendMessage:message
sendMessage:message completionHandler:^(NSError *_Nullable error) {
completionHandler:^(NSError *_Nullable error) { if (error) {
if (error) { [self reportError:error];
[self reportError:error]; }
} }];
}];
}); });
} }
@@ -307,14 +305,15 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
} }
NSArray *alternatives = channel[@"alternatives"]; NSArray *alternatives = channel[@"alternatives"];
if (![alternatives isKindOfClass:[NSArray class]] || alternatives.count == 0) { if (![alternatives isKindOfClass:[NSArray class]] ||
alternatives.count == 0) {
return; return;
} }
NSDictionary *firstAlt = alternatives.firstObject; NSDictionary *firstAlt = alternatives.firstObject;
NSString *transcript = firstAlt[@"transcript"] ?: @""; NSString *transcript = firstAlt[@"transcript"] ?: @"";
BOOL isFinal = [json[@"is_final"] boolValue] || BOOL isFinal =
[json[@"speech_final"] boolValue]; [json[@"is_final"] boolValue] || [json[@"speech_final"] boolValue];
if (transcript.length == 0) { if (transcript.length == 0) {
return; return;
@@ -364,11 +363,10 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
} }
- (void)reportErrorWithMessage:(NSString *)message { - (void)reportErrorWithMessage:(NSString *)message {
NSError *error = [NSError errorWithDomain:kDeepgramWebSocketClientErrorDomain NSError *error =
code:-1 [NSError errorWithDomain:kDeepgramWebSocketClientErrorDomain
userInfo:@{ code:-1
NSLocalizedDescriptionKey : message ?: @"" userInfo:@{NSLocalizedDescriptionKey : message ?: @""}];
}];
[self reportError:error]; [self reportError:error];
} }
@@ -376,8 +374,8 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
self.connected = NO; self.connected = NO;
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
if ([self.delegate respondsToSelector:@selector if ([self.delegate
(deepgramClientDidDisconnect:)]) { respondsToSelector:@selector(deepgramClientDidDisconnect:)]) {
[self.delegate deepgramClientDidDisconnect:error]; [self.delegate deepgramClientDidDisconnect:error];
} }
}); });
@@ -391,7 +389,8 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
self.connected = YES; self.connected = YES;
NSLog(@"[DeepgramWebSocketClient] Connected"); NSLog(@"[DeepgramWebSocketClient] Connected");
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
if ([self.delegate respondsToSelector:@selector(deepgramClientDidConnect)]) { if ([self.delegate
respondsToSelector:@selector(deepgramClientDidConnect)]) {
[self.delegate deepgramClientDidConnect]; [self.delegate deepgramClientDidConnect];
} }
}); });
@@ -404,8 +403,7 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
if (!self.webSocketTask) { if (!self.webSocketTask) {
return; return;
} }
NSLog(@"[DeepgramWebSocketClient] Closed with code: %ld", NSLog(@"[DeepgramWebSocketClient] Closed with code: %ld", (long)closeCode);
(long)closeCode);
[self notifyDisconnect:nil]; [self notifyDisconnect:nil];
[self disconnectInternal]; [self disconnectInternal];
} }

View File

@@ -55,7 +55,7 @@ static NSString *const kVoiceChatStreamingManagerErrorDomain =
} }
- (void)dealloc { - (void)dealloc {
[self disconnect]; [self disconnectInternal];
} }
- (void)setServerURL:(NSString *)serverURL { - (void)setServerURL:(NSString *)serverURL {
@@ -102,17 +102,21 @@ static NSString *const kVoiceChatStreamingManagerErrorDomain =
- (void)disconnect { - (void)disconnect {
dispatch_async(self.stateQueue, ^{ dispatch_async(self.stateQueue, ^{
if (self.streaming) { [self disconnectInternal];
[self.audioCapture stopCapture];
self.streaming = NO;
}
[self.webSocketClient disableAudioSending];
[self.webSocketClient disconnect];
[self.audioSession deactivateSession];
self.sessionId = nil;
}); });
} }
- (void)disconnectInternal {
if (self.streaming) {
[self.audioCapture stopCapture];
self.streaming = NO;
}
[self.webSocketClient disableAudioSending];
[self.webSocketClient disconnect];
[self.audioSession deactivateSession];
self.sessionId = nil;
}
#pragma mark - Private Methods #pragma mark - Private Methods
- (void)startInternal { - (void)startInternal {

View File

@@ -35,7 +35,7 @@ static NSString *const kVoiceChatWebSocketClientErrorDomain =
} }
- (void)dealloc { - (void)dealloc {
[self disconnect]; [self disconnectInternal];
} }
#pragma mark - Public Methods #pragma mark - Public Methods