Spaces:
Sleeping
Sleeping
Commit
·
cf7649e
1
Parent(s):
fb48a7c
Webrtc to fastrtc
Browse files
ui.py
CHANGED
@@ -95,46 +95,72 @@ def build_ui():
|
|
95 |
|
96 |
// Set up WebRTC connection to Render signaling server
|
97 |
async function setupWebRTC() {
|
98 |
-
|
99 |
-
rtcConnection
|
100 |
-
|
101 |
-
|
102 |
-
// Create new RTCPeerConnection
|
103 |
-
rtcConnection = new RTCPeerConnection();
|
104 |
-
|
105 |
-
// Add audio track to connection
|
106 |
-
mediaStream.getAudioTracks().forEach(track => {
|
107 |
-
rtcConnection.addTrack(track, mediaStream);
|
108 |
-
});
|
109 |
-
|
110 |
-
// Create data channel for signaling
|
111 |
-
const dataChannel = rtcConnection.createDataChannel('audio');
|
112 |
-
|
113 |
-
// Create and set local description
|
114 |
-
const offer = await rtcConnection.createOffer();
|
115 |
-
await rtcConnection.setLocalDescription(offer);
|
116 |
-
|
117 |
-
// Connect to signaling server and exchange SDP
|
118 |
-
const signalingUrl = window.RENDER_SIGNALING_URL;
|
119 |
-
const response = await fetch(signalingUrl, {
|
120 |
-
method: 'POST',
|
121 |
-
headers: { 'Content-Type': 'application/json' },
|
122 |
-
body: JSON.stringify({ sdp: rtcConnection.localDescription })
|
123 |
-
});
|
124 |
-
|
125 |
-
const data = await response.json();
|
126 |
-
await rtcConnection.setRemoteDescription(new RTCSessionDescription(data.sdp));
|
127 |
-
|
128 |
-
// Handle ICE candidates
|
129 |
-
rtcConnection.onicecandidate = event => {
|
130 |
-
if (event.candidate) {
|
131 |
-
fetch(signalingUrl, {
|
132 |
-
method: 'POST',
|
133 |
-
headers: { 'Content-Type': 'application/json' },
|
134 |
-
body: JSON.stringify({ candidate: event.candidate })
|
135 |
-
});
|
136 |
}
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
}
|
139 |
|
140 |
// Set up WebSocket connection to HF Space for conversation updates
|
|
|
95 |
|
96 |
// Set up WebRTC connection to Render signaling server
|
97 |
async function setupWebRTC() {
|
98 |
+
try {
|
99 |
+
if (rtcConnection) {
|
100 |
+
rtcConnection.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
}
|
102 |
+
|
103 |
+
// Use FastRTC's connection approach
|
104 |
+
const pc = new RTCPeerConnection({
|
105 |
+
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
|
106 |
+
});
|
107 |
+
|
108 |
+
// Add audio track
|
109 |
+
mediaStream.getAudioTracks().forEach(track => {
|
110 |
+
pc.addTrack(track, mediaStream);
|
111 |
+
});
|
112 |
+
|
113 |
+
// Connect to FastRTC signaling via WebSocket
|
114 |
+
const signalWs = new WebSocket(window.RENDER_SIGNALING_URL.replace('wss://', 'wss://'));
|
115 |
+
|
116 |
+
// Handle signaling messages
|
117 |
+
signalWs.onmessage = async (event) => {
|
118 |
+
const message = JSON.parse(event.data);
|
119 |
+
|
120 |
+
if (message.type === 'offer') {
|
121 |
+
await pc.setRemoteDescription(new RTCSessionDescription(message));
|
122 |
+
const answer = await pc.createAnswer();
|
123 |
+
await pc.setLocalDescription(answer);
|
124 |
+
signalWs.send(JSON.stringify(pc.localDescription));
|
125 |
+
} else if (message.type === 'candidate') {
|
126 |
+
if (message.candidate) {
|
127 |
+
await pc.addIceCandidate(new RTCIceCandidate(message));
|
128 |
+
}
|
129 |
+
}
|
130 |
+
};
|
131 |
+
|
132 |
+
// Send ICE candidates
|
133 |
+
pc.onicecandidate = (event) => {
|
134 |
+
if (event.candidate) {
|
135 |
+
signalWs.send(JSON.stringify({
|
136 |
+
type: 'candidate',
|
137 |
+
candidate: event.candidate
|
138 |
+
}));
|
139 |
+
}
|
140 |
+
};
|
141 |
+
|
142 |
+
// Keep connection reference
|
143 |
+
rtcConnection = pc;
|
144 |
+
|
145 |
+
// Wait for connection to be established
|
146 |
+
await new Promise((resolve, reject) => {
|
147 |
+
const timeout = setTimeout(() => reject(new Error("WebRTC connection timeout")), 10000);
|
148 |
+
pc.onconnectionstatechange = () => {
|
149 |
+
if (pc.connectionState === 'connected') {
|
150 |
+
clearTimeout(timeout);
|
151 |
+
resolve();
|
152 |
+
} else if (pc.connectionState === 'failed' || pc.connectionState === 'disconnected') {
|
153 |
+
clearTimeout(timeout);
|
154 |
+
reject(new Error("WebRTC connection failed"));
|
155 |
+
}
|
156 |
+
};
|
157 |
+
});
|
158 |
+
|
159 |
+
updateStatus('connected');
|
160 |
+
} catch (err) {
|
161 |
+
console.error('WebRTC setup error:', err);
|
162 |
+
updateStatus('error', 'WebRTC setup failed: ' + err.message);
|
163 |
+
}
|
164 |
}
|
165 |
|
166 |
// Set up WebSocket connection to HF Space for conversation updates
|