/// <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)); }
/// <summary> /// Saves on the data structure the data contained in a chunk. /// </summary> /// <param name="row">Image's chunk message.</param> private void SaveChunk(TextureChunkMessage row) { TextureStruc textureStruc = msgData[row.id]; for (int i = 0; i < row.data.Length && i < row.size && textureStruc.pixelsReceived < textureStruc.data.Length; i++) { textureStruc.data[i + row.order * textureStruc.chunkSize] = row.data[i]; textureStruc.pixelsReceived += 1; } msgData[row.id] = textureStruc; Debug.Log(textureStruc.pixelsReceived + "/" + textureStruc.data.Length + " pixels currently recived for ID " + row.id + "."); }
/// <summary> /// Processes the chunk messages received on the client. /// </summary> /// <param name="row">Image's chunk message.</param> private void OnTextureChunkReceived(TextureChunkMessage row) { msgData.AddChunk(row, SaveChunk); }