Skip to content

Commit b8f8cf8

Browse files
Update frame selection with respect to spectator view ping time. (microsoft#159)
1 parent 2e42345 commit b8f8cf8

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

SpectatorView/Compositor/CompositorDLL/VideoEncoder.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ bool VideoEncoder::Initialize(ID3D11Device* device)
3737
HRESULT hr = E_PENDING;
3838
hr = MFStartup(MF_VERSION);
3939

40+
QueryPerformanceFrequency(&freq);
41+
4042
#if HARDWARE_ENCODE_VIDEO
4143
MFCreateDXGIDeviceManager(&resetToken, &deviceManager);
4244

@@ -179,9 +181,6 @@ void VideoEncoder::WriteAudio(byte* buffer, LONGLONG timestamp)
179181
return;
180182
}
181183

182-
LARGE_INTEGER freq;
183-
QueryPerformanceFrequency(&freq);
184-
185184
LONGLONG sampleTimeNow = timestamp;
186185
if (sampleTimeNow < 0) { sampleTimeNow *= -1; }
187186
LONGLONG sampleTimeStart = startTime;
@@ -267,9 +266,6 @@ void VideoEncoder::WriteVideo(byte* buffer, LONGLONG timestamp, LONGLONG duratio
267266
return;
268267
}
269268

270-
LARGE_INTEGER freq;
271-
QueryPerformanceFrequency(&freq);
272-
273269
LONGLONG sampleTimeNow = timestamp;
274270
if (sampleTimeNow < 0) { sampleTimeNow *= -1; }
275271
LONGLONG sampleTimeStart = startTime;

SpectatorView/Compositor/CompositorDLL/VideoEncoder.h

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class VideoEncoder
4848
void WriteVideo(byte* buffer, LONGLONG timestamp, LONGLONG duration);
4949
void WriteAudio(byte* buffer, LONGLONG timestamp);
5050

51+
LARGE_INTEGER freq;
52+
5153
class VideoInput
5254
{
5355
public:

SpectatorView/Compositor/UnityCompositorInterface/UnityCompositorInterface.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ UNITYDLL void GetEarliestHologramPose(
189189
// Set hologram poses when we get them on the network.
190190
UNITYDLL void SetHologramPose(
191191
float rotX, float rotY, float rotZ, float rotW,
192-
float posX, float posY, float posZ)
192+
float posX, float posY, float posZ, float msOffset)
193193
{
194194
if (ci == nullptr)
195195
{
@@ -201,7 +201,10 @@ UNITYDLL void SetHologramPose(
201201

202202
LONGLONG timestamp = time.QuadPart;
203203

204-
auto hologramFrame = ci->GetNextHologramFrame(timestamp);
204+
// Convert offset to microseconds.
205+
LONGLONG offset = (LONGLONG)(msOffset * 1000.0f);
206+
207+
auto hologramFrame = ci->GetNextHologramFrame(timestamp - offset);
205208
if (hologramFrame != nullptr)
206209
{
207210
hologramFrame->rotX = rotX;
@@ -240,11 +243,9 @@ UNITYDLL void UpdateSpectatorView()
240243
if (cachedTime != colorTime)
241244
{
242245
LONGLONG frameDuration = ci->GetColorDuration();
243-
float buffer = 0.1f * frameDuration;
244-
245246
// Find a pose on the leading end of this color frame.
246247
const auto frame = ci->FindClosestHologramFrame(
247-
colorTime - frameDuration + buffer, (LONGLONG)(_frameOffset * (float)frameDuration));
248+
colorTime - frameDuration, (LONGLONG)(_frameOffset * (float)frameDuration));
248249

249250
if (frame != nullptr)
250251
{

SpectatorView/Samples/SharedHolograms/Assets/Addons/HolographicCameraRig/SV_UNET/Scripts/PlayerController.cs

+20-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private static extern bool GetHeadTransform(System.IntPtr unityCoordinateSystem,
2626
[DllImport("UnityCompositorInterface")]
2727
private static extern bool SetHologramPose(
2828
float rotX, float rotY, float rotZ, float rotW,
29-
float posX, float posY, float posZ);
29+
float posX, float posY, float posZ, float msOffset);
3030
#endif
3131
#endregion
3232

@@ -40,7 +40,6 @@ public override float GetNetworkSendInterval()
4040
return 0.033f;
4141
}
4242

43-
4443
private static PlayerController _Instance = null;
4544
public static PlayerController Instance
4645
{
@@ -73,6 +72,9 @@ public bool CanEstablishAnchor()
7372
[SyncVar]
7473
private Vector3 localPosition;
7574

75+
[SyncVar]
76+
int spectatorViewPing = 0;
77+
7678
/// <summary>
7779
/// The rotation relative to the shared world anchor.
7880
/// </summary>
@@ -109,10 +111,15 @@ public void SetColorDuration(long value)
109111
/// <param name="postion">the localPosition to set</param>
110112
/// <param name="rotation">the localRotation to set</param>
111113
[Command(channel = 1)]
112-
public void CmdTransform(Vector3 postion, Quaternion rotation)
114+
public void CmdTransform(Vector3 postion, Quaternion rotation, int ping)
113115
{
114116
localPosition = postion;
115117
localRotation = rotation;
118+
119+
if (IsSV())
120+
{
121+
spectatorViewPing = ping;
122+
}
116123
}
117124

118125
[SyncVar(hook = "AnchorEstablishedChanged")]
@@ -612,7 +619,7 @@ private void Update()
612619
if (IsSV())
613620
{
614621
SetHologramPose(localRotation.x, localRotation.y, localRotation.z, localRotation.w,
615-
localPosition.x, localPosition.y, localPosition.z);
622+
localPosition.x, localPosition.y, localPosition.z, (float)spectatorViewPing / 2.0f);
616623
}
617624
#endif
618625
return;
@@ -657,9 +664,15 @@ private void Update()
657664
transform.rotation = Camera.main.transform.rotation;
658665
}
659666

660-
// For UNET we use a command to signal the host to update our local position
661-
// and rotation
662-
CmdTransform(transform.localPosition, transform.localRotation);
667+
// For UNET we use a command to signal the host to update our local position and rotation
668+
if (IsSV())
669+
{
670+
CmdTransform(transform.localPosition, transform.localRotation, NetworkManager.singleton.client.GetRTT());
671+
}
672+
else
673+
{
674+
CmdTransform(transform.localPosition, transform.localRotation, 0);
675+
}
663676
}
664677

665678
/// <summary>

0 commit comments

Comments
 (0)