Iframe Embedding - Express Capture 2.0
Iframe Embedding
If you can't load the recorder runtime in your own page, frame Kaltura's hosted loader page instead and talk to it with postMessage. Every public event reaches your page as a message.
If you can load the runtime directly, prefer that: see Getting Started. Both channels are sent from the same place, so they never disagree.
Frame the Loader Page
<iframe
id="recorder"
src="https://unisphere.nvp1.ovp.kaltura.com/v1/static/applications/recorder/recorder-assets/1.0.6/assets/loader/loader.html?parentOrigin=https://your.host"
allow="camera; microphone; display-capture; fullscreen"
style="width: 800px; height: 450px; border: 0"
></iframe>
- The
allowattribute is required. Without it, the browser blocks camera, microphone and screen capture inside the frame. - Replace
https://your.hostwith your page's origin. See Pin the Origin. - The page sizes the recorder to fill the iframe, so size the iframe itself.
Start the Recorder
The loader page sends recorderInit when it is ready. Answer with loadRecorder, carrying the same settings you would pass to the runtime and the region:
const frame = document.getElementById('recorder');
const RECORDER_ORIGIN = new URL(frame.src).origin;
window.addEventListener('message', (event) => {
if (event.origin !== RECORDER_ORIGIN) return;
if (event.source !== frame.contentWindow) return;
if (event.data?.messageType === 'recorderInit') {
frame.contentWindow.postMessage(
{
messageType: 'loadRecorder',
payload: {
env: 'nvp1',
config: {
ks: 'YOUR_KS_TOKEN',
partnerId: 123456,
uiConfId: 7890123,
serviceUrl: 'https://www.kaltura.com',
playerUrl: 'https://cdnapisec.kaltura.com',
},
},
},
RECORDER_ORIGIN
);
}
});
config takes every property described in Settings, including recorderConfig. env is the region: nvp1, frp2 or irp2.
Messages from the Recorder
messageType | payload | Mirrors |
|---|---|---|
recorderInit | none | The loader page is ready for loadRecorder |
recordingStarted | none | onRecordingStarted |
recordingComplete | JSON string: { durationMs } | onRecordingComplete |
useThisSelected | JSON string: { fileName, durationMs, mimeType, size } | onUseThisSelected |
uploadComplete | JSON string: { entries: [{ entryId, source, role }] } | onUploadComplete |
orientationChanged | Plain object: { orientation } | onOrientationChanged |
Object payloads arrive as JSON strings, so call JSON.parse(payload). The one exception is orientationChanged, whose payload is a plain object, read as payload.orientation.
The same rules apply as for the runtime events: uploadComplete is sent once per upload, after every entry has uploaded. It's never sent for a failed or cancelled upload, including one where the screen uploaded but the camera didn't, and never at all under saveMode: 'non_kaltura'. See Upload Results.
In production today, uploadComplete is sent once per entry, with a single entry's { entryId, source }: twice for a screen share with the camera on. The listener below handles both.
Getting the Recording
The recorded media can't go inside a JSON string, so recordingComplete and useThisSelected carry it as separate fields on the message, cameraBlob and screenBlob, next to payload. A stream that wasn't recorded is null.
The blobs are sent only when you pin your origin with ?parentOrigin=. A recording of the user's camera and screen is never broadcast to any page that might frame the recorder. Without parentOrigin, both messages still arrive with their metadata, but the blob fields are absent (not null), so you can tell "not recorded" from "not sent".
Pin the Origin
Always add ?parentOrigin=<your origin> to the iframe URL. With it, the recorder:
- Sends its messages only to your origin, instead of to any parent.
- Accepts
loadRecorderonly from your origin. This matters because that message carries a KS. - Attaches the recorded blobs to
recordingCompleteanduseThisSelected.
A Complete Listener
window.addEventListener('message', (event) => {
// Only this frame, and only the origin it was loaded from.
if (event.origin !== RECORDER_ORIGIN) return;
if (event.source !== frame.contentWindow) return;
const { messageType, payload } = event.data ?? {};
if (messageType === 'orientationChanged') {
const portrait = payload.orientation === 'portrait';
frame.style.width = portrait ? '450px' : '800px';
frame.style.height = portrait ? '800px' : '450px';
}
if (messageType === 'useThisSelected') {
const { fileName, size } = JSON.parse(payload);
const { cameraBlob, screenBlob } = event.data; // present only with ?parentOrigin=
console.log('Received', fileName, size, 'bytes');
}
if (messageType === 'uploadComplete') {
const data = JSON.parse(payload);
const entries = data.entries ?? [data]; // production today sends one entry per message
for (const { entryId, source } of entries) {
console.log('Uploaded', source, 'as entry', entryId);
}
}
});
Check both event.origin and event.source. Checking only the origin still accepts messages from any other frame served by the same origin.
orientationChanged fires only on a change. Size the iframe for the orientation you set in recorderConfig (default landscape) rather than waiting for a message.