A codec is a format for audio or video. The sender uses the codec to make the media smaller before it sends the media. The receiver uses the same codec to play the media. If a receiver does not support the codec, the receiver gets no video or no audio.
All RealtimeKit SDKs, on web and on mobile, use the following codecs by default:
| Media | Default codec |
|---|---|
| Video | VP8 |
| Audio | Opus |
Newer video codecs, such as VP9, AV1, and H.265, need less data for the same quality. But some browsers and devices do not support them. Almost every browser and device supports VP8 and Opus. For this reason, RealtimeKit uses VP8 and Opus by default.
In the Web SDK, you can choose a different codec for the media that a participant sends.
Most apps do not need to change the codec. Change the codec only if you are sure that every browser and device in your meetings supports the new codec.
You can change the codec in cases like these:
- Your app has minimum browser and device requirements. For example, your app works only on recent versions of Chrome and Edge on desktop computers. You tested the codec on these browsers, and it works in all cases.
- You know your users' devices in advance. For example, your customer is a hospital or a call center. All staff use the same approved laptops and the same browser, and you tested the codec on them.
- You provide the devices. For example, your product runs on your own kiosks or meeting room devices, and you know that this hardware supports the codec.
Use the default codecs in cases like these:
- Anyone can join your meetings. For example, your users join from their own phones, tablets, and laptops, with any browser. You cannot know in advance what these devices support.
- You are not sure. If you cannot test the codec on every browser and device that your users have, use the default codecs.
The following table lists the codecs that you can choose in the Web SDK. The MIME type is the name of the codec in the browser:
| Media | SDK value | Codec | Browser MIME type |
|---|---|---|---|
| Video | VP8 |
VP8 | video/VP8 |
| Video | VP9 |
VP9 | video/VP9 |
| Video | H264 |
H.264 | video/H264 |
| Video | H265 |
H.265 | video/H265 |
| Video | AV1 |
AV1 | video/AV1 |
| Audio | OPUS |
Opus | audio/opus |
| Audio | PCMA |
G.711 A-law | audio/PCMA |
| Audio | PCMU |
G.711 μ-law | audio/PCMU |
Not all browsers and devices support all of these codecs. You can use a codec only if every user in the meeting has a browser and a device that support it.
To check which browsers support a codec, refer to the following resources:
- Codecs used by WebRTC ↗︎ on MDN
- Browser support tables on caniuse.com: VP8 and VP9 ↗︎, H.264 ↗︎, H.265 ↗︎, AV1 ↗︎, and Opus ↗︎
The caniuse.com tables show whether a browser can play a codec. A browser that can play a codec cannot always send that codec in a meeting.
Before you initialize the SDK, check which codecs the browser supports. Browsers have two methods for this:
RTCRtpSender.getCapabilities()returns the codecs that the browser can send.RTCRtpReceiver.getCapabilities()returns the codecs that the browser can receive.
Check both. A participant must be able to send the codec that you choose. The participant must also be able to receive the codecs that other participants send.
The following example checks whether a browser can send and receive VP9 video. Use the MIME type from the supported codecs table:
function supportsCodec(kind, mimeType) {
const matches = (codec) =>
codec.mimeType.toLowerCase() === mimeType.toLowerCase();
const canSend = RTCRtpSender.getCapabilities(kind)?.codecs.some(matches);
const canReceive = RTCRtpReceiver.getCapabilities(kind)?.codecs.some(matches);
return Boolean(canSend && canReceive);
}
const canUseVp9 = supportsCodec("video", "video/VP9");This check is for one browser only. You must also make sure that every other participant and the recorder support the codec.
For more information, refer to RTCRtpSender.getCapabilities() ↗︎ on MDN.
To choose a codec, pass mediaProducerConfiguration in the defaults option when you initialize the SDK. You can set a codec for each kind of media that a participant sends:
| Field | Media | Accepted values | Default |
|---|---|---|---|
video |
Camera video | VP8, VP9, H264, H265, AV1 |
VP8 |
audio |
Microphone audio | OPUS, PCMA, PCMU |
OPUS |
screenshareVideo |
Video of a shared screen | VP8, VP9, H264, H265, AV1 |
VP8 |
screenshareAudio |
Audio of a shared screen | OPUS, PCMA, PCMU |
OPUS |
Every field is optional. If you do not set a field, the SDK uses the default codec for that field.
The following example sends camera video and screen share video in VP9:
import { useEffect } from "react";
import { useRealtimeKitClient } from "@cloudflare/realtimekit-react";
export default function App() {
const [meeting, initMeeting] = useRealtimeKitClient();
useEffect(() => {
initMeeting({
authToken: "<auth-token>",
defaults: {
mediaProducerConfiguration: {
video: { codec: "VP9" },
screenshareVideo: { codec: "VP9" },
},
},
});
}, []);
return <div>Your meeting component comes here</div>;
}const meeting = await RealtimeKitClient.init({
authToken: "<auth-token>",
defaults: {
mediaProducerConfiguration: {
video: { codec: "VP9" },
screenshareVideo: { codec: "VP9" },
},
},
});This setting changes only the media that this participant sends. It does not change the media that this participant receives. Each participant decides which codecs to send.
There are two cases. In the first case, the sender does not support the codec. In the second case, a receiver does not support the codec. The SDK reports an error only in the first case.
When you initialize the SDK, it checks whether the browser can send each codec that you chose. Initialization fails with error code 0001 in the following cases:
- The browser cannot send the codec.
- The value is not one of the accepted values.
The error message tells you the field and the codec.
Do not wait for this error. Check the browser before you initialize the SDK, and pass only a codec that the browser supports.
The SDK does not check what other participants support. RealtimeKit also does not convert media from one codec to another codec.
For example, participant A sends VP9 video, but the browser of participant B does not support VP9. Then participant B does not see the video of participant A.
The same is true for recording. The recorder joins the meeting and receives media like a participant. If the recorder does not support a codec, the recording does not contain that media.
Before you change a codec, make sure that all of the following are true:
- Every participant in the meeting supports the codec.
- If you record your meetings, the recorder supports the codec.
- All participants send the same codec for each kind of media. If participants send different codecs, every participant and the recorder must support all of these codecs.
Before you release the change, test a complete meeting on your real devices. Also test a recording.