Author Topic: Can TNet be used to stream video?  (Read 174 times)

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 8
  • -Receive: 0
  • Posts: 33
    • View Profile
Can TNet be used to stream video?
« on: September 13, 2017, 02:02:54 AM »
Can TNet be used stream video?
Specifically...Is it capable of sending a webcam.device / webtexture (with video) through RFC's to other players / users?

Ive tried searching the forum and found this: http://www.tasharen.com/forum/index.php?topic=5387.msg25649#msg25649

but I'n not 100% sure its related...

If TNet is capable...Is there a basic example to get one started in the right direction?

cmifwdll

  • Global Moderator
  • Full Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 132
  • Posts: 241
  • TNet Alchemist
    • View Profile
Re: Can TNet be used to stream video?
« Reply #1 on: September 13, 2017, 04:45:21 AM »
In the end, TNet just sends bytes like everything else, so yes it's supported.
But video is heavy, so you're going to have to perform a lot of compression to achieve any kind of suitable result.
I don't have any experience in this area. My brief (5 minutes of googling) research shows H.264 seems to be the standard.
I'd look at how something like OBS works and go from there. I think OBS is open source? I mean, it has open in the name, so I'd hope so.
Then you just have to get the bytes from the webcam, feed them to your encoder, and send the output across the network. Then decode and display on the other end.

A lot harder than it sounds, I'm sure :D maybe Unity already has an encoder you can use? I'd look into their new video player stuff.

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 8
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: Can TNet be used to stream video?
« Reply #2 on: September 13, 2017, 05:42:54 AM »
thanks for the reply!
My cameras does the encoding to h.264.....

I have a scene setup where :
  1. public RawImage videoholder;
  2.  
  3. void Start(){
  4.                 WebCamDevice[] devices = WebCamTexture.devices;
  5.  
  6.                 // for debugging purposes, prints available devices to the console
  7.                 for (int i = 0; i < devices.Length; i++) {
  8.                         print ("Webcam available: " + devices [i].name);
  9.                 }
  10.  
  11.                 Renderer rend = this.GetComponentInChildren<Renderer> ();
  12.  
  13.                 // assuming the first available WebCam is desired
  14.                 WebCamTexture tex = new WebCamTexture (devices [0].name);
  15.                 videoholder.texture = tex;
  16.                 tex.Play ();
  17.         }

All good there... the build detects the clients camera and shows it on a RawImage object (videoholder)..

Im stuck here at how to send the data on the RawImage through a RFC..... or maybe the data directly from the devices camera?

cmifwdll

  • Global Moderator
  • Full Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 132
  • Posts: 241
  • TNet Alchemist
    • View Profile
Re: Can TNet be used to stream video?
« Reply #3 on: September 13, 2017, 05:55:16 AM »
WebCamTexture.GetPixels32 is the function you need, I think. See this page for an example: https://docs.unity3d.com/ScriptReference/WebCamTexture-didUpdateThisFrame.html
Keep a close eye on your bytes per second.

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 8
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: Can TNet be used to stream video?
« Reply #4 on: September 13, 2017, 07:35:49 AM »
Thanks for the link....

Can I send the 'data' through RFC like this:
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ExampleClass : MonoBehaviour {
  5.     public WebCamTexture webcamTexture;
  6.     public Color32[] data;
  7.     void Start() {
  8.         webcamTexture = new WebCamTexture();
  9.         webcamTexture.Play();
  10.         data = new Color32[webcamTexture.width * webcamTexture.height];
  11.     }
  12.     void Update() {
  13.         if (webcamTexture.didUpdateThisFrame){
  14.             webcamTexture.GetPixels32(data);
  15. tno.SendQuickly("WebCam",Target.All, data);
  16. }
  17.         }
  18.  
  19. }

cmifwdll

  • Global Moderator
  • Full Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 132
  • Posts: 241
  • TNet Alchemist
    • View Profile
Re: Can TNet be used to stream video?
« Reply #5 on: September 13, 2017, 08:22:13 AM »
Yep, but a few major problems:
1. You haven't created a "WebCam" RFC. You probably just omitted it from your example, but thought I'd point it out.
2. data is a buffer with size webcamTexture.width * webcamTexture.height of a type that's 4 bytes long. The most popular webcam on Amazon is 1920x1080, so this means the buffer is 8294400 bytes.
3. You're sending 8294400 bytes every frame. Does your game run at and your webcam capture at 60 FPS? That's 497664000 bytes a second (roughly 500 MB a second, big B).

You're going to need to take the data from GetPixels32 and run it through an encoder to shrink that size dramatically. You'll probably have to restrict the webcam capture to a certain rate, too, but I guess that depends on the encoder. If your webcam performs hardware encoding there should be some way to read that encoded stream from the driver. This is obviously device- and OS-specific but I think it'd be ideal. I've never worked with drivers though so I don't know if that's even possible.

Maybe ArenMook or someone else has networked a webcam through Unity and can provide more direction.

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 8
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: Can TNet be used to stream video?
« Reply #6 on: September 13, 2017, 11:37:06 AM »
Thanks for the help and direction cmifwdll..... I'll do a bit more research and post some results on my progress for the sake of how ....if it can be done..