internal void AddDecodedAudio(float[] pcmData, int pcmLength, byte[] posData, bool reevaluateInitialBuffer) { DecodedPacket decodedPacket = new DecodedPacket { PcmData = pcmData, PosData = posData, PcmLength = pcmLength, ReadOffset = 0 }; //if (reevaluateInitialBuffer) //Debug.Log("Will refill our initial buffer"); int count = 0; lock (_bufferLock) { count = _decodedBuffer.Count; if (count > MumbleConstants.RECEIVED_PACKET_BUFFER_SIZE) { // TODO this seems to happen at times Debug.LogWarning("Max recv buffer size reached, dropping for user " + _name); } else { _decodedBuffer.Enqueue(decodedPacket); Interlocked.Add(ref _decodedCount, pcmLength); // this is set if the previous received packet was a last packet // or if there was an abrupt change in sequence number if (reevaluateInitialBuffer) { HasFilledInitialBuffer = false; } if (!HasFilledInitialBuffer && (count + 1 >= InitialSampleBuffer)) { HasFilledInitialBuffer = true; } } } // Make sure the next position data is loaded lock (_posLock) { if (_nextPosData == null) { _nextPosData = posData; } } //Debug.Log("Adding " + pcmLength + " num packets: " + count + " total decoded: " + _decodedCount); }
/// <summary> /// Read data that has already been decoded /// </summary> /// <param name="dst"></param> /// <param name="dstOffset"></param> /// <param name="count"></param> /// <returns></returns> private int ReadFromBuffer(float[] dst, int dstOffset, int count) { // Get the next DecodedPacket to use int numInSample = _currentPacket.PcmLength - _currentPacket.ReadOffset; if (numInSample == 0) { lock (_bufferLock) { if (_decodedBuffer.Count == 0) { Debug.LogError("No available decode buffers!"); return(0); } _currentPacket = _decodedBuffer.Dequeue(); // If we have a packet, let's update the positions lock (_posLock) { _lastBufferTime = AudioSettings.dspTime; _previousPosData = _currentPacket.PosData; _nextPosData = null; // try to load the next buffer if (_decodedBuffer.Count > 0) { _nextPosData = _decodedBuffer.Peek().PosData; } } numInSample = _currentPacket.PcmLength - _currentPacket.ReadOffset; } } int readCount = Math.Min(numInSample, count); Array.Copy(_currentPacket.PcmData, _currentPacket.ReadOffset, dst, dstOffset, readCount); //Debug.Log("We have: " //+ _decodedCount + " samples decoded " //+ numInSample + " samples in curr packet " //+ _currentPacket.ReadOffset + " read offset " //+ readCount + " numRead"); Interlocked.Add(ref _decodedCount, -readCount); _currentPacket.ReadOffset += readCount; return(readCount); }
public void Reset() { lock (_bufferLock) { _name = null; if (_session != 0) { _audioDecodeThread.RemoveDecoder(_session); } NumPacketsLost = 0; HasFilledInitialBuffer = false; _decodedCount = 0; _decodedBuffer.Clear(); _currentPacket = new DecodedPacket { }; _session = 0; } lock (_posLock) { _previousPosData = null; _nextPosData = null; _lastBufferTime = 0; } }