Search Unity

Video Sending realtime texture over Photon Network

Discussion in 'Audio & Video' started by jobjongebloet, May 11, 2019.

  1. jobjongebloet

    jobjongebloet

    Joined:
    Apr 8, 2019
    Posts:
    7
    I want to copy real-time video from one unity to another over the Photon network.
    I managed to get it working. See picture 1.
    I don't have experience in Networking and hope someone can take a quick look at my code and give me some advice to get good performance. As of right now it is very slow, inverted, and doesn't output the right texture.

    I will have a look at setting up a stream with the bolt engine.
    https://doc.photonengine.com/en-us/bolt/current/in-depth/streaming-bytes

    Thank you for taking a look at my thread.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class Send : MonoBehaviour
    7. {
    8.     public Texture MyTexture;
    9.     public Texture2D receivedTexture;
    10.     public byte[] receivedBytes;
    11.     public byte[] N;
    12.     public PhotonView PV;
    13.     public Texture2D tex2D;
    14.  
    15.  
    16.     void Start()
    17.     {
    18.         MyTexture = null;
    19.         PV = GetComponent<PhotonView>();
    20.     }
    21.  
    22.         static public Texture2D GetRTPixels(RenderTexture rt)
    23.         {
    24.             // Remember currently active render texture
    25.             RenderTexture currentActiveRT = RenderTexture.active;
    26.  
    27.             // Set the supplied RenderTexture as the active one
    28.             RenderTexture.active = rt;
    29.  
    30.             // Create a new Texture2D and read the RenderTexture image into it
    31.             Texture2D tex = new Texture2D(rt.width, rt.height);
    32.             tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
    33.  
    34.             // Restorie previously active render texture
    35.             RenderTexture.active = currentActiveRT;
    36.             return tex;
    37.         }
    38.  
    39.     void Update()
    40.     {
    41.  
    42.         PV = GetComponent<PhotonView>();
    43.  
    44.         if (PhotonNetwork.IsMasterClient)
    45.         {
    46.             MyTexture = GetComponent<Renderer>().material.mainTexture;
    47.             //RenderTexture.active = myRenderTexture;
    48.             //myTexture2D.ReadPixels(new Rect(0, 0, myRenderTexture.width, myRenderTexture.height), 0, 0);
    49.             //myTexture2D.Apply();
    50.             Texture2D tex2D = (Texture2D)MyTexture;
    51.             tex2D = new Texture2D(tex2D.width, tex2D.height);
    52.             StartCoroutine(GetRenderTexturePixel(tex2D));
    53.             PV.RPC("UpdateMonitor", RpcTarget.Others, N);
    54.             return;
    55.         }
    56.         else if (!PhotonNetwork.IsMasterClient)
    57.         {
    58.             if (MyTexture != null)
    59.             {
    60.                 tex2D = new Texture2D(MyTexture.width, MyTexture.height);
    61.                 tex2D.LoadImage(N);
    62.                 GetComponent<Renderer>().material.mainTexture = tex2D;
    63.             }      
    64.         }
    65.  
    66.     }
    67.  
    68.     IEnumerator GetRenderTexturePixel(Texture2D tex)
    69.     {
    70.         Texture2D tempTex = new Texture2D(tex.width, tex.height);
    71.         yield return new WaitForEndOfFrame();
    72.         tempTex.ReadPixels(new Rect(0, 0, 400, 300), 0, 0);
    73.         tempTex.Apply();
    74.         N = tempTex.EncodeToPNG();
    75.     }
    76.  
    77.     [PunRPC]
    78.     public void ReceiveWebcamPNG(byte[] receivedByte)
    79.     {
    80.         receivedTexture = null;
    81.         receivedTexture = new Texture2D(1, 1);
    82.         receivedTexture.LoadImage(receivedByte);
    83.         GetComponent<Renderer>().material.mainTexture = receivedTexture;
    84.     }
    85. }
    86.  
    87.  
    upload_2019-5-11_20-20-36.png
     
  2. vsksanthosh15

    vsksanthosh15

    Joined:
    Aug 6, 2019
    Posts:
    1
    Can you give me the git link
     
    Dennysvital likes this.
  3. Dennysvital

    Dennysvital

    Joined:
    Sep 5, 2014
    Posts:
    4
    Can you help me too?
     
  4. Everysimo

    Everysimo

    Joined:
    Nov 9, 2022
    Posts:
    1
    I'm trying with Photon Fusion, did you manage to do it?