/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
namespace NRKernal
{
using UnityEngine;
/// A camera model view used to receive camera image.
public class CameraModelView
{
/// Gets the width.
/// The width.
public int Width
{
get
{
return m_NativeCameraProxy.Resolution.width;
}
}
/// Gets the height.
/// The height.
public int Height
{
get
{
return m_NativeCameraProxy.Resolution.height;
}
}
/// Gets a value indicating whether this object is playing.
/// True if this object is playing, false if not.
public bool IsPlaying
{
get
{
return m_State == State.Playing;
}
}
/// Values that represent states.
public enum State
{
/// An enum constant representing the playing option.
Playing,
/// An enum constant representing the paused option.
Paused,
/// An enum constant representing the stopped option.
Stopped
}
/// The state.
private State m_State = State.Stopped;
/// Gets a value indicating whether the did update this frame.
/// True if did update this frame, false if not.
public bool DidUpdateThisFrame
{
get
{
return m_NativeCameraProxy.HasFrame();
}
}
/// Gets or sets the number of frames.
/// The number of frames.
public int FrameCount { get; protected set; }
/// The native camera proxy.
protected NativeCameraProxy m_NativeCameraProxy;
/// Gets or sets the native camera proxy.
/// The native camera proxy.
public NativeCameraProxy NativeCameraProxy
{
get
{
return this.m_NativeCameraProxy;
}
set
{
this.m_NativeCameraProxy = value;
}
}
public CameraImageFormat ImageFormat { get; protected set; }
/// Default constructor.
public CameraModelView() { }
/// Constructor.
/// Camera image format.
public CameraModelView(CameraImageFormat format)
{
ImageFormat = format;
this.CreateRGBCameraProxy(format);
m_NativeCameraProxy.Regist(this);
}
/// Use RGB_888 format default.
/// (Optional) Camera image format.
protected void CreateRGBCameraProxy(CameraImageFormat format = CameraImageFormat.RGB_888)
{
if (m_NativeCameraProxy != null)
{
return;
}
m_NativeCameraProxy = CameraProxyFactory.CreateRGBCameraProxy();
m_NativeCameraProxy.SetImageFormat(format);
}
/// Plays this object.
public void Play()
{
if (m_State == State.Playing)
{
return;
}
NRKernalUpdater.OnUpdate += UpdateTexture;
m_NativeCameraProxy.Play();
m_NativeCameraProxy.Regist(this);
m_State = State.Playing;
}
/// Pauses this object.
public void Pause()
{
if (m_State == State.Paused || m_State == State.Stopped)
{
return;
}
NRKernalUpdater.OnUpdate -= UpdateTexture;
m_State = State.Paused;
}
/// Updates the texture.
private void UpdateTexture()
{
if (!DidUpdateThisFrame || !IsPlaying)
{
return;
}
FrameRawData frame = m_NativeCameraProxy.GetFrame();
if (frame.data == null)
{
NRDebugger.Error("[CameraModelView] Get camera raw data faild...");
return;
}
FrameCount++;
OnRawDataUpdate(frame);
}
/// Stops this object.
public void Stop()
{
if (m_State == State.Stopped)
{
return;
}
m_NativeCameraProxy.Remove(this);
m_NativeCameraProxy.Stop();
NRKernalUpdater.OnUpdate -= UpdateTexture;
FrameCount = 0;
m_State = State.Stopped;
this.OnStopped();
}
/// Load raw texture data.
/// .
protected virtual void OnRawDataUpdate(FrameRawData frame) { }
/// On texture stopped.
protected virtual void OnStopped() { }
}
}