Created
December 18, 2024 01:48
-
-
Save bran921007/3593c5429fc859a990e70b663de1553e to your computer and use it in GitHub Desktop.
The Realtime API with WebRTC - OpenAI api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function createRealtimeSession(inStream, outEl, token) { | |
const pc = new RTCPeerConnection(); | |
pc.ontrack = e => outEl.srcObject = e.streams[0]; | |
pc.addTrack(inStream.getTracks()[0]); | |
const offer = await pc.createOffer(); | |
await pc.setLocalDescription(offer); | |
const headers = { Authorization: `Bearer ${token}`, 'Content-Type': 'application/sdp' }; | |
const opts = { method: 'POST', body: offer.sdp, headers }; | |
const resp = await fetch('https://api.openai.com/v1/realtime', opts); | |
await pc.setRemoteDescription({ type: 'answer', sdp: await resp.text() }); | |
return pc; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment