Unity-Technologies / UnityRenderStreaming

Streaming server for Unity
Other
1.31k stars 365 forks source link

[BUG]: Screen is flipped vertically and Datachannel dead when close up one of multi-tabs #930

Open lyclyc171 opened 1 year ago

lyclyc171 commented 1 year ago

Package version

3.1.0-exp.6

Environment

* OS: Debian 11
* Unity version:2022.2.2f1c1
* Graphics API: OpenGL
* Browser: FireFox

Steps To Reproduce

  1. Running broadcast sample on debian 11
  2. Open multiple tabs(>1) on browser
  3. Close one of tabs

Current Behavior

  1. Observing screen is flipped vertically
  2. Do not receive any mouse and keyboard event

Expected Behavior

screen and user interface all going well

Anything else?

Actually I have find the reason by debugging. BroadCast.Disconnect() will be called when close a tab, then following functions RemoveSender and RemoveChannel are called subsequently.

private void Disconnect(string connectionId)
{
    if (!connectionIds.Contains(connectionId))
        return;
    connectionIds.Remove(connectionId);

    foreach (var sender in streams.OfType<IStreamSender>())
    {
        RemoveSender(connectionId, sender);  // Cause problem 1
    }
    foreach (var receiver in streams.OfType<IStreamReceiver>())
    {
        RemoveReceiver(connectionId, receiver);
    }
    foreach (var channel in streams.OfType<IDataChannel>())
    {
        RemoveChannel(connectionId, channel);   // Cause problem 2
    }
}
  1. RemoveSender lead to call Dispose() in VideoStreamSourceScreen, then mscreenCopyTexture will be destroyed image which is the reference to sourceTexture in VideoTrackSource image so the sourceTexture_ is null and Graphics.Blit will copy a correct screen texture to upside down.

  2. As for RemoveChannel, I think there is already a commit to fix it: https://github.com/Unity-Technologies/UnityRenderStreaming/pull/925

JohnnyNgo commented 1 year ago

@lyclyc171 yeah i had this problem long time ago, and i think it was because of this RemoveSender method in SignalingHandlerBase which was probably removing the track for that one user, then recreating a new track which wrongfully flips the texture again for the other users. So I commented it out: image and i also updated the SetTransceiver method in StreamSenderBase too image Now, this might get things working for you, but I'm not fully sure if I'm fully cleaning up everything memory-wise, so insights here would be appreciated.

karasusan commented 1 year ago

@lyclyc171 How about the google chrome?

karasusan commented 1 year ago

@JohnnyNgo Is this issue already fixed by #925 ?

JohnnyNgo commented 1 year ago

@karasusan I'm using Render Streaming 3.1.0-exp.6 for reference, so I don't think I have tried that solution. I never had the issue of inputs or resolution changes as mentioned in #925, probably because I don't use Broadcast.cs (I made my own custom script to handle removing/adding senders/channels to each users)

So I'd say my solution here is more specific to the Vertical Flipping part and is not already fixed.

lyclyc171 commented 1 year ago

@lyclyc171 How about the google chrome?

No, it doesn't work

lyclyc171 commented 1 year ago

@lyclyc171 yeah i had this problem long time ago, and i think it was because of this RemoveSender method in SignalingHandlerBase which was probably removing the track for that one user, then recreating a new track which wrongfully flips the texture again for the other users. So I commented it out: image and i also updated the SetTransceiver method in StreamSenderBase too image Now, this might get things working for you, but I'm not fully sure if I'm fully cleaning up everything memory-wise, so insights here would be appreciated.

@JohnnyNgo Does the screen image turn to normal after you comment out "RemoveTrack"? Actually my debugging result tell me "OnStroppedStream" ( in your second pic ) is the reason who calls Dispose().

Anyway, thanks for your clue, I will try to comment RemoveTrack out to check result.

karasusan commented 1 year ago

memo: URS-624

RaoulBickmann commented 1 year ago

Hey I have a similar issue in 3.1.0-exp.7. When I have multiple tabs open and close one the image for the others is distorted.
Just changing the SetTransceiver method to how @JohnnyNgo commented seems to fix it for me. (Or just never calling RemoveSender to begin with) Are there already plans to implement this or sth similar @karasusan and if yes do you know when this might happen?