示例#1
0
    private static void CollectPacket(UdpSocketPacket packet, SenderPacketCollection senderPacketCollection)
    {
        int senderSessionId         = PacketHelper.getSessionIdFromSenderPacketBytes(packet.Bytes);
        SenderPacketType packetType = PacketHelper.getPacketTypeFromSenderPacketBytes(packet.Bytes);

        if (packetType == SenderPacketType.Confirm)
        {
            senderPacketCollection.ConfirmPacketInfoList.Add(new ConfirmPacketInfo(packet.EndPoint, senderSessionId, ConfirmSenderPacketData.Parse(packet.Bytes)));
            return;
        }

        SenderPacketSet senderPacketSet;

        if (!senderPacketCollection.SenderPacketSets.TryGetValue(senderSessionId, out senderPacketSet))
        {
            return;
        }

        // Heartbeat packets turns on ReceivedAny.
        senderPacketSet.ReceivedAny = true;
        switch (packetType)
        {
        case SenderPacketType.VideoInit:
            senderPacketSet.InitPacketDataList.Add(VideoInitSenderPacketData.Parse(packet.Bytes));
            break;

        case SenderPacketType.Frame:
            senderPacketSet.VideoPacketDataList.Add(VideoSenderPacketData.Parse(packet.Bytes));
            break;

        case SenderPacketType.Parity:
            senderPacketSet.FecPacketDataList.Add(ParitySenderPacketData.Parse(packet.Bytes));
            break;

        case SenderPacketType.Audio:
            senderPacketSet.AudioPacketDataList.Add(AudioSenderPacketData.Parse(packet.Bytes));
            break;

        case SenderPacketType.Floor:
            senderPacketSet.FloorPacketDataList.Add(FloorSenderPacketData.Parse(packet.Bytes));
            break;
        }
    }
        public void Run(KinectReceiver kinectReceiver,
                        int senderSessionId,
                        ConcurrentQueue <VideoSenderPacketData> videoPacketDataQueue,
                        ConcurrentQueue <FecSenderPacketData> fecPacketDataQueue,
                        ConcurrentQueue <AudioSenderPacketData> audioPacketDataQueue)
        {
            SocketError error = SocketError.WouldBlock;

            while (true)
            {
                var packet = kinectReceiver.udpSocket.Receive(out error);
                if (packet == null)
                {
                    break;
                }

                int sessionId  = PacketHelper.getSessionIdFromSenderPacketBytes(packet);
                var packetType = PacketHelper.getPacketTypeFromSenderPacketBytes(packet);

                if (sessionId != senderSessionId)
                {
                    continue;
                }

                if (packetType == SenderPacketType.Frame)
                {
                    videoPacketDataQueue.Enqueue(VideoSenderPacketData.Parse(packet));
                }
                else if (packetType == SenderPacketType.Fec)
                {
                    fecPacketDataQueue.Enqueue(FecSenderPacketData.Parse(packet));
                }
                else if (packetType == SenderPacketType.Audio)
                {
                    audioPacketDataQueue.Enqueue(AudioSenderPacketData.Parse(packet));
                }
            }

            if (error != SocketError.WouldBlock)
            {
                UnityEngine.Debug.Log($"Error from receiving packets: {error.ToString()}");
            }
        }
    public static SenderPacketSet Receive(UdpSocket udpSocket)
    {
        var senderPacketSet = new SenderPacketSet();

        while (true)
        {
            var packet = udpSocket.Receive();
            if (packet == null)
            {
                break;
            }

            //int sessionId = PacketHelper.getSessionIdFromSenderPacketBytes(packet);
            senderPacketSet.ReceivedAny = true;
            switch (PacketHelper.getPacketTypeFromSenderPacketBytes(packet))
            {
            case SenderPacketType.Init:
                senderPacketSet.InitPacketDataList.Add(InitSenderPacketData.Parse(packet));
                break;

            case SenderPacketType.Frame:
                senderPacketSet.VideoPacketDataList.Add(VideoSenderPacketData.Parse(packet));
                break;

            case SenderPacketType.Parity:
                senderPacketSet.FecPacketDataList.Add(ParitySenderPacketData.Parse(packet));
                break;

            case SenderPacketType.Audio:
                senderPacketSet.AudioPacketDataList.Add(AudioSenderPacketData.Parse(packet));
                break;

            case SenderPacketType.Floor:
                senderPacketSet.FloorPacketDataList.Add(FloorSenderPacketData.Parse(packet));
                break;
            }
        }

        return(senderPacketSet);
    }
示例#4
0
    private void Update()
    {
        var         audioPacketDataSet = new List <AudioSenderPacketData>();
        SocketError error = SocketError.WouldBlock;

        while (true)
        {
            var packet = udpSocket.Receive(out error);
            if (packet == null)
            {
                break;
            }

            audioPacketDataSet.Add(AudioSenderPacketData.Parse(packet));
        }

        audioPacketDataSet.Sort((x, y) => x.frameId.CompareTo(y.frameId));

        float[] pcm   = new float[KH_SAMPLES_PER_FRAME * KH_CHANNEL_COUNT];
        int     index = 0;

        while (ringBuffer.FreeSamples >= pcm.Length)
        {
            if (index >= audioPacketDataSet.Count)
            {
                break;
            }

            var audioPacketData = audioPacketDataSet[index++];
            if (audioPacketData.frameId <= lastAudioFrameId)
            {
                continue;
            }

            audioDecoder.Decode(audioPacketData.opusFrame, pcm, KH_SAMPLES_PER_FRAME);
            ringBuffer.Write(pcm);
            lastAudioFrameId = audioPacketData.frameId;
        }
    }