Skip to content

Commit d30cef0

Browse files
authored
Merge pull request #16 from martian56/develop
Develop --> Main
2 parents 54cc690 + 1193839 commit d30cef0

File tree

4 files changed

+52
-28
lines changed

4 files changed

+52
-28
lines changed

.github/workflows/deploy_front.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jobs:
1010
env:
1111
VITE_API_URL: https://videoo-call.onrender.com
1212
VITE_WS_URL: wss://videoo-call.onrender.com
13+
VITE_METERED_API_KEY: ${{ secrets.VITE_METERED_API_KEY }}
1314
steps:
1415
- name: Checkout repository
1516
uses: actions/checkout@v4
@@ -104,4 +105,4 @@ jobs:
104105
raise SystemExit('❌ Upload failed')
105106
106107
print('✅ Upload succeeded')
107-
PYTHON_SCRIPT
108+
PYTHON_SCRIPT

frontend/.env.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Backend API URLs
2+
VITE_API_URL=http://localhost:8000
3+
VITE_WS_URL=ws://localhost:8000
4+
5+
# Production URLs (uncomment when deploying)
6+
# VITE_API_URL=https://videoo-call.onrender.com
7+
# VITE_WS_URL=wss://videoo-call.onrender.com
8+
9+
# Metered TURN Server API Key
10+
# Get your API key from: https://www.metered.ca/
11+
VITE_METERED_API_KEY=your_metered_api_key_here

frontend/src/config.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,35 @@
22
export const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000';
33
export const WS_BASE_URL = import.meta.env.VITE_WS_URL || 'ws://localhost:8000';
44

5-
// WebRTC Configuration with STUN and TURN servers
5+
// Metered TURN Server API configuration
6+
export const METERED_API_KEY = import.meta.env.VITE_METERED_API_KEY;
7+
export const METERED_API_URL = `https://linkup-ufazien.metered.live/api/v1/turn/credentials?apiKey=${METERED_API_KEY}`;
8+
9+
// Function to fetch TURN server credentials
10+
export const fetchIceServers = async (): Promise<RTCIceServer[]> => {
11+
try {
12+
const response = await fetch(METERED_API_URL);
13+
if (!response.ok) {
14+
throw new Error('Failed to fetch TURN credentials');
15+
}
16+
const iceServers = await response.json();
17+
return iceServers;
18+
} catch (error) {
19+
console.error('Error fetching ICE servers:', error);
20+
// Fallback to Google STUN servers if Metered API fails
21+
return [
22+
{
23+
urls: 'stun:stun.l.google.com:19302'
24+
}
25+
];
26+
}
27+
};
28+
29+
// Default RTC configuration (will be updated with fetched credentials)
630
export const RTC_CONFIG: RTCConfiguration = {
731
iceServers: [
832
{
9-
urls: 'stun:stun.relay.metered.ca:80'
10-
},
11-
{
12-
urls: 'turn:global.relay.metered.ca:80',
13-
username: '81df41330ffff0c3d75e77ed',
14-
credential: 'yn1jtZhMFeVgGmYo'
15-
},
16-
{
17-
urls: 'turn:global.relay.metered.ca:80?transport=tcp',
18-
username: '81df41330ffff0c3d75e77ed',
19-
credential: 'yn1jtZhMFeVgGmYo'
20-
},
21-
{
22-
urls: 'turn:global.relay.metered.ca:443',
23-
username: '81df41330ffff0c3d75e77ed',
24-
credential: 'yn1jtZhMFeVgGmYo'
25-
},
26-
{
27-
urls: 'turns:global.relay.metered.ca:443?transport=tcp',
28-
username: '81df41330ffff0c3d75e77ed',
29-
credential: 'yn1jtZhMFeVgGmYo'
33+
urls: 'stun:stun.l.google.com:19302'
3034
}
3135
]
3236
};

frontend/src/hooks/useWebRTC.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useRef, useCallback, useState } from 'react';
2-
import { RTC_CONFIG } from '../config';
2+
import { fetchIceServers } from '../config';
33

44
export interface PeerConnection {
55
peerConnection: RTCPeerConnection;
@@ -19,6 +19,7 @@ export const useWebRTC = (options?: UseWebRTCOptions) => {
1919
const screenStreamRef = useRef<MediaStream | null>(null);
2020
const originalVideoTrackRef = useRef<MediaStreamTrack | null>(null);
2121
const [isScreenSharing, setIsScreenSharing] = useState(false);
22+
const iceServersRef = useRef<RTCIceServer[] | null>(null);
2223

2324
const initializeLocalStream = useCallback(async (): Promise<boolean> => {
2425
try {
@@ -39,8 +40,15 @@ export const useWebRTC = (options?: UseWebRTCOptions) => {
3940
}
4041
}, []);
4142

42-
const createPeerConnection = useCallback((clientId: string): RTCPeerConnection => {
43-
const pc = new RTCPeerConnection(RTC_CONFIG);
43+
const createPeerConnection = useCallback(async (clientId: string): Promise<RTCPeerConnection> => {
44+
// Fetch ICE servers if not already cached
45+
if (!iceServersRef.current) {
46+
console.log('Fetching TURN server credentials...');
47+
iceServersRef.current = await fetchIceServers();
48+
console.log('ICE servers fetched:', iceServersRef.current);
49+
}
50+
51+
const pc = new RTCPeerConnection({ iceServers: iceServersRef.current });
4452

4553
// Add local stream tracks
4654
// If we're screen sharing, use the screen track, otherwise use camera
@@ -171,7 +179,7 @@ export const useWebRTC = (options?: UseWebRTCOptions) => {
171179
return null;
172180
}
173181

174-
const pc = createPeerConnection(clientId);
182+
const pc = await createPeerConnection(clientId);
175183

176184
// Double-check that tracks were added
177185
if (pc.getSenders().length === 0) {
@@ -219,7 +227,7 @@ export const useWebRTC = (options?: UseWebRTCOptions) => {
219227
let pc = peerConnectionsRef.current.get(clientId);
220228
if (!pc) {
221229
console.log('Creating new peer connection for offer from:', clientId);
222-
pc = createPeerConnection(clientId);
230+
pc = await createPeerConnection(clientId);
223231

224232
if (!pc) {
225233
console.error('Failed to create peer connection for:', clientId);

0 commit comments

Comments
 (0)