* Helper host: video protocol types, reply rules and discovery Part of RFC-0031, phase 1. The host side of docs/video-helper-protocol.md that needs no process: the messages, what to do with each reply, and finding the program. Starting the helper and the client come next. - video::wire: encode.open/frame/flush/close, cancel, hello and the replies, as the page documents them; control lines are at most 1 MiB. - video::jobs: job and frame ids, the 8 frame / 256 MiB budget and the "What EffectCraft does with each reply" table (accept, drop, job error, protocol break) as plain logic. - video::check_hello and find_video_helper: the version and app_min checks, and the program is looked for only in helpers/ next to the executable. Effect-helper scans now skip it. * Helper host: video helper client, stand-in helper and gate tests Part of RFC-0031, phase 1. Starts a video helper and runs encode jobs on it, following docs/video-helper-protocol.md and the reply rules added in the previous commit. - video::client: VideoHelper starts the program, checks its hello, opens encode jobs, sends pictures (waiting when 8 frames or 256 MiB are out), flushes, cancels and closes. Packets are copied out of the exchange file as they arrive. A crash, hang or broken reply stops the helper and fails the export; 3 stops turn it off. A refused job ends that job only. - Stopping kills the helper process and waits at most 5 s, then drops the exchange file so a stray process can't touch a reused region. The spec no longer promises a process-group or job-object kill, which needs unsafe code; its wording follows what is implemented. - tests/it/video.rs: a stand-in helper (this test binary, driven by a scenario) and 19 gate checks: crash, hang, bad and out-of-order replies, out-of-bounds packets, version mismatch, missing helper, lost device, packet_too_large as a job error, cancel, ignored cancel, 3 stops and retry. * Helper host: harden the video client against a misbehaving helper Review fixes on the video client and its tests. - Packets in one reply may not add up to more than their region, so repeated references to the same bytes can't make the host copy them many times. The reader's queue is bounded; a helper that floods it waits on its own pipe. - A picture is sized with checked arithmetic and refused over 256 MiB before any layout or exchange file growth; an encode.open that can't be written is refused before the job, its ids and regions exist. - Waits end at their deadline even while replies keep arriving, a poll looks at a bounded number of late replies, the flush batch and encode.flushed share one timeout, and a timeout too long to represent no longer panics. - close is private: a job is closed only after encode.flushed or cancelled. An unsent frame is given back when writing its planes fails; a failed copy of packets stops the helper without counting it as a crash. - Contents/Helpers is searched only for an executable in Contents/MacOS. An incomplete base64 group is rejected. - Tests assert the protocol diagnostic of each malformed reply and cover the flood, the flush deadline, oversized pictures and an oversized encode.open. The spec table drops every reply except cancelled for a failed job that has not been cancelled yet. * Helper host: allow one encoder job at a time on a video helper open_encoder accepted a second job while the first was open. If the first job then reported an error while the client was waiting on the second, the error was dropped and its caller only saw "no such video job". A second open is now refused with a request error until the first job is finished or cancelled, so each job's error reaches its own caller. The receiver state machine still tracks several jobs; only the client API is single-job. The gate test refuses a second open while one is active and accepts a new one after finish and after cancel. * Helper host: reject malformed base64 padding, align two protocol notes base64_decode stripped every trailing "=", so "====" decoded to an empty codec configuration that open_encoder then accepted. It now accepts at most two "=" and only when the length is a multiple of 4. The protocol page said job ids are never reused even after a restart, but the counter is per process, and said nothing about replies that were already queued when the frame timeout passed. Both now describe what the client does. * Helper host: fix two code comments about job id reuse The comments on NEXT_JOB and Jobs::new said ids are never reused across helper processes or restarts. The counter is per process and a new helper starts from the highest id opened so far, so the comments now say that, matching docs/video-helper-protocol.md. * Helper host: cap the encoder delay a video helper may report A helper-reported delay sized the flush region (and the exchange file) with unchecked arithmetic. A delay over 64 is now a protocol break, the flush region uses checked arithmetic, and the protocol page says so. Also moves a test doc comment back onto the test it describes. --------- Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
17 KiB
Video helper protocol (proposed)
Status: proposed. This page specifies how EffectCraft talks to a hardware video helper, as
decided in RFC-0031 (hardware video in a helper process). The host side is in
crates/helper-host (video), tested against a stand-in helper; there is no real helper yet and
export does not use it. Phase 1 covers encoding; decoding is reserved for phase 2
(decoding). Where this page is more specific than the RFC or differs from it,
Differences from RFC-0031 lists it.
A video helper is an external helper that offers hardware video encoding instead of effects. It reuses that page's process model: the helper is a child process, requests and replies are JSON lines, pixels and packets go through the frame exchange file, a helper that crashes, hangs or breaks the protocol is stopped, and one that does so 3 times is turned off. This page covers only what is different for video.
Starting and finding it
- The user decides. Starting a video helper needs the same consent as an effect helper (see Using helpers). Opening a project whose output module says Hardware Encoding: Auto does not start it. Confined agents and scripts without write and network permission cannot start it either.
- Only the app's own install folder. The program is
effectcraft-helper-video, found inhelpers/next to the EffectCraft executable (Contents/Helpersin a macOS bundle). It is not looked for in the settings folder, inEFFECTCRAFT_HELPER_PATH, inPATHor in the current folder. Effect-helper scans skip it. - Checked before any work. EffectCraft reads the
helloreply (below) first and sends nothing else until the versions fit.
hello
EffectCraft adds "video": 1 to its hello: the highest video protocol version it speaks. The
helper answers with the usual fields plus a video object:
{"op":"hello","version":1,"name":"Hardware video","helper_version":"0.1.0","loads":[],
"video":{"version":1,"app_min":"0.25.0","devices":[
{"id":"nvenc:0","name":"NVIDIA GeForce RTX 3090","api":"nvenc","driver":"NVIDIA 570.86",
"encode":[{"codec":"h264","profiles":["high"],"bit_depths":[8],"min_size":[64,64],
"max_size":[4096,4096],"rate_control":["bitrate"]}],
"decode":[]}]}}
video.versionis the video protocol version the helper will use. If EffectCraft does not speak it, or is older thanapp_min, it does not use the helper, and the message names the installed helper version and the version to install.deviceslists every GPU the helper can use, each with a stableid, theapiit reaches it through (vaapi,nvenc,videotoolbox, or latermediafoundation), thedriver, and what it canencodeanddecode. Two GPUs that both offer H.264 are both listed; EffectCraft picks one and its render log says which. The rule for choosing is still open in RFC-0031.- The helper answers
describewith no effects. - Start-up order. The helper starts its hardware APIs (CUDA, VA-API and others) on one thread,
in a fixed order, before it answers
hello. Starting NVENC and VA-API on two threads at once can deadlock with NVIDIA's VA-API driver.
Pictures
Video does not use the effect protocol's float RGBA image refs (16 bytes a pixel, about 133 MB at
4K). A picture is 8-bit 4:2:0, three planes, Y then U then V. The luma plane is width × height; each chroma plane is width/2 × height/2. Width and height are even and within the
device's min_size and max_size; other sizes are declined before the encoder opens. Every
encode.frame describes the three planes itself (see Encoding). EffectCraft version 1
always sends them tightly packed (stride = plane width), Y then U then V, one after the other in
the slot's in region; a later version can add padding without changing the message. Colour is
given once per job, in encode.open, as H.273 code points.
Only limited range is used at first.
Encoding
encode.open → encode.opened → (encode.frame → encode.packets)… → encode.flush → encode.flushed
→ encode.close
| Request | Reply | |
|---|---|---|
{"op":"encode.open","job":1,"device":"nvenc:0","codec":"h264","width":1920,"height":1080,"rate":[30,1],"bitrate_kbps":15000,"profile":"high","keyint":60,"colour":{"primaries":1,"transfer":1,"matrix":1,"range":"limited"},"slots":[{"in":{"offset":0,"len":3110400},"out":{"offset":3110400,"len":1048576}},…]} |
{"op":"encode.opened","job":1,"codec_config":"<base64 avcC>","delay":2} |
job is chosen by EffectCraft and is never reused. slots is the memory the helper may use, decided by EffectCraft. codec_config is the avcC (H.264) or hvcC (HEVC) record for the MP4 sample entry. delay is the number of frames the encoder holds back (B-frames), at most 64; a larger delay is a protocol break. |
{"op":"encode.frame","job":1,"frame":0,"slot":0,"planes":[{"offset":0,"stride":1920,"size":[1920,1080],"len":2073600},{"offset":2073600,"stride":960,"size":[960,540],"len":518400},{"offset":2592000,"stride":960,"size":[960,540],"len":518400}]} |
{"op":"encode.packets","job":1,"frame":0,"packets":[{"offset":0,"len":41230,"pts":0,"dts":-2,"key":true}]} |
The picture is in slots[0].in. planes lists Y, U and V: offset is from the start of the file, size is in samples, len is stride × height bytes. EffectCraft sets them, and the helper reads only them. The reply means the slot may be reused and says where the packets are, inside slots[0].out. It may hold no packets (the encoder is still holding frames) or several. |
{"op":"encode.flush","job":1,"out":{"offset":…,"len":…}} |
{"op":"encode.packets","job":1,"frame":null,"packets":[…]} then {"op":"encode.flushed","job":1} |
EffectCraft sends it only after every encode.frame has been answered, and after it sends only cancel or encode.close. The helper answers with exactly one encode.packets with "frame":null (its packets may be empty), then exactly one encode.flushed. The packets are the rest of the stream, inside out, and their offsets are counted from it. EffectCraft sizes out for the frames the encoder still holds: at least (delay + 1) times the length of a slot's out region. If the encoder's output still does not fit, the helper answers error with code packet_too_large. |
{"op":"encode.close","job":1} |
none | Free everything for the job. EffectCraft sends it after encode.flushed or cancelled, and frees or reuses the job's regions only then. If the helper was stopped first, there is no reply to wait for: the exchange file is dropped and its regions are never reused (see stopping). |
{"op":"cancel","job":1} |
{"op":"cancelled","job":1} |
Stop now. After it sends cancel, EffectCraft ignores every reply for the job except cancelled, so replies for frames still in flight are not protocol breaks. The helper stops using the job's regions before it replies cancelled, and cancelled ends every frame of the job that is still outstanding. |
- Packets are MP4 samples. Each is a sequence of length-prefixed NAL units, every length a
4-byte big-endian number, which is what
codec_configdeclares (lengthSizeMinusOne3). A helper whose API produces Annex B converts it. EffectCraft writes the bytes into the MP4 as they are. - Packet offsets are counted from the start of the region the reply uses (
slots[slot].out, or theoutregion ofencode.flush). In the example the packet starts at file offset 3110400 + 0. EffectCraft adds the region's offset and checks thatoffset + lenis within the region's length. A packet outside the region is a protocol break (see the table below), not apacket_too_largeerror. - Timestamps are in units of 1 /
rate[0]seconds, so frameihaspts = i × rate[1].dtsmay be earlier thanpts. - Who owns what. EffectCraft decides every offset. The helper reads only the slot it is told
to and writes only that slot's
outregion (or theoutregion ofencode.flush). EffectCraft checks every offset and length it gets back with checked arithmetic against the regions it gave out. - Backpressure. At most 8 frames, and at most 256 MiB, are queued plus in flight together. When the budget is used up EffectCraft waits for a reply before sending the next frame, so a slow GPU slows the export and does not use more memory.
- Replies are in frame order,
errorreplies included. - Control lines are at most 1 MiB. Pictures and packets never go through the pipe.
A normal job
An encoder that holds back one frame (delay 1), two frames. E is EffectCraft, H the helper:
E encode.open job 1 H encode.opened delay 1
E encode.frame job 1, frame 0, slot 0 H encode.packets frame 0, packets []
E encode.frame job 1, frame 1, slot 1 H encode.packets frame 1, 1 packet (pts 0)
E encode.flush job 1, out {…} (sent once both frame replies are in)
H encode.packets frame null, 1 packet (pts 1)
H encode.flushed
E encode.close job 1
Out of order: after frames 0 and 1 are sent, a reply for frame 1 before the reply for frame 0 is a
protocol break, and so is encode.flushed before the encode.packets with "frame":null.
Anything the helper sends for job 1 after encode.flushed arrives for an ended job (EffectCraft
sends encode.close as soon as it reads encode.flushed) and is dropped.
Job and frame ids
job is a positive whole number that EffectCraft takes from a counter that only goes up, so an id
is never reused within one helper process. A helper that is restarted, or a second helper process,
is a separate connection and may see the same number again. frame counts from 0 inside a job and is
never reused in it. Every id EffectCraft has sent is issued. A job is open from
encode.open until EffectCraft sends cancel or encode.close; after that it is ended, and
it stays ended. An id EffectCraft has not sent is never issued.
What EffectCraft does with each reply
A protocol break stops the helper and counts towards the 3 stops. A job error ends one job and the helper carries on; it does not count.
| Reply | EffectCraft |
|---|---|
encode.opened for an open job with an encode.open outstanding |
Accepts it; the job can take frames. |
encode.packets or error for an open job, for the oldest outstanding frame, with every packet inside its region |
Accepts it. |
After encode.flush: the first encode.packets with "frame":null, every packet inside the out region of that encode.flush |
Accepts it. |
After that batch: encode.flushed |
Accepts it; the job is complete and EffectCraft sends encode.close. |
encode.packets with "frame":null or encode.flushed when no encode.flush is outstanding; a second encode.packets with "frame":null; encode.flushed before the batch; encode.opened when no encode.open is outstanding |
Protocol break. |
No reply to encode.open, encode.frame, or encode.flush (the batch and encode.flushed both count) in time |
Protocol break: EffectCraft stops the helper (timeouts). |
encode.packets or error for an open job with a frame that was never sent, already answered, or not the oldest outstanding; or error without frame when no encode.open or encode.flush is outstanding |
Protocol break. |
encode.packets with a packet whose offset + len is outside its region |
Protocol break. |
encode.opened with a delay over 64 |
Protocol break. |
error without frame while an encode.open or encode.flush is outstanding (for a flush, instead of the batch or of encode.flushed) |
Job error: EffectCraft sends cancel, then encode.close. |
error with packet_too_large: the helper says the encoder's output does not fit the out region it was given, and it did not write past it |
Job error. |
cancelled for a job EffectCraft sent cancel for |
Accepts it; the job's frames are over. |
cancelled for an open job |
Protocol break. |
Any other reply for an ended job (late replies for frames that were in flight, a second cancelled), and any reply except cancelled for a job that has just answered with an error and has not been cancelled yet |
Dropped and logged. The regions it names are not read. Not a break. |
| Any reply for a job that was never issued | Protocol break. |
| A line that does not parse, or is over 1 MiB | Protocol break. |
Timeouts and errors
- Per frame, not per export. A frame is outstanding from
encode.frameuntil its reply, untilcancelled, or until the helper has exited. If the oldest outstanding frame waits longer than the timeout (30 s by default, set withEFFECTCRAFT_HELPER_VIDEO_TIMEOUT, asEFFECTCRAFT_HELPER_TIMEOUTis for effects), EffectCraft stops (kills) the helper and waits for it to exit. A GPU that hangs without exiting ends the helper instead of making the export look busy. The timeout detects a hung helper; it is not a strict arrival deadline: a reply that is already queued when EffectCraft next reads is accepted even if it came after the timeout.encode.openandencode.flushuse the load timeout (600 s); for a flush it covers theencode.packetsand theencode.flushedtogether. Aftercancelthe frames of that job have no timeout;cancelledis due within the same 30 s, or EffectCraft stops the helper. {"op":"error","job":1,"frame":3,"code":"…","message":"…"}answers one request.frameis there when it answers anencode.frame(it ends that frame's timeout) and absent forencode.openandencode.flush. The helper process carries on, but the job is over: EffectCraft sendscancel, thenencode.closeoncecancelledarrives. Frames sent before the error are not answered; a late reply for one is dropped. Codes:unsupported(the settings are declined),device_lost,out_of_memory,packet_too_large(the encoder's output for a frame does not fit theoutregion),encoder. All of them are job errors.- EffectCraft stops the helper when it exits, closes stdout, sends a line that does not parse
or does not fit, breaks the rules above, or misses a timeout. After 3 stops it is turned off and
listed in
helper_blocklist.txtuntil the user retries in Settings, as for effect helpers. - A stopped helper's exchange file is never reused. A child of the helper can map the exchange
file too, and on Unix it can leave the process group (
setsid), so EffectCraft does not try to prove that every such process has exited. After a stop it drops the exchange file and gives the next helper a new one, so a stray process can only write into a file nobody reads. Regions are reused only inside one helper process that is still running, afterencode.flushedorcancelled. - Stopping the helper is bounded. EffectCraft kills the helper process and waits for it to exit for at most 5 s. If it is stuck (for example in an uninterruptible driver call), EffectCraft gives up waiting, logs it, and the export fails with an error instead of hanging. Processes the helper started are not tracked or killed: the rule above is what keeps their writes harmless.
What EffectCraft does with a failure:
| When | What happens |
|---|---|
Before the first frame (unsupported, no helper, version mismatch, blocked) |
With Hardware Encoding: Auto, the built-in encoder runs and the render log says why. |
After frames have been sent (a crash, a hang, device_lost) |
The export ends with an error that says what failed. The format is never changed silently, and hardware and software output are never mixed in one file. |
A hardware export writes to a temporary file and replaces the destination only when it finishes, so a failed or cancelled export leaves any existing file as it was.
Differences from RFC-0031
D3 describes the frame format. This page follows it, with these differences and additions:
- Plane descriptors are in every
encode.frame, as D3 says. Colour information is not: it is sent once per job, inencode.open, because it cannot change inside a job. - Replies for a cancelled or old job are ignored, as D3 says, but only for ids EffectCraft has issued. A reply for a job id it never issued is a protocol break.
- Limits D3 leaves open ("a few frames", "a cap on the bytes") are 8 frames and 256 MiB.
Decoding (phase 2)
decode.open, decode.sample and the picture replies are reserved for phase 2 and will be
specified with it. They must cover rebuilding the decoder's state after a failure (reference
frames, presentation order, timestamps, seeks, flushes and colour information), as RFC-0031 D6
requires.
Not decided here
- How Auto chooses between two GPUs that both offer a codec.
- Who builds, signs and releases the helper, and where its code lives.
- 10-bit pictures, zero-copy transfer from GPU textures and codecs other than H.264 and HEVC.