Skip to content

Commit 4b1d068

Browse files
authored
Expose Bitrate and Change Render Streaming (#6)
- Added Bit Rate - Added New VR Camera Streaming Component - Updated Samples with new Prefab Changes
1 parent 9ed56fd commit 4b1d068

File tree

8 files changed

+192
-170
lines changed

8 files changed

+192
-170
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ All notable changes to com.unity.renderstreaming package will be documented in t
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.4.0] - 2021-08-17
8+
9+
### Changed
10+
11+
- **MAJOR** Combined left eye and right eye into a single render texture on one video track to increase encoding performance & maintain synconization between eyes
12+
- Removed Video Resizing as this implementation needs to be re-evaluated
13+
- Increased default resolution to 2700 x 1500 (both eyes)
14+
15+
### Added
16+
17+
- Added new VRCamStreaming component that exposes bitrate and framerate encoder parameters
18+
719
## [0.3.0] - 2021-08-10
820

921
### Changed

Runtime/Prefabs/Render Streaming Services.prefab

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ GameObject:
1111
- component: {fileID: 1793859182182230346}
1212
- component: {fileID: 1793859182182230347}
1313
- component: {fileID: 1270632014}
14+
- component: {fileID: 1084652618}
1415
m_Layer: 0
1516
m_Name: WebVR Camera
1617
m_TagString: Untagged
@@ -49,9 +50,7 @@ MonoBehaviour:
4950
m_EditorClassIdentifier:
5051
local: 0
5152
label:
52-
VRCameras:
53-
- {fileID: 1793859182864016192}
54-
- {fileID: 1793859183251251332}
53+
VRCameras: {fileID: 1084652618}
5554
VRPoseEvent:
5655
m_PersistentCalls:
5756
m_Calls:
@@ -106,6 +105,24 @@ MonoBehaviour:
106105
VRAxisEvent:
107106
m_PersistentCalls:
108107
m_Calls: []
108+
--- !u!114 &1084652618
109+
MonoBehaviour:
110+
m_ObjectHideFlags: 0
111+
m_CorrespondingSourceObject: {fileID: 0}
112+
m_PrefabInstance: {fileID: 0}
113+
m_PrefabAsset: {fileID: 0}
114+
m_GameObject: {fileID: 1793859182182230341}
115+
m_Enabled: 1
116+
m_EditorHideFlags: 0
117+
m_Script: {fileID: 11500000, guid: 88dc1ef0a0e7d1b4a897f095b852ab38, type: 3}
118+
m_Name:
119+
m_EditorClassIdentifier:
120+
streamingSize: {x: 2700, y: 1500}
121+
leftEye: {fileID: 1793859182864016192}
122+
rightEye: {fileID: 1793859183251251332}
123+
depth: 0
124+
antiAliasing: 1
125+
BIT_RATE: 9000000
109126
--- !u!1 &1793859182664022047
110127
GameObject:
111128
m_ObjectHideFlags: 0
@@ -256,7 +273,6 @@ GameObject:
256273
m_Component:
257274
- component: {fileID: 1793859182864016220}
258275
- component: {fileID: 1793859182864016192}
259-
- component: {fileID: 1793859182864016198}
260276
m_Layer: 0
261277
m_Name: Left Eye Render Streaming Camera
262278
m_TagString: Untagged
@@ -321,21 +337,6 @@ Camera:
321337
m_OcclusionCulling: 1
322338
m_StereoConvergence: 10
323339
m_StereoSeparation: 0.022
324-
--- !u!114 &1793859182864016198
325-
MonoBehaviour:
326-
m_ObjectHideFlags: 0
327-
m_CorrespondingSourceObject: {fileID: 0}
328-
m_PrefabInstance: {fileID: 0}
329-
m_PrefabAsset: {fileID: 0}
330-
m_GameObject: {fileID: 1793859182864016223}
331-
m_Enabled: 1
332-
m_EditorHideFlags: 0
333-
m_Script: {fileID: 11500000, guid: e3379f8b7ab35724b965e26351c2004e, type: 3}
334-
m_Name:
335-
m_EditorClassIdentifier:
336-
streamingSize: {x: 1440, y: 1600}
337-
depth: 0
338-
antiAliasing: 2
339340
--- !u!1 &1793859182968099965
340341
GameObject:
341342
m_ObjectHideFlags: 0
@@ -529,7 +530,6 @@ GameObject:
529530
m_Component:
530531
- component: {fileID: 1793859183251251333}
531532
- component: {fileID: 1793859183251251332}
532-
- component: {fileID: 1793859183251251328}
533533
m_Layer: 0
534534
m_Name: Right Eye Render Streaming Camera
535535
m_TagString: Untagged
@@ -594,21 +594,6 @@ Camera:
594594
m_OcclusionCulling: 1
595595
m_StereoConvergence: 10
596596
m_StereoSeparation: 0.022
597-
--- !u!114 &1793859183251251328
598-
MonoBehaviour:
599-
m_ObjectHideFlags: 0
600-
m_CorrespondingSourceObject: {fileID: 0}
601-
m_PrefabInstance: {fileID: 0}
602-
m_PrefabAsset: {fileID: 0}
603-
m_GameObject: {fileID: 1793859183251251330}
604-
m_Enabled: 1
605-
m_EditorHideFlags: 0
606-
m_Script: {fileID: 11500000, guid: e3379f8b7ab35724b965e26351c2004e, type: 3}
607-
m_Name:
608-
m_EditorClassIdentifier:
609-
streamingSize: {x: 1440, y: 1600}
610-
depth: 0
611-
antiAliasing: 2
612597
--- !u!1 &1793859183478774782
613598
GameObject:
614599
m_ObjectHideFlags: 0
@@ -730,9 +715,9 @@ MonoBehaviour:
730715
m_Script: {fileID: 11500000, guid: 31b94e633688a294abc5410d3190b6f5, type: 3}
731716
m_Name:
732717
m_EditorClassIdentifier:
718+
gameID:
733719
streams:
734-
- {fileID: 1793859182864016198}
735-
- {fileID: 1793859183251251328}
720+
- {fileID: 1084652618}
736721
- {fileID: 1793859183478774780}
737722
- {fileID: 1793859182182230347}
738723
--- !u!1 &9010668948091610382

Runtime/Scripts/VRBroadcast.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,16 @@ public void OnAddChannel(SignalingEventData data) {
114114
private string GetGameID(string sdp) {
115115
string[] parameters = sdp.Split('\n'); //split on new line
116116
string customParam = parameters[parameters.Length - 2].Split('=')[1]; //get line before last new line and split on =
117-
string value = ""; //get dictionary value
118-
JsonConvert.DeserializeObject<Dictionary<string, string>>(customParam).TryGetValue("user", out value);
119-
return value;
117+
118+
Dictionary<string, string> jsonMap = JsonConvert.DeserializeObject<Dictionary<string, string>>(customParam);
119+
if (jsonMap != null) {
120+
string value = ""; //get dictionary value
121+
jsonMap.TryGetValue("user", out value);
122+
return value;
123+
} else {
124+
return ""; //TODO: check if this is a valid fail case
125+
}
126+
120127
}
121128
}
122129
}

Runtime/Scripts/VRCamStream.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using UnityEngine;
2+
using Unity.RenderStreaming;
3+
using Unity.WebRTC;
4+
5+
namespace FusedVR.VRStreaming {
6+
public class VRCamStream : VideoStreamBase {
7+
8+
#region Variables
9+
[SerializeField]
10+
[Tooltip("The Left Eye of the VR Camera")]
11+
private Camera leftEye;
12+
13+
[SerializeField]
14+
[Tooltip("The Right Eye of the VR Camera")]
15+
private Camera rightEye;
16+
17+
[SerializeField]
18+
[Tooltip("Defines the depth buffer used for render streaming (0, 16, 24, 32)")]
19+
private int depth = 0;
20+
21+
[SerializeField]
22+
[Tooltip("Defines the number of samples for anti-aliasing (1, 2, 4, 8)")]
23+
private int antiAliasing = 1;
24+
25+
[SerializeField]
26+
[Tooltip("Defines the default bitrate to be used by the encoders in bits per second")]
27+
private ulong BIT_RATE = 9000000; //default bitrate of 9 Mbps
28+
29+
/// <summary>
30+
/// The Main Connection ID that this instance is connected with
31+
/// </summary>
32+
private string mainConnection = "";
33+
34+
35+
public const uint MAX_FRAMERATE = 90; //default max framerate target
36+
37+
public override Texture SendTexture => leftEye.targetTexture; //should be the same as right eye
38+
#endregion
39+
40+
#region Events
41+
// Start is called before the first frame update
42+
void Start() {
43+
OnStartedStream += StartStream;
44+
}
45+
46+
private void StartStream(string connectionId) {
47+
mainConnection = connectionId;
48+
ChangeSendParameters(BIT_RATE, MAX_FRAMERATE);
49+
}
50+
51+
/// <summary>
52+
/// Assigns Render Textures to our cameras and then
53+
/// Creates a Video Streaming Track that references the render textures
54+
/// </summary>
55+
protected override MediaStreamTrack CreateTrack() {
56+
57+
RenderTextureFormat format = WebRTC.GetSupportedRenderTextureFormat(SystemInfo.graphicsDeviceType);
58+
RenderTexture rt = new RenderTexture(streamingSize.x, streamingSize.y, depth, format) {
59+
antiAliasing = antiAliasing
60+
};
61+
rt.Create();
62+
63+
leftEye.targetTexture = rt;
64+
leftEye.rect = new Rect(Vector2.zero, new Vector2(0.5f, 1f));
65+
66+
rightEye.targetTexture = rt;
67+
rightEye.rect = new Rect(new Vector2(0.5f, 0f), new Vector2(0.5f, 1f));
68+
69+
return new VideoStreamTrack("VR Camera", rt);
70+
}
71+
#endregion
72+
73+
#region Public Methods
74+
/// <summary>
75+
/// Change Parameters associated with encoders for sending data to browser
76+
/// </summary>
77+
public void ChangeSendParameters(ulong? bitrate , uint? framerate) {
78+
if (Senders.TryGetValue(mainConnection, out var sender)) {
79+
RTCRtpSendParameters parameters = sender.GetParameters();
80+
foreach (var encoding in parameters.encodings) {
81+
if (bitrate != null) {
82+
encoding.minBitrate = bitrate;
83+
encoding.maxBitrate = bitrate;
84+
}
85+
86+
if (framerate != null) {
87+
encoding.maxFramerate = framerate;
88+
}
89+
}
90+
91+
sender.SetParameters(parameters);
92+
}
93+
}
94+
#endregion
95+
96+
}
97+
}
98+

Runtime/Scripts/VRCamStream.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Scripts/VRInputManager.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class VRInputManager : InputChannelReceiverBase {
2424
/// Camera that are used for VR Render Streaming
2525
/// </summary>
2626
[Tooltip("The Cameras that are responsible for VR Render Streaming")]
27-
public Camera[] VRCameras;
27+
public VRCamStream VRCameras;
2828

2929
#region Constants
3030
/// <summary>
@@ -126,11 +126,12 @@ protected override void OnMessage(byte[] bytes) {
126126
case VRDataType.Display:
127127
int width = (int)BitConverter.ToUInt32(bytes, 2);
128128
int height = (int)BitConverter.ToUInt32(bytes, 6);
129-
foreach (Camera cam in VRCameras) {
130-
cam.targetTexture.Release();
131-
cam.targetTexture.width = width / 2; // half since the the width covers both eyes
132-
cam.targetTexture.height = height;
133-
}
129+
//foreach (Camera cam in VRCameras) {
130+
// cam.targetTexture.Release();
131+
// cam.targetTexture.width = width;
132+
// cam.targetTexture.height = height;
133+
//}
134+
//TODO: need to find proper way to resize texture based on data so that the video channel updates
134135

135136
break;
136137
}

0 commit comments

Comments
 (0)