BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

Demos: recording and playback

BF22142

A demo is not a video and not a list of inputs. It is what the server would have sent to one more client: the same packets, with the tick each belongs to, written to a file. Playing it back is running the client on those packets instead of a socket's. Everything on the packets and ghosts pages holds inside a demo.

Recording: one more connection. Playback: the same packets again. Server the simulation, 30 ticks connection 0x81 scoped every frame, no packet rate <name>.bf2demo records: tick, length, payload; zlib joined like a client: client info, load complete, database complete; then special packet 0 sv.demoQuality 1..10: 260 to 4096 bytes a packet, 40 to 255 ghost entries, 13-bit state sizes <name>.bf2demo read record by record readPacket the records of this tick Client game no connection, no local player each tick: receive, the world tick, the objects' update, predict the ghosts; the listener is the demo camera keys 1 to 9 set the time scale 0, 100, 5, 25, 50, 75, 150, 300, 1000 %; there is no seek, a restart reopens the file bf2_w32ded.exe!0x0044a9a0 (startDemoRecording), 0x0044b2e0 (sendToClients); BF2.exe!0x004d41c0 (startDemoPlayback)
Above: recording. Below: playback. The file in the middle is the same.

Recording#

demo.recordDemo <name> on a running server (bf2_w32ded.exe!0x00411a60; a client refuses: "can't record demos as a client"). The file is Demos/<name>.bf2demo when the name has no extension.

GameServer::startDemoRecording (0x0044a9a0):

  1. creates a client connection for player 0x81, of its own kind, and runs the join steps for it as for a player: the client's info, load complete, client loaded, database complete, each followed by a flush that builds packets until none is left and records each;
  2. writes special packet 0, the start mark.

From then on GameServer::sendToClients (0x0044b2e0) treats the demo connection like a client that is sent every frame: it scopes ghosts every frame, builds the packet, records the payload when it is not empty with the simulation tick, and acknowledges it at once. A demo connection never loses a packet, so nothing in a demo is ever re-sent.

demo.stopRecording removes the connection, writes the end marker with the current tick and closes the file's zlib stream.

How much fits: sv.demoQuality#

The demo connection's budget comes from the server setting sv.demoQuality (1 to 10, default 1), with q = (quality - 1) x 0.11111111:

QualityBytes a packet (260 + trunc(3836 q))Ghost entries a packet (40 + floor(215 q))
126040
51,964135
104,096255

A player's connection has 11-bit ghost state sizes; the demo connection has 13, so a first state too big for a player's packet fits (ghosts). At quality 1 a busy round fills every packet to the last byte: the largest payload in five quality-1 demos was 260 bytes, at quality 10 it was 2,569.

Automatic recording#

AutoRecorder (constructed in GameServer::init, 0x00560840) records rounds by itself when the server's settings ask for it; its demos are named by the date and time, and it has a hook and a notification for a finished demo.

Playback#

+demo <file> on the command line, or the menu's demo list, creates a client game without a connection (BF2Engine::startDemo BF2.exe!0x00406450) and hands the recorder to GameClient::startDemoPlayback (BF2.exe!0x004d41c0), which makes a server connection of the playback kind with the server name demo_playback.

Each tick the receive loop (BF2.exe!0x004d4ea0) pulls the records of that tick with DemoRecorder::readPacket and processes them as received data packets: the same packet, event and ghost handlers as a live connection's. Then the client game's own tick runs as in a live game (GameClient::simulate BF2.exe!0x004d5740): the world tick, the objects' update, the ghosts' prediction; there is no local player and so no control block. The listener for the sound is the demo camera.

  • There is no seek. A record more than 100 ticks ahead makes the simulation tick jump forward; going back is demo.restartDemo, which resets the world, the sound and the HUD and opens the file again.
  • The tick clock of a demo is its own (TickClock(10, 30.0)); a time demo or a movie render advances it by a fixed step instead of real time (the frame and the tick).

The controls#

Key or commandEffect
keys 1 to 9 (weapon select)time scale 0, 100, 5, 25, 50, 75, 150, 300, 1000 % (table BF2.exe!0x008fc938)
alternate fire, demo.toggleCameraDemofree camera or follow camera
action (with sprint held: backwards), demo.togglePlayerDemofollow the next player: the 256 ids are walked from the current one to the first player with a controlled object
demo.toggleDemoPauseOrPlaypause; a finished demo cannot be unpaused
reloadstop a camera recording and a movie render

The free camera is an object of its own class with a field of view of 75 degrees. A camera path can be recorded to a .bf2cam file beside the demo and played with it.

What the port had to learn#

Playback runs the whole client game

The port's first demo player advanced the world tick, predicted the ghosts and applied the ghost streams. A demo then played the ambience and nothing else: no gunshot, no engine, no footstep, no explosion. The original runs the client game's tick during a demo exactly as in a live game: remote weapons and their fire replay, projectiles, armor effects, detonations, the effect bundles. After the port did the same, sixty seconds of a demo recorded by the original server (16 bots on Karkand) went from 0 world sounds to 3,752, from 0 effect bundles to 1,129, and from 0 projectiles to 647.

Demos are also a test rig: a demo recorded by the original server is a long, real packet stream that our client must play without an error, and a demo recorded by our server must play in the same way (test rigs). The check demo-tour counts world sounds, weapon fire, effect bundles and projectiles on both.

How it is tested#

  • Reading (crates/bf2-client/src/demo.rs, 29 tests): the packet reader and the playback states: which special packet moves the render state, what ends a playback, the recorder's "is playing" answer in each mode.
  • The file's header and the recorder's settings (crates/bf2-demo/tests): the header's constructor, read and write, the settings' sanity check and the automatic demo name, each from Battlefield 2's listing.
  • Timing during playback rests on the frame timer's tests (frame and tick order).

Not proven: playback of a demo recorded by the original server through a whole round with pictures compared. The Heroes sweeps note that a demo's playback was "not played" for their rows. Known playback faults are in the notes.

In the code#

WhatWhere
the file format, the stream, the config, auto-recordingcrates/bf2-demo (format, stream, config, autorecord)
the demo connection's ratesautorecord::DemoConnectionRates
the server's sidecrates/bf2-server, crates/bf2-net/src/join_demo.rs
playbackcrates/bf2-client/app (Client::demo_simulate)

Notes: notes/subsystems/demo-recorder.md, notes/formats/bf2demo.md.

Source: docs/dev/network/demos.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.