Skip to content

Commit 7e8a20c

Browse files
committed
session
1 parent 53c5816 commit 7e8a20c

File tree

5 files changed

+70
-29
lines changed

5 files changed

+70
-29
lines changed

src/app/[sessionId]/page.tsx

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use client";
2+
3+
import Microphone from "@/components/Microphone";
4+
import Stream from "@/components/Stream";
5+
import { useParams } from "next/navigation";
6+
7+
export default function Session() {
8+
const sessionId = useParams().sessionId.toString();
9+
return (
10+
<div className="flex flex-row items-center justify-center h-screen space-y-10">
11+
<div>
12+
<Microphone sessionId={sessionId} />
13+
</div>
14+
<div>
15+
<Stream sessionId={sessionId} />
16+
</div>
17+
</div>
18+
);
19+
}

src/app/page.tsx

+37-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,43 @@
1-
import Microphone from "@/components/Microphone";
1+
"use client";
2+
3+
import { useRouter } from "next/navigation";
24

35
export default function Home() {
6+
const router = useRouter();
7+
8+
const createSession = async () => {
9+
try {
10+
const response = await fetch("http://localhost:4000/start", {
11+
method: "POST",
12+
headers: {
13+
"Content-Type": "application/json",
14+
},
15+
});
16+
17+
if (!response.ok) {
18+
throw new Error("Network response was not ok");
19+
}
20+
21+
const data = await response.json();
22+
const { streamSid, websocketAddress } = data;
23+
24+
// Assuming you want to use websocketAddress somewhere, otherwise you can remove it
25+
console.log("WebSocket Address:", websocketAddress);
26+
27+
router.push(`/${streamSid}`);
28+
} catch (error) {
29+
console.error("Failed to create session:", error);
30+
}
31+
};
32+
433
return (
5-
<div>
6-
<Microphone />
34+
<div className="flex flex-col items-center justify-center h-screen">
35+
<button
36+
onClick={createSession}
37+
className="mt-4 p-2 bg-blue-500 text-white rounded"
38+
>
39+
Create Session
40+
</button>
741
</div>
842
);
943
}

src/app/stream/page.tsx

-9
This file was deleted.

src/components/Microphone.tsx

+8-15
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
import React, { useState, useRef, useCallback } from "react";
44
// import { v4 as uuidv4 } from "uuid";
55

6-
const Microphone = () => {
6+
const Microphone = ({ sessionId }: { sessionId: string }) => {
77
const [isConnected, setIsConnected] = useState(false);
8-
const [streamId, setStreamId] = useState<string | null>(null);
98
const mediaStreamRef = useRef<MediaStream | null>(null);
109
const webSocketRef = useRef<WebSocket | null>(null);
1110
const mediaRecorderRef = useRef<MediaRecorder | null>(null);
@@ -14,11 +13,11 @@ const Microphone = () => {
1413

1514
const sendAudioData = useCallback(
1615
({
17-
streamId,
16+
streamSid,
1817
audioBlob,
1918
mimeType,
2019
}: {
21-
streamId: string;
20+
streamSid: string;
2221
audioBlob: Blob;
2322
mimeType: string;
2423
}) => {
@@ -28,9 +27,10 @@ const Microphone = () => {
2827
const arrayBuffer = reader.result as ArrayBuffer;
2928
const base64String = Buffer.from(arrayBuffer).toString("base64");
3029
const event = {
31-
streamId,
30+
streamSid,
3231
event: "media",
3332
media: {
33+
track: "inbound",
3434
payload: base64String,
3535
mimeType,
3636
},
@@ -76,18 +76,13 @@ const Microphone = () => {
7676

7777
webSocketRef.current = new WebSocket("ws://localhost:4000/connection");
7878

79-
// const newStreamId = uuidv4();
80-
const newStreamId = "new-1";
81-
82-
setStreamId(newStreamId);
83-
8479
mediaRecorderRef.current = new MediaRecorder(stream);
8580

8681
mediaRecorderRef.current.ondataavailable = (event) => {
87-
if (event.data.size > 0 && newStreamId) {
82+
if (event.data.size > 0 && sessionId) {
8883
console.log("Captured audioBlob:", event.data);
8984
sendAudioData({
90-
streamId: newStreamId,
85+
streamSid: sessionId,
9186
audioBlob: event.data,
9287
mimeType: mediaRecorderRef.current?.mimeType || "",
9388
});
@@ -135,17 +130,15 @@ const Microphone = () => {
135130
webSocketRef.current.close();
136131
}
137132
setIsConnected(false);
138-
setStreamId(null);
139133
}, []);
140134

141135
return (
142136
<div>
143137
<p>
144138
<button onClick={isConnected ? stopConnection : startConnection}>
145-
{isConnected ? "Stop Connection" : "Start Connection"}
139+
{isConnected ? "Stop microphone" : "Start microphone"}
146140
</button>
147141
</p>
148-
<p>{streamId}</p>
149142
</div>
150143
);
151144
};

src/components/Stream.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import React, { useEffect, useRef, useState, useCallback } from "react";
44

5-
const Stream = () => {
5+
const Stream = ({ sessionId }: { sessionId: string }) => {
66
const [isConnected, setIsConnected] = useState(false);
77
const audioContextRef = useRef<AudioContext | null>(null);
88
const sourceBufferRef = useRef<SourceBuffer | null>(null);
@@ -17,7 +17,11 @@ const Stream = () => {
1717
webSocketRef.current.onmessage = (event) => {
1818
console.log("event", event);
1919
const data = JSON.parse(event.data);
20-
if (data.event === "media" && data.media.payload) {
20+
if (
21+
data.event === "media" &&
22+
data.media.payload &&
23+
data.streamSid === sessionId
24+
) {
2125
const audioBuffer = Uint8Array.from(atob(data.media.payload), (c) =>
2226
c.charCodeAt(0)
2327
);

0 commit comments

Comments
 (0)