Beispiel #1
0
        private async Task CloseConnections()
        {
            for (var i = 0; i < _ClientChannelConnections.Count(); i += Options.Parallelism)
            {
                var clientChannelConnections = _ClientChannelConnections.Skip(i).Take(Options.Parallelism);

                Console.Error.WriteLine($"Closing {clientChannelConnections.Count()} connections (group #{1 + i / Options.Parallelism})...");

                var stopwatch = new ManagedStopwatch();
                stopwatch.Start();

                await Task.WhenAll(
                    clientChannelConnections.Select(async ccc =>
                {
                    await ccc.Connection?.Close();

                    ccc.RemoteAudioTrack?.Destroy();
                    ccc.RemoteVideoTrack?.Destroy();
                })
                    ).ConfigureAwait(false);

                stopwatch.Stop();

                Console.Error.WriteLine($"Closing completed in {stopwatch.ElapsedMilliseconds}ms ({clientChannelConnections.Count() * 1000.0 / stopwatch.ElapsedMilliseconds:n2}/s).");
            }
        }
Beispiel #2
0
        private async Task OpenConnections(CancellationToken cancellationToken)
        {
            _ClientChannelConnections = _ClientChannels.SelectMany(cc => Enumerable.Range(0, Options.ConnectionCount).Select(_ => new ClientChannelConnection
            {
                Client    = cc.Client,
                ChannelId = cc.ChannelId,
                Channel   = cc.Channel
            })).ToArray();

            for (var i = 0; i < _ClientChannelConnections.Count(); i += Options.Parallelism)
            {
                var clientChannelConnections = _ClientChannelConnections.Skip(i).Take(Options.Parallelism);

                Console.Error.WriteLine($"Opening {clientChannelConnections.Count()} connections (group #{1 + i / Options.Parallelism})...");

                var stopwatch = new ManagedStopwatch();
                stopwatch.Start();

                var result = await Task.WhenAny(
                    Task.Delay(Timeout.Infinite, cancellationToken),
                    Task.WhenAll(
                        clientChannelConnections.Select(ccc =>
                {
                    ccc.RemoteAudioTrack = new AudioTrack(new NullAudioSink(new Opus.Format
                    {
                        IsPacketized = true
                    }));
                    ccc.RemoteVideoTrack = new VideoTrack(new NullVideoSink(new Vp8.Format
                    {
                        IsPacketized = true
                    }));

                    ccc.Connection = ccc.Channel.CreateSfuDownstreamConnection(
                        Utility.GenerateId(),
                        new AudioStream(null, ccc.RemoteAudioTrack),
                        new VideoStream(null, ccc.RemoteVideoTrack)
                        );

                    ccc.Connection.PrivateIPAddresses = _PrivateIPAddresses;

                    return(ccc.Connection.Open().AsTaskAsync());
                })
                        )
                    ).ConfigureAwait(false);

                stopwatch.Stop();

                // check cancellation
                cancellationToken.ThrowIfCancellationRequested();

                // check faulted
                if (result.IsFaulted)
                {
                    throw new ConnectionOpenException("One or more connections could not be opened.", result.Exception);
                }

                Console.Error.WriteLine($"Opening completed in {stopwatch.ElapsedMilliseconds}ms ({clientChannelConnections.Count() * 1000.0 / stopwatch.ElapsedMilliseconds:n2}/s).");
            }
        }
Beispiel #3
0
        private async Task JoinChannels(CancellationToken cancellationToken)
        {
            var channelIds = Enumerable.Range(0, Options.ChannelCount).Select(_ => Utility.GenerateId()).ToArray();

            if (Options.ChannelBurst)
            {
                _ClientChannels = channelIds.SelectMany(channelId => _Clients.Select(client => new ClientChannel
                {
                    Client    = client,
                    ChannelId = channelId
                })).ToArray();
            }
            else
            {
                _ClientChannels = _Clients.SelectMany(client => channelIds.Select(channelId => new ClientChannel
                {
                    Client    = client,
                    ChannelId = channelId
                })).ToArray();
            }

            for (var i = 0; i < _ClientChannels.Count(); i += Options.Parallelism)
            {
                var clientChannels = _ClientChannels.Skip(i).Take(Options.Parallelism);

                Console.Error.WriteLine($"Joining {clientChannels.Count()} channels (group #{1 + i / Options.Parallelism})...");

                var stopwatch = new ManagedStopwatch();
                stopwatch.Start();

                var result = await Task.WhenAny(
                    Task.Delay(Timeout.Infinite, cancellationToken),
                    Task.WhenAll(
                        clientChannels.Select(async cc =>
                {
                    cc.Channel = await cc.Client.Join(Token.GenerateClientJoinToken(cc.Client, new ChannelClaim(cc.ChannelId), Options.SharedSecret)).AsTask(TaskCreationOptions.RunContinuationsAsynchronously);
                })
                        )
                    ).ConfigureAwait(false);

                stopwatch.Stop();

                // check cancellation
                cancellationToken.ThrowIfCancellationRequested();

                // check faulted
                if (result.IsFaulted)
                {
                    throw new ChannelJoinException("One or more channels could not be joined.", result.Exception);
                }

                Console.Error.WriteLine($"Joining completed in {stopwatch.ElapsedMilliseconds}ms ({clientChannels.Count() * 1000.0 / stopwatch.ElapsedMilliseconds:n2}/s).");
            }
        }
Beispiel #4
0
        public int ReadFrameHeader()
        {
            if (_FirstFrameTimestamp == -1)
            {
                _FirstFrameTimestamp = ManagedStopwatch.GetTimestamp();
            }
            if (_NextFrameLength == -1)
            {
                _NextFrameLength = ReadNextFrameLength();
            }
            var frameLength = _NextFrameLength;

            _NextFrameLength = -1;
            _StreamPosition += frameLength;
            return(frameLength);
        }
Beispiel #5
0
        public long WaitForFrameTimestamp(int clockRate)
        {
            var elapsedTicks  = ManagedStopwatch.GetTimestamp() - _FirstFrameTimestamp;
            var nanoTimestamp = (_ClusterTimecode + _ClusterBlockTimecode) * _SegmentInfo.TimecodeScale;
            var tickTimestamp = nanoTimestamp / 100;

            if (tickTimestamp > elapsedTicks)
            {
                // hold up
                ManagedThread.Sleep((int)((tickTimestamp - elapsedTicks) / Constants.TicksPerMillisecond));
            }

            _ClusterBlockTrackNumber = -1;
            _ClusterBlockTimecode    = -1;
            _ClusterBlockFlags       = 0;

            return(clockRate * tickTimestamp / Constants.TicksPerSecond);
        }
Beispiel #6
0
        private async Task LeaveChannels()
        {
            for (var i = 0; i < _ClientChannels.Count(); i += Options.Parallelism)
            {
                var clientChannels = _ClientChannels.Skip(i).Take(Options.Parallelism);

                Console.Error.WriteLine($"Leaving {clientChannels.Count()} channels (group #{1 + i / Options.Parallelism})...");

                var stopwatch = new ManagedStopwatch();
                stopwatch.Start();

                await Task.WhenAll(
                    clientChannels.Select(cc =>
                {
                    return(cc.Client.Leave(cc.ChannelId).AsTask(TaskCreationOptions.RunContinuationsAsynchronously));
                })
                    ).ConfigureAwait(false);

                stopwatch.Stop();

                Console.Error.WriteLine($"Leaving completed in {stopwatch.ElapsedMilliseconds}ms ({clientChannels.Count() * 1000.0 / stopwatch.ElapsedMilliseconds:n2}/s).");
            }
        }
Beispiel #7
0
        private async Task RegisterClients(CancellationToken cancellationToken)
        {
            _Clients = Enumerable.Range(0, Options.ClientCount).Select(_ => new Client(Options.GatewayUrl, Options.ApplicationId)).ToArray();

            for (var i = 0; i < _Clients.Count(); i += Options.Parallelism)
            {
                var clients = _Clients.Skip(i).Take(Options.Parallelism);

                Console.Error.WriteLine($"Registering {clients.Count()} clients (group #{1 + i / Options.Parallelism})...");

                var stopwatch = new ManagedStopwatch();
                stopwatch.Start();

                var result = await Task.WhenAny(
                    Task.Delay(Timeout.Infinite, cancellationToken),
                    Task.WhenAll(
                        clients.Select(client =>
                {
                    return(client.Register(Token.GenerateClientRegisterToken(client, new ChannelClaim[0], Options.SharedSecret)).AsTask(TaskCreationOptions.RunContinuationsAsynchronously));
                })
                        )
                    ).ConfigureAwait(false);

                stopwatch.Stop();

                // check cancellation
                cancellationToken.ThrowIfCancellationRequested();

                // check faulted
                if (result.IsFaulted)
                {
                    throw new ClientRegisterException("One or more clients could not be registered.", result.Exception);
                }

                Console.Error.WriteLine($"Registering completed in {stopwatch.ElapsedMilliseconds}ms ({clients.Count() * 1000.0 / stopwatch.ElapsedMilliseconds:n2}/s).");
            }
        }