0

I am trying to build a Unity application that sends a video feed from a USB camera plugged into an Android phone through an Agora.io video call. I am using this Unity asset:

https://assetstore.unity.com/packages/tools/integration/usb-camera-for-unity-android-151744

The asset has a method called GetFrame() that I am using to get each frame and send through Agora.io as a byte[] array. I need to convert the Texture2D returned by GetFrame() into a byte[] before sending it. The problem is that Unity's Texture2D.GetRawTextureData() method is returning an empty byte array. The Texture2D is not null and the texture appears fine when output to a panel in the scene. Is there another way of doing this? What could I be doing wrong?

while (!usbStopped)
{
    tempTexture2D = usbCamera.GetFrame(0);
    byte[] bytes = tempTexture2D.GetRawTextureData();
    
    if (bytes.Length == 0)
        throw new Exception("Frame bytes empty!");

    yield return StartCoroutine(PushFrame(bytes, 640, 480, () => bytes = null);
}

The Exception is always getting hit. Any help much appreciated.

Salbrox
  • 143
  • 1
  • 15

1 Answers1

0

GetRawTextureData() should work for Texture2D instance and I had tested this before. The possible cause that you are getting 0 length may come from the renderer to that texture. Is there a way you can check if the usbCamera.GetFrame(0) call actually return some contents? Does it happen to need a renderer set up first? Also consider this line of description on the documentation:

Also note that the system memory copy might not match what is in the GPU texture data at the moment. For example, after calling SetPixels the system memory copy is already modified, but the GPU copy will only match after calling Apply(). Some cases of Graphics.CopyTexture might be copying only the GPU texture side (e.g. copying from a RenderTexture into a Texture2D), and this will not be reflected in GetRawTextureData contents.
Rick Cheng
  • 654
  • 3
  • 7