Пример #1
0
    /// <summary>
    /// Corutine that captures an image from the camera, obtains its relevant data
    /// and sends the corresponding header and chunk messages.
    /// </summary>
    /// <returns>IEnumerator to be run.</returns>
    protected override IEnumerator SendStream()
    {
        uint textId = nextId;

        nextId += 1;
        Debug.Log("Sending snapshoot with ID " + textId + ".");
        //Texture2D texture = Paint(1000);
        int       minTexSide = Mathf.Min(cam.width, cam.height);
        int       x          = minTexSide == cam.width ? 0 : (cam.width - minTexSide) / 2;
        int       y          = minTexSide == cam.height ? 0 : (cam.height - minTexSide) / 2;
        Texture2D texture    = new Texture2D(minTexSide, minTexSide);

        texture.SetPixels(cam.GetPixels(x, y, minTexSide, minTexSide));
        texture.Apply(true);
        TextureScale.Bilinear(texture, resolution, resolution);
        //lastCapturedFrame = texture;
        Color32[] pixelData = texture.GetPixels32(0);
        int       size      = Mathf.FloorToInt(pixelData.Length / Mathf.Ceil(pixelData.Length * 4.0f / maxChunkSize));

        Debug.Log("Chunk size " + size);
        var headerMessage = new TextureHeaderMessage(networkIdentity.netId.Value, textId, texture.width, texture.height, size);

        SendHeaderMessage(textureMsgType, headerMessage);

        List <(int, Color32[], int)> chunks = DivideArrayInChunks(pixelData, size);

        foreach (var chunk in chunks)
        {
            var chunkMessage = new TextureChunkMessage(networkIdentity.netId.Value, textId, chunk.Item1, chunk.Item2, chunk.Item3);
            SendChunkMessage(textureMsgType, chunkMessage);
        }

        Debug.Log("SnapShoot for ID " + textId + " has been sent.");
        yield return(new WaitForSeconds(0.01f));
    }
Пример #2
0
    /// <summary>
    /// Processes the header messages received on the client.
    /// </summary>
    /// <param name="header">Image's header message.</param>
    private void OnTextureHeaderReceived(TextureHeaderMessage header)
    {
        TextureStruc textS = new TextureStruc(header.width, header.height, header.chunkSize);

        msgData.RecoverEarlyChunks(header, textS, SaveChunk);
        if (msgData.CheckTimestamp(header))
        {
            StartCoroutine(
                msgData.WaitTillReceiveAllTheStream(
                    header,
                    (uint id) => msgData[id].pixelsReceived < msgData[id].data.Length,
                    streamTimeout));
        }
    }