Skip to content

Commit de74629

Browse files
committed
Add missing AudioComponentInstanceDispose for OpenAI realtime
1 parent 1973a54 commit de74629

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

Sources/AIProxy/AIProxyLogger.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ internal let aiproxyLogger = Logger(
2626
// https://developer.apple.com/forums/thread/774931
2727
@inline(__always)
2828
internal func logIf(_ logLevel: AIProxyLogLevel) -> Logger? {
29-
logLevel.isAtOrAboveThresholdLevel(aiproxyCallerDesiredLogLevel) ? aiproxyLogger : nil
29+
return logLevel.isAtOrAboveThresholdLevel(aiproxyCallerDesiredLogLevel) ? aiproxyLogger : nil
3030
}

Sources/AIProxy/MicrophonePCMSampleVendor.swift

+1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ open class MicrophonePCMSampleVendor {
245245
if let au = self.audioUnit {
246246
AudioOutputUnitStop(au)
247247
AudioUnitUninitialize(au)
248+
AudioComponentInstanceDispose(au)
248249
self.audioUnit = nil
249250
}
250251
self.audioConverter = nil

Sources/AIProxy/OpenAI/OpenAIRealtimeSession.swift

+10-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ open class OpenAIRealtimeSession {
2929
self.receiveMessage()
3030
}
3131

32+
deinit {
33+
logIf(.debug)?.debug("OpenAIRealtimeSession is being freed")
34+
}
35+
3236
/// Messages sent from OpenAI are published on this receiver as they arrive
3337
public var receiver: AsyncStream<OpenAIRealtimeMessage> {
3438
return AsyncStream { continuation in
@@ -39,7 +43,7 @@ open class OpenAIRealtimeSession {
3943
/// Sends a message through the websocket connection
4044
public func sendMessage(_ encodable: Encodable) async {
4145
guard !self.isTearingDown else {
42-
logIf(.warning)?.warning("Can't send a websocket message, the RT session is tearing down.")
46+
logIf(.debug)?.debug("Ignoring ws sendMessage. The RT session is tearing down.")
4347
return
4448
}
4549
do {
@@ -52,6 +56,7 @@ open class OpenAIRealtimeSession {
5256

5357
/// Close the websocket connection
5458
public func disconnect() {
59+
logIf(.debug)?.debug("Disconnecting from realtime session")
5560
self.isTearingDown = true
5661
self.continuation?.finish()
5762
self.continuation = nil
@@ -76,7 +81,10 @@ open class OpenAIRealtimeSession {
7681

7782
/// Handles socket errors. We disconnect on all errors.
7883
private func didReceiveWebSocketError(_ error: NSError) {
79-
if (error.code == 57) {
84+
guard !isTearingDown else {
85+
return
86+
}
87+
if error.code == 57 {
8088
logIf(.warning)?.warning("WS disconnected. Check that your AIProxy project is websocket enabled and you've followed the DeviceCheck integration guide")
8189
} else {
8290
logIf(.error)?.error("Received ws error: \(error.localizedDescription)")

0 commit comments

Comments
 (0)