private void SendFixedSizedChunk(NetworkConnection connection, byte[] data, int dataSize)
        {
            DateTime currentTime = DateTime.Now;
            float    seconds     = (float)(currentTime - timeOfLastPacketSend).TotalSeconds;

            timeOfLastPacketSend = currentTime;
            if (seconds < 10.0)
            {
                if (worstTimeBetweenPackets < seconds)
                {
                    worstTimeBetweenPackets = seconds;
                }

                if (ShowInterPacketTime)
                {
                    Debug.LogFormat("Microphone: Milliseconds since last sent: {0}, Worst: {1}",
                                    (seconds * 1000.0).ToString(CultureInfo.InvariantCulture),
                                    (worstTimeBetweenPackets * 1000.0).ToString(CultureInfo.InvariantCulture));
                }
            }

            int clientId = SharingStage.Instance.Manager.GetLocalUser().GetID();

            // pack the header
            NetworkOutMessage msg = connection.CreateMessage((byte)MessageID.AudioSamples);

            int dataCountFloats = dataSize / 4;

            msg.Write((byte)5); // 8 byte header size

            Int32 pack = 0;

            versionPacker.SetBits(ref pack, 1);                   // version
            audioStreamCountPacker.SetBits(ref pack, 1);          // AudioStreamCount
            channelCountPacker.SetBits(ref pack, 1);              // ChannelCount
            sampleRatePacker.SetBits(ref pack, sampleRateType);   // SampleRate: 1 = 16000, 3 = 48000
            sampleTypePacker.SetBits(ref pack, 0);                // SampleType
            sampleCountPacker.SetBits(ref pack, dataCountFloats); // SampleCount (data count is in bytes and the actual data is in floats, so div by 4)
            codecTypePacker.SetBits(ref pack, 0);                 // CodecType
            mutePacker.SetBits(ref pack, Mute ? 1 : 0);
            sequenceNumberPacker.SetBits(ref pack, sequenceNumber++);
            sequenceNumber %= 32;

            msg.Write(pack); // the packed bits

            // This is where stream data starts. Write all data for one stream

            msg.Write(0.0f);     // average amplitude.  Not needed in direction from client to server.
            msg.Write(clientId); // non-zero client ID for this client.

            // HRTF position bits

            Vector3 cameraPosRelativeToGlobalAnchor       = Vector3.zero;
            Vector3 cameraDirectionRelativeToGlobalAnchor = Vector3.zero;

            if (GlobalAnchorTransform != null)
            {
                cameraPosRelativeToGlobalAnchor = MathUtils.TransformPointFromTo(
                    null,
                    GlobalAnchorTransform,
                    CameraCache.Main.transform.position);

                cameraDirectionRelativeToGlobalAnchor = MathUtils.TransformDirectionFromTo(
                    null,
                    GlobalAnchorTransform,
                    CameraCache.Main.transform.position);
            }

            cameraPosRelativeToGlobalAnchor.Normalize();
            cameraDirectionRelativeToGlobalAnchor.Normalize();

            // Camera position
            msg.Write(cameraPosRelativeToGlobalAnchor.x);
            msg.Write(cameraPosRelativeToGlobalAnchor.y);
            msg.Write(cameraPosRelativeToGlobalAnchor.z);

            // HRTF direction bits
            msg.Write(cameraDirectionRelativeToGlobalAnchor.x);
            msg.Write(cameraDirectionRelativeToGlobalAnchor.y);
            msg.Write(cameraDirectionRelativeToGlobalAnchor.z);

            msg.WriteArray(data, (uint)dataCountFloats * 4);

            connection.Send(msg, MessagePriority.Immediate, MessageReliability.ReliableOrdered, MessageChannel.Audio, true);
        }
Exemplo n.º 2
0
    private void SendFixedSizedChunk(float[] dataFloats, int dataCountFloats)
    {
        System.DateTime currentTime = System.DateTime.Now;
        float           seconds     = (float)(currentTime - this.timeOfLastPacketSend).TotalSeconds;

        this.timeOfLastPacketSend = currentTime;
        if (seconds < 10.0)
        {
            if (this.worstTimeBetweenPackets < seconds)
            {
                this.worstTimeBetweenPackets = seconds;
            }

            if (ShowInterPacketTime)
            {
                UnityEngine.Debug.Log("Microphone: Millisecs since last sent: " + seconds * 1000.0 + "  Worst: " + this.worstTimeBetweenPackets * 1000.0);
            }
        }

        // pack the header
        NetworkOutMessage msg = this.connection.CreateMessage((byte)MessageID.AudioSamples);

        msg.Write((byte)5);     // 8 byte header size

        Int32 pack = 0;

        versionPacker.SetBits(ref pack, 1);                   // version
        audioStreamCountPacker.SetBits(ref pack, 1);          // AudioStreamCount
        channelCountPacker.SetBits(ref pack, 1);              // ChannelCount
        sampleRatePacker.SetBits(ref pack, sampleRateType);   // SampleRate: 1 = 16000, 3 = 48000
        sampleTypePacker.SetBits(ref pack, 0);                // SampleType
        sampleCountPacker.SetBits(ref pack, dataCountFloats); // SampleCount (data count is in bytes and the actual data is in floats, so div by 4)
        codecTypePacker.SetBits(ref pack, 0);                 // CodecType
        mutePacker.SetBits(ref pack, this.muteMicrophone ? 1 : 0);
        sequenceNumberPacker.SetBits(ref pack, sequenceNumber++);
        sequenceNumber %= 32;

        msg.Write((int)pack);                   // the packed bits

        // This is where stream data starts. Write all data for one stream

        msg.Write((float)0.0f);         // average amplitude.  Not needed in direction from client to server.
        msg.Write((int)this.clientId);  // non-zero client ID for this client.

        // HRTF position bits

        Vector3 cameraPosRelativeToGlobalAnchor       = Vector3.zero;
        Vector3 cameraDirectionRelativeToGlobalAnchor = Vector3.zero;

        if (this.GlobalAnchorTransform != null)
        {
            cameraPosRelativeToGlobalAnchor = MathUtils.TransformPointFromTo(
                null,
                this.GlobalAnchorTransform,
                Camera.main.transform.position);

            cameraDirectionRelativeToGlobalAnchor = MathUtils.TransformDirectionFromTo(
                null,
                this.GlobalAnchorTransform,
                Camera.main.transform.position);
        }

        cameraPosRelativeToGlobalAnchor.Normalize();
        cameraDirectionRelativeToGlobalAnchor.Normalize();

        // Camera position
        msg.Write(cameraPosRelativeToGlobalAnchor.x);
        msg.Write(cameraPosRelativeToGlobalAnchor.y);
        msg.Write(cameraPosRelativeToGlobalAnchor.z);

        // HRTF direction bits
        msg.Write(cameraDirectionRelativeToGlobalAnchor.x);
        msg.Write(cameraDirectionRelativeToGlobalAnchor.y);
        msg.Write(cameraDirectionRelativeToGlobalAnchor.z);

        if (this.dataStreamSendBytes == null || this.dataStreamSendBytes.Length != dataCountFloats * 4)
        {
            this.dataStreamSendBytes = new byte[dataCountFloats * 4];
        }
        dataFloats.ToByteArray4(this.dataStreamSendBytes, dataCountFloats);
        msg.WriteArray(this.dataStreamSendBytes, 4 * (uint)dataCountFloats);

        this.connection.Send(msg, MessagePriority.Immediate, MessageReliability.ReliableOrdered, MessageChannel.Audio, true);
    }