-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideoDecodeExample.cs
92 lines (80 loc) · 2.99 KB
/
VideoDecodeExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.SDK3.Components.Video;
// This example loads and decodes a random color from random-data-api.com and prints it out on the console.
//
// It depends on:
// * Udon Video Decoder
// * UdonDecodeLib
// * UdonXML
//
// Attach these dependencies to a gameobject in your scene, and reference them in the variables defined bellow.
// Make sure to add a Render Texture and Blank Texture to the Video Decoder.
public class VideoDecodeExample : UdonSharpBehaviour
{
// Libraries. Remember to link these up in Unity.
public VideoDecoder videoDecoder;
public UdonDecodeLib decodeLib;
public UdonXML udonXML;
// URL to request, must be defined at compile time, can't be generated at runtime.
public VRCUrl url = new VRCUrl(
"https://[PIXEL PROXY HOST]/proxy?auth=[PIXEL PROXY KEY]&rt=mp4&df=xml&et=binary&url="
+ "https%3A//random-data-api.com/api/color/random_color"
);
// Place to put returned data or error code.
private byte[] data;
private VideoError errorCode;
void Start()
{
// Must give video player some time to initialize.
SendCustomEventDelayedSeconds("LoadStuff", 5);
}
public void LoadStuff()
{
Debug.Log("Loading...");
// Where the magic happens.
// Reference *this*, as callback parameters are on this script.
// Callback variables and methods are referenced by name as a string.
videoDecoder.LoadURL(url, this, "data", "OnLoaded", "errorCode", "OnError");
}
// Called when data is successfully loaded.
public void OnLoaded()
{
// Pixel proxy data is set to be base64 encoded XML, so decode that.
var xml = udonXML.LoadXml(
decodeLib.DecodeBase64String(
decodeLib.DecodeByteArray(data)));
// Decoding methods return null on failure.
if(xml == null)
{
Debug.Log("Failed decoding data!");
return;
}
Debug.Log("Got XML: \n" + udonXML.SaveXmlWithIdent(xml, " "));
// Get data->hex_value from XML.
object hex_value = udonXML.GetChildNodeByName(
udonXML.GetChildNodeByName(xml, "data"), "hex_value");
if(hex_value != null)
{
float[] f = decodeLib.DecodeHexColor(udonXML.GetNodeValue(hex_value));
Color color = new Color(f[0], f[1], f[2], f[3]);
Debug.Log("Got color: " + color);
}
}
// Called on video player error
public void OnError()
{
// Retry automatically if rate limited.
// VRChat limits requests to one every 5 seconds.
if (errorCode == VideoError.RateLimited)
{
Debug.Log("Rate limited, retrying in 5...");
SendCustomEventDelayedSeconds("LoadStuff", 5);
}
else
{
Debug.Log("Failed loading!");
}
}
}