Recorder SDK and shared core
The recorder SDK and shared recorder core provide common capture services for Cap and for outside products that embed recording into a web experience. The SDK turns browser media access into a small recording session API. The core shares capture, format choice, upload, progress, and recovery contracts across the web recorder, comment capture, and browser extension.
Recorder SDK
The recorder SDK is the public entry point for a third-party web app that needs to create a Cap recording. packages/…/index.ts exposes CapRecorder, while packages/…/useCapRecorder.ts maps the same session into React state.
Recording session
CapRecorder owns one browser recording session from permission request through upload. A host supplies a public key and may select the capture mode, camera, microphone, system audio, user identity, API base, and maximum duration through packages/…/types.ts.
The session reports a phase, elapsed duration, and final video links. Hosts can subscribe to phase, duration, error, and completion events, or use the React hook to receive state and start, pause, resume, and stop actions.
| Session result | Meaning |
|---|---|
videoId | Stable identifier for the created Cap video |
shareUrl | URL that a viewer can open |
embedUrl | URL that a host can place in an embedded player |
The recording path asks the browser for display, camera, and microphone streams. It combines the available tracks into one recording stream, records in a browser-supported format, and sends the finished data to the Cap SDK API. A display track ending stops the session because the user has ended screen sharing. packages/…/index.ts• L112-169 and packages/…/stream-manager.ts• L16-105.
The SDK uses a simple state model so the host can keep its controls in step with the recorder:
stateDiagram-v2
[*] --> idle
idle --> requesting_permission: start
requesting_permission --> recording: streams ready
requesting_permission --> error: permission or capture failure
recording --> paused: pause
paused --> recording: resume
recording --> stopping: stop or display ends
paused --> stopping: stop
stopping --> uploading: media ready
uploading --> complete: video uploaded
uploading --> error: upload failure
complete --> [*]
error --> [*]
The React binding preserves this lifecycle while handling object creation, event cleanup, and destruction when its component leaves the page. This lets a product use the recorder without managing the recorder instance directly. packages/…/useCapRecorder.ts• L5-89.
References: packages/…/index.ts• L29-289 packages/…/types.ts• L1-36 packages/…/useCapRecorder.ts• L5-89
Capture modes
The SDK supports three display choices and a camera-only choice. Display modes use the browser's screen picker. Camera mode records the camera without a display stream. packages/…/types.ts• L1-22 and packages/…/stream-manager.ts• L16-50.
Camera and microphone access are optional. If an optional device cannot be opened, the SDK can continue with the sources that are available. The host can also select exact device IDs when it needs a known camera or microphone. packages/…/stream-manager.ts• L39-65.
The shared core has the richer capture path used by Cap's product recorders. It supports display-surface preferences, system-audio fallback, microphone processing, and live microphone replacement. It also turns browser capture errors into messages that distinguish permission, device, browser, and network problems. These interfaces are reusable by a product flow that needs more control than the small third-party SDK API. packages/…/capture-streams.ts• L21-105 and packages/…/capture-streams.ts• L308-367.
References: packages/…/types.ts• L1-22 packages/…/stream-manager.ts• L3-105 packages/…/capture-streams.ts• L21-105 packages/…/capture-streams.ts• L308-367
Upload result
After capture stops, the SDK creates a video record and uploads the recorded blob in parts. The upload service returns the video ID and viewer URLs, so the host can direct the user to the result or place it in another product flow. packages/…/index.ts• L221-258 and packages/…/multipart-client.ts• L31-121.
The upload uses the public key as its bearer credential and sends data to the SDK API surface. The client divides large media into multipart pieces and reports progress after each completed piece. A failed piece aborts the upload session instead of leaving an incomplete upload active. packages/…/multipart-client.ts• L3-29 and packages/…/multipart-client.ts• L44-120.
References: packages/…/index.ts• L221-258 packages/…/multipart-client.ts• L1-121
Embed SDK
The embed SDK lets a product display an existing Cap video without building its own player surface. packages/…/index.ts exposes URL creation, while the React and vanilla entry points provide the two common host integration styles.
Embedded player
The embed flow represents the video as a Cap-hosted iframe. A host supplies the video ID and public key, then may request autoplay or set a logo and accent color. packages/…/types.ts• L1-10 and packages/…/cap-embed.ts• L3-14.
The React component creates the same URL and renders a responsive iframe with the browser permissions needed for autoplay, full-screen viewing, and picture-in-picture. The vanilla helper appends an iframe to a chosen element. The loader also exposes window.Cap.embed for script-based integrations. packages/…/CapEmbed.tsx• L5-35 and packages/…/cap-embed-loader.ts• L4-18.
flowchart LR
Host[Host product] --> Options[Video ID and display options]
Options --> URL[Cap embed URL]
URL --> Frame[Cap iframe player]
Frame --> Viewer[Viewer playback]
The iframe boundary keeps player behavior and video delivery in Cap while the host controls its placement and basic visual settings. This same result can follow an SDK recording session through the returned embedUrl. packages/…/cap-embed.ts• L5-40 and packages/…/index.ts• L229-245.
References: packages/…/index.ts• L1-2 packages/…/types.ts• L1-10 packages/…/CapEmbed.tsx• L5-35 packages/…/cap-embed.ts• L3-40 packages/…/cap-embed-loader.ts• L4-18
Shared recorder core
The shared recorder core is the common browser-media layer for product and integration capture experiences. packages/…/index.ts re-exports capture streams, recorder types, format selection, upload, local backup, and recovery so each client can compose the parts it needs without copying their contracts.
Capture interfaces
The core defines the shared language for capture capabilities, recording pipelines, display modes, media formats, and upload status. These types connect browser events to product state and also derive video and storage identifiers from the server domain model. The capability check lets a client decide whether the browser can capture user media, capture the display, and encode a recording before it opens the recorder. packages/…/recorder-types.ts• L3-65 and packages/…/recorder-utils.ts• L10-61.
The capture helpers separate source access from the client that presents controls. Display access can steer the browser picker toward a monitor, window, or tab. When a browser rejects a preference or system audio request, the helper retries with a less specific request. User cancellation remains distinct so a closed picker does not become a false recorder failure. packages/…/capture-streams.ts• L57-208.
Audio sources can pass through a shared mixer. The mixer produces one stable audio track and can replace the microphone source while recording continues. It waits for the browser audio context to become ready and closes that context when setup fails or the client finishes. This allows product flows such as screen recording and video comments to use the same audio behavior without leaving an audio resource open. packages/…/capture-streams.ts• L211-306.
References: packages/…/index.ts• L1-8 packages/…/recorder-types.ts• L3-65 packages/…/recorder-utils.ts• L10-61 packages/…/capture-streams.ts• L57-208 packages/…/capture-streams.ts• L211-306
Recording pipelines
The core chooses a recording pipeline from browser support and audio needs. A streaming WebM pipeline can send data while the user records. A buffered raw pipeline holds the media until the recording ends when streaming is not a good fit. packages/…/recorder-utils.ts• L22-35 and packages/…/recorder-utils.ts• L140-189.
The choice also captures the negotiated MIME type and file extension. This keeps the upload label aligned with the format the browser can actually produce, including codec differences across browsers. The core derives video and audio codec labels from that MIME type so stored metadata does not assume one codec for every browser. packages/…/recorder-constants.ts• L82-99 and packages/…/recorder-utils.ts• L225-260.
flowchart TD
Sources[Camera, display, and audio sources] --> Mix[One recording stream]
Mix --> Support[Browser capability and codec check]
Support --> Stream[Streaming WebM]
Support --> Buffer[Buffered WebM or MP4]
Stream --> Upload[Progressive multipart upload]
Buffer --> Upload
Upload --> Processing[Server processing]
The dashboard web recorder uses this choice to decide whether it can begin an upload before stop. The comment recorder uses the same selection rules for captured comment video, while voice notes use the related single-upload path. apps/…/useWebRecorder.ts• L837-905 and apps/…/upload-comment-media.ts• L67-129.
References: packages/…/recorder-utils.ts• L105-223 packages/…/recorder-utils.ts• L225-260 packages/…/recorder-constants.ts• L82-99 apps/…/useWebRecorder.ts• L814-905 apps/…/upload-comment-media.ts• L67-129
Multipart upload
InstantRecordingUploader is the shared upload engine for recordings that need progressive transfer or a common multipart contract. It accepts recorded chunks, aligns them into provider-sized parts without losing bytes, tracks byte and part progress, retries temporary failures, and completes the server-side upload with media details. It can also split a finished blob into parts when live upload was not used. packages/…/instant-mp4-uploader.ts• L319-384 and packages/…/instant-mp4-uploader.ts• L476-614.
The uploader supports both the default object store and Google Drive upload targets. It limits work in flight, waits for connectivity after offline errors, and stops the recording when the pending upload buffer cannot keep up. It also times control requests and aborts stalled part requests so a lost network response cannot hold the recording open forever. Completion has its own retry path because the data may already exist even when the final response is lost. After a transient completion failure, the uploader reports an uncertain result instead of treating a later missing session as proof that the upload failed. packages/…/instant-mp4-uploader.ts• L3-34, packages/…/instant-mp4-uploader.ts• L121-160, packages/…/instant-mp4-uploader.ts• L232-296, packages/…/instant-mp4-uploader.ts• L664-813, and packages/…/instant-mp4-uploader.ts• L975-1065.
The API options make the engine reusable for product routes beyond the main video upload. A caller can change the base URL or multipart route and add body data such as a capability token. The comment-media flow uses this contract to upload recorded video under a comment-specific path, then creates the comment after upload completion. Finalization checks that every recorded byte reached a consecutive part before it asks the server to assemble the object. It accepts a server response that confirms the upload even when processing has not started yet, so the caller can decide how to handle that state. packages/…/instant-mp4-uploader.ts• L40-59, packages/…/instant-mp4-uploader.ts• L232-296, packages/…/instant-mp4-uploader.ts• L975-1069, and apps/…/upload-comment-media.ts• L73-141.
References: packages/…/instant-mp4-uploader.ts• L3-34 packages/…/instant-mp4-uploader.ts• L40-59 packages/…/instant-mp4-uploader.ts• L121-160 packages/…/instant-mp4-uploader.ts• L232-296 packages/…/instant-mp4-uploader.ts• L319-384 packages/…/instant-mp4-uploader.ts• L476-614 packages/…/instant-mp4-uploader.ts• L664-813 packages/…/instant-mp4-uploader.ts• L975-1069 apps/…/upload-comment-media.ts• L73-141
Local recovery
The core provides local recovery for a recording whose upload or browser session fails. RecordingSpool stores media chunks and session metadata in a browser storage backend, keeps live sessions marked as active, and can rebuild a stranded recording for retry or download. packages/…/recording-spool.ts• L1-51 and packages/…/recording-spool.ts• L211-367.
Recovery distinguishes an active recording from an orphaned one. A heartbeat updates the session even while a paused recorder produces no chunks, so another tab does not offer the live session as a recovered copy. Clients can list metadata, recover one known session, recover old sessions, or delete a recovered session. packages/…/recording-spool.ts• L306-331 and packages/…/recording-spool.ts• L403-493.
If durable browser storage is not available or cannot keep up, the core supplies a memory backup contract. The fallback changes local retention to full mode and combines the persisted data with chunks still held by the recorder. The result keeps the already persisted prefix and the current in-memory tail together, so a storage failure does not discard the recording that the user may need to retry or download. packages/…/recording-spool-fallback.ts• L3-30 and packages/…/local-recording-backup.ts• L1-65.
The dashboard presents recovered recordings for user action. The browser extension can also recover a known session and download the original media when an upload fails. apps/…/useWebRecorder.ts• L336-457 and apps/…/main.ts• L393-419.
References: packages/…/recording-spool.ts• L1-51 packages/…/recording-spool.ts• L211-367 packages/…/recording-spool.ts• L403-493 packages/…/recording-spool-fallback.ts• L3-30 packages/…/local-recording-backup.ts• L1-65 apps/…/useWebRecorder.ts• L336-457 apps/…/main.ts• L393-419
Client relationships
The SDK packages serve external product surfaces, while recorder core supplies reusable behavior to Cap's own clients. The boundaries are deliberate: the public recorder SDK gives third parties a small session interface, and product clients can use the core interfaces directly when they need custom upload, recovery, or capture policy.
| Client need | Shared contract | Result |
|---|---|---|
| Record a Cap video in a web product | @cap/sdk-recorder | Session controls and video links |
| Display a Cap video in a web product | @cap/sdk-embed | React, vanilla, or global iframe integration |
| Build a Cap web recorder | @cap/recorder-core | Capture, format, upload, and recovery primitives |
| Record video comments | Core capture and multipart upload | Media upload followed by comment creation |
| Recover extension recordings | Core spool and upload types | Retry or local download after failure |
For the wider capture-client map, see Capture clients and integrations. The mobile and browser capture clients are described in Mobile and browser capture clients, command-line and chat entry points are described in CLI and chat integrations, and the desktop product is described in Desktop application.
References: packages/…/package.json• L1-27 packages/…/package.json• L1-29 packages/…/package.json• L1-18 apps/…/useWebRecorder.ts• L837-905 apps/…/upload-comment-media.ts• L67-141 apps/…/recorder.ts• L1-21
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the public recorder phase and event payloads aligned with the React hook state returned by
useCapRecorder. - Keep the SDK upload result aligned with the API response fields that provide
videoId,shareUrl, andembedUrl. - Keep MIME type selection, derived codec labels, file extensions, and upload subpaths aligned so server processing receives the format that the browser produced.
- Keep multipart route bodies aligned between the shared uploader and specialized routes such as comment media.
- Keep provider part sizes, part boundaries, part ordering, and byte totals aligned with the server's multipart contract. A change in one can turn a recoverable upload into an incomplete one.
- Keep request timeouts and completion retry rules aligned with the distinction between a confirmed upload, a processing-not-started result, and an uncertain completion. A client must not discard media after a completion response is lost.
- Preserve the spool liveness and recovery rules when changing chunk persistence, heartbeat timing, or recovered-session cleanup.
- Preserve the full-recording handoff when durable spool storage fails. The persisted prefix and recorder-held tail must remain part of the same local backup.
- Preserve the type-only boundary from
@cap/web-domainbecause recorder core is bundled into clients without runtime dependencies.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The user cancels the display picker | A recording does not start | Capture helpers preserve cancellation errors for the caller to handle as a silent close |
| The browser rejects display preferences or system audio | The first capture request fails | Display capture retries with less specific options and can continue without system audio |
| A selected optional camera or microphone is unavailable | The recording continues without that source | SDK stream acquisition treats optional camera and microphone failures as absent streams |
| The browser cannot encode a supported format | No usable recording pipeline exists | Capability and MIME checks fail before recording starts |
| The audio context cannot start or capture graph setup fails | Audio capture cannot start and browser resources can remain allocated if cleanup is skipped | The mixer bounds startup wait and closes the context on setup failure |
| Upload parts fail during a network interruption or stall | Upload progress stops temporarily or the recording fails after retries | The shared uploader retries, waits for the browser to come online, and aborts stalled requests before it reports fatal failure |
| The upload is larger than the pending buffer or its recorded byte total does not match uploaded parts | Recording stops or completion is refused before server assembly | The uploader bounds pending bytes, drains before the final flush, aligns part bytes, and checks consecutive parts and total size |
| The final multipart response is lost after server completion | The client cannot know if processing started and may repeat a completed operation | Completion requests have a timeout and retry path, then raise an uncertain-completion error instead of claiming a clean failure |
| Completion succeeds while server processing has not started | The upload is complete but downstream processing may still need attention | The uploader keeps finalization successful and exposes the processing state to the caller |
| IndexedDB is unavailable or spool writes fall behind | Durable local recovery is lost | The client moves to an in-memory backup with a bounded retention strategy |
| A live spool is mistaken for an orphan | Dismissing recovery can delete the active recording's backup | Heartbeat timestamps exclude recently updated sessions from recovery sweeps |
Key files
| File | Why to read it |
|---|---|
| packages/…/index.ts | Public recorder session and its end-to-end lifecycle |
| packages/…/types.ts | Third-party recorder options, phases, events, and result contract |
| packages/…/useCapRecorder.ts | React-facing recorder session contract |
| packages/…/cap-embed.ts | Common embed URL and iframe behavior |
| packages/…/CapEmbed.tsx | React embedding surface |
| packages/…/capture-streams.ts | Shared device capture, fallback, audio mixing, and user-facing capture errors |
| packages/…/recorder-utils.ts | Browser capability, recording pipeline, codec, and fallback choices |
| packages/…/instant-mp4-uploader.ts | Progressive multipart upload, byte alignment, timeout, completion, and failure handling |
| packages/…/instant-recording-uploader.test.ts | Multipart byte alignment, retry, cancellation, and uncertain-completion contracts |
| packages/…/recording-spool.ts | Durable local recovery and orphan-session rules |
| packages/…/recording-spool-fallback.ts | Handoff from persisted recording data to full in-memory backup |
| packages/…/local-recording-backup.ts | In-memory recovery policy when durable storage cannot be used |
| apps/…/useWebRecorder.ts | Product recorder composition of capture, pipeline, upload, and recovery |
| apps/…/upload-comment-media.ts | Reuse of core upload behavior for recorded comments |
| apps/…/recorder.ts | Browser extension composition of the shared recorder services |