Unity-Technologies / Unity-Technologies/com.unity.webrtc

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

Open
#1,047 0 comments 0 reactions 1 assignee View on GitHub

@karasusan is already working on this.

Since Sep 4, 2024.

bug
Dominant language
Assembly
Stars
852
Forks
238
PR merge metrics
No merged PRs in 30d

Description

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);
                }
            }
        }
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.