Esempio n. 1
0
        internal static OpusHeader ParsePacket(byte[] packet, int packetLength)
        {
            if (packetLength < 19)
            {
                return(null);
            }

            if (!"OpusHead".Equals(Encoding.UTF8.GetString(packet, 0, 8)))
            {
                return(null);
            }

            OpusHeader header = new OpusHeader();

            header.version       = packet[8];
            header.channel_count = packet[9];
            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(packet, 10, 2);
                Array.Reverse(packet, 12, 4);
                Array.Reverse(packet, 16, 2);
            }
            header.pre_skip          = BitConverter.ToUInt16(packet, 10);
            header.input_sample_rate = BitConverter.ToUInt32(packet, 12);
            header.output_gain       = BitConverter.ToInt16(packet, 16);
            header.mapping_family    = packet[18];

            return(header);
        }
Esempio n. 2
0
        /// <summary>
        /// Looks for the next opus data packet in the Ogg stream and queues it up.
        /// If the end of stream has been reached, this does nothing.
        /// </summary>
        private void QueueNextPacket()
        {
            if (_endOfStream)
            {
                return;
            }

            DataPacket packet = _packetProvider.GetNextPacket();

            if (packet == null || packet.IsEndOfStream)
            {
                _endOfStream    = true;
                _nextDataPacket = null;
                return;
            }

            byte[] buf = new byte[packet.Length];
            packet.Read(buf, 0, packet.Length);
            packet.Done();

            if (buf.Length > 8 && "OpusHead".Equals(Encoding.UTF8.GetString(buf, 0, 8)))
            {
                _header = OpusHeader.ParsePacket(buf, buf.Length);
                QueueNextPacket();
            }
            else if (buf.Length > 8 && "OpusTags".Equals(Encoding.UTF8.GetString(buf, 0, 8)))
            {
                _tags = OpusTags.ParsePacket(buf, buf.Length);
                QueueNextPacket();
            }
            else
            {
                _nextDataPacket = buf;
            }
        }