Skip to content

Commit 096cb14

Browse files
committed
improve WebRTC Connector
1 parent 3ee1249 commit 096cb14

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/Connectors/WebRTCConnector.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ public final class WebRTCConnector: NSObject, Connector, Sendable {
4343
}
4444
self.connection = connection
4545

46-
guard let dataChannel = self.connection.dataChannel(forLabel: "WebRTCData", configuration: RTCDataChannelConfiguration()) else {
46+
let audioTrackSource = WebRTCConnector.factory.audioSource(with: nil)
47+
let audioTrack = WebRTCConnector.factory.audioTrack(with: audioTrackSource, trackId: "audio0")
48+
let mediaStream = WebRTCConnector.factory.mediaStream(withStreamId: "stream0")
49+
mediaStream.addAudioTrack(audioTrack)
50+
self.connection.add(audioTrack, streamIds: ["stream0"])
51+
52+
guard let dataChannel = self.connection.dataChannel(forLabel: "oai-events", configuration: RTCDataChannelConfiguration()) else {
4753
throw WebRTCError.failedToCreateDataChannel
4854
}
4955
self.dataChannel = dataChannel
@@ -57,13 +63,19 @@ public final class WebRTCConnector: NSObject, Connector, Sendable {
5763

5864
var request = request
5965

60-
let offer = try await self.connection.offer(for: .init(mandatoryConstraints: nil, optionalConstraints: nil))
66+
let offer = try await self.connection.offer(for: RTCMediaConstraints(mandatoryConstraints: nil, optionalConstraints: [
67+
"OfferToReceiveAudio": "true",
68+
"googEchoCancellation": "true",
69+
"googAutoGainControl": "true",
70+
"googNoiseSuppression": "true",
71+
"googHighpassFilter": "true",
72+
]))
6173
try await self.connection.setLocalDescription(offer)
6274

6375
request.httpBody = offer.sdp.data(using: .utf8)
6476

6577
let (data, res) = try await URLSession.shared.data(for: request)
66-
guard (res as? HTTPURLResponse)?.statusCode == 200, let sdp = String(data: data, encoding: .utf8) else {
78+
guard let res = res as? HTTPURLResponse, res.statusCode == 201, let sdp = String(data: data, encoding: .utf8) else {
6779
throw WebRTCError.badServerResponse
6880
}
6981

0 commit comments

Comments
 (0)