Audio track switch: close the chain, re-mount, restore config
An audio track switch is one command, queued and executed in order. What the command thread does falls into three steps.
Step one closes the current chain. Every audio track object is closed uniformly: the decode thread and the render thread stop, the packet queue and the frame queue are drained — the selected track is closed along with the rest, since it is about to be re-mounted anyway. One thing is set apart: the config packet. It is sent only once during stream scanning, and after the queues are drained, switching back to this track cannot bring the decoder up without its config, so I cache one copy per track.
Step two mounts the new track. The track description is handed to the track object, and an unsupported codec is stopped right here, the track staying invalid and never starting; then the cached config packet is delivered, the decode thread starts first, and the render thread starts once decode output is ready. A switch can land in a paused state or at a speed other than 1x, and the new track has to catch both up: pause rendering if paused, and adopt the current speed.
Step three, on sources that can seek, re-positions to the current render position. This step is forced, and the reason lies in packet dispatch.
Both ends of the operation, mount and close, send a track event up to the layer above (opened, closed); UI refresh and analytics key off it, so a switch is just an ordinary state change to the caller, who keeps no state of their own. There is also a ready-made interface for querying the currently selected track, and an external audio chain follows the selected track — the render handle you fetch belongs to the current track, and once the track closes it comes back empty.
Packets of unselected tracks never mount
Packets of every audio track coming out of the demuxer pass through the same dispatch point. The gating rule: config packets are cached per track but delivered only to the selected track; a data packet that does not belong to the selected track never mounts at all — it is not decoded, makes no sound, and takes no room in the packet queue to choke the IO.
The tracks themselves are mounted lazily: when the player starts, every audio track's object exists, but only the selected one actually carries a decode chain. What the track description brings is attributes like codec and channel count, which the layer above turns into a list for the user to pick from; an unsupported codec is stopped at the moment of mounting, the track staying invalid and never starting.

This gating fixes the problem of several audio tracks playing at once: give every track a chain and, unchecked, several tracks decode and sound at the same time. It has a side effect, though. Switch tracks mid-playback and the new track has been gated all along, so the packets it can receive start only from the demuxer's current read position, and that read position runs ahead of the playback position by roughly one buffer depth. The new track's first packets do not line up with the picture on screen: the audio jumps a few seconds forward, sound and picture drift apart, and the audio clock drags the master clock into a jump with it.
So on sources that can seek, after switching I re-position to the render position and let the new track start receiving from where the picture sits. The earlier article on speed switching across 4x uses the same trick. Turning audio off (selecting the negative value) skips this step: the chain is closed anyway, so there is no lining up left to do.
Subtitle track switch: only the routing changes
Subtitles do not touch the packet queue or the frame queue; they are a bypass, the selected track pushed straight into the subtitle view. Switching a subtitle track therefore closes no chain at all — only the routing moves.
First, what comes before a track is selected. The layer above can only pick a track if it knows which tracks exist: the demuxer's stream scan produces track descriptions — codec, language, attributes of that kind — the query interface hangs off the source info, and which one to pick is the caller's call.
When subtitle packets reach the dispatch point they pass three gates. The script header is retained per track; if a track was selected before the demuxer even started, the header is fed to the view retroactively. Data packets look at the selection state: nothing selected yet, queue first — a bounded queue, oldest dropped when full, replayed once a track is chosen. An external subtitle selected, embedded packets are all dropped. A track already selected, only that one is kept. The select command also flips the routing switch on the IO side, and bitmap-subtitle decoding with its bypass changes lanes with it.
The other end of the retention has to be wired too: the select command runs on the player thread, the scan runs on the IO thread, and nothing guarantees which arrives first. Selection first, header later: the moment the header shows up and the selected track has not been fed yet, it is fed on the spot. The other way around, the header is stored first and fed when the selection arrives. Write both ends and you never bet on the order.

On the view side, activating a new track tears down the slot it displaces; when the script header arrives it is loaded, plain-text tracks take the text-loading path, converted into dialogue lines and laid out; the previously queued packets replay at this point. The view has to attach to the video track — the render wiring and the open-track channel both live on that side, and without a video track subtitles mean nothing. Closing the subtitle track only closes the embedded slot; the external and speech-recognition slots are untouched.
Embedded, external, recognized: three slots, last activation wins
Subtitles come from three sources: embedded tracks, external files, and speech-recognition output. All three are pushed into the view, only one is displayed at any moment, the most recently activated wins, and an empty window does not fall back — remove an external subtitle and the picture does not quietly return to the embedded track.
Embedded track switching uses the routing path above. Loading and unloading an external file are two separate commands, and they are the only two control commands that carry a receipt: an atomic state settles the result internally, the caller polls and waits, three seconds at most, and a timeout counts as failure. Loading an external file displaces the embedded track from the winner slot, embedded packets are dropped right at the dispatch point, and when switching back to the embedded track, the replay path above catches exactly the gap from before the selection.
On media change all of this state clears along with it: the queued subtitle packets and the per-track script headers, together with the audio config cache on the other side, all belong to one playback session, and the next playback starts from scratch.
The engine avox is open source on GitHub: github.com/xxxzhou/avox, where both of these track-switching paths live.