SPB Git

Repository Public

webrtc_browser.html 2.3 KB 36 lines html

1<!doctype html>2<!--3STATUS: UNVERIFIED (browser-only; not executed by the atlas). Documented flow from4https://developers.openai.com/api/docs/guides/voice-webrtc?api=realtime ("Connecting using an ephemeral token").51. Your server mints an ephemeral key: POST /v1/realtime/client_secrets (see client_secrets.*) and exposes it at /token.62. The browser POSTs its SDP offer to https://api.openai.com/v1/realtime/calls with `Authorization: Bearer ek_…`7   and `Content-Type: application/sdp`; the answer SDP comes back as text. Events flow on the data channel "oai-events".8   The response's `Location` header holds the call_id (rtc_…) for a server sideband: wss://api.openai.com/v1/realtime?call_id=…9-->10<html lang="en"><head><meta charset="utf-8"><title>Realtime WebRTC (ephemeral key)</title></head>11<body>12<button id="start">Start</button> <pre id="log"></pre> <audio id="out" autoplay></audio>13<script type="module">14const log = (s) => (document.getElementById("log").textContent += s + "\n");15document.getElementById("start").onclick = async () => {16  const { value: EPHEMERAL_KEY } = await fetch("/token").then((r) => r.json());   // your server → client_secrets17  const pc = new RTCPeerConnection();18  pc.ontrack = (e) => (document.getElementById("out").srcObject = e.streams[0]);  // model audio19  const mic = await navigator.mediaDevices.getUserMedia({ audio: true });20  pc.addTrack(mic.getTracks()[0]);21  const dc = pc.createDataChannel("oai-events");                                  // JSON events both ways22  dc.addEventListener("message", (e) => { const ev = JSON.parse(e.data); log(ev.type); });23  dc.addEventListener("open", () => dc.send(JSON.stringify({ type: "session.update", session: { type: "realtime", instructions: "Be brief." } })));24  const offer = await pc.createOffer();25  await pc.setLocalDescription(offer);26  const res = await fetch("https://api.openai.com/v1/realtime/calls", {27    method: "POST", body: offer.sdp,28    headers: { Authorization: `Bearer ${EPHEMERAL_KEY}`, "Content-Type": "application/sdp" },29  });30  log("call_id location: " + res.headers.get("Location"));                          // may be hidden by CORS31  await pc.setRemoteDescription({ type: "answer", sdp: await res.text() });32  log("connected — speak (server VAD on by default)");33};34</script>35</body></html>36
Files main
Menu

Theme