Skip to content

Bug fix | Race on initial publishing causing mute state to be desync#3622

Closed
BillCarsonFr wants to merge 3 commits intolivekitfrom
fix/bug_publishing_muted_on_start
Closed

Bug fix | Race on initial publishing causing mute state to be desync#3622
BillCarsonFr wants to merge 3 commits intolivekitfrom
fix/bug_publishing_muted_on_start

Conversation

@BillCarsonFr
Copy link
Copy Markdown
Member

No description provided.

Copy link
Copy Markdown
Contributor

@toger5 toger5 left a comment

Choose a reason for hiding this comment

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

I think this is the main place we need to use mute/unmute?

**
   * Observe changes in the mute states and update the LiveKit room accordingly.
   * @param scope
   * @private
   */
  private observeMuteStates(scope: ObservableScope): void {
    const lkRoom = this.connection.livekitRoom;
    this.muteStates.audio.setHandler(async (desired) => {
      try {
        await lkRoom.localParticipant.setMicrophoneEnabled(desired);
      } catch (e) {
        this.logger.error("Failed to update LiveKit audio input mute state", e);
      }
      return lkRoom.localParticipant.isMicrophoneEnabled;
    });
    this.muteStates.video.setHandler(async (desired) => {
      try {
        await lkRoom.localParticipant.setCameraEnabled(desired);
      } catch (e) {
        this.logger.error("Failed to update LiveKit video input mute state", e);
      }
      return lkRoom.localParticipant.isCameraEnabled;
    });
  }

Comment on lines +42 to +48
let scope: ObservableScope;

beforeEach(() => {
scope = new ObservableScope();
});

afterEach(() => scope.end());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You might also find the testScope utility to be convenient

@BillCarsonFr
Copy link
Copy Markdown
Member Author

I think this is the main place we need to use mute/unmute?

I wonder if I don't prefer that we always use the high level API, it is doing quite a bunch of things.
But given the current high level API we cannot create without publishing. How much of a performance gain it is?

Copy link
Copy Markdown
Contributor

@toger5 toger5 left a comment

Choose a reason for hiding this comment

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

Our race is:

  • publishTrack (await lkRoom.localParticipant.publishTrack(track))
  • enableAudio

might be called at the same time once at the mute state handler and once in the createAndSetupTracks method.

Is this race also fixed by this PR?

this.logger.error("Failed to create tracks", error);
});
}
// TODO why throw here? should we just do nothing?
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes I agree.

If we do the conditional check in here anyways the throw motivates/requests, that we do an additional check to never call this method if both are false. So the throw requests us to do it twice which is odd.

Removing the throw and adding a docstring to the function:
This function will no-op in case both (audio and video) are not enabled (muted)
seems like the way to go. We might still want to log in case we end up not creating tracks that could help us debug that case.

// or only use the low level create/publish APIs and have our own pending publication protection.
// Maybe we could change the livekit api to pre-load tracks without publishing them yet?
// Are we sure this is needed at all? What are the gains?
let isEnabled: boolean;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this should probably be request_enabled or should_enable


if (!isEnabled) {
// TODO should we also drop it?
// I believe the high-level LiveKit APIs will recreate a track?
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That definitly needs checking. what does enableVideo/enabledAudio do if there is a muted track?

  • unmute
  • create new track

@toger5
Copy link
Copy Markdown
Contributor

toger5 commented Jan 13, 2026

@BillCarsonFr What is the status of this one. ha this be fixed somehwere else?

@BillCarsonFr
Copy link
Copy Markdown
Member Author

@BillCarsonFr What is the status of this one. ha this be fixed somehwere else?

We can close, we are using the highlevel API now #3632

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