Trust absolute timestamps in linux audio/video sources #11108
Replies: 5 comments 1 reply
-
Sure but you would need to verify it is impossible to send invalid timestamps, as such all timestamps must not ever originate in devices (or untrusted code) in any case. I don't think this is actually true and that v4l2 and pulseaudio probably have options to trust devices. If that is the case then we must continue not to trust timestamps which may be garbage but just so happen not to be garbage for you. |
Beta Was this translation helpful? Give feedback.
-
What do you mean about untrusted code? In v4l2 and pulseaudio I think you can ask in what clock the timestamps are. You would have to make sure it's in the same clock as the os_gettime_ns(), which is the usual monotone clock afaik. That's what the linux drivers will usually provide, additional to more timestamps if required. |
Beta Was this translation helpful? Give feedback.
-
The only timestamps we can trust have to be in kernel monotonic clock, sure. Any other clock would require synchronisation. |
Beta Was this translation helpful? Give feedback.
-
Hm maybe it's not so easy to get the monotonic kernel timestamps from the pulseaudio driver. The alsa driver provides them, but not pulseaudio. I'm checking in my system with linux-v4l2 and linux-pulseaudio, and it seems that for v4l2 I have a timestamp-to-systemtime of 32ms, and for pulseaudio a latency between 10 and 30ms. I'm struggling to understand why I need to set a 200ms sync_offset to the audio stream, to get more or less synchronous AV in obs. |
Beta Was this translation helpful? Give feedback.
-
It seems I had enabled "Low latency audio" in settings; disabling this, pulseaudio recording latency went down from the ~10-30ms to quite steady 9ms. |
Beta Was this translation helpful? Give feedback.
-
OBS is structured towards synchronising audio/video based on relative timestamps, instead of absolute timestamps.
For example, linux-v4l2 changes all frame timestamps relative to first frame:
obs-studio/plugins/linux-v4l2/v4l2-input.c
Lines 267 to 270 in 1451554
Also, linux-pulseaudio ignores the timestamp set in pulseaudio frames, and picks the cpu timestamp at frame reception time, with a hardcoded offset:
obs-studio/plugins/linux-pulseaudio/pulse-input.c
Lines 212 to 215 in 1451554
The sequential startup of drivers makes all relative timestamps to be offset between them. For example I have to delay about 200ms my audio, to match video. Then, due to timestamps at reception time, OBS stills needs to figure out how to synchronise all them without common clock (
obs-studio/libobs/obs-source.c
Lines 1556 to 1571 in 1451554
But the fact is that the both v4l2 and pulseaudio timestamp audio and video frames with good absolute CPU timestamps set at interrupt time, that could be perfectly used for proper av sync without any effort
I think that it would be nice if the obs_source_frame allowed a flag that said "timestamp is in absolute time" (in os_gettime_ns() clock), so drivers can provide timestamps in absolute time. That would solve many AV-desync problems and it's trivial to implement I think. At least for v4l2+pulseaudio.
Would any of that be welcome?
Beta Was this translation helpful? Give feedback.
All reactions