The application showcases the core WebRTC workflow and the practical use of WebRTC APIs in React.
This tutorial provides a brief introduction to WebRTC and demonstrates how they work in context of this application.
WebRTC (Web Real-Time Communication) is a set of technologies that enable peer-to-peer communication in web browsers and mobile apps. It supports audio, video, and data sharing in real-time.
-
RTCPeerConnection:
- A core WebRTC API for establishing peer-to-peer communication.
- Handles the connection, signaling, and media streaming.
-
MediaStream:
- Represents media (audio or video) from a source like a microphone or webcam.
getUserMedia()is used to access media devices.
-
SDP (Session Description Protocol):
- A format for describing multimedia communication sessions.
- Used in WebRTC for exchanging connection information between peers.
-
Signaling:
- A process for exchanging information (like SDP) to establish a WebRTC connection.
- Typically involves a server to relay messages between peers.
- Media Access: Use
getUserMedia()to access the microphone or camera. - Connection Setup:
- Create an
RTCPeerConnectionobject. - Generate an offer (SDP) and set it as the local description.
- Create an
- Signaling: Exchange SDP between peers via a signaling server to initiate the connection setup.
- Media Streaming: Stream media tracks (audio or video) using the
RTCPeerConnection. Once signaling completes, peers can exchange media data directly. - Connection Close: Properly close connections to release resources and end the session cleanly.
This section demonstrates how the WebRTC concepts are implemented in the OpenAI WebRTC Audio app.
The app accesses the microphone using getUserMedia:
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
audioStreamRef.current = stream;- Purpose: Captures audio input from the user's microphone.
- Integration: Streams the audio into the WebRTC connection.
The app initializes an RTCPeerConnection object and sets up media tracks:
const pc = new RTCPeerConnection();
pc.ontrack = (e) => (audioEl.srcObject = e.streams[0]);
pc.addTrack(stream.getTracks()[0]);- Purpose: Establishes the peer-to-peer connection and streams audio.
- Integration: Adds the microphone audio track to the connection.
The app generates an offer, sets the local description, and exchanges it with the server:
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
const response = await fetch(`${baseUrl}?model=${model}`, {
method: 'POST',
body: offer.sdp,
headers: {
Authorization: `Bearer ${ephemeralToken}`,
'Content-Type': 'application/sdp',
},
});
await pc.setRemoteDescription({
type: 'answer',
sdp: await response.text(),
});- Purpose: Establishes the WebRTC session by exchanging SDP with the server.
The app streams audio tracks using the RTCPeerConnection:
pc.ontrack = (e) => (audioEl.srcObject = e.streams[0]);- Purpose: Streams audio tracks directly to the peer.
- Integration: Ensures the audio is played on the receiving side.
The app manages connection start and stop through user actions:
const handleStartStopClick = () => {
if (isSessionActive) {
stopSession();
} else {
startSession();
}
};- Purpose: Ensures proper resource management and user control.
The app uses the Web Audio API to monitor audio levels:
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const source = audioContext.createMediaStreamSource(stream);
const analyzer = audioContext.createAnalyser();
analyzer.fftSize = 256;
source.connect(analyzer);- Purpose: Detects audio activity levels to provide visual feedback (e.g., toggling the active state of an indicator).
- Start Session:
- Click "Start Session" to initialize the WebRTC connection and stream audio.
- Stop Session:
- Click "Stop Session" to close the connection and release resources.