- publish your camera and show the edited video
- start an edit from an uploaded reference image
- switch the image or edit type while the edit runs
- end the edit, and handle an edit that ends on its own or a switch that fails
<video id="camera"> previews the webcam, and
<video id="edited"> shows the model’s output. Add a button with id="switch" if you want to
change the look. The snippets use showStatus(), hideStatus(), showMessage(), and
offerStartAgain() as placeholders for your own UI. The render() and showError() functions
appear later in this tutorial. Get jwtToken from your server as Authentication
describes.
Install the SDK
- npm
- pnpm
Understand the model
The session works like this:- Your camera is the whole input. The model edits nothing until your camera reaches it. Publish
it, and watch
camera_forwardingon the snapshot. session_stateshows the current edit. It arrives on connect and on every change. Use it to show progress and decide when controls are available.- Model refusals and edit failures arrive as
command_error. Check itscode. Useoriginto distinguish invalid input from a service failure. Handle SDK connection and upload errors separately. A failed switch can send this error after its reply.
Connect
Mint a token on your server, as Authentication describes, and pass it toconnect(). Subscribe to messages and tracks before you connect, so you do not miss the first
snapshot.
session_state when the session connects. Wait for it before you send a
command. The wasLive check resumes the output track each time an edit enters live, including
later edits in the same session.
Style the output <video> with object-fit: contain so the whole edited frame stays visible. The
output can differ from the camera in size and orientation.
Publish the camera
Publish the camera as soon as the connection is ready. Show it in a second<video> so the user can
see what goes in.
Start an edit
Let the user pick a reference image and an edit type, then start the edit.starting and warming_up to live. The first edited frame can arrive
after the phase becomes live. Drive the loading state from the snapshot:
Switch the look
Callswitch_reference with what you want to change. What you omit stays as it is.
reference_switched, means the switch was sent. The picture changes within a few
seconds. There is no later success message, so do not wait for one, and do not resend the switch
when nothing seems to change.
If the switch fails, a command_error for switch_reference can arrive after the reply. The edit
keeps running with the previous look. Handle that error in Surface errors.
End the edit
ending to ended. This tutorial leaves the camera published. A “Start
again” button can send start_edit without another camera step. If your app unpublishes the camera
between edits, publish it again before the next edit.
A session bills while it is ready, including time between edits. Call reactor.disconnect() when
the user is done, or after a few idle minutes with no edit.
An edit can also end on its own. Handle it in render() from end_reason:
edit_max_seconds and edit_elapsed_seconds on the snapshot let you show how long the edit has
left.
Surface errors
Thecommand_error handler in Connect calls showError(). When a switch fails, tell
the user and keep the previous look on screen. Use origin and retryable for other model errors:
command_error
messages.