Exemplo n.º 1
0
        internal BufferedAudioTarget(DiscordVoiceAPIClient client, int samplesPerFrame, int bufferMillis, CancellationToken cancelToken)
        {
            _client        = client;
            _ticksPerFrame = samplesPerFrame / 48;
            int queueLength = (bufferMillis + (_ticksPerFrame - 1)) / _ticksPerFrame; //Round up

            _cancelTokenSource = new CancellationTokenSource();
            _cancelToken       = CancellationTokenSource.CreateLinkedTokenSource(_cancelTokenSource.Token, cancelToken).Token;
            _queuedFrames      = new ConcurrentQueue <Frame>();
            _bufferPool        = new ConcurrentQueue <byte[]>();
            for (int i = 0; i < queueLength; i++)
            {
                _bufferPool.Enqueue(new byte[1275]);
            }
            _queueLock = new SemaphoreSlim(queueLength, queueLength);

            _task = Run();
        }
Exemplo n.º 2
0
        /// <summary> Creates a new REST/WebSocket discord client. </summary>
        public AudioClient(SocketGuild guild, int id)
        {
            Guild = guild;

            _audioLogger = Discord.LogManager.CreateLogger($"Audio #{id}");
#if BENCHMARK
            _benchmarkLogger = logManager.CreateLogger("Benchmark");
#endif

            _connectionLock = new SemaphoreSlim(1, 1);

            _serializer = new JsonSerializer {
                ContractResolver = new DiscordContractResolver()
            };
            _serializer.Error += (s, e) =>
            {
                _audioLogger.WarningAsync(e.ErrorContext.Error).GetAwaiter().GetResult();
                e.ErrorContext.Handled = true;
            };

            ApiClient = new DiscordVoiceAPIClient(guild.Id, Discord.WebSocketProvider);

            ApiClient.SentGatewayMessage += async opCode => await _audioLogger.DebugAsync($"Sent {opCode}").ConfigureAwait(false);

            ApiClient.SentDiscovery += async() => await _audioLogger.DebugAsync($"Sent Discovery").ConfigureAwait(false);

            ApiClient.SentData += async bytes => await _audioLogger.DebugAsync($"Sent {bytes} Bytes").ConfigureAwait(false);

            ApiClient.ReceivedEvent  += ProcessMessageAsync;
            ApiClient.ReceivedPacket += ProcessPacketAsync;
            ApiClient.Disconnected   += async ex =>
            {
                if (ex != null)
                {
                    await _audioLogger.WarningAsync($"Connection Closed", ex).ConfigureAwait(false);
                }
                else
                {
                    await _audioLogger.WarningAsync($"Connection Closed").ConfigureAwait(false);
                }
            };

            LatencyUpdated += async(old, val) => await _audioLogger.VerboseAsync($"Latency = {val} ms").ConfigureAwait(false);
        }
Exemplo n.º 3
0
        /// <summary> Creates a new REST/WebSocket discord client. </summary>
        internal AudioClient(SocketGuild guild, int clientId, ulong channelId)
        {
            Guild        = guild;
            ChannelId    = channelId;
            _audioLogger = Discord.LogManager.CreateLogger($"Audio #{clientId}");

            ApiClient = new DiscordVoiceAPIClient(guild.Id, Discord.WebSocketProvider, Discord.UdpSocketProvider);
            ApiClient.SentGatewayMessage += async opCode => await _audioLogger.DebugAsync($"Sent {opCode}").ConfigureAwait(false);

            ApiClient.SentDiscovery += async() => await _audioLogger.DebugAsync("Sent Discovery").ConfigureAwait(false);

            //ApiClient.SentData += async bytes => await _audioLogger.DebugAsync($"Sent {bytes} Bytes").ConfigureAwait(false);
            ApiClient.ReceivedEvent  += ProcessMessageAsync;
            ApiClient.ReceivedPacket += ProcessPacketAsync;

            _stateLock  = new SemaphoreSlim(1, 1);
            _connection = new ConnectionManager(_stateLock, _audioLogger, 30000,
                                                OnConnectingAsync, OnDisconnectingAsync, x => ApiClient.Disconnected += x);
            _connection.Connected    += () => _connectedEvent.InvokeAsync();
            _connection.Disconnected += (ex, recon) => _disconnectedEvent.InvokeAsync(ex);
            _heartbeatInterval        = 41250;
            _heartbeatTimes           = new ConcurrentQueue <long>();
            _keepaliveTimes           = new ConcurrentQueue <KeyValuePair <ulong, int> >();
            _ssrcMap = new ConcurrentDictionary <uint, ulong>();
            _streams = new ConcurrentDictionary <ulong, StreamPair>();

            _serializer = new JsonSerializer {
                ContractResolver = new DiscordContractResolver()
            };
            _serializer.Error += (s, e) =>
            {
                _audioLogger.WarningAsync(e.ErrorContext.Error).GetAwaiter().GetResult();
                e.ErrorContext.Handled = true;
            };

            LatencyUpdated += async(old, val) => await _audioLogger.DebugAsync($"Latency = {val} ms").ConfigureAwait(false);

            UdpLatencyUpdated += async(old, val) => await _audioLogger.DebugAsync($"UDP Latency = {val} ms").ConfigureAwait(false);
        }
Exemplo n.º 4
0
 public DirectAudioTarget(DiscordVoiceAPIClient client)
 {
     _client = client;
 }