aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.WebRTC/WebRtcTransportAdapter.cs
blob: 59784fa56b3348cca39b4d5a6b7d13b0edd3be1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Tango.RemoteRunner.UI")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

//In order to begin building localizable applications, set
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
//inside a <PropertyGroup>.  For example, if you are using US english
//in your source files, set the <UICulture> to en-US.  Then uncomment
//the NeutralResourceLanguage attribute below.  Update the "en-US" in
//the line below to match the UICulture setting in the project file.

//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]


[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
                                     //(used if a resource is not found in the page,
                                     // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
                                              //(used if a resource is not found in the page,
                                              // app, or any theme specific resource dictionaries)
)]


// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.4.1119")]
/a> 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Core.Threading;
using Tango.Transport;
using Tango.Transport.Adapters;
using Tango.Transport.Compression;
using Tango.WebRTC.Network;

namespace Tango.WebRTC
{
    public class WebRtcTransportAdapter : TransportAdapterBase
    {
        private WebRtcClient _client;
        private bool _answerReceived;
        private List<IceCandidate> _queuedIceCandidates;

        public event EventHandler Ready;

        public ITransporter SignalingTransporter { get; set; }

        public WebRtcTransportAdapterMode Mode { get; set; }

        public String DataChannelName { get; set; }

        public WebRtcTransportAdapter(ITransporter signalingTransporter, WebRtcTransportAdapterMode mode) : this(signalingTransporter, mode, null)
        {

        }

        public WebRtcTransportAdapter(ITransporter signalingTransporter, WebRtcTransportAdapterMode mode, String dataChannelName)
        {
            SignalingTransporter = signalingTransporter;
            Mode = mode;
            DataChannelName = dataChannelName;
            Address = dataChannelName;
            ComponentName = $"WebRTC Adapter {_component_counter++}";

            SignalingTransporter.RegisterRequestHandler<IceCandidateRequest>(OnIceCandidateRequestReceived);
            SignalingTransporter.RegisterRequestHandler<OfferRequest>(OnOfferRequestReceived);
        }

        public override void Write(byte[] data, bool immidiate = false)
        {
            ThrowIfDisposed();

            try
            {
                if (EnableCompression)
                {
                    data = GZipHelper.Compress(data);
                }

                _client.SendBinary(data);
            }
            catch (Exception ex)
            {
                OnFailed(ex);
            }
        }

        public override Task Connect()
        {
            ThrowIfDisposed();

            TaskCompletionSource<object> completionSource = new TaskCompletionSource<object>();
            bool completed = false;

            _queuedIceCandidates = new List<IceCandidate>();
            _answerReceived = false;

            ThreadFactory.StartNew(async () =>
            {
                if (State != TransportComponentState.Connected)
                {
                    try
                    {
                        _client = new WebRtcClient();

                        if (DataChannelName != null)
                        {
                            _client.DataChannelName = DataChannelName;
                        }

                        Address = _client.DataChannelName;

                        _client.NewIceCandidate += WebRtc_NewIceCandidate;
                        _client.Disconnected += WebRtc_Disconnected;
                        _client.BinaryMessageReceived += WebRtc_BinaryMessageReceived;
                        _client.Ready += (x, e) =>
                        {
                            if (!completed)
                            {
                                LogManager.Log($"{ComponentName}: WebRTC Active Transport Adapter is ready.");
                                completed = true;
                                State = TransportComponentState.Connected;
                                completionSource.SetResult(true);
                                Ready?.Invoke(this, new EventArgs());
                            }

                            if (Mode == WebRtcTransportAdapterMode.Passive)
                            {
                                LogManager.Log($"{ComponentName}: WebRTC Passive Transport Adapter is ready.");
                                Ready?.Invoke(this, new EventArgs());
                            }
                        };

                        LogManager.Log($"{ComponentName}: Initializing WebRTC client...");
                        await _client.Init();

                        if (Mode == WebRtcTransportAdapterMode.Active)
                        {
                            LogManager.Log($"{ComponentName}: Creating WebRTC offer...");
                            var offer = await _client.CreateOffer();

                            LogManager.Log($"{ComponentName}: Sending WebRTC offer via signaling transporter...");
                            var response = await SignalingTransporter.SendGenericRequest<OfferRequest, OfferResponse>(new OfferRequest() { Offer = offer }, new TransportRequestConfig()
                            {
                                Timeout = TimeSpan.FromSeconds(30),
                            });

                            LogManager.Log($"{ComponentName}: WebRTC offer sent and responded with an answer. Setting WebRTC answer...");
                            _client.SetAnswer(response.Answer);
                            _answerReceived = true;

                            foreach (var ice in _queuedIceCandidates.ToList())
                            {
                                LogManager.Log($"{ComponentName}: Sending existing ice candidate '{ice.Sdp}'...");

                                try
                                {
                                    await SignalingTransporter.SendGenericRequest<IceCandidateRequest, IceCandidateResponse>(new IceCandidateRequest() { IceCandidate = ice }, new TransportRequestConfig()
                                    {
                                        Timeout = TimeSpan.FromSeconds(30),
                                    });
                                }
                                catch (Exception ex)
                                {
                                    LogManager.Log(ex, $"{ComponentName}: Error sending ice candidate.");
                                }
                            }
                        }
                        else
                        {
                            LogManager.Log($"{ComponentName}: Waiting for offer...");
                            State = TransportComponentState.Connected;

                            if (!completed)
                            {
                                completed = true;
                                completionSource.SetResult(true);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!completed)
                        {
                            completed = true;
                            completionSource.SetException(ex);
                        }
                    }
                }
                else
                {
                    if (!completed)
                    {
                        completed = true;
                        completionSource.SetResult(true);
                    }
                }
            });

            if (Mode == WebRtcTransportAdapterMode.Active)
            {
                TimeoutTask.StartNew(() =>
                {
                    if (!completed)
                    {
                        completed = true;
                        completionSource.SetException(new TimeoutException("Could not reach the remote peer using the WebRTC adapter."));
                    }

                }, TimeSpan.FromSeconds(30));
            }

            return completionSource.Task;
        }

        private void WebRtc_BinaryMessageReceived(object sender, DataMessageReceivedEventArgs<byte[]> e)
        {
            if (EnableCompression)
            {
                try
                {
                    var decompressed = GZipHelper.Decompress(e.Data);
                    OnDataAvailable(decompressed);
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("GZip"))
                    {
                        //Temporarily ignore, probably switching protocol definitions...
                    }
                    else
                    {
                        //Do nothing... ?
                    }
                }
            }
            else
            {
                OnDataAvailable(e.Data);
            }
        }

        private async void OnOfferRequestReceived(ITransporter transporter, OfferRequest request, string token)
        {
            if (Mode == WebRtcTransportAdapterMode.Passive)
            {
                try
                {
                    var answer = await _client.CreateAnswer(request.Offer);
                    await SignalingTransporter.SendGenericResponse(new OfferResponse() { Answer = answer }, token);
                    _answerReceived = true;

                    foreach (var ice in _queuedIceCandidates.ToList())
                    {
                        LogManager.Log($"{ComponentName}: Sending existing ice candidate '{ice.Sdp}'...");

                        try
                        {
                            await SignalingTransporter.SendGenericRequest<IceCandidateRequest, IceCandidateResponse>(new IceCandidateRequest() { IceCandidate = ice }, new TransportRequestConfig()
                            {
                                Timeout = TimeSpan.FromSeconds(30),
                            });
                        }
                        catch (Exception ex)
                        {
                            LogManager.Log(ex, $"{ComponentName}: Error sending ice candidate to remote peer.");
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, $"{ComponentName}: Error occurred while trying to return WebRTC answer.");
                }
            }
        }

        private async void WebRtc_NewIceCandidate(object sender, NewIceCandidateEventArgs e)
        {
            try
            {
                if (_answerReceived)
                {
                    LogManager.Log($"{ComponentName}: New WebRTC candidate available. Sending ice to remote peer...");

                    await SignalingTransporter.SendGenericRequest<IceCandidateRequest, IceCandidateResponse>(new IceCandidateRequest() { IceCandidate = e.IceCandidate }, new TransportRequestConfig()
                    {
                        Timeout = TimeSpan.FromSeconds(30),
                    });
                }
                else
                {
                    if (Mode == WebRtcTransportAdapterMode.Active)
                    {
                        LogManager.Log($"{ComponentName}: New WebRTC candidate available. Will be sent after an answer is received...");
                    }
                    else
                    {
                        LogManager.Log($"{ComponentName}: New WebRTC candidate available. Will be sent after an offer is received...");
                    }

                    _queuedIceCandidates.Add(e.IceCandidate);
                }
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, $"{ComponentName}: Error sending ice candidate to remote peer.");
            }
        }

        private async void OnIceCandidateRequestReceived(ITransporter transporter, IceCandidateRequest request, string token)
        {
            try
            {
                LogManager.Log($"{ComponentName}: Ice candidate request received from the remote peer.");
                await SignalingTransporter.SendGenericResponse(new IceCandidateResponse() { }, token);

                LogManager.Log($"{ComponentName}: Adding ice candidate '{request.IceCandidate.Sdp}'...");
                _client.AddIceCandidate(request.IceCandidate);
                LogManager.Log($"{ComponentName}: Ice candidate added.");
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, $"{ComponentName}: Error occurred on ice candidate received handling.");
            }
        }

        private void WebRtc_Disconnected(object sender, EventArgs e)
        {
            OnFailed(new WebRtcTransportAdapterDisconnectedException("WebRtc Transport Adapter RTC client has disconnected."));
        }

        public override Task Disconnect()
        {
            TaskCompletionSource<object> completionSource = new TaskCompletionSource<object>();

            ThreadFactory.StartNew(() =>
            {
                if (State != TransportComponentState.Disconnected)
                {
                    if (_client != null)
                    {
                        LogManager.Log($"{ComponentName}: Disposing WebRTC client...");

                        _client.NewIceCandidate -= WebRtc_NewIceCandidate;
                        _client.Disconnected -= WebRtc_Disconnected;
                        _client.BinaryMessageReceived -= WebRtc_BinaryMessageReceived;

                        try
                        {
                            _client.Dispose();
                            _client = null;
                            SignalingTransporter.UnregisterRequestHandler<IceCandidateRequest>(OnIceCandidateRequestReceived);
                            SignalingTransporter.UnregisterRequestHandler<OfferRequest>(OnOfferRequestReceived);
                            LogManager.Log($"{ComponentName}: WebRTC client disposed.");
                        }
                        catch (Exception ex)
                        {
                            LogManager.Log(ex, $"{ComponentName}: Error disposing WebRTC client.");
                        }
                    }

                    State = TransportComponentState.Disconnected;
                    completionSource.SetResult(true);
                }
                else
                {
                    completionSource.SetResult(true);
                }
            });

            return completionSource.Task;
        }
    }
}