1+ // Fill out your copyright notice in the Description page of Project Settings.
2+
3+
4+ #include " InSceneRecord.h"
5+ #include " InRecordGameViewportClient.h"
6+
7+ #include < string>
8+
9+ // Sets default values
10+ AInSceneRecord::AInSceneRecord ()
11+ {
12+
13+ }
14+
15+ void AInSceneRecord::Destroyed ()
16+ {
17+ if (nullptr != m_ImageBuf)
18+ {
19+ delete[] m_ImageBuf;
20+ m_ImageBuf = nullptr ;
21+ }
22+ if (nullptr != m_WrapOpenCv)
23+ {
24+ m_WrapOpenCv->m_VideoWriter .release ();
25+ delete m_WrapOpenCv;
26+ m_WrapOpenCv = nullptr ;
27+ }
28+
29+ }
30+ void AInSceneRecord::StartRecord (const FString FilePath, const int Fps)
31+ {
32+ UE_LOG (LogTemp, Log, TEXT (" AInSceneRecord StartRecord FilePath=%s" ),*FilePath);
33+ if (true == m_IsRecording)
34+ {
35+ UE_LOG (LogTemp, Error, TEXT (" AInSceneRecord StartRecord IsRecording ture" ));
36+ return ;
37+ }
38+ m_IsRecording = true ;
39+ m_FilePath = FilePath;
40+ m_Fps = Fps;
41+ if (false == FPaths::ValidatePath (m_FilePath))
42+ {
43+ UE_LOG (LogTemp, Error, TEXT (" AInSceneRecord StartRecord ValidatePath m_FilePath=%s" ), *m_FilePath);
44+ return ;
45+ }
46+ FString FoldPath = FPaths::GetPath (m_FilePath);
47+ if (false == FPaths::DirectoryExists (FoldPath))
48+ {
49+ UE_LOG (LogTemp, Log, TEXT (" AInSceneRecord StartRecord CreateDirectoryTree=%s" ), *FoldPath);
50+ FPlatformFileManager::Get ().GetPlatformFile ().CreateDirectoryTree (*FoldPath);
51+ }
52+ auto world = GetWorld ();
53+ if (nullptr == world)
54+ {
55+ UE_LOG (LogTemp, Error, TEXT (" AInSceneRecord StartRecord GetWorld false" ));
56+ return ;
57+ }
58+
59+ UInRecordGameViewportClient* ViewPortClient = Cast<UInRecordGameViewportClient>(
60+ world->GetGameViewport ());
61+ if (nullptr == ViewPortClient)
62+ {
63+ UE_LOG (LogTemp, Error, TEXT (" AInSceneRecord StartRecord UInRecordGameViewportClient nullptr" ));
64+ return ;
65+ }
66+ if (true == ViewPortClient->OnFrameData .IsBound ())
67+ {
68+ ViewPortClient->OnFrameData .Unbind ();
69+ }
70+ ViewPortClient->OnFrameData .BindUObject (this , &AInSceneRecord::HandleFrameData);
71+
72+ if (nullptr != m_ImageBuf)
73+ {
74+ delete[] m_ImageBuf;
75+ m_ImageBuf = nullptr ;
76+ }
77+ if (nullptr == m_WrapOpenCv)
78+ {
79+ m_WrapOpenCv = new WrapOpenCv ();
80+ }
81+
82+ GetWorld ()->GetTimerManager ().SetTimer (m_TimeHandle,this , &AInSceneRecord::OnRequestFrame,1 .0f / Fps, true , 0 );
83+ }
84+
85+ void AInSceneRecord::StoptRecord ()
86+ {
87+ UE_LOG (LogTemp, Log, TEXT (" AInSceneRecord StoptRecord " ));
88+ if (false == m_IsRecording)
89+ {
90+ UE_LOG (LogTemp, Error, TEXT (" AInSceneRecord StoptRecord IsRecording false" ));
91+ return ;
92+ }
93+ GetWorld ()->GetTimerManager ().ClearTimer (m_TimeHandle);
94+
95+ if (nullptr != m_ImageBuf)
96+ {
97+ delete[] m_ImageBuf;
98+ m_ImageBuf = nullptr ;
99+ }
100+ m_WrapOpenCv->m_VideoWriter .release ();
101+ }
102+
103+ void AInSceneRecord::OnRequestFrame ()
104+ {
105+ auto world = GetWorld ();
106+ if (nullptr == world)
107+ {
108+ UE_LOG (LogTemp, Error, TEXT (" AInSceneRecord OnRequestFrame GetWorld false" ));
109+ return ;
110+ }
111+
112+ UInRecordGameViewportClient* ViewPortClient = Cast<UInRecordGameViewportClient>(
113+ world->GetGameViewport ());
114+
115+ if (nullptr == ViewPortClient)
116+ {
117+ UE_LOG (LogTemp, Error, TEXT (" AInSceneRecord OnRequestFrame UInRecordGameViewportClient nullptr" ));
118+ return ;
119+ }
120+ ViewPortClient->RequestFrame ();
121+ }
122+ void AInSceneRecord::HandleFrameData (TArray<FColor> Bitmap, int32 x, int32 y)
123+ {
124+ int length = Bitmap.Num () * 3 ;
125+ if (nullptr != m_ImageBuf)
126+ {
127+ if (m_ImageX != x || m_ImageY != y)
128+ {
129+ UE_LOG (LogTemp, Error, TEXT (" AInSceneRecord HandleFrameData m_ImageX=%d m_ImageY=%d x=%d y=%d" ), m_ImageX, m_ImageY,x,y);
130+ return ;
131+ }
132+ }
133+ if (nullptr == m_ImageBuf)
134+ {
135+ UE_LOG (LogTemp, Log, TEXT (" AInSceneRecord HandleFrameData x=%d y=%d" ), x,y);
136+
137+ m_ImageBuf = new char [length];
138+ m_ImageX = x;
139+ m_ImageY = y;
140+ std::string cvFilePath (TCHAR_TO_UTF8 (*m_FilePath));
141+
142+ m_WrapOpenCv->m_VideoWriter .release ();
143+ m_WrapOpenCv->m_VideoWriter .open (cvFilePath, cv::VideoWriter::fourcc (' X' , ' V' , ' I' , ' D' ), m_Fps, cv::Size (m_ImageX, m_ImageY));
144+ }
145+
146+ int count = 0 ;
147+ for (int i = 0 ; i < Bitmap.Num (); i++)
148+ {
149+ m_ImageBuf[count] = Bitmap[i].B ;
150+ m_ImageBuf[count + 1 ] = Bitmap[i].G ;
151+ m_ImageBuf[count + 2 ] = Bitmap[i].R ;
152+ count += 3 ;
153+ }
154+
155+ if (false == m_WrapOpenCv->m_VideoWriter .isOpened ())
156+ {
157+ UE_LOG (LogTemp, Error, TEXT (" AInSceneRecord m_VideoWriter isOpened" ));
158+ return ;
159+ }
160+ cv::Mat img (m_ImageY,m_ImageX,CV_8UC3, (unsigned char *)m_ImageBuf);
161+ m_WrapOpenCv->m_VideoWriter .write (img);
162+ }
0 commit comments