Ejemplo n.º 1
0
        public async Task StartSendingAudio(ushort groupId)
        {
            if (_sendingTask != null)
            {
                await _sendingTask;
                _sendingTask = null;
            }
            _sendingCancellationTokenSource = new CancellationTokenSource();

            var token = _sendingCancellationTokenSource.Token;

            _sendingTask = new Task(() =>
            {
                short[] audio = new short[160];
                //bump the sequence number by 1000, this allows the receivers to reset there jitter buffers on new overs.
                //this is required because we don't have a over id in media packets
                _sequenceNumber += 1000;

                _audioSource.Start();
                while (!token.IsCancellationRequested)
                {
                    _audioSource.ReadAudio(audio);
                    if (token.IsCancellationRequested)
                    {
                        break; //cancelled
                    }
                    if (_clientSettings.UserId == null)
                    {
                        throw new InvalidOperationException("Cannot send audio because no UserId is set");
                    }
                    SendMediaPacket(groupId, _sequenceNumber, _clientSettings.UserId.Value, audio);
                    _sequenceNumber++;
                }
                _audioSource.Stop();
            }, TaskCreationOptions.LongRunning);
            _sendingTask.Start();
            await _sendingTask;
        }