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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Video Call</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/webrtc-adapter/8.1.2/adapter.min.js"></script>
</head>
<body>
    <div class="container">
        <h1>Video Call</h1>
        <div class="video-grid">
            <div>
                <h2>Local Video</h2>
                <video id="localVideo" autoplay muted playsinline></video>
            </div>
            <div>
                <h2>Remote Video</h2>
                <video id="remoteVideo" autoplay playsinline></video>
            </div>
        </div>
        <div class="controls">
            <button id="startButton">Start Call</button>
            <input id="sessionInput" placeholder="Enter session ID to join">
            <button id="joinButton">Join Call</button>
            <button id="endButton">End Call</button>
            <div>
                <label for="sessionId">Session ID:</label>
                <input id="sessionId" readonly>
                <button id="copyButton">Copy</button>
            </div>
        </div>
    </div>

    <script>
        const APP_ID = "d8f5af0fe38ea134ebd03e7bf0a8ae14"; // Thay thế bằng APP_ID của bạn
        const APP_TOKEN = "3d9031d1c83112f68636517a1b4c765161bf0b7667fe9a665b528f760b1f4f28"; // Thay thế bằng APP_TOKEN của bạn
        const API_BASE = `https://rtc.live.cloudflare.com/v1/apps/${APP_ID}`;

        let localStream;
        let localPeerConnection;
        let localSessionId;
        let remoteSessionId = null; // Thêm biến để lưu session id của người tham gia

        const localVideo = document.getElementById('localVideo');
        const remoteVideo = document.getElementById('remoteVideo');
        const startButton = document.getElementById('startButton');
        const endButton = document.getElementById('endButton');
        const sessionInput = document.getElementById('sessionInput');
        const joinButton = document.getElementById('joinButton');
        const sessionIdInput = document.getElementById('sessionId');
        const copyButton = document.getElementById('copyButton');

        startButton.onclick = startCall;
        endButton.onclick = endCall;
        joinButton.onclick = joinCall;
        copyButton.onclick = () => {
            sessionIdInput.select();
            document.execCommand('copy');
            alert('Session ID copied to clipboard');
        };

        async function createSession() {
            const response = await fetch(`${API_BASE}/sessions/new`, {
                method: "POST",
                headers: {
                    "Authorization": `Bearer ${APP_TOKEN}`,
                    "Content-Type": "application/json"
                }
            }).then(res => res.json());

            if (response.errorCode) {
                throw new Error(response.errorDescription);
            }
            return response.sessionId;
        }

        async function createPeerConnection() {
            const peerConnection = new RTCPeerConnection({
                iceServers: [{
                    urls: "stun:stun.cloudflare.com:3478"
                }],
                bundlePolicy: "max-bundle"
            });

            // Add ICE candidate handling
            peerConnection.onicecandidate = (event) => {
                if (event.candidate) {
                    console.log("New ICE candidate:", event.candidate);
                }
            };

            // Add connection state handling
            peerConnection.onconnectionstatechange = (event) => {
                console.log("Connection state:", peerConnection.connectionState);
            };

            // Add track handling
            peerConnection.ontrack = (event) => {
                console.log("Received remote track:", event);
                if (remoteVideo.srcObject !== event.streams[0]) {
                    remoteVideo.srcObject = event.streams[0];
                }
            };

            return peerConnection;
        }

        async function startCall() {
            try {
                // Get local media stream
                localStream = await navigator.mediaDevices.getUserMedia({
                    audio: true,
                    video: true
                });
                localVideo.srcObject = localStream;

                // Create new session
                localSessionId = await createSession();
                localPeerConnection = await createPeerConnection();

                // Add tracks using addTransceiver API like in demo.html
                const transceivers = localStream.getTracks().map(track => 
                    localPeerConnection.addTransceiver(track, {
                        direction: "sendonly"
                    })
                );

                // Create and set local offer
                const offer = await localPeerConnection.createOffer();
                await localPeerConnection.setLocalDescription(offer);

                // Format request according to API schema
                const requestBody = {
                    sessionDescription: {
                        sdp: offer.sdp,
                        type: "offer"
                    },
                    tracks: transceivers.map(({mid, sender}) => ({
                        location: "local",
                        mid: mid,
                        trackName: sender.track?.id
                    }))
                };

                // Set up ICE connection state handler
                const connected = new Promise((resolve, reject) => {
                    setTimeout(() => reject(new Error("ICE connection timeout")), 5000);
                    
                    const iceConnectionStateChangeHandler = () => {
                        if (localPeerConnection.iceConnectionState === "connected") {
                            localPeerConnection.removeEventListener(
                                "iceconnectionstatechange",
                                iceConnectionStateChangeHandler
                            );
                            resolve();
                        }
                    };
                    
                    localPeerConnection.addEventListener(
                        "iceconnectionstatechange",
                        iceConnectionStateChangeHandler
                    );
                });

                // Send tracks to API
                const response = await fetch(`${API_BASE}/sessions/${localSessionId}/tracks/new`, {
                    method: "POST", 
                    headers: {
                        "Authorization": `Bearer ${APP_TOKEN}`,
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify(requestBody)
                });

                if (!response.ok) {
                    throw new Error(`API error: ${response.status}`);
                }

                const responseData = await response.json();

                // Set remote description from response
                await localPeerConnection.setRemoteDescription(
                    new RTCSessionDescription(responseData.sessionDescription)
                );

                // Wait for ICE connection
                await connected;

                // Update UI
                sessionIdInput.value = localSessionId;
                startSessionCheck(localSessionId);

            } catch (err) {
                console.error("Error starting call:", err);
                alert("Error starting call: " + err.message);
            }
        }

        async function joinCall() {
            try {
                const sessionToJoin = sessionInput.value.trim();
                if (!sessionToJoin) {
                    alert('Please enter a session ID');
                    return;
                }

                // Get session state first
                const sessionState = await fetch(`${API_BASE}/sessions/${sessionToJoin}`, {
                    headers: {
                        "Authorization": `Bearer ${APP_TOKEN}`
                    }
                }).then(res => res.json());

                console.log("Session state:", sessionState);

                if (!sessionState.tracks || sessionState.tracks.length === 0) {
                    throw new Error("No active tracks in session");
                }

                // Initialize local resources
                localStream = await navigator.mediaDevices.getUserMedia({
                    audio: true,
                    video: true
                });
                localVideo.srcObject = localStream;
                localPeerConnection = await createPeerConnection();
                localSessionId = await createSession();

                // First add our local tracks
                const localTransceivers = localStream.getTracks().map(track =>
                    localPeerConnection.addTransceiver(track, {
                        direction: "sendonly"
                    })
                );

                // Create and set local offer for our tracks
                const localOffer = await localPeerConnection.createOffer();
                await localPeerConnection.setLocalDescription(localOffer);

                // Send our tracks first
                const addLocalTracksResponse = await fetch(`${API_BASE}/sessions/${localSessionId}/tracks/new`, {
                    method: "POST",
                    headers: {
                        "Authorization": `Bearer ${APP_TOKEN}`,
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify({
                        sessionDescription: {
                            sdp: localOffer.sdp,
                            type: "offer"
                        },
                        tracks: localTransceivers.map(({mid, sender}) => ({
                            location: "local",
                            mid: mid,
                            trackName: sender.track?.id
                        }))
                    })
                }).then(res => res.json());

                // Handle local tracks response
                await localPeerConnection.setRemoteDescription(
                    new RTCSessionDescription(addLocalTracksResponse.sessionDescription)
                );

                // Now pull the remote tracks
                const pullRequest = {
                    tracks: sessionState.tracks
                        .filter(track => track.status === 'active')
                        .map(track => ({
                            location: "remote",
                            sessionId: sessionToJoin,
                            trackName: track.trackName
                        }))
                };

                // Set up track handling for remote tracks
                const resolvingTracks = new Promise((resolve, reject) => {
                    let receivedTracks = [];
                    const timeout = setTimeout(() => reject(new Error("Track timeout")), 10000);

                    const handleTrack = (event) => {
                        console.log("Received track:", event.track);
                        receivedTracks.push(event.track);
                        
                        if (receivedTracks.length === pullRequest.tracks.length) {
                            clearTimeout(timeout);
                            localPeerConnection.removeEventListener('track', handleTrack);
                            resolve(receivedTracks);
                        }
                    };

                    localPeerConnection.addEventListener('track', handleTrack);
                });

                // Pull remote tracks
                const pullResponse = await fetch(`${API_BASE}/sessions/${localSessionId}/tracks/new`, {
                    method: "POST",
                    headers: {
                        "Authorization": `Bearer ${APP_TOKEN}`,
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify(pullRequest)
                }).then(res => res.json());

                // Handle remote tracks
                if (pullResponse.requiresImmediateRenegotiation) {
                    await localPeerConnection.setRemoteDescription(
                        new RTCSessionDescription(pullResponse.sessionDescription)
                    );

                    const answer = await localPeerConnection.createAnswer();
                    await localPeerConnection.setLocalDescription(answer);

                    await fetch(`${API_BASE}/sessions/${localSessionId}/renegotiate`, {
                        method: "PUT",
                        headers: {
                            "Authorization": `Bearer ${APP_TOKEN}`,
                            "Content-Type": "application/json"
                        },
                        body: JSON.stringify({
                            sessionDescription: {
                                sdp: answer.sdp,
                                type: "answer"
                            }
                        })
                    }).then(res => res.json());
                }

                // Wait for and display remote tracks
                const pulledTracks = await resolvingTracks;
                const remoteStream = new MediaStream();
                pulledTracks.forEach(track => remoteStream.addTrack(track));
                remoteVideo.srcObject = remoteStream;

                startSessionCheck(localSessionId);
                startSessionCheck(sessionToJoin);

            } catch (err) {
                console.error("Error joining call:", err);
                alert("Error joining call: " + err.message);
            }
        }

        async function endCall() {
            try {
                if (localSessionId) {
                    // Close tracks following schema
                    await fetch(`${API_BASE}/sessions/${localSessionId}/tracks/close`, {
                        method: "PUT",
                        headers: {
                            "Authorization": `Bearer ${APP_TOKEN}`,
                            "Content-Type": "application/json"
                        },
                        body: JSON.stringify({
                            tracks: [], // Close all tracks
                            force: true // Force close without renegotiation
                        })
                    });
                }
                
                // Clean up resources
                if (localStream) {
                    localStream.getTracks().forEach(track => track.stop());
                }
                if (localPeerConnection) {
                    localPeerConnection.close();
                }
                localVideo.srcObject = null;
                remoteVideo.srcObject = null;
                sessionIdInput.value = '';
                sessionInput.value = '';
                
            } catch (err) {
                console.error("Error ending call:", err);
            }
        }

        // Sửa lại hàm startSessionCheck để thêm xử lý khi có người tham gia
        function startSessionCheck(sessionId) {
            const checkInterval = setInterval(async () => {
                try {
                    const response = await fetch(`${API_BASE}/sessions/${sessionId}`, {
                        headers: {
                            "Authorization": `Bearer ${APP_TOKEN}`
                        }
                    }).then(res => res.json());

                    if (response.errorCode) {
                        clearInterval(checkInterval);
                        console.log(`Session ${sessionId} ended`);
                        return;
                    }

                    // Kiểm tra nếu là session gốc và có tracks mới
                    if (sessionId === localSessionId && response.tracks) {
                        const newTracks = response.tracks.filter(track => 
                            track.status === 'active' && 
                            track.sessionId !== localSessionId &&
                            track.sessionId !== remoteSessionId
                        );

                        if (newTracks.length > 0) {
                            console.log("New participant joined, pulling their tracks");
                            remoteSessionId = newTracks[0].sessionId;
                            await pullRemoteTracks(newTracks);
                        }
                    }
                } catch (err) {
                    clearInterval(checkInterval);
                    console.error("Session check failed:", err);
                }
            }, 5000);
        }

        // Thêm hàm mới để pull tracks từ người tham gia
        async function pullRemoteTracks(tracks) {
            try {
                // Set up track handling
                const resolvingTracks = new Promise((resolve, reject) => {
                    let receivedTracks = [];
                    const timeout = setTimeout(() => reject(new Error("Track timeout")), 10000);

                    const handleTrack = (event) => {
                        console.log("Received remote participant track:", event.track);
                        receivedTracks.push(event.track);
                        
                        if (receivedTracks.length === tracks.length) {
                            clearTimeout(timeout);
                            localPeerConnection.removeEventListener('track', handleTrack);
                            resolve(receivedTracks);
                        }
                    };

                    localPeerConnection.addEventListener('track', handleTrack);
                });

                // Pull remote tracks
                const pullResponse = await fetch(`${API_BASE}/sessions/${localSessionId}/tracks/new`, {
                    method: "POST",
                    headers: {
                        "Authorization": `Bearer ${APP_TOKEN}`,
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify({
                        tracks: tracks.map(track => ({
                            location: "remote",
                            sessionId: track.sessionId,
                            trackName: track.trackName
                        }))
                    })
                }).then(res => res.json());

                if (pullResponse.requiresImmediateRenegotiation) {
                    await localPeerConnection.setRemoteDescription(
                        new RTCSessionDescription(pullResponse.sessionDescription)
                    );

                    const answer = await localPeerConnection.createAnswer();
                    await localPeerConnection.setLocalDescription(answer);

                    await fetch(`${API_BASE}/sessions/${localSessionId}/renegotiate`, {
                        method: "PUT",
                        headers: {
                            "Authorization": `Bearer ${APP_TOKEN}`,
                            "Content-Type": "application/json"
                        },
                        body: JSON.stringify({
                            sessionDescription: {
                                sdp: answer.sdp,
                                type: "answer"
                            }
                        })
                    }).then(res => res.json());
                }

                // Wait for and display remote tracks
                const pulledTracks = await resolvingTracks;
                const remoteStream = remoteVideo.srcObject instanceof MediaStream ? 
                    remoteVideo.srcObject : new MediaStream();
                pulledTracks.forEach(track => remoteStream.addTrack(track));
                remoteVideo.srcObject = remoteStream;

            } catch (err) {
                console.error("Error pulling remote tracks:", err);
            }
        }
    </script>

    <style>
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        .video-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin: 20px 0;
        }
        video {
            width: 100%;
            background: #333;
        }
        .controls {
            text-align: center;
        }
        button {
            padding: 10px 20px;
            margin: 0 10px;
        }
        .controls div {
            margin-top: 10px;
        }
        #sessionId {
            width: 200px;
            margin-right: 10px;
        }
    </style>
</body>
</html>