Skip to content
This repository has been archived by the owner on Mar 22, 2022. It is now read-only.

Commit

Permalink
Add Unity integration for external video tracks
Browse files Browse the repository at this point in the history
Add some Unity integration of the external video tracks feature by way
of a CustomVideoSource component allowing the user to inject some custom
video feed in the WebRTC connection.

As an example application, provide a SceneVideoSource component
capturing the content rendered by a given Unity camera and streaming it
as a video feed to the remote peer.

The VideoChatDemo Unity scene is updated to add a scene capture view
showing via a MediaPlayer component the scene content captured from the
camera back-buffer.

Bug: #35
  • Loading branch information
djee-ms committed Dec 16, 2019
1 parent c634bb2 commit 7f08751
Show file tree
Hide file tree
Showing 31 changed files with 2,953 additions and 1,787 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using UnityEngine;
using UnityEditor;

namespace Microsoft.MixedReality.WebRTC.Unity.Editor
{
/// <summary>
/// Property drawer for <see cref="CaptureCameraAttribute"/>, to report an error to the user if
/// the associated <see xref="UnityEngine.Camera"/> property instance cannot be used for framebuffer
/// capture by <see cref="SceneVideoSource"/>.
/// </summary>
[CustomPropertyDrawer(typeof(CaptureCameraAttribute))]
public class CaptureCameraDrawer : PropertyDrawer
{
private const int c_errorMessageHeight = 42;

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
try
{
Validate(property.objectReferenceValue as Camera);
}
catch (Exception ex)
{
// Display error message below the property
var totalHeight = position.height;
position.yMin = position.yMax - c_errorMessageHeight;
EditorGUI.HelpBox(position, ex.Message, MessageType.Warning);

// Adjust rect for the property itself
position.yMin = position.yMax - totalHeight;
position.yMax -= c_errorMessageHeight;
}

EditorGUI.PropertyField(position, property, label);
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
float height = base.GetPropertyHeight(property, label);
try
{
Validate(property.objectReferenceValue as Camera);
}
catch (Exception)
{
// Add extra space for the error message
height += c_errorMessageHeight;
}
return height;
}

/// <summary>
/// Validate that a given <see xref="UnityEngine.Camera"/> instance can be used for framebuffer
/// capture by <see cref="SceneVideoSource"/> based on the current settings of the Unity Player
/// for the current build platform.
/// </summary>
/// <param name="camera">The camera instance to test the settings of.</param>
/// <exception xref="System.NotSupportedException">
/// The camera has settings not compatible with its use with <see cref="SceneVideoSource"/>.
/// </exception>
/// <seealso cref="CaptureCameraAttribute.Validate(Camera)"/>
public static void Validate(Camera camera)
{
if (PlayerSettings.virtualRealitySupported && (camera != null))
{
if (PlayerSettings.stereoRenderingPath == StereoRenderingPath.MultiPass)
{
// Ensure camera is not rendering to both eyes in multi-pass stereo, otherwise the command buffer
// is executed twice (once per eye) and will produce twice as many frames, which leads to stuttering
// when playing back the video stream resulting from combining those frames.
if (camera.stereoTargetEye == StereoTargetEyeMask.Both)
{
throw new NotSupportedException("Capture camera renders both eyes in multi-pass stereoscopic rendering. This is not" +
" supported by the capture mechanism which cannot discriminate them. Set Camera.stereoTargetEye to either Left or" +
" Right, or use a different rendering mode (Player Settings > XR Settings > Stereo Rendering Mode).");
}
}
#if !UNITY_2019_1_OR_NEWER
else if (PlayerSettings.stereoRenderingPath == StereoRenderingPath.Instancing)
{
throw new NotSupportedException("Capture camera does not support single-pass instanced stereoscopic rendering before Unity 2019.1." +
" Use a different stereoscopic rendering mode (Player Settings > XR Settings > Stereo Rendering Mode) or upgrade to Unity 2019.1+.");
}
#endif
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using UnityEditor;
Expand All @@ -18,6 +18,7 @@ namespace Microsoft.MixedReality.WebRTC.Unity.Editor
public class LocalVideoSourceEditor : UnityEditor.Editor
{
SerializedProperty _peerConnection;
SerializedProperty _trackName;
SerializedProperty _autoStartCapture;
SerializedProperty _preferredVideoCodec;
SerializedProperty _enableMixedRealityCapture;
Expand Down Expand Up @@ -76,7 +77,8 @@ enum SdpVideoCodecs
/// </summary>
void OnEnable()
{
_peerConnection = serializedObject.FindProperty("PeerConnection");
_peerConnection = serializedObject.FindProperty("PeerConnection");
_trackName = serializedObject.FindProperty("TrackName");
_autoStartCapture = serializedObject.FindProperty("AutoStartCapture");
_preferredVideoCodec = serializedObject.FindProperty("PreferredVideoCodec");
_enableMixedRealityCapture = serializedObject.FindProperty("EnableMixedRealityCapture");
Expand All @@ -101,6 +103,7 @@ public override void OnInspectorGUI()

GUILayout.Space(10);
EditorGUILayout.PropertyField(_peerConnection);
EditorGUILayout.PropertyField(_trackName);
EditorGUILayout.PropertyField(_autoAddTrack);
EditorGUILayout.PropertyField(_autoStartCapture);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//using UnityEngine;
//using UnityEditor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using UnityEngine;
using UnityEngine.UI;
Expand Down
Loading

0 comments on commit 7f08751

Please sign in to comment.