qtmux: Wrong trun offset written for fragmented MP4 files
I've been trying to get GStreamer to generate fragmented MP4 files for some time but no matter what I tried, the files were always corrupt, so I decided to get to the bottom of this issue, and I believe I found it.
The problem is caused by this line: https://gitlab.freedesktop.org/gstreamer/gst-plugins-good/-/blob/242f3cae6da748ac128e86b5cadcd406fa61aff6/gst/isomp4/gstqtmux.c#L4412
I think it is wrong in two different ways:
-
size
is not the correct variable to use here, I think it contains the maximum size of thedata
buffer, not the actual used bytes. I believe the correct variable would beoffset
, as that is zeroed before the call toatom_moof_copy_data
and contains the number of bytes copied. - The
size + 8
is not sufficient, sinceatom_trun_set_offset
adds another 4 bytes due to adding an optional u32 field. This should be eitheroffset + 12
, or a dummy value should be written before callingatom_moof_copy_data
(e.g.atom_trun_set_offset(first_trun, 0)
) to reserve the bytes for the data offset field.
Does any of this make sense? If so, I could probably open a merge request to fix this bug, though it is only 1 or 2 lines that need to be changed.
With the fix applied, the MP4 files are playable and look fine.