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

[BUG]: WebRTC app crashes when using remote desktop to access a Windows Server VM #1047

Open
averkis-westland opened this issue Sep 4, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@averkis-westland
Copy link

Package version

3.0.0-pre.6

Environment

* OS: Microsoft Windows Server 2022 Datacenter
* Unity version: 2022.3.26f1

Steps To Reproduce

  1. Run application using WebRTC package streaming to remote client
  2. Disconnect RDP session
  3. Reconnect RDP session
  4. Unity Editor crashes

Current Behavior

Unity Editor crashes when remote desktop connection is opened

Expected Behavior

Unity continues as normal

Anything else?

Running on an AWS g5.xlarge instance.
GPU: Nvidia A10G running Studio Driver Version 560.81
CPU: AMD EPYC 7R32, 2800 Mhz, 2 Core(s), 4 Logical Processor(s)
Crash Logs Stack Trace:

========== OUTPUTTING STACK TRACE ==================

0x00007FFA6A803560 (webrtc) GetUpdateTextureFunc
0x00007FFA6AAD9BF9 (webrtc) GetUpdateTextureFunc
0x00007FFA6AAD939C (webrtc) GetUpdateTextureFunc
0x00007FFA6A7CAD7A (webrtc) GetUpdateTextureFunc
0x00007FFA6AB4143F (webrtc) GetUpdateTextureFunc
0x00007FFA6AB40662 (webrtc) GetUpdateTextureFunc
0x00007FFA6AB3F978 (webrtc) GetUpdateTextureFunc
0x00007FFA6AB38179 (webrtc) GetUpdateTextureFunc
0x00007FFA6A806779 (webrtc) GetUpdateTextureFunc
0x00007FFA6A8A167C (webrtc) GetUpdateTextureFunc
0x00007FFA9C774CB0 (KERNEL32) BaseThreadInitThunk
0x00007FFA9E2FECEB (ntdll) RtlUserThreadStart

========== END OF STACKTRACE ===========

I currently have a second camera in a scene with a script attached that is sending the stream to a SFU server via WHIP. The application works when started and will run indefinitely until I disconnect from the RDP session and reconnect, which causes the Unity Editor to crash.

Here is the code I am using:
using System.Collections;
using System.Collections.Generic;
using Unity.WebRTC;
using UnityEngine;
using UnityEngine.Networking;

[RequireComponent(typeof(AudioListener))]
public class WebRTCPublish : MonoBehaviour
{
    public string whipUrl;
    public Vector2 streamResolution;

    RTCPeerConnection peerConnection;
    MediaStreamTrack videoTrack;
    AudioStreamTrack audioTrack;
    Camera cam;

    void Start()
    {
        StartCoroutine(WebRTC.Update());
        peerConnection = new RTCPeerConnection
        {
            OnIceConnectionChange = state => {
                // TODO add retry logic if connection fails
                Debug.Log("Peer Connection: " + state);
            }
        };
        cam = GetComponent<Camera>();
        videoTrack = cam.CaptureStreamTrack((int)streamResolution.x, (int)streamResolution.y);
        peerConnection.AddTrack(videoTrack);
        AudioListener audioListener = cam.GetComponent<AudioListener>();
        audioTrack = new AudioStreamTrack(audioListener) { Loopback = true };
        peerConnection.AddTrack(audioTrack);
        StartCoroutine(DoWHIP());
    }

    private void OnApplicationQuit()
    {
        peerConnection.Close();
    }

    IEnumerator DoWHIP()
    {
        RTCSessionDescriptionAsyncOperation offer = peerConnection.CreateOffer();
        yield return offer;

        RTCSessionDescription offDesc = offer.Desc;
        RTCSetSessionDescriptionAsyncOperation opLocal = peerConnection.SetLocalDescription(ref offDesc);
        yield return opLocal;

        string filteredSdp = "";
        foreach (string sdpLine in offer.Desc.sdp.Split("\r\n"))
        {
            if (!sdpLine.StartsWith("a=extmap"))
            {
                filteredSdp += sdpLine + "\r\n";
            }
        }


        using (UnityWebRequest www = new UnityWebRequest(whipUrl))
        {
            Debug.Log("Connecting to WHIP server on URL: " + whipUrl);
            www.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.ASCII.GetBytes(filteredSdp));
            www.downloadHandler = new DownloadHandlerBuffer();
            www.method = UnityWebRequest.kHttpVerbPOST;
            www.SetRequestHeader("Content-Type", "application/sdp");
            www.SetRequestHeader("PublishKey", "publish");
            www.SetRequestHeader("StreamName", "unity");
            yield return www.SendWebRequest();

            if (www.result != UnityWebRequest.Result.Success)
            {
                Debug.Log("Request failed:");
                Debug.Log(www.error);
            }
            else
            {
                RTCSessionDescription answer = new RTCSessionDescription { type = RTCSdpType.Answer, sdp = www.downloadHandler.text };
                RTCSetSessionDescriptionAsyncOperation opRemote = peerConnection.SetRemoteDescription(ref answer);
                yield return opRemote;

                if (opRemote.IsError)
                {
                    Debug.Log(opRemote.Error);
                }
            }
        }
    }
}
@averkis-westland averkis-westland added the bug Something isn't working label Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants