Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove lock for frame delay cacluation #305

Merged
merged 18 commits into from
Sep 6, 2024
Merged

Conversation

harryz2000
Copy link
Contributor

No description provided.

@harryz2000 harryz2000 changed the title Improve the locking for frame delay cacluation Remove lock for frame delay cacluation Jul 4, 2024
@murillo128
Copy link
Member

I think it would be easier to add a TimeService to the coordinator and call the OnFrame and SetMaxDelayMs on an Async call. The MediaFrameBridgeListener call is already thread safe.

@harryz2000
Copy link
Contributor Author

I think it would be easier to add a TimeService to the coordinator and call the OnFrame and SetMaxDelayMs on an Async call. The MediaFrameBridgeListener call is already thread safe.

Yes. That would solve the thread lock issue. But to me, it seems not ideal as the calculated delay may not be used for current frame. For cases such as receive a batch of audio frames, they may still use a same delay, which might not be smooth.

Although, in practice, it might not be a issue. I am trying to see if it could be solved by another way. Got some progress, will need spend a bit more time to ensure it works.

@harryz2000
Copy link
Contributor Author

This should be a draft full solution for this issue. There is still a test to fix. But it should work.

It calculates the delay in synchronously without lock. On the other hand it reduces latency asynchronously if all frames come early. Now the OnFrame is thread safe.

Yes. It has been a bit harder to implement this. But imho it suits better for our expectation.

@harryz2000 harryz2000 marked this pull request as ready for review July 5, 2024 06:18
@harryz2000
Copy link
Contributor Author

@murillo128 Now all unit tests have passed. I did some manual tests well. It works well to me, so I think it is ready for formal review. Personally, I would prefer this solution. It actually becomes cleaner now: One part is for delay calculation, which is lock-free sync operations. Another part for latency reduction, which uses async calls.

The test is using a TestTimeService which does sync calls for Async(), therefore most of the tests passed. The only test failed and has been fixed is the latency reduction may be delayed by one frame, as we are not reduce the delay immediately. However it shouldn't matter.

src/FrameDelayCalculator.cpp Outdated Show resolved Hide resolved
include/FrameDelayCalculator.h Outdated Show resolved Hide resolved
Copy link
Member

@murillo128 murillo128 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

include/FrameDelayCalculator.h Outdated Show resolved Hide resolved
test/unit/TestFrameDelayCalculator.cpp Outdated Show resolved Hide resolved
src/FrameDelayCalculator.cpp Outdated Show resolved Hide resolved
src/FrameDelayCalculator.cpp Outdated Show resolved Hide resolved
src/FrameDelayCalculator.cpp Outdated Show resolved Hide resolved
src/FrameDelayCalculator.cpp Outdated Show resolved Hide resolved
src/FrameDelayCalculator.cpp Outdated Show resolved Hide resolved
src/FrameDelayCalculator.cpp Outdated Show resolved Hide resolved
src/FrameDelayCalculator.cpp Outdated Show resolved Hide resolved
src/FrameDelayCalculator.cpp Outdated Show resolved Hide resolved
src/FrameDelayCalculator.cpp Outdated Show resolved Hide resolved
src/FrameDelayCalculator.cpp Outdated Show resolved Hide resolved
@harryz2000
Copy link
Contributor Author

@murillo128 Please have a look at this commit: 4ec2a6e
I tweaked the code as currently the FrameArriveInfo would only be changed asynchronously. Also added "State" to clarify the case.

#include <unordered_map>
#include <optional>
#include <chrono>
#include <queue>
#include <algorithm>

class FrameDelayCalculator
class FrameDelayCalculator : public std::enable_shared_from_this<FrameDelayCalculator>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe irrelevant, what's the benefit of inheriting enabled_shared_from_this? i dont see shared_from_this or weak_from_this being used in framedelaycalculator.

Copy link
Contributor Author

@harryz2000 harryz2000 Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do use the weak_from_this() in the implement file. See line 91 in src/FrameDelayCalculator.cpp
timeService.Async([selfWeak = weak_from_this(), early, now, unifiedTs, refTime, refTimestamp,

@harryz2000 harryz2000 merged commit af54657 into master Sep 6, 2024
2 checks passed
@murillo128 murillo128 deleted the hz_lock_improvement branch October 22, 2024 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants