UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
HighlightRecorder.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "WmfPrivate.h"
6
7#include "RHI.h"
8#include "RHIResources.h"
10#include "HAL/Thread.h"
11#include "HAL/ThreadSafeBool.h"
12
13#include "WmfRingBuffer.h"
14
16
17class FWmfMp4Writer;
18class FThread;
19
21
23{
24public:
27
28 enum class EState { Stopped, Recording, Paused };
29
31 { return State; }
32
33 bool Start(double RingBufferDurationSecs);
34 bool Pause(bool bPause);
35 void Stop();
36 bool IsSaving() const
37 {
38 return bSaving;
39 }
40
41 using FDoneCallback = TFunction<void(bool /* bSuccess */, const FString& /* FullPathToFile */)>;
42 bool SaveHighlight(const TCHAR* Filename, FDoneCallback DoneCallback, double MaxDurationSecs = 1.0 * 60 * 60);
43
44private:
45 bool SaveHighlightInBackground(const FString& Filename, double MaxDurationSecs);
46 bool SaveHighlightInBackgroundImpl(const FString& Filename, double MaxDurationSecs);
47 bool InitialiseMp4Writer(const FString& Filename, bool bHasAudio);
49 bool GetSavingStart(const TArray<AVEncoder::FMediaPacket>& Samples, FTimespan MaxDuration, int& OutStartIndex, FTimespan& OutStartTime) const;
51
52 // takes into account if we've been paused and shifts current time back to compensate paused state
53 // so all timestamps are continuous even over paused pieces
54 FTimespan GetRecordingTime() const;
55
56 //
57 // IGameplayMediaEncoderListener implementation
58 //
60 void OnMediaSample(const AVEncoder::FMediaPacket& Sample) override;
62
63private:
65
67
68 FWmfRingBuffer RingBuffer;
69 // we take note of how long we've been paused and then "shift" samples timestamps by paused duration
70 // so effectively gluing different pieces of video together
71
72 uint64 NumPushedFrames = 0;
73
74 FTimespan RecordingStartTime = 0;
75
76 // if currently paused, when it happened
77 FTimespan PauseTimestamp = 0;
78 // for how long recording has been paused since it's started
79 FTimespan TotalPausedDuration = 0;
80
81 TUniquePtr<FThread> BackgroundSaving;
82 FDoneCallback DoneCallback;
83 FThreadSafeBool bSaving = false;
84
85 DWORD AudioStreamIndex = 0;
86 DWORD VideoStreamIndex = 0;
87
88#pragma region testing
89public:
90 static void Start(const TArray<FString>& Args, UWorld*, FOutputDevice& Output)
91 {
92 // Initialize the Singleton if necessary. This is only useful if using project other than Fortnite. E.g: QAGame.
93 // When using QAGame, there is no HighlightFeature, and we just use these direct console commands to test
94 if (!Singleton)
95 {
96 Singleton = new FHighlightRecorder();
97 }
98
99 if (Args.Num() > 1)
100 {
101 Output.Logf(ELogVerbosity::Error, TEXT("zero or one parameter expected: Start [max_duration_secs=30.0]"));
102 return;
103 }
104
105 double MaxDurationSecs = 30;
106 if (Args.Num() == 1)
107 {
109 }
110 Get()->Start(MaxDurationSecs);
111 }
112
113 static void StopCmd()
114 {
115 if (!CheckSingleton())
116 {
117 return;
118 }
119
120 Get()->Stop();
121 delete Singleton;
122 Singleton = nullptr;
123 }
124
125 static void PauseCmd()
126 {
127 if (!CheckSingleton())
128 {
129 return;
130 }
131
132 Get()->Pause(true);
133 }
134
135 static void ResumeCmd()
136 {
137 if (!CheckSingleton())
138 {
139 return;
140 }
141
142 Get()->Pause(false);
143 }
144
145 static void SaveCmd(const TArray<FString>& Args, UWorld*, FOutputDevice& Output)
146 {
147 if (!CheckSingleton())
148 {
149 return;
150 }
151
152 if (Args.Num() > 2)
153 {
154 Output.Logf(ELogVerbosity::Error, TEXT("0-2 parameters expected: Save [filename=\"test.mp4\"] [max_duration_secs= ring buffer duration]"));
155 return;
156 }
157
158 FString Filename = "test.mp4";
159 if (Args.Num() >= 1)
160 {
161 Filename = Args[0];
162 }
163
164 double MaxDurationSecs = 1 * 60 * 60;
165 if (Args.Num() == 2)
166 {
168 }
169
170 Get()->SaveHighlight(
171 *Filename,
172 [](bool bRes, const FString& InFullPathToFile)
173 {
174 UE_LOG(HighlightRecorder, Log, TEXT("saving done: %d"), bRes);
175 },
177 );
178 }
179
180private:
181
182 static bool CheckSingleton()
183 {
184 if (Singleton)
185 {
186 return true;
187 }
188 else
189 {
190 UE_LOG(HighlightRecorder, Error, TEXT("HighlightRecorder not initialized."));
191 return false;
192 }
193 }
194
195 static FHighlightRecorder* Get()
196 {
197 check(Singleton);
198 return Singleton;
199 }
200 static FHighlightRecorder* Singleton;
201#pragma endregion testing
202};
203
204
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
#define check(expr)
Definition AssertionMacros.h:314
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define PRAGMA_ENABLE_DEPRECATION_WARNINGS
Definition GenericPlatformCompilerPreSetup.h:12
#define PRAGMA_DISABLE_DEPRECATION_WARNINGS
Definition GenericPlatformCompilerPreSetup.h:8
#define DECLARE_LOG_CATEGORY_EXTERN(CategoryName, DefaultVerbosity, CompileTimeVerbosity)
Definition LogMacros.h:361
#define UE_LOG(CategoryName, Verbosity, Format,...)
Definition LogMacros.h:270
Definition HighlightRecorder.h:23
bool Start(double RingBufferDurationSecs)
Definition HighlightRecorder.cpp:51
FHighlightRecorder()
Definition HighlightRecorder.cpp:40
static void ResumeCmd()
Definition HighlightRecorder.h:135
void Stop()
Definition HighlightRecorder.cpp:113
~FHighlightRecorder()
Definition HighlightRecorder.cpp:45
static void Start(const TArray< FString > &Args, UWorld *, FOutputDevice &Output)
Definition HighlightRecorder.h:90
static void StopCmd()
Definition HighlightRecorder.h:113
bool SaveHighlight(const TCHAR *Filename, FDoneCallback DoneCallback, double MaxDurationSecs=1.0 *60 *60)
Definition HighlightRecorder.cpp:175
static void PauseCmd()
Definition HighlightRecorder.h:125
bool IsSaving() const
Definition HighlightRecorder.h:36
EState GetState() const
Definition HighlightRecorder.h:30
static void SaveCmd(const TArray< FString > &Args, UWorld *, FOutputDevice &Output)
Definition HighlightRecorder.h:145
EState
Definition HighlightRecorder.h:28
bool Pause(bool bPause)
Definition HighlightRecorder.cpp:83
TFunction< void(bool, const FString &)> FDoneCallback
Definition HighlightRecorder.h:41
Definition OutputDevice.h:133
Definition ThreadSafeBool.h:17
Definition Thread.h:24
Definition WmfMp4Writer.h:13
Definition WmfRingBuffer.h:11
Definition GameplayMediaEncoder.h:33
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
Definition Atomic.h:538
Definition UniquePtr.h:107
Definition World.h:918
@ Error
Definition LogVerbosity.h:28
Definition Timespan.h:76
static UE_FORCEINLINE_HINT double Atod(const CharType *String)
Definition CString.h:1191