Skip to content

[BUG]: OnMessage Event Not Triggered in Unity WebRTC #952

@jwielebnowski

Description

@jwielebnowski

Package version

3.1.0-exp.6

Environment

* OS: Windows
* Unity version:2022.3.5f1
* Graphics API:
* Browser:Chrome

Steps To Reproduce

I am encountering an issue where the OnMessage event in my Unity WebRTC implementation is not being triggered, despite having properly set up the RTCPeerConnection and data channels. While the OnDataChannel event is firing correctly, and video and audio are streaming as expected, the data channel messages are not being received on the application side. I can confirm in the webrtc-internals that the data channel is opened and the messages are being sent from the client.

Code Snippets:

RTCPeerConnection Initialization

var pc = new RTCPeerConnection();
pc.OnDataChannel = new DelegateOnDataChannel(channel => { OnDataChannel(pc, e.playerId, channel); });

OnDataChannel Event Implementation

void OnDataChannel(RTCPeerConnection pc, int playerId, RTCDataChannel channel) {
    channel.OnMessage = bytes => input.ProcessInput(bytes, playerId, HandleInteractionEvent);
}

RTCDataChannel Constructor

internal RTCDataChannel(IntPtr ptr, RTCPeerConnection peerConnection) 
    : base(ptr) 
{
    WebRTC.Table.Add(self, this);
    WebRTC.Context.DataChannelRegisterOnMessage(self, DataChannelNativeOnMessage);
    WebRTC.Context.DataChannelRegisterOnOpen(self, DataChannelNativeOnOpen);
    WebRTC.Context.DataChannelRegisterOnClose(self, DataChannelNativeOnClose);
}

DataChannelNativeOnMessage Method

[AOT.MonoPInvokeCallback(typeof(DelegateNativeOnMessage))]
static void DataChannelNativeOnMessage(IntPtr ptr, byte[] msg, int len) 
{
    WebRTC.Sync(ptr, () => 
    {
        if (WebRTC.Table[ptr] is RTCDataChannel channel) 
        {
            channel.onMessage?.Invoke(msg);
        }
    });
}

Issue
The DataChannelNativeOnMessage method is being called but it never enters the WebRTC.Sync and channel.onMessage is not invoked

        static void DataChannelNativeOnMessage(IntPtr ptr, byte[] msg, int len)
        {
            try
            {
                WebRTC.Sync(ptr, () =>
                {
                    if (WebRTC.Table[ptr] is RTCDataChannel channel)
                    {
                        channel.onMessage?.Invoke(msg);
                    }
                });
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }

        }```
No exception cought.

**Attempts to Resolve**
I have cross-verified my implementation with your sample code and could not pinpoint any discrepancies that would cause this issue. I looked into your client code and did not see anything special when you create a data channel over there. It looks like something is preventing from WebRTC.Sync to work. No idea yet what.

**Request for Assistance**
I am pretty sure that I am missing a crucial step in my implementation. I would highly appreciate any insights or suggestions you can provide to help identify and rectify the issue.

Thank you,

Jacek


### Current Behavior


### Expected Behavior


### Anything else?

![image](https://github.com/Unity-Technologies/UnityRenderStreaming/assets/8700191/65689c72-f921-4052-8622-dc6b7c3bd9d3)

![image](https://github.com/Unity-Technologies/UnityRenderStreaming/assets/8700191/03da163d-7b22-49e9-b6c8-19e432634bb3)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions