/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
namespace NRKernal.Experimental.StreammingCast
{
using UnityEngine;
using NRKernal.Record;
using System.Collections.Generic;
using System;
/// A nr observer view capture.
public class NRObserverViewCapture : IDisposable
{
/// Default constructor.
public NRObserverViewCapture()
{
IsRecording = false;
}
/// Finalizer.
~NRObserverViewCapture()
{
}
/// A list of all the supported device resolutions for observer view videos.
/// The supported resolutions.
public static IEnumerable SupportedResolutions
{
get
{
NativeResolution rgbResolution = new NativeResolution(1280, 720);
if (NRDevice.Subsystem.IsFeatureSupported(NRSupportedFeature.NR_FEATURE_RGB_CAMERA))
rgbResolution = NRFrame.GetDeviceResolution(NativeDevice.RGB_CAMERA);
Resolution stand_resolution = new Resolution()
{
width = rgbResolution.width,
height = rgbResolution.height,
refreshRate = NativeConstants.RECORD_FPS_DEFAULT,
};
yield return stand_resolution;
Resolution low_resolution = new Resolution()
{
width = stand_resolution.width / 2,
height = stand_resolution.height / 2,
refreshRate = NativeConstants.RECORD_FPS_DEFAULT,
};
yield return low_resolution;
Resolution high_resolution = new Resolution()
{
width = stand_resolution.width * 3 / 2,
height = stand_resolution.height * 3 / 2,
refreshRate = NativeConstants.RECORD_FPS_DEFAULT,
};
yield return high_resolution;
}
}
///
/// Indicates whether or not the VideoCapture instance is currently recording video.
/// True if this object is recording, false if not.
public bool IsRecording { get; private set; }
/// Context for the capture.
private ObserverViewFrameCaptureContext m_CaptureContext;
/// Gets the context.
/// The context.
public ObserverViewFrameCaptureContext GetContext()
{
return m_CaptureContext;
}
/// Gets the preview texture.
/// The preview texture.
public Texture PreviewTexture
{
get
{
return m_CaptureContext?.PreviewTexture;
}
}
/// Creates an asynchronous.
/// True to show, false to hide the holograms.
/// The on created callback.
public static void CreateAsync(bool showHolograms, OnObserverViewResourceCreatedCallback onCreatedCallback)
{
NRObserverViewCapture capture = new NRObserverViewCapture();
capture.m_CaptureContext = new ObserverViewFrameCaptureContext();
onCreatedCallback?.Invoke(capture);
}
///
/// Returns the supported frame rates at which a video can be recorded given a resolution.
/// A recording resolution.
/// The frame rates at which the video can be recorded.
public static IEnumerable GetSupportedFrameRatesForResolution(Resolution resolution)
{
yield return NativeConstants.RECORD_FPS_DEFAULT;
}
/// Dispose must be called to shutdown the PhotoCapture instance.
public void Dispose()
{
if (m_CaptureContext != null)
{
m_CaptureContext.Release();
m_CaptureContext = null;
}
}
/// Starts observer view mode asynchronous.
/// Options for controlling the setup.
/// State of the audio.
/// The on video mode started callback.
public void StartObserverViewModeAsync(CameraParameters setupParams, AudioState audioState, OnObserverViewModeStartedCallback onVideoModeStartedCallback, bool autoAdaptBlendMode = false)
{
setupParams.camMode = CamMode.VideoMode;
if (autoAdaptBlendMode)
{
var blendMode = m_CaptureContext.AutoAdaptBlendMode(setupParams.blendMode);
if (blendMode != setupParams.blendMode)
{
NRDebugger.Warning("[VideoCapture] AutoAdaptBlendMode : {0} => {1}", setupParams.blendMode, blendMode);
setupParams.blendMode = blendMode;
}
}
m_CaptureContext.StartCaptureMode(setupParams);
var result = new ObserverViewCaptureResult();
result.resultType = CaptureResultType.Success;
onVideoModeStartedCallback?.Invoke(result);
}
/// Starts observer view asynchronous.
/// The IP.
/// The on started recording video callback.
public void StartObserverViewAsync(string ip, OnStartedObserverViewCallback onStartedRecordingVideoCallback)
{
var captureResult = new ObserverViewCaptureResult();
if (IsRecording)
{
captureResult.resultType = CaptureResultType.UnknownError;
onStartedRecordingVideoCallback?.Invoke(captureResult);
}
else
{
m_CaptureContext.StartCapture(ip, (result) =>
{
if (result)
{
IsRecording = true;
captureResult.resultType = CaptureResultType.Success;
onStartedRecordingVideoCallback?.Invoke(captureResult);
}
else
{
IsRecording = false;
captureResult.resultType = CaptureResultType.ServiceIsNotAvailable;
onStartedRecordingVideoCallback?.Invoke(captureResult);
}
});
}
}
/// Stops observer view asynchronous.
/// The on stopped recording video callback.
public void StopObserverViewAsync(OnStoppedObserverViewCallback onStoppedRecordingVideoCallback)
{
var result = new ObserverViewCaptureResult();
if (!IsRecording)
{
result.resultType = CaptureResultType.UnknownError;
onStoppedRecordingVideoCallback?.Invoke(result);
}
else
{
try
{
m_CaptureContext.StopCapture();
}
catch (Exception e)
{
NRDebugger.Info("Stop recording error :" + e.ToString());
throw;
}
IsRecording = false;
result.resultType = CaptureResultType.Success;
onStoppedRecordingVideoCallback?.Invoke(result);
}
}
/// Stops observer view mode asynchronous.
/// The on video mode stopped callback.
public void StopObserverViewModeAsync(OnObserverViewModeStoppedCallback onVideoModeStoppedCallback)
{
m_CaptureContext.StopCaptureMode();
var result = new ObserverViewCaptureResult();
result.resultType = CaptureResultType.Success;
onVideoModeStoppedCallback?.Invoke(result);
}
/// Contains the result of the capture request.
public enum CaptureResultType
{
///
/// Specifies that the desired operation was successful.
///
Success,
///
/// Service is not available.
///
ServiceIsNotAvailable,
///
/// Specifies that an unknown error occurred.
///
UnknownError
}
///
/// Specifies what audio sources should be recorded while recording the video.
public enum AudioState
{
///
/// Only include the mic audio in the video recording.
///
MicAudio = 0,
///
/// Only include the application audio in the video recording.
///
ApplicationAudio = 1,
///
/// Include both the application audio as well as the mic audio in the video recording.
///
ApplicationAndMicAudio = 2,
///
/// Do not include any audio in the video recording.
///
None = 3
}
///
/// A data container that contains the result information of a video recording operation.
public struct ObserverViewCaptureResult
{
///
/// A generic result that indicates whether or not the VideoCapture operation succeeded.
public CaptureResultType resultType;
/// The specific HResult value.
public long hResult;
/// Indicates whether or not the operation was successful.
/// True if success, false if not.
public bool success
{
get
{
return resultType == CaptureResultType.Success;
}
}
}
/// Called when the web camera begins recording the video.
/// Indicates whether or not video recording started successfully.
public delegate void OnStartedObserverViewCallback(ObserverViewCaptureResult result);
/// Called when a VideoCapture resource has been created.
/// The VideoCapture instance.
public delegate void OnObserverViewResourceCreatedCallback(NRObserverViewCapture captureObject);
/// Called when video mode has been started.
/// Indicates whether or not video mode was successfully activated.
public delegate void OnObserverViewModeStartedCallback(ObserverViewCaptureResult result);
/// Called when video mode has been stopped.
/// Indicates whether or not video mode was successfully deactivated.
public delegate void OnObserverViewModeStoppedCallback(ObserverViewCaptureResult result);
/// Called when the video recording has been saved to the file system.
/// Indicates whether or not video recording was saved successfully to the
/// file system.
public delegate void OnStoppedObserverViewCallback(ObserverViewCaptureResult result);
}
}