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

Fix SimulcastConsumer fails to switch spatial layers #993

Closed
wants to merge 3 commits into from

Conversation

satoren
Copy link
Contributor

@satoren satoren commented Jan 27, 2023

Fix #990

#990 (comment)

It appears #825 was not fully fixed.

@satoren satoren changed the title fix 990 Fix SimulcastConsumer fails to switch spatial layers Jan 27, 2023
@ibc
Copy link
Member

ibc commented Jan 27, 2023

Is it still a draft PR? or can we review it already?

@ibc ibc requested review from ibc and jmillan January 27, 2023 13:50
@satoren
Copy link
Contributor Author

satoren commented Jan 27, 2023

@ibc
I suspect that the CI error that is occurring #992 will also occur here, so I will make ready after that is resolved.

@satoren
Copy link
Contributor Author

satoren commented Jan 27, 2023

It's already late in my time zone, so tomorrow

@jmillan
Copy link
Member

jmillan commented Jan 27, 2023

I believe the one below is a more concise and less error prone fix:

This way we are avoiding the buggy scenario by only setting the syncRequired and spatialLayerToSync variables for the packet that will actually sync the stream.

Before this change we would set those flags for a non keyframe packet (even if it belonged to a target spatial layer) so the flags would remain set and the packet would be dropped.

This solution is more consistent and robust.

Nice catch @satoren!

diff --git a/worker/src/RTC/SimulcastConsumer.cpp b/worker/src/RTC/SimulcastConsumer.cpp
index 100f86c8d..70bd13012 100644
--- a/worker/src/RTC/SimulcastConsumer.cpp
+++ b/worker/src/RTC/SimulcastConsumer.cpp
@@ -660,12 +660,14 @@ namespace RTC

                // Check whether this is the packet we are waiting for in order to update
                // the current spatial layer.
-               if (this->currentSpatialLayer != this->targetSpatialLayer && spatialLayer == this->targetSpatialLayer)
+               // clang-format off
+               if (
+                       this->currentSpatialLayer != this->targetSpatialLayer &&
+                       spatialLayer == this->targetSpatialLayer &&
+                       packet->IsKeyFrame()
+               )
+               // clang-format on
                {
-                       // Ignore if not a key frame.
-                       if (!packet->IsKeyFrame())
-                               return;
-
                        shouldSwitchCurrentSpatialLayer = true;

                        // Need to resync the stream.

@jmillan
Copy link
Member

jmillan commented Jan 27, 2023

@satoren, could you please try it whenever suits you?

@satoren satoren force-pushed the fix_990 branch 3 times, most recently from a60535f to aa9645a Compare January 28, 2023 01:36
@satoren
Copy link
Contributor Author

satoren commented Jan 28, 2023

@jmillan Thank you. Seems to be working fine.

@satoren satoren marked this pull request as ready for review January 28, 2023 01:42
Copy link
Member

@ibc ibc left a comment

Choose a reason for hiding this comment

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

Guys I'm looking at the changes and rationale and I don't understand which behavior changes here or how it fixes the problem. I'm not saying it's wrong. May you please explain the scenario those changes fix?

@jmillan
Copy link
Member

jmillan commented Jan 30, 2023

May you please explain the scenario those changes fix?

It's explained here

@jmillan
Copy link
Member

jmillan commented Jan 30, 2023

May you please explain the scenario those changes fix?

Definitely my patch is totally a placebo. It changes nothing.., it just move a condition up to the parent conditional statement. What was I thinking about...

@ibc
Copy link
Member

ibc commented Jan 30, 2023

May you please explain the scenario those changes fix?

Definitely my patch is totally a placebo. It changes nothing.., it just move a condition up to the parent conditional statement. What was I thinking about...

Let' see:

Scenario 1 (non keyframe received)

  • currentSpatialLayer is 1
  • targetSpatialLayer is 2
  • spatial layer of received packet is 2
  • received packet is not keyframe

In v3:

  1. We enter condition 1 block.
  2. Since it's not a key frame (immediate condition within the block), we return early.
  3. So no flag was changed and packet was discarded.

In this PR:

  1. We don't enter condition 1 block (since it's not a keyframe).
  2. We enter condition 2 block (since spatial layer of the packet is not the same as current spatial layer), so we return early.
  3. So no flag was changed and packet was discarded.

So no changes in Scenario 1 between v3 and this PR.

Scenario 2 (keyframe received)

  • currentSpatialLayer is 1
  • targetSpatialLayer is 2
  • spatial layer of received packet is 2
  • received packet is keyframe

In v3:

  1. We enter condition 1 block.
  2. It's a key frame (immediate condition within the block).
  3. So we change flags and keep processing the packet.

In this PR:

  1. We enter condition 1 block.
  2. So we change flags and keep processing the packet.

So no changes in Scenario 2 between v3 and this PR.

Scenario 3 (non keyframe received II)

  • currentSpatialLayer is 1
  • targetSpatialLayer is 2
  • spatial layer of received packet is 1
  • received packet is not keyframe

In v3:

  1. We don't enter condition 1 block.
  2. We don't enter condition 2 block (since spatial layer of the packet matches currentSpatialLayer).
  3. So we don't change flags and keep processing the packet.

In this PR:

  1. We don't enter condition 1 block (since it's not a keyframe).
  2. We don't enter condition 2 block (since spatial layer of the packet matches currentSpatialLayer).
  3. So we don't change flags and keep processing the packet.

So no changes in Scenario 3 between v3 and this PR.

Conclusion

Unless I miss some scenario, I don't see how changes in this PR can change anything.

@ibc
Copy link
Member

ibc commented Jan 31, 2023

Let's close this PR since, as proven above, it cannot fix anything. Alternative PR here #999

@ibc ibc closed this Jan 31, 2023
@satoren satoren deleted the fix_990 branch January 31, 2023 23:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Consumer video freezes when setPreferredLayers is executed continuously
3 participants