Skip to content

MediaToolkit.NET 1.0.0

Latest

Choose a tag to compare

@yartat yartat released this 21 Nov 10:44
e38b000

MediaToolkit.NET 1.0.0

The first stable release. It follows a full audit of the 0.3.1 code for bugs,
resource leaks and vulnerabilities (AUDIT.md in the repository holds every
finding, with its evidence). Each fix was written test first: a test that
failed for the stated reason, then the change, then the test passing. The
findings were reproduced on Windows 11 and on Debian 13 under WSL before they
were fixed, not only read from the code.

From 1.0.0 the public API follows semantic versioning. This release does change
behaviour in four places, listed first, because each one can break a caller that
worked before.

Changes that can break existing code

FFmpeg inputs are local files by default

FFmpegProber, FFmpegMediaReader and FFmpegTranscoder now open local files
and pipes only
. A path that came from somebody else could otherwise make the
process connect to a host of that somebody's choosing, or read other files
through a playlist.

  • Network URLs (http://, https://, rtsp://, tcp://, udp://, and
    every protocol but file, pipe, crypto and data) are refused with
    avformat_open_input(...): Invalid argument, without connecting.
  • Playlists (HLS, DASH, concat) are refused: they name other files to read.
  • FFmpegPlayer and capture devices are not restricted.

To open URLs on purpose, allow them once at start-up
(FFmpegDemuxer.AllowNetworkByDefault = true) or for one reader through
MediaReaderOptions.DemuxerOptions["protocol_whitelist"]. A playlist can be
opened with FFmpegDemuxer.Open(url, allowPlaylists: true). Examples are in the
README. GStreamer and mpv accept URLs as before.

Transcoders write to a partial file

All three transcoders now write .<name>.partial-<id><ext> beside the output and
rename it over the output only when the job has finished (PartialOutput).

  • An output path that is a link to an input no longer overwrites the input.
    With 0.3.1 an FFmpeg or mpv job through a symbolic link replaced the input with
    its own output and reported success, and a GStreamer job emptied it.
  • A job that fails or is refused no longer deletes a file that was already at
    the output path.
  • The output directory must now allow creating a file; a cancelled or failed job
    removes its partial file.

Native library search is narrower

  • The development convenience of walking up from the application folder to a
    src/FFMpeg/<platform> folder now applies only below a folder that holds
    MediaToolkit.NET.slnx. On an installed application the walk reached folders
    other users may write to, which allowed a planted DLL to be loaded.
  • Relative and empty entries of PATH are no longer used, and on Windows the
    loader no longer falls back to the default search order, which includes the
    current directory.

Small changes of contract

  • MpvPlayer sets ytdl=no and load-scripts=no by default; put them back
    through MpvPlayer.Options if you rely on them.
  • MpvFilterChain.Lavfi returns lavfi=graph=%N%... instead of lavfi=[...],
    which a ] inside the chain would end early.
  • Pushing planar audio with more channels than a frame view can hold (8) to a
    filter graph throws ArgumentException.
  • Ole32.Initialize returns true for S_FALSE as well, since that call must be
    balanced too.

Fixed

Crashes and memory safety

  • GStreamer read past a short buffer. The planes of a video frame came from
    the caps alone; a buffer shorter than they describe aborted the process.
    FrameBounds.FitsIn now checks every frame against its buffer, in the
    GStreamer, DirectShow, Media Foundation, V4L2 and AVFoundation producers.
    Along the way: a bottom-up Media Foundation picture is addressed from its last
    row, V4L2 skips buffers the driver flagged as incomplete, and AVFoundation
    skips a buffer whose lock failed.
  • MpvPlayer: an access violation when Stop, Pause or Volume ran while
    another thread disposed the player. Native calls now run under a read lock and
    the handle is destroyed under the write lock.
  • FFmpegPlayer.
    • Seek while paused used the decoder from two threads at once; the worker
      applies seeks now.
    • Dispose from inside a frame handler waited out a two-second join and then
      freed the media under the running worker; the worker closes it when it
      leaves.
    • A read blocked on the network held Dispose for the whole timeout; the
      input is opened through avio_open2 with an interrupt callback, and
      FFmpegDemuxer.Interrupt and ClearInterrupt are public.
    • Play after the end of the stream no longer drops its cancellation source
      without disposing it.
  • A subscriber that throws from Failed ended the process on every backend's
    worker thread and native callback. They are raised through
    SafeEvents.RaiseFailed.

Wrong output

  • FFmpeg audio frames carried the format the decoder reported at open, not
    the frame's own: a stream whose audio starts later read as 0 Hz, 0 ch, and one
    that changes rate or channels mid-way described its frames wrongly. The format
    now comes from each frame, and FFmpegMediaReader rebuilds its conversion graph
    when it changes.
  • TryReadAt on MPEG-TS returned the first key frame after the requested
    time; it now seeks back until the frame on screen at that time is the one read.
  • Transcoding audio from MP2 failed, because the check compared the decoded
    frame with the container's sample format. A timestamp that jumps back is
    repaired instead of reaching the muxer, and a video that changes size part way
    through is rebuilt for the new size.
  • Encoder options nobody recognised vanished. FFmpeg and mpv now report each as
    a warning in TranscodeResult.Warnings.
  • WasapiRenderer.Write waited for ever when the endpoint had not been
    started and the data was longer than the free buffer.

Resource leaks

  • FFmpegDemuxer.Open, FFmpegRecorder (constructor and stream setup) and the
    DirectShowGraph constructor release what they hold when they fail.
  • DirectShowGraph kept every wrapper it ever handed out until the graph was
    disposed; wrappers now remove themselves, and Describe releases what it
    enumerates.
  • V4L2 unmaps buffers mapped before a failure and opens the device with
    O_CLOEXEC.
  • The DirectShow property reader clears a variant that is not a string.
  • COM initialisation is balanced on the WASAPI and Media Foundation capture
    threads.
  • mpv's argument vector is allocated with NativeMemory, and zeroed: a failure
    part way through the old stackalloc loop freed garbage.

New public API

  • PartialOutput (Abstractions): the partial file a job writes before the rename.
  • FrameBounds.FitsIn (Abstractions): checks a frame's planes against its buffer.
  • SafeEvents.RaiseFailed (Abstractions): raises an event without letting a
    subscriber's exception through.
  • FFmpegDemuxer.AllowNetworkByDefault, Open(..., allowPlaylists, allowNetwork),
    Interrupt() and ClearInterrupt().

Not covered, and why

The audit lists these in AUDIT.md; they could not be reproduced or tested
without hardware, or without a Mac:

  • WASAPI, Media Foundation, V4L2, ALSA and PulseAudio teardown: the worker thread
    is joined with a two-second timeout and the native objects are then freed, so a
    handler that takes longer, or a device that stops answering, can still free them
    under the worker. FFmpegPlayer and MpvPlayer no longer have this.
  • A capture whose loop failed still reports IsRunning until Stop is called
    (WASAPI, V4L2, ALSA, PulseAudio, Media Foundation).
  • AVFoundation and AudioQueue on macOS: a callback in flight during Dispose, and
    restarting a capture after Stop. The macOS code changed in this release (frame
    checks, Failed handling) and has never been run, as before.
  • A connect that is already in progress inside FFmpegDemuxer.Open cannot be
    interrupted; pass rw_timeout through DemuxerOptions.
  • The Media Foundation recorder does not check a frame's format against its
    stream's.
  • FFmpegPlayer.Stop interrupts network reads, but the prober, the reader and the
    transcoder have no interrupt of their own.

Verified by running

  • Debian 13 under WSL, FFmpeg 7.1.5, GStreamer 1.26.2, libmpv 2.5: 213 unit tests
    and 124 integration tests pass.
  • Windows 11: 213 unit tests and 107 integration tests pass, 15 skip (GStreamer is
    Linux-only). The DirectShow graph (ds-filters, ds-grab on a bottom-up H.264
    file), device enumeration, WASAPI capture, Media Foundation audio recording and
    camera capture were also run by hand after the changes to them.
  • Races are tested in a child process (tests/MediaToolkitNet.CrashProbe), since
    the failures they look for end the process. The player seek race was run 15
    times in a row, the mpv one three.
  • Some integration tests need the ffmpeg program to make their media: it is found
    in MEDIATOOLKITNET_FFMPEG_DIR or on PATH.

Not run: FFmpeg 8 and 9 transcoding, mpv playback, the macOS backend, and the V4L2,
ALSA and PulseAudio paths on Linux, as in 0.3.1.