Skip to content

Short (encoding) lag can destroy A/V sync for both monitoring audio and recordings in sub systems (Render Delay filter, NDI, Teleport) #6673

Open

Description

@YorVeX

Operating System Info

Windows 10

Other OS

No response

OBS Studio Version

27.2.4

OBS Studio Version (Other)

No response

OBS Studio Log URL

https://obsproject.com/logs/3WNHLPaQpUstffUB

OBS Studio Crash Log URL

No response

Expected Behavior

Whatever lag occurs, after it is over OBS should be able to go back to normal operation, i.e. synchronized audio/video for both monitoring audio and recordings (especially if buffers were not permanently exceeded), and any Sync Offset or Render Delay filters should still be correctly applied.

Current Behavior

When keeping on restarting OBS and repeating the lag test, any of these will happen randomly:

  • A/V desync only for monitoring/preview but the recording is fine
  • A/V desync only for the recording but monitoring/preview is fine
  • A/V desync for both recording and monitoring/preview
  • the synchronization immediately recovers after the lags end and both monitoring/preview and recording are fine
  • sometimes a desync monitoring/preview will slowly recover within the next 20 seconds, but sometimes not even after 30+ seconds and it looks like it will never - and A/V sync in the recording will never recover, not even in cases where monitoring/preview recovers within 20 seconds

Steps to Reproduce

  1. Run a video loop suitable for testing A/V sync such as this based on either Media source or VLC source
  2. Configure a Render Delay filter of 500 ms for that video loop
  3. Configure a Sync Offset of 500 ms for that video loop
  4. Run a recording and do something that causes a lag in OBS, e.g. toggle visibility of an Image source a few times that has the "Unload image when not showing" option enabled and loads a rather big image like this one
  5. Check the audio monitoring vs. preview sync and also the recording.
    As stated above repeat the test with OBS restarts between test runs to see the various results, as the behavior is inconsistent.

This is the capture of such a test session showing how monitoring audio vs. preview becomes desync and and this is the recording that was produced by the OBS instance that is seen there showing that also the recorded video file is desync.

Anything else we should know?

What initially brought me to this is investigating A/V desyncs that I had with NDI. I always thought the NDI plugin was at fault. Then Teleport was released, I switched and was confused to see that it suffers from exactly the same issues, so I started to investigate further. My test scenario initially was between two OBS instances on the same PC transferring video and audio to each other by both Teleport and NDI. And surely lags on the sender system (also produced by the Image source) would cause A/V desync for both the NDI and Teleport streams and recordings on the receiving OBS instance.
To make matters worse, if a chain of several OBS instances is used each of them having a small hiccup will cause the whole chain to be desync, because the desync will transfer over NDI (the same for the newer Teleport plugin) through the next instances.

It seems that when lag occurs OBS somehow compensates that lag for itself but doesn't enable any sub system like the Teleport/NDI plugin or filters like Render Delay to do the same (e.g. by not giving them necessary information like corrected timestamps?).

Since the scenario with the Render Delay filter is simpler I decided to open the issue for this. I would assume that any fix for this would also fix the Teleport/NDI issues, if it turns out to be wrong I could still open a follow-up for this.

Bust just in case, here's videos showing these scenarios too:

Note that this really is not about the Image source causing lags, although maybe an issue of its own should be opened for this, why can't it load images asynchronously? But that's not the point here, lag can also be caused by a Game Capture source when switching between full-screen game and desktop (especially when they have different screen resolutions) or is randomly caused by the Media source or the system has a short load peak. My scenario was tested on a Ryzen 5800X that never went above 12% even during causing the lags. Saying that lag should just always be prevented is simply not realistic, it can occur on the best systems and as shown can be caused by OBS Studio's very own standard sources. Especially during an hour-long streaming or recording session it's almost impossible to never have any hiccup. Therefore OBS should should always be able to recover after lags including any plugins.

Activity

  1. jp9000 commented on Jul 1, 2022

    @jp9000
    Member

    I'll be introducing a change in the next patch that should help alleviate this issue.

  2. YorVeX commented on Jul 1, 2022

    @YorVeX
    Author

    I'll be introducing a change in the next patch that should help alleviate this issue.

    @jp9000 You're talking about this, right? I was hoping that this is something that helps with this issue, thanks for your confirmation. As soon as there is a frontend/GUI to enable fixed audio buffering (I am not smart enough to figure out at which places I'd need to change involved variables to enable it just from code) I will run new tests with it.

  3. YorVeX commented on Jul 3, 2022

    @YorVeX
    Author

    Someone who unlike me was smart enough added a GUI to configure fixed audio buffering and the audio buffer size and compiled the current master branch for me so I was able to do some first tests.

    Leaving it set to dynamic and playing with the buffer size didn't improve anything, whether I set it to 200, 1000, 2000 or 10000 ms, any lag will still cause A/V desync with my scenario described here (although it does have lots of other effects e.g. on when the max audio buffer is reached and whether it happens at all anymore...).

    But setting the audio buffer to fixed does actually fix the problem presented above at least for the recording. No amount of lag I produce will cause A/V desync in it anymore. Unfortunately the monitoring audio still desyncs from the preview. Maybe that simply is a separate issue, something like described in #4531 and by causing lag I just trigger that immediately? Maybe the main problem is that monitoring audio just has it's own buffering mechanism which is working independently from everything else, but I am just guessing here.

    Unfortunately the bad news (at least for me) don't end there. Because as soon as I turn on fixed audio buffering in a scenario with transmission of the feed between a sender and receiver OBS both NDI and Teleport suffer from the problem that occasionally (even right after a fresh OBS start) the sound received in OBS will be distorted. And the worst thing: the A/V desync after lags still occurs even with fixed audio buffering in this scenario. This recording shows both the audio distortion and the desync after lags were produced.
    I know you said "alleviate" and not "fix", but TBH I was still a bit disappointed to see that it really fixes the simple scenario I described here in this issue but makes the scenario that is actually more important to me (with transmission of the feed between OBS instances) even worse 😞

    On the bright side the Teleport developer created an experimental build of the Teleport plugin where he attempted to improve timestamp handling and he was very successful at that. Because with this build even on OBS 27.2.4 with dynamic buffering lag doesn't kill the A/V sync anymore. So right now at least for me personally that is the better option, although it's a bit disappointing as I said, because I was hoping that with the combo of fixed audio buffering and the new Teleport plugin finally the A/V sync stuff would become very stable.

  4. jp9000 commented on Jul 3, 2022

    @jp9000
    Member

    If the fixed audio buffering commit at a low latency doesn't help with some specific problem, then that problem probably wasn't caused by audio buffering to begin with.

    If you have an issue with audio monitoring having a bit of desync from the preview over time, that is indeed a separate issue. I've actually encountered that problem myself, and I tested that pretty extensively; my assessment is that the issue isn't with obs (and I'm not saying that lightly), at least on Windows. I'm fairly sure that the problem is that there's some sort of buffering being built up on the WASAPI side, after the audio data has been submitted to the audio output API. Or at least that's my best assessment based upon (pretty extensive) testing. I attempted to implement the low-latency WASAPI API but I ran into some issues with it, and unfortunately didn't really have time to work on it. Resetting it always seems to alleviate it, so as a temporary measure it might be possible to just make it programmatically reset every couple minutes or something until a more thorough solution has been found.

    As for glitches in low-latency audio buffering mode, lowering the maximum audio buffering latency is going to introduce potential for audio glitches. That's kind of the point of having buffering in the first place; if something has to stall and an audio segment becomes unexpectedly delayed, normally buffering would be able to prevent that by being able to insert that audio back in time via buffering to ensure the audio data doesn't get cut off.

    In the first place, this whole issue is mostly because I'm pretty sure NDI/etc probably can't really be designed to respect timestamps if its being used in a low-latency manner. However, those timestamps are required for a synchronized playback when buffering exists. If something is not designed to rely on timestamps and cannot buffer with your client/server setup, then inevitably any buffering OBS difference between audio/video will then create desynchronization, because those timestamps themselves are the only way to synchronize when audio buffering is enabled in OBS. Those timestamps are the key. But a low-latency-only setup is incompatible with buffering, and that's why I added a low-latency audio buffering mode.

    But again, in a low latency environment, any data that exceeds that low latency requirement will be removed, possibly cutting off data and causing playback glitches. So basically, you either design your setup around low latency and risk playback glitches, or you design your setup around buffering, and you risk delay. There's just no in between. And depending on the configuration, you can't mix the two in a pipeline.

    So if you experience glitches in audio in low-latency audio mode, that's simply a consequence of data that exceeds that maximum latency being discarded.

    I hope some of this makes sense. I don't know what's causing encoding lag for you, but in conjunction with low latency, the first order of business would be to investigate what's causing that encoder lag first and foremost and try to solve that first and foremost.

  5. YorVeX commented on Jul 3, 2022

    @YorVeX
    Author

    Monitoring audio

    I don't have a problem with monitoring buffer becoming desync over time, I create that desync a few seconds after a fresh OBS start by nothing more than loading an image with the Image source. I was just thinking that it still might be related with the "desync over time" issue but maybe I am simply wrong, I rather start to think this is an entirely different thing and maybe mentioning that caused the confusion, so, my bad 😛
    Also the important part: this only happens when I set a Sync Offset and a Render Delay in OBS, which makes me doubt that this would be related to Windows audio handling in general or WASAPI specifically- it doesn't happen with Sync Offset 0 and no Render Delay filter, so only very OBS specific mechanisms trigger this issue.

    • With Render Delay filter on the video loop and Sync Offset set to 0 ms loading an image does not cause any problems: video / log.
    • With Render Delay filter on the video loop and Sync Offset set to 500 ms loading an image does cause problems: video / log.
      (Videos created with fixed audio buffering enabled and a buffer of 1000 ms as should be visible in the logs)

    Fun fact visible from the video that shows the issue (with 500 ms artifical delay):

    • before the image is loaded the audio meter is not in sync with the actual sound, but the sound is in sync with the preview
    • after the image is loaded (short lag produced) the sound is no longer in sync with the preview, but now the audio meter is in sync with the actual sound

    Glitches in low-latency audio buffering mode

    the first order of business would be to investigate what's causing that encoder lag first

    I have already given examples what's causing it for me, e.g.:

    • loading an image with an Image source
    • having an active Game Capture source with the game captured being switched into full-screen or back from full-screen to desktop

    How am I supposed to avoid that? Even if the behavior of these sources in OBS would be improved, as I said, the best systems can still have a small hiccup within several hours of recording or streaming. Of course I agree that this is (regardless of this issue) always the first thing to be worked on, but I can see that even the biggest national TV news stations sometimes have a hiccup. Fortunately afterwards their audio and video is not desync for the following hours 😉

    The examples above don't even cause the audio glitches, that was just my answer to "what causes encoding lags". The only thing that has caused the glitches to me so far was: starting OBS. Either the glitches are there right after startup or they will also not appear later (at least I haven't found a test scenario yet that cause them later on). Again, a bit hard to avoid. 😐
    I rather believe this is some kind of race condition or timing issue, e.g. when OBS is loading NDI or Teleport already while it didn't finish initializing the audio buffer yet?

  6. Kiran-GC commented on Sep 6, 2023

    @Kiran-GC

    Was this fixed? Cause im still facing the same issue

  7. YorVeX commented on Sep 6, 2023

    @YorVeX
    Author

    Some things have improved a bit, but fixed, no, not really. I think the most relevant issue to track this is #4531 - specifically this comment will probably be interesting for you and also the next one from Fenrir.

  8. Kiran-GC commented on Sep 7, 2023

    @Kiran-GC

    I guess I'll have to wait for it to get fixed. For now I'm using NDIs Dedicated audio filter to get the audio in sync with video

  9. YorVeX commented on Sep 7, 2023

    @YorVeX
    Author

    I guess I'll have to wait for it to get fixed. For now I'm using NDIs Dedicated audio filter to get the audio in sync with video

    If that works, then what exactly doesn't, and why would you want to switch? I did all my tests with combined A/V data and learned that many of the delay/sync issues (but not all) arise from how OBS tries to keep them in sync, especially the part where OBS increases the overall delay of a feed (but still with proper A/V sync) over time or when a lag occurs, whereas these sync mechanisms shouldn't apply to feeds that only contain video or audio separately - but it's still on my TODO list to actually make more experiments with that, so any insights are very welcome.

  10. Kiran-GC commented on Sep 8, 2023

    @Kiran-GC

    I guess I'll have to wait for it to get fixed. For now I'm using NDIs Dedicated audio filter to get the audio in sync with video

    If that works, then what exactly doesn't, and why would you want to switch?

    Yeah when it's just a single audio source like a mic I actually don't need to change anything. But I'm using obs to conduct animated classes for students ( I'm an employee who work at an edutech company) So if its just a mic adding a filter seems more than enough for me. But some animations have sound alongside it so I'll have to give filter to every animation I have to present on every single class which seems hectic. So if this gets fixed i could just not worry about adding filter to every audio source and just use the main source from ndi

  11. YorVeX commented on Sep 9, 2023

    @YorVeX
    Author

    I guess I'll have to wait for it to get fixed. For now I'm using NDIs Dedicated audio filter to get the audio in sync with video

    If that works, then what exactly doesn't, and why would you want to switch?

    Yeah when it's just a single audio source like a mic I actually don't need to change anything. But I'm using obs to conduct animated classes for students ( I'm an employee who work at an edutech company) So if its just a mic adding a filter seems more than enough for me. But some animations have sound alongside it so I'll have to give filter to every animation I have to present on every single class which seems hectic. So if this gets fixed i could just not worry about adding filter to every audio source and just use the main source from ndi

    I have a hard time imagining your setup from this description alone and also don't want to steer away from the scope of this issue too much with this discussion, but if you don't have a hard requirement to use NDI and can work with transmitting the full output of an OBS instance (as opposed to just a source using a filter) you could try my Beam plugin for the OBS to OBS transmission. Beside overall better desync stability it also has a fixed render delay mode where it uses a buffer to compensate any changes in delay from OBS to guarantee a stable final total delay.
    image
    The obvious downside is that for creating the headroom of this compensation mechanic you need to add some artificial delay that you also need to consider for all sources that you're using in addition (e.g. separate mic sources). With 1000 ms you should be fine, 500 ms works if you're lucky.

    But it has no NDI compatibility, you need to use it for both the sender and receiver side. And filters (= single source transmissions) are not supported yet, though I am working on that right now and try to release it as soon as possible. If you want to try and got any questions feel free to pop in the discussion forum there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions