1#include "VideoCaptureSystem.h"
3#include "VideoCaptureRenderer.h"
4#include "IVideoEncoder.h"
7#include "Components/Core/CameraComponent.h"
8#include "Renderer/RenderResources.h"
9#include "Renderer/RenderTexture.h"
10#include "Systems/Core/CameraSystem.h"
11#include "Resources/Texture.h"
13#include "Rendering/IContext.h"
15#include "Foundation/Platform/Timer.h"
16#include "Foundation/Logging/Logger.h"
47 if (!context->videoEncoderContext)
return;
48 for (
auto & capComp : pool) {
49 auto & capData = getData(&capComp);
50 if (!capData.encoder || capComp.endOfStream)
continue;
51 if(capComp.updateTimestamp)
52 capComp.timestamp = Timer::currentTimeMicroseconds();
58 if (!context->videoEncoderContext)
return;
59 for (
auto & capComp : pool) {
67 int64_t now = Timer::currentTimeMilliseconds();
69 if (capComp.restart) {
70 capComp.endOfStream =
false;
73 if (((!capData.encoder && (capData.nextRestartTime < now)) || capComp.restart || capComp.reconfig) && !capComp.endOfStream) {
74 if (capData.encoder && capComp.restart) {
75 context->videoEncoderContext->destroyVideoEncoder(capData.encoder);
76 capData.encoder =
nullptr;
80 desc.codec = capComp.codec;
81 desc.encodeWidth = capComp.width;
82 desc.encodeHeight = capComp.height;
83 desc.darWidth = desc.encodeWidth;
84 desc.darHeight = desc.encodeHeight;
85 desc.maxWidth = capComp.maxWidth;
86 desc.maxHeight = capComp.maxHeight;
87 desc.bitrate = capComp.bitrate;
88 desc.maxBitrate = capComp.maxBitrate;
89 desc.bFrameCount = capComp.bFrameCount;
90 desc.bFrameReference = (uint32_t)capComp.bFrameReference;
91 desc.frameRate = 1000.0f / capComp.frameInterval;
92 desc.lookahead = capComp.lookahead;
93 desc.recordToDisk = capComp.recordToDisk;
94 desc.recordPath = capComp.recordPath;
95 desc.callback = capComp.callback;
96 desc.useMSAA = capComp.useMSAA;
98 if (!capData.encoder) {
99 capData.encoder = context->videoEncoderContext->createVideoEncoder(desc);
101 if (!capData.encoder) {
104 capData.nextRestartTime = now + 3000;
108 capData.nextRestartTime = 0;
112 capData.encoder->reconfigure(desc,
false,
true);
114 capComp.width = desc.encodeWidth;
115 capComp.height = desc.encodeHeight;
116 capComp.maxWidth = desc.maxWidth;
117 capComp.maxHeight = desc.maxHeight;
118 capComp.bFrameCount = desc.bFrameCount;
119 capComp.restart =
false;
120 capComp.reconfig =
false;
124 if (!capData.encoder || capComp.endOfStream) {
127 camComp->
resolveTarget = capData.encoder->getNextInputBuffer();
136void Cogs::Core::VideoCaptureSystem::postRender(
Context * context)
138 if (!context->videoEncoderContext) {
141 for (
auto& capComp : pool) {
153 auto & capData = getData(&capComp);
154 if (!capData.encoder) {
157 if (capComp.endOfStream) {
158 if (capData.encoder) {
159 context->videoEncoderContext->destroyVideoEncoder(capData.encoder);
160 capData.encoder =
nullptr;
164 int64_t now = Timer::currentTimeMilliseconds();
166 if (capData.nextEncodeTime < now) {
167 capData.nextEncodeTime = now + capComp.frameInterval;
172 input.timestamp = capComp.timestamp;
173 input.duration = capComp.duration;
174 input.forceIntra = capComp.forceIntra;
175 input.forceIDR = capComp.forceIDR;
176 input.outputSPSPPS = capComp.outputSPSPPS;
178 capData.encoder->encode(input);
179 capComp.forceIntra =
false;
180 capComp.forceIDR =
false;
181 capComp.outputSPSPPS =
false;
188 if (context->videoEncoderContext){
202 context->videoEncoderContext->destroyVideoEncoder(capData.encoder);
203 capData.encoder =
nullptr;
void setChanged()
Sets the component to the ComponentFlags::Changed state with carry.
ComponentType * getComponent() const
CameraFlags flags
Camera behavior flags.
glm::vec2 viewportSize
Size of the viewport covered by this instance, given in pixels.
TextureHandle resolveTarget
The texture to which the rendered output will be resolved. (Needs an offscreen renderTexture for this...
TextureHandle renderTexture
The render texture to output the rendered scene from the camera to.
virtual void cleanup(Context *)
Provided for custom cleanup logic in derived systems.
Context * context
Pointer to the Context instance the system lives in.
virtual void initialize(Context *context)
Initialize the system.
virtual void destroyComponent(ComponentHandle)
Destroy the component held by the given handle.
void update()
Updates the system state to that of the current frame.
void preUpdate()
Run the pre-update method of the system.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
class IRenderer * renderer
Renderer.
virtual void registerExtension(IRendererExtension *extension)=0
Register an extension with the renderer.
virtual void unregisterExtension(IRendererExtension *extension)=0
Unregister an extension with the renderer.
void destroyComponent(ComponentHandle component) override
void initialize(Context *context) override
Initialize the system.
void cleanup(Context *context) override
Provided for custom cleanup logic in derived systems.
Log implementation class.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
@ EnableRender
Renderable.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Handle to a Component instance.
ComponentType * resolveComponent() const