Browser permissions and STUN
Before any audio leaves the browser, two things have to succeed. The user must grant the microphone permission, and the browser must learn its own public IP and port through a STUN exchange. Either failure looks like a hung connection from the user side.
Permission and STUN handshake
getUserMedia gates the microphone. STUN gates the network address. Both must succeed before SDP can be exchanged.
static/client.js
javascript
// Ask for the mic up-front so failures show as a clean rejection
// rather than a stuck connection.
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
pc.addTrack(stream.getAudioTracks()[0], stream);
} catch (err) {
status.textContent = 'Microphone permission denied';
return;
}
const pc = new RTCPeerConnection({
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }],
});Request the mic before creating the offer. Configure a public STUN server so the browser can advertise a routable candidate. Failures here are loud and recoverable.
Quiz: Quiz
Loading practice…