/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
namespace NRKernal.Experimental.StreammingCast
{
using NRKernal.Record;
using System.Collections;
using UnityEngine;
/// An observer view frame provider.
public class ObserverViewFrameProvider : AbstractFrameProvider
{
/// Source camera.
private Camera m_SourceCamera;
/// Source frame.
private UniversalTextureFrame m_SourceFrame;
/// True if is play, false if not.
private bool isPlay = false;
/// The FPS.
private int _FPS;
/// Init provider with the camera target texture.
/// camera target texture.
/// (Optional) The FPS.
public ObserverViewFrameProvider(Camera camera, int fps = 30)
{
this.m_SourceCamera = camera;
this.m_SourceFrame.textures = new Texture[1];
this.m_SourceFrame.textureType = TextureType.RGB;
this._FPS = fps;
NRKernalUpdater.Instance.StartCoroutine(UpdateFrame());
}
/// Updates the frame.
/// An IEnumerator.
public IEnumerator UpdateFrame()
{
while (true)
{
if (isPlay)
{
m_SourceFrame.textures[0] = m_SourceCamera.targetTexture;
m_SourceFrame.timeStamp = NRTools.GetTimeStamp();
OnUpdate?.Invoke(m_SourceFrame);
}
yield return new WaitForSeconds(1 / _FPS);
}
}
/// Gets frame information.
/// The frame information.
public override Resolution GetFrameInfo()
{
Resolution resolution = new Resolution();
resolution.width = m_SourceFrame.textures[0].width;
resolution.height = m_SourceFrame.textures[0].height;
return resolution;
}
/// Plays this object.
public override void Play()
{
isPlay = true;
}
/// Stops this object.
public override void Stop()
{
isPlay = false;
}
/// Releases this object.
public override void Release()
{
isPlay = false;
NRKernalUpdater.Instance.StopCoroutine(UpdateFrame());
}
}
}