예제 #1
0
        public void AddTXData(byte[] tx_data)
        {
            _byteSumTX += tx_data.Length;

            if (_txPacket == null)
            {
                _txPacket = new VitaOpusDataPacket();
                _txPacket.header.pkt_type = VitaPacketType.ExtDataWithStream;
                _txPacket.header.c        = true;
                _txPacket.header.t        = false;
                _txPacket.header.tsi      = VitaTimeStampIntegerType.Other;
                _txPacket.header.tsf      = VitaTimeStampFractionalType.SampleCount;

                //_txPacket.stream_id = _txStreamID;
                _txPacket.stream_id    = 0x4B000000; // TODO: don't hardcode this
                _txPacket.class_id.OUI = 0x001C2D;
                _txPacket.class_id.InformationClassCode = 0x543C;
                _txPacket.class_id.PacketClassCode      = 0x03E3;

                //_txPacket.payload = new float[256];
                _txPacket.payload = new byte[tx_data.Length];
            }

            int samples_sent = 0;

            while (samples_sent < tx_data.Length)
            {
                // how many samples should we send?
                //int num_samples_to_send = Math.Min(256, tx_data.Length - samples_sent);
                int num_samples_to_send = Math.Min(tx_data.Length, tx_data.Length - samples_sent);
                _txPacket.payload = new byte[tx_data.Length];
                //int num_samples_to_send = tx_data.Length;

                // copy the incoming data into the packet payload
                Array.Copy(tx_data, samples_sent, _txPacket.payload, 0, num_samples_to_send);

                // set the length of the packet
                // packet_size is the 32 bit word length?
                _txPacket.header.packet_size = (ushort)Math.Ceiling((double)num_samples_to_send / 4.0 + 7.0); // 7*4=28 bytes of Vita overhead

                try
                {
                    // send the packet to the radio
                    _txSocket.SendTo(_txPacket.ToBytesTX(), _txDataEndPoint);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("OpusStream: AddTXData sendTo() exception = " + e.ToString());
                }
                // bump the packet count
                _txPacket.header.packet_count = (byte)((_txPacket.header.packet_count + 1) % 16);

                // adjust the samples sent
                samples_sent += num_samples_to_send;
            }
        }
예제 #2
0
        internal void AddRXData2(VitaOpusDataPacket packet)
        {
            OpusPacketTotalCount++;
#if DEBUG_STATS
            if (_opusPacketTotalCount % 1000 == 0)
            {
                PrintStats();
            }
#endif

            lock (this)
            {
                _byteSumRX += packet.Length;
                //Debug.WriteLine("packet.Length: " + packet.Length);
            }

            int packet_count = packet.header.packet_count;

            //OnRXDataReady(this, packet);
            /* This will give at most 100ms of latency and will drop to 50 ms in an overflow case*/
            if (!force_drop && _opusRXQueue.Count < 10)
            {
                _opusRXQueue.Enqueue(packet);
            }
            else if (_opusRXQueue.Count < 5)
            {
                force_drop = false;
            }
            else
            {
                Debug.WriteLine("xxxxFlushing Opus RX Queuexxxx - Count " + _opusRXQueue.Count);
                force_drop = true;
            }
            //normal case -- this is the next packet we are looking for, or it is the first one
            if (packet_count == (last_packet_count + 1) % 16 || last_packet_count == NOT_INITIALIZED)
            {
                last_packet_count = packet_count;

                return;
            }

            Debug.WriteLine("Opus Audio: Expected " + ((last_packet_count + 1) % 16) + "  got " + packet_count);
            ErrorCount++;
            if (packet_count > last_packet_count)
            {
                last_packet_count = packet_count;
            }
        }