Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fa849f3

Browse files
committed
call working but not ok
1 parent ff5e638 commit fa849f3

File tree

5 files changed

+163
-101
lines changed

5 files changed

+163
-101
lines changed

server/requirements/node_server/App/src/Global/IncomingCall.svelte

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
export let calling: boolean = false;
88
99
let inProgress: boolean = false;
10-
let finished: boolean = false;
1110
let callTime: number = 0;
1211
let intervalCallTime: any = null;
1312
let mediaRecorder: any = null;
14-
let audioChunks: any = [];
1513
1614
onMount(() => {
1715
function inCall()
@@ -65,18 +63,33 @@
6563
{
6664
const ws = globalThis.ws;
6765
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
68-
69-
mediaRecorder = new MediaRecorder(stream, {
70-
mimeType: 'audio/webm;codecs=opus'
71-
});
72-
73-
mediaRecorder.ondataavailable = (e) => {
74-
if (e.data.size > 0)
75-
ws.send(e.data);
66+
67+
const options = {
68+
mimeType: 'audio/webm;codecs=opus',
69+
audioBitsPerSecond: 128000
70+
};
71+
if (!MediaRecorder.isTypeSupported(options.mimeType))
72+
options.mimeType = '';
73+
74+
mediaRecorder = new MediaRecorder(stream, options);
75+
76+
mediaRecorder.ondataavailable = async (e) => {
77+
if (e.data.size > 0) {
78+
try {
79+
const arrayBuffer = await e.data.arrayBuffer();
80+
ws.send(arrayBuffer);
81+
} catch (error) {
82+
console.error("Erreur d'envoi audio:", error);
83+
}
84+
}
85+
};
86+
mediaRecorder.start(50);
87+
mediaRecorder.onerror = (event) => {
88+
console.error("Erreur d'enregistrement:", event);
7689
};
77-
mediaRecorder.start(100);
7890
}
7991
92+
8093
function stopRecording()
8194
{
8295
if (mediaRecorder)

server/requirements/node_server/App/src/websocket/callFunctions.ts

Lines changed: 72 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,83 @@
33
/* ::: :::::::: */
44
/* callFunctions.ts :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
6+
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/03/11 10:01:52 by edbernar #+# #+# */
9-
/* Updated: 2025/03/12 08:52:12 by edbernar ### ########.fr */
9+
/* Updated: 2025/03/12 17:55:23 by edbernar ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

13-
export function callFunctions(data: any)
13+
const audioContext = new AudioContext();
14+
let mediaSource: MediaSource | null = null;
15+
let sourceBuffer: SourceBuffer | null = null;
16+
let audioElement: HTMLAudioElement | null = null;
17+
18+
export async function callFunctions(data: any)
1419
{
15-
if (data.action === "incoming")
16-
{
17-
const incomingCall = new CustomEvent("incomingCall", {detail: {user1: data.content.user1, user2: data.content.user2}});
18-
window.dispatchEvent(incomingCall);
19-
}
20-
else if (data.action === "end")
21-
{
22-
const endCall = new CustomEvent("endCall", {});
23-
window.dispatchEvent(endCall);
24-
}
25-
else if (data.action === "inCall")
26-
{
27-
const inCall = new CustomEvent("inCall", {});
28-
window.dispatchEvent(inCall);
29-
}
30-
else if (data.action === "calling")
31-
{
32-
const calling = new CustomEvent("calling", {detail: {user1: data.content.user2, user2: data.content.user1}});
33-
window.dispatchEvent(calling);
34-
}
35-
else if (data.action === "voiceData")
20+
let event:any = null;
21+
22+
if (data.action === "incoming")
23+
event = new CustomEvent("incomingCall", { detail: { user1: data.content.user1, user2: data.content.user2 } });
24+
else if (data.action === "end")
25+
event = new CustomEvent("endCall", {});
26+
else if (data.action === "inCall")
27+
event = new CustomEvent("inCall", {});
28+
else if (data.action === "calling")
29+
event = new CustomEvent("calling", { detail: { user1: data.content.user2, user2: data.content.user1 } });
30+
else if (data.action === "voiceData")
3631
{
37-
const audioBlob = new Blob([data.data], { type: 'audio/webm' });
38-
const audioUrl = URL.createObjectURL(audioBlob);
39-
new Audio(audioUrl).play();
40-
}
41-
32+
if (!mediaSource) {
33+
await initializeAudioPlayer();
34+
}
35+
await appendAudioData(data.data);
36+
}
37+
if (event !== null)
38+
document.dispatchEvent(event);
39+
}
40+
41+
async function initializeAudioPlayer()
42+
{
43+
audioElement = document.createElement('audio');
44+
document.body.appendChild(audioElement);
45+
46+
mediaSource = new MediaSource();
47+
audioElement.src = URL.createObjectURL(mediaSource);
48+
49+
return new Promise<void>((resolve) => {
50+
mediaSource!.addEventListener('sourceopen', () => {
51+
sourceBuffer = mediaSource!.addSourceBuffer('audio/webm; codecs=opus');
52+
sourceBuffer.mode = 'sequence';
53+
resolve();
54+
});
55+
});
56+
}
57+
58+
async function appendAudioData(arrayBuffer: ArrayBuffer)
59+
{
60+
if (!sourceBuffer || sourceBuffer.updating)
61+
{
62+
await new Promise(resolve => setTimeout(resolve, 50));
63+
return appendAudioData(arrayBuffer);
64+
}
65+
return new Promise<void>((resolve, reject) => {
66+
const onUpdateEnd = () => {
67+
sourceBuffer!.removeEventListener('updateend', onUpdateEnd);
68+
resolve();
69+
};
70+
const onError = (e: Event) => {
71+
sourceBuffer!.removeEventListener('error', onError);
72+
reject(new Error('Erreur SourceBuffer'));
73+
};
74+
75+
sourceBuffer?.addEventListener('updateend', onUpdateEnd);
76+
sourceBuffer?.addEventListener('error', onError);
77+
78+
try {
79+
sourceBuffer?.appendBuffer(arrayBuffer);
80+
audioElement?.play().catch(console.error);
81+
} catch (e) {
82+
reject(e);
83+
}
84+
});
4285
}

server/requirements/node_server/App/src/websocket/ws.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
/* ::: :::::::: */
44
/* ws.ts :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
6+
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/02/18 11:03:32 by edbernar #+# #+# */
9-
/* Updated: 2025/03/11 10:27:33 by edbernar ### ########.fr */
9+
/* Updated: 2025/03/12 16:41:20 by edbernar ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
import { callFunctions } from "./callFunctions.ts";
1414

1515
class Ws
1616
{
17-
private socket: any;
17+
socket: WebSocket;
1818

1919
constructor()
2020
{
2121
this.socket = new WebSocket("/ws");
22+
this.socket.binaryType = "arraybuffer";
2223

2324
this.socket.onopen = function()
2425
{
@@ -31,10 +32,16 @@ class Ws
3132
let data: any = null;
3233

3334
console.log("Message received: " + event.data);
35+
if (event.data instanceof ArrayBuffer)
36+
{
37+
callFunctions({action: "voiceData", data: event.data});
38+
return;
39+
}
3440
try {
3541
data = JSON.parse(event.data);
3642
}
3743
catch (e) {
44+
console.error("Error parsing JSON:", e);
3845
return;
3946
}
4047
if (data.type === "message")

server/requirements/node_server/server/src/Websocket/Websocket.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* Websocket.js :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
6+
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/12/14 23:36:18 by edbernar #+# #+# */
9-
/* Updated: 2025/03/12 09:00:36 by edbernar ### ########.fr */
9+
/* Updated: 2025/03/12 15:00:03 by edbernar ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -32,8 +32,6 @@ class Websocket
3232

3333
ws.on('message', (message) => this.onMessage(message));
3434
ws.on('close', () => this.onClose(ws));
35-
36-
ws.send('Connected to websocket. Welcome !');
3735
}
3836

3937
onClose(ws)
@@ -48,13 +46,19 @@ class Websocket
4846
let json;
4947

5048
// try {
51-
const isBinary = Buffer.isBuffer(message) || message instanceof Uint8Array;
52-
if (isBinary)
49+
if (typeof message === 'string')
50+
json = JSON.parse(message);
51+
else if (message instanceof Buffer)
5352
{
54-
wsCall(users, { action: "voiceData", data: message }, this.id);
55-
return;
53+
const strMessage = message.toString('utf8');
54+
55+
if (strMessage.startsWith('{'))
56+
json = JSON.parse(strMessage);
57+
else {
58+
wsCall(users, { action: "voiceData", data: message }, this.id, this.db);
59+
return;
60+
}
5661
}
57-
json = JSON.parse(message);
5862
if (json.type == 'message')
5963
wsMessage(users, json.content, this.id, json.to, this.db);
6064
else if (json.type == 'message_seen')

0 commit comments

Comments
 (0)