コード例 #1
0
 private RTCPeerConnection(IJSRuntime jsRuntime, JsObjectRef jsObjectRef, RTCConfiguration rtcConfiguration)
     : base(jsRuntime, jsObjectRef)
 {
     AddNativeEventListener("connectionstatechange", (s, e) => OnConnectionStateChanged?.Invoke(s, e));
     AddNativeEventListenerForObjectRef("datachannel", (s, e) => OnDataChannel?.Invoke(s, e),
                                        RTCDataChannelEvent.Create);
     AddNativeEventListenerForObjectRef("icecandidate", (s, e) => OnIceCandidate?.Invoke(s, e),
                                        RTCPeerConnectionIceEvent.Create);
     AddNativeEventListener("iceconnectionstatechange", (s, e) => OnIceConnectionStateChange?.Invoke(s, e));
     AddNativeEventListener("icegatheringstatechange", (s, e) => OnIceGatheringStateChange?.Invoke(s, e));
     AddNativeEventListener("negotiationneeded", (s, e) => OnNegotiationNeeded?.Invoke(s, e));
     AddNativeEventListener("signallingstatechange", (s, e) => OnSignallingStateChange?.Invoke(s, e));
     AddNativeEventListenerForObjectRef("track", (s, e) => OnTrack?.Invoke(s, e),
                                        RTCTrackEvent.Create);
 }
コード例 #2
0
 public void DidOpenDataChannel(Webrtc.RTCPeerConnection peerConnection, Webrtc.RTCDataChannel dataChannel)
 {
     OnDataChannel?.Invoke(this, RTCDataChannelEvent.Create(dataChannel));
 }
コード例 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:WebAssembly.Net.WebSockets.ClientWebSocket"/> class.
        /// </summary>
        public RTCPeerConnection()
        {
            state = created;
            cts   = new CancellationTokenSource();

            innerRtcPeerConnection = new HostObject("RTCPeerConnection");

            onIcecandidate = new Action <JSObject>((e) =>
            {
                //var json = e.Invoke("toJSON");
                var c = e.GetObjectProperty("candidate") as JSObject;
                RTCIceCandidate iceCandidate = null;
                if (c != null)
                {
                    iceCandidate               = new RTCIceCandidate();
                    iceCandidate.candidate     = c?.GetObjectProperty("candidate") as string;
                    iceCandidate.sdpMid        = c?.GetObjectProperty("sdpMid") as string;
                    iceCandidate.sdpMLineIndex = (int)c?.GetObjectProperty("sdpMLineIndex");
                }
                OnIceCandidate?.Invoke(this, new RTCIceCandidateInit()
                {
                    candidate = iceCandidate
                });
                e.Dispose();
            });

            // Attach the onError callback
            innerRtcPeerConnection.SetObjectProperty("onicecandidate", onIcecandidate);

            // Setup the onOpen callback
            onDataChannel = new Action <JSObject>((evt) =>
            {
                var channel = (JSObject)evt.GetObjectProperty("channel");
                var dc      = new RTCDataChannel(channel);
                OnDataChannel?.Invoke(this, dc);
                evt.Dispose();
            });

            // Attach the onOpen callback
            innerRtcPeerConnection.SetObjectProperty("ondatachannel", onDataChannel);

            onNegotiationNeeded = new Action <JSObject>((e) =>
            {
                OnNegotiationNeeded?.Invoke(this, EventArgs.Empty);
                e.Dispose();
            });

            // Attach the onError callback
            innerRtcPeerConnection.SetObjectProperty("onnegotiationneeded", onNegotiationNeeded);

            onConnectionstatechange = new Action <JSObject>((e) =>
            {
                OnConnectionstatechange?.Invoke(this, EventArgs.Empty);
                e.Dispose();
            });

            // Attach the onError callback
            innerRtcPeerConnection.SetObjectProperty("onconnectionstatechange", onConnectionstatechange);

            onIcegatheringstatechange = new Action <JSObject>((e) =>
            {
                var s = this.iceGatheringState;
                e.Dispose();
            });

            // Attach the onError callback
            innerRtcPeerConnection.SetObjectProperty("onicegatheringstatechange", onIcegatheringstatechange);

            onIcecandidateerror = new Action <JSObject>((e) =>
            {
                e.Dispose();
            });

            innerRtcPeerConnection.Invoke("addEventListener", "icecandidateerror", onIcecandidateerror);


            onIceconnectionstatechange = new Action <JSObject>((e) =>
            {
                e.Dispose();
            });

            innerRtcPeerConnection.Invoke("addEventListener", "icecandidateerror", onIceconnectionstatechange);

            onSignalingstatechange = new Action <JSObject>((e) =>
            {
                Console.WriteLine("state:" + this.SignalingState);
                e.Dispose();
            });

            innerRtcPeerConnection.Invoke("addEventListener", "signalingstatechange", onSignalingstatechange);
        }
コード例 #4
0
 void Webrtc.PeerConnection.IObserver.OnDataChannel(Webrtc.DataChannel p0) =>
 OnDataChannel?.Invoke(this, RTCDataChannelEvent.Create(p0));
コード例 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:WebAssembly.Net.WebSockets.ClientWebSocket"/> class.
        /// </summary>
        public RTCPeerConnection(RTCPeerConfiguration configuration = null)
        {
            state = created;
            cts   = new CancellationTokenSource();


            var rtcPeerConfiguration = new JSObject();
            var json   = (JSObject)System.Runtime.InteropServices.JavaScript.Runtime.GetGlobalObject("JSON");
            var config = json.Invoke("parse", "{\"iceServers\":[{\"urls\":\"stun:stun.l.google.com:19302\"}]}");

            innerRtcPeerConnection = new HostObject("RTCPeerConnection", config);
            onIcecandidate         = new Action <JSObject>((e) =>
            {
                //var json = e.Invoke("toJSON");
                var c = e.GetObjectProperty("candidate") as JSObject;
                RTCIceCandidate iceCandidate = null;
                if (c != null)
                {
                    iceCandidate               = new RTCIceCandidate();
                    iceCandidate.candidate     = c?.GetObjectProperty("candidate") as string;
                    iceCandidate.sdpMid        = c?.GetObjectProperty("sdpMid") as string;
                    iceCandidate.sdpMLineIndex = (int)c?.GetObjectProperty("sdpMLineIndex");
                    Console.WriteLine(iceCandidate.candidate);
                }
                OnIceCandidate?.Invoke(this, new RTCIceCandidateInit()
                {
                    candidate = iceCandidate
                });
                e.Dispose();
            });

            // Attach the onError callback
            innerRtcPeerConnection.SetObjectProperty("onicecandidate", onIcecandidate);

            // Setup the onOpen callback
            onDataChannel = new Action <JSObject>((evt) =>
            {
                var channel = (JSObject)evt.GetObjectProperty("channel");
                var dc      = new RTCDataChannel(channel);
                OnDataChannel?.Invoke(this, dc);
                evt.Dispose();
            });

            // Attach the onOpen callback
            innerRtcPeerConnection.SetObjectProperty("ondatachannel", onDataChannel);

            onNegotiationNeeded = new Action <JSObject>((e) =>
            {
                OnNegotiationNeeded?.Invoke(this, EventArgs.Empty);
                e.Dispose();
            });

            // Attach the onError callback
            innerRtcPeerConnection.SetObjectProperty("onnegotiationneeded", onNegotiationNeeded);

            onConnectionstatechange = new Action <JSObject>((e) =>
            {
                OnConnectionstatechange?.Invoke(this, EventArgs.Empty);
                e.Dispose();
            });

            // Attach the onError callback
            innerRtcPeerConnection.SetObjectProperty("onconnectionstatechange", onConnectionstatechange);

            onIcegatheringstatechange = new Action <JSObject>((e) =>
            {
                var s = this.iceGatheringState;
                e.Dispose();
            });

            // Attach the onError callback
            innerRtcPeerConnection.SetObjectProperty("onicegatheringstatechange", onIcegatheringstatechange);

            onIcecandidateerror = new Action <JSObject>((e) =>
            {
                e.Dispose();
            });

            innerRtcPeerConnection.Invoke("addEventListener", "icecandidateerror", onIcecandidateerror);


            onIceconnectionstatechange = new Action <JSObject>((e) =>
            {
                e.Dispose();
            });

            innerRtcPeerConnection.Invoke("addEventListener", "icecandidateerror", onIceconnectionstatechange);

            onSignalingstatechange = new Action <JSObject>((e) =>
            {
                Console.WriteLine("state:" + this.SignalingState);
                e.Dispose();
            });

            innerRtcPeerConnection.Invoke("addEventListener", "signalingstatechange", onSignalingstatechange);
        }
コード例 #6
0
        private async Task <IJSInProcessObjectReference> Init()
        {
            var loaded = await moduleTask.Value;

            if (configuration == null)
            {
                innerRtcPeerConnection = new HostObject("RTCPeerConnection");
            }
            else
            {
                await loaded.InvokeVoidAsync("init", DotNetObjectReference.Create(this));

                var window = (JSObject)System.Runtime.InteropServices.JavaScript.Runtime.GetGlobalObject("window");
                //var options = new JsonSerializerOptions
                //{
                //	DefaultIgnoreCondition = Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
                //	PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                //};
                //var json = System.Text.Json.JsonSerializer.Serialize(configuration, options);
                var desc = (JSObject)window.Invoke("CreatePeerConnection");
                innerRtcPeerConnection = desc;
            }

            onIcecandidate = new Action <JSObject>((e) =>
            {
                //var json = e.Invoke("toJSON");
                var c = e.GetObjectProperty("candidate") as JSObject;
                RTCIceCandidate iceCandidate = null;
                if (c != null)
                {
                    iceCandidate               = new RTCIceCandidate();
                    iceCandidate.candidate     = c?.GetObjectProperty("candidate") as string;
                    iceCandidate.sdpMid        = c?.GetObjectProperty("sdpMid") as string;
                    iceCandidate.sdpMLineIndex = (int)c?.GetObjectProperty("sdpMLineIndex");
                }
                OnIceCandidate?.Invoke(this, new RTCIceCandidateInit()
                {
                    candidate = iceCandidate
                });
                e.Dispose();
            });

            // Attach the onError callback
            innerRtcPeerConnection.SetObjectProperty("onicecandidate", onIcecandidate);

            // Setup the onOpen callback
            onDataChannel = new Action <JSObject>((evt) =>
            {
                var channel = (JSObject)evt.GetObjectProperty("channel");
                var dc      = new RTCDataChannel(channel);
                OnDataChannel?.Invoke(this, dc);
                evt.Dispose();
            });

            // Attach the onOpen callback
            innerRtcPeerConnection.SetObjectProperty("ondatachannel", onDataChannel);

            onNegotiationNeeded = new Action <JSObject>((e) =>
            {
                OnNegotiationNeeded?.Invoke(this, EventArgs.Empty);
                e.Dispose();
            });

            // Attach the onError callback
            innerRtcPeerConnection.SetObjectProperty("onnegotiationneeded", onNegotiationNeeded);

            onConnectionstatechange = new Action <JSObject>((e) =>
            {
                OnConnectionstatechange?.Invoke(this, EventArgs.Empty);
                e.Dispose();
            });

            // Attach the onError callback
            innerRtcPeerConnection.SetObjectProperty("onconnectionstatechange", onConnectionstatechange);

            onIcegatheringstatechange = new Action <JSObject>((e) =>
            {
                var s = this.iceGatheringState;
                e.Dispose();
            });

            // Attach the onError callback
            innerRtcPeerConnection.SetObjectProperty("onicegatheringstatechange", onIcegatheringstatechange);

            onIcecandidateerror = new Action <JSObject>((e) =>
            {
                e.Dispose();
            });

            innerRtcPeerConnection.Invoke("addEventListener", "icecandidateerror", onIcecandidateerror);


            onIceconnectionstatechange = new Action <JSObject>((e) =>
            {
                e.Dispose();
            });

            innerRtcPeerConnection.Invoke("addEventListener", "oniceconnectionstatechange", onIceconnectionstatechange);

            onSignalingstatechange = new Action <JSObject>((e) =>
            {
                Console.WriteLine("state:" + this.SignalingState);
                e.Dispose();
            });

            innerRtcPeerConnection.Invoke("addEventListener", "signalingstatechange", onSignalingstatechange);

            return(loaded);
        }