Exemplo n.º 1
0
        internal void InitForSampleRate(int sampleRate)
        {
            if (_encoder != null)
            {
                Debug.LogError("Destroying opus encoder");
                _encoder.Dispose();
                _encoder = null;
            }
            _encoder = new OpusEncoder(sampleRate, 1)
            {
                EnableForwardErrorCorrection = false
            };

            if (_pendingBitrate > 0)
            {
                Debug.Log("Using pending bitrate");
                SetBitrate(_pendingBitrate);
            }

            if (_encodingThread == null)
            {
                _encodingThread = new Thread(EncodingThreadEntry)
                {
                    IsBackground = true
                };
                _encodingThread.Start();
            }
        }
Exemplo n.º 2
0
        public bool InitializeEncoderWithSampleRate(int newSampleRate)
        {
            if (_encoder != null)
            {
                return(false);
            }

            _encoder = new OpusEncoder(newSampleRate, 1)
            {
                EnableForwardErrorCorrection = false
            };
            return(_encoder != null);
        }
Exemplo n.º 3
0
        public ArraySegment <byte> Encode(OpusEncoder encoder, out bool isStop, out bool isEmpty)
        {
            isStop  = false;
            isEmpty = false;
            PcmArray            nextPcmToSend = null;
            ArraySegment <byte> encoder_buffer;

            lock (_unencodedBuffer)
            {
                if (_unencodedBuffer.Count == 0)
                {
                    isEmpty = true;
                }
                else
                {
                    if (_unencodedBuffer.Count == 1 && _isWaitingToSendLastPacket)
                    {
                        isStop = true;
                    }

                    TargettedSpeech speech = _unencodedBuffer.Dequeue();
                    isStop        = isStop || speech.IsStop;
                    nextPcmToSend = speech.PcmData;

                    if (isStop)
                    {
                        _isWaitingToSendLastPacket = false;
                    }
                }
            }

            if (nextPcmToSend == null || nextPcmToSend.Pcm.Length == 0)
            {
                isEmpty = true;
            }

            encoder_buffer = isEmpty ? EmptyByteSegment : encoder.Encode(nextPcmToSend.Pcm);

            if (nextPcmToSend != null)
            {
                nextPcmToSend.IsAvailable = true;
            }

            if (isStop)
            {
                Debug.Log("Resetting encoder state");
                encoder.ResetState();
            }
            return(encoder_buffer);
        }
Exemplo n.º 4
0
        public void Dispose()
        {
            _isRunning = false;
            _waitHandle.Set();

            if (_encodingThread != null)
            {
                _encodingThread.Abort();
            }
            _encodingThread = null;
            if (_encoder != null)
            {
                _encoder.Dispose();
            }
            _encoder = null;
        }
Exemplo n.º 5
0
        public CompressedBuffer Encode(OpusEncoder encoder, out bool isStop, out bool isEmpty)
        {
            isStop  = false;
            isEmpty = false;
            PcmArray            nextPcmToSend = null;
            ArraySegment <byte> encoder_buffer;


            lock (_bufferLock)
            {
                // Make sure we have data, or an end event
                if (_unencodedBuffer.Count == 0)
                {
                    Monitor.Wait(_bufferLock);
                }

                // If there are still no unencoded buffers, then we return an empty packet
                if (_unencodedBuffer.Count == 0)
                {
                    isEmpty = true;
                }
                else
                {
                    if (_unencodedBuffer.Count == 1 && _isWaitingToSendLastPacket)
                    {
                        isStop = true;
                    }

                    TargettedSpeech speech = _unencodedBuffer.Dequeue();
                    isStop        = isStop || speech.IsStop;
                    nextPcmToSend = speech.PcmData;

                    if (isStop)
                    {
                        _isWaitingToSendLastPacket = false;
                    }
                }
            }

            if (nextPcmToSend == null || nextPcmToSend.Pcm.Length == 0)
            {
                isEmpty = true;
            }

            encoder_buffer = isEmpty ? EmptyByteSegment : encoder.Encode(nextPcmToSend.Pcm);
            byte[] pos    = nextPcmToSend == null ? null : nextPcmToSend.PositionalData;
            int    posLen = nextPcmToSend == null ? 0 : nextPcmToSend.PositionalDataLength;

            if (nextPcmToSend != null)
            {
                nextPcmToSend.UnRef();
            }

            if (isStop)
            {
                Debug.Log("Resetting encoder state");
                encoder.ResetState();
            }


            CompressedBuffer compressedBuffer = new CompressedBuffer
            {
                EncodedData          = encoder_buffer,
                PositionalData       = pos,
                PositionalDataLength = posLen
            };

            return(compressedBuffer);
        }