Unity-Technologies / com.unity.webrtc

WebRTC package for Unity
Other
780 stars 198 forks source link

[BUG]: Client side is not receiving Video Frames #1072

Open nihalar opened 2 months ago

nihalar commented 2 months ago

Package version

3.0.0-pre.7

Environment

* OS:Windows 11
* Unity version: 2023.2

Steps To Reproduce

I have both server and client running in unity. The server creates a Video Stream like this:

    _peerConnection = new RTCPeerConnection(ref config);
    _peerConnection.OnIceCandidate += OnIceCandidate;
    _peerConnection.OnIceConnectionChange += OnIceConnectionChange;
    _peerConnection.OnConnectionStateChange += OnConnectionStateChange;

    _localVideoTrack = _mainCamera.CaptureStreamTrack(1280,720);
    var sendStream = new MediaStream();
    _peerConnection.AddTrack(_localVideoTrack, sendStream);

The client tries to read the video stream. But only the ontrack is called only once. var config = GetRTCConfiguration(); _peerConnection = new RTCPeerConnection(ref config); _peerConnection.OnIceCandidate += OnIceCandidate; _peerConnection.OnConnectionStateChange += OnConnectionStateChange; //_peerConnection.OnTrack += OnTrack;

    _peerConnection.OnTrack = e =>
    {
        if (e.Track is VideoStreamTrack track)
        {
            track.OnVideoReceived += tex =>
            {
                Debug.Log("Video Received");
                _receiveImage.texture = tex;
            };
        }
    };

"Video Received" Log is printed just once. I was expecting it to print every frame. Can someone direct me the problem?

Current Behavior

No response

Expected Behavior

No response

Anything else?

No response

KarolisKrakys commented 1 month ago

Were you able to figure this out? @nihalar

KarolisKrakys commented 1 month ago

Were you able to figure this out? @nihalar

Hey, I was able to figure this out. I'm not sure why but I had the same issue where in the console it only logs that I receive one frame, but I was actually receiving all of the frames. The issue was that I was pointing my camera at the wrong side of the 3D Object so I couldn't see the feed, had to rotate my 3d object by 180 degrees and it fixed it lol... hopefully you have the same issue

AlexRadacki commented 1 month ago

I'm not sure, but it may be possible that you need to call WebRTC.Update inside the OnTrack event. Try this:

  1. Create a boolean to check if the coroutine is already running (videoUpdateStarted)
  2. Modify your OnTrack like this:
    _peerConnection.OnTrack = e =>
    {
        if (e.Track is VideoStreamTrack track)
        {
            track.OnVideoReceived += tex =>
            {
                Debug.Log("Video Received");
                _receiveImage.texture = tex;
            };
           if (!videoUpdateStarted)
       {
        StartCoroutine(WebRTC.Update());
        videoUpdateStarted = true;
       }
        }
    };