Skip to content

Commit 9b40578

Browse files
committed
基础实现了视频播放功能
1 parent 77513f5 commit 9b40578

10 files changed

Lines changed: 826 additions & 252 deletions

File tree

Project/示例项目/Intermediate/runtimeScene.vgasset

Lines changed: 33 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,38 @@
77
"SegmentType": "Script",
88
"ComponentNumber": 0,
99
"SegmentType": "Entity",
10-
"EntityNumber": 5,
11-
"Label": "音频源",
12-
"ID": 7172084121472160308,
10+
"EntityNumber": 4,
11+
"Label": "视频播放器",
12+
"ID": 11714570583010177713,
1313
"Parent": 0,
1414
"ComponentIDs": [
15-
7172084121472160308,
16-
1486735350898506029
15+
11714570583010177713,
16+
17829223618953380900
1717
],
1818
"Label": "相机",
19-
"ID": 2441793602883057820,
19+
"ID": 2505263940555156723,
2020
"Parent": 0,
2121
"ComponentIDs": [
22-
2441793602883057820,
23-
7565472559013602276
24-
],
25-
"Label": "精灵",
26-
"ID": 7565472559013602276,
27-
"Parent": 0,
28-
"ComponentIDs": [
29-
6557717273463339951,
30-
10179991923200072659
22+
8463515069893347238,
23+
3659918089117912369
3124
],
3225
"Label": "UI文档",
33-
"ID": 12240215965250073802,
26+
"ID": 13499148990373561517,
3427
"Parent": 0,
3528
"ComponentIDs": [
36-
5530299029171720523,
37-
15262516470008218613
29+
13499148990373561517,
30+
16696080643510278071
3831
],
3932
"Label": "Scene",
4033
"ID": 0,
4134
"Parent": 0,
4235
"ComponentIDs": [],
4336
"SegmentType": "AudioSourceComponent",
44-
"ComponentNumber": 1,
45-
"AudioSourceComponent": {
46-
"EntityComponentID": 1486735350898506029,
47-
"HasAudio": true,
48-
"m_AudioPath": "/assets/声音\\背景音乐\\BGM2.mp3",
49-
"playOnAwake": true,
50-
"loop": false,
51-
"volume": 1.0
52-
},
37+
"ComponentNumber": 0,
5338
"SegmentType": "Transform",
54-
"ComponentNumber": 4,
39+
"ComponentNumber": 3,
5540
"Transform": {
56-
"EntityComponentID": 7172084121472160308,
41+
"EntityComponentID": 11714570583010177713,
5742
"location": {
5843
"x": 0.0,
5944
"y": 0.0,
@@ -73,7 +58,7 @@
7358
"visible": true
7459
},
7560
"Transform": {
76-
"EntityComponentID": 2441793602883057820,
61+
"EntityComponentID": 8463515069893347238,
7762
"location": {
7863
"x": 0.0,
7964
"y": 0.0,
@@ -93,27 +78,7 @@
9378
"visible": true
9479
},
9580
"Transform": {
96-
"EntityComponentID": 6557717273463339951,
97-
"location": {
98-
"x": 0.0,
99-
"y": 0.0,
100-
"z": 0.0
101-
},
102-
"rotation": {
103-
"x": 0.0,
104-
"y": 0.0,
105-
"z": 0.0,
106-
"w": 1.0
107-
},
108-
"scale": {
109-
"x": 1.0,
110-
"y": 1.0,
111-
"z": 1.0
112-
},
113-
"visible": true
114-
},
115-
"Transform": {
116-
"EntityComponentID": 5530299029171720523,
81+
"EntityComponentID": 13499148990373561517,
11782
"location": {
11883
"x": 0.0,
11984
"y": 0.0,
@@ -133,46 +98,35 @@
13398
"visible": true
13499
},
135100
"SegmentType": "SpriteRenderer",
136-
"ComponentNumber": 1,
137-
"SpriteRenderer": {
138-
"EntityComponentID": 10179991923200072659,
139-
"HasSprite": true,
140-
"m_SpriteSize": {
141-
"x": 1921.0,
142-
"y": 1080.0
143-
},
144-
"m_SpritePosition": {
145-
"x": 0.0,
146-
"y": 0.0
147-
},
148-
"m_SpriteTexturePath": "/assets/UI\\主菜单\\主菜单背景.png",
149-
"Color": {
150-
"x": 1.0,
151-
"y": 1.0,
152-
"z": 1.0,
153-
"w": 1.0
154-
},
155-
"Flip": {
156-
"x": 0,
157-
"y": 0
158-
}
159-
},
101+
"ComponentNumber": 0,
160102
"SegmentType": "RmlUIDocumentComponent",
161103
"ComponentNumber": 1,
162104
"RmlUIDocumentComponent": {
163-
"EntityComponentID": 15262516470008218613,
105+
"EntityComponentID": 16696080643510278071,
164106
"HasDocument": true,
165-
"m_DocumentPath": "/assets/UI\\主菜单\\主菜单.html"
107+
"m_DocumentPath": "/assets/引擎开发使用\\游戏Demo\\主菜单HTML.html"
166108
},
167109
"SegmentType": "Camera",
168110
"ComponentNumber": 1,
169111
"Camera": {
170-
"EntityComponentID": 7565472559013602276,
112+
"EntityComponentID": 3659918089117912369,
171113
"HasCamera": true,
172114
"m_CameraType": "Letterbox2D"
173115
},
174116
"SegmentType": "VideoPlayer",
175-
"ComponentNumber": 0,
117+
"ComponentNumber": 1,
118+
"VideoPlayer": {
119+
"EntityComponentID": 17829223618953380900,
120+
"HasVideo": true,
121+
"m_VideoSize": {
122+
"x": 1920.0,
123+
"y": 1080.0
124+
},
125+
"m_VideoPath": "/assets/引擎开发使用\\游戏Demo\\yzk.mp4",
126+
"playOnAwake": true,
127+
"waitForFirstFrame": true,
128+
"loop": false
129+
},
176130
"SegmentType": "GalGameEngine",
177131
"ComponentNumber": 0
178132
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[VGASSET];12419947099986423995;Scene
1+
[VGASSET];6440051137362963717;Scene
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"MainScene": "/assets/主菜单场景.vgasset"
2+
"MainScene": "/assets/引擎开发使用\\游戏Demo\\游戏Demo.vgasset"
33
}

Runtime/VGEngine/Include/Resource/Video.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,17 @@ namespace VisionGal {
4343
void Stop();
4444
bool IsRunning() const;
4545
double GetDuration() const;
46-
void PlayBackLoop();
46+
//void PlayBackLoop();
47+
48+
void SetLoop(bool enable);
49+
bool IsLoop();
4750

4851
void Update();
4952

5053
Sprite* GetSprite() const { return sprite.get(); }
5154
private:
55+
void ProcessVideoUpdate(uint8_t* frameData, int& linesize, int width, int height, double pts);
56+
5257
Ref<VideoDecoder> decoder;
5358
//std::unique_ptr<OpenGLRenderer> renderer;
5459
std::thread playbackThread;
@@ -58,7 +63,13 @@ namespace VisionGal {
5863
double totalDuration;
5964

6065
Ref<Sprite> sprite;
61-
uint8_t* frameData = nullptr;
66+
uint8_t* m_FrameData = nullptr;
67+
68+
double m_LastPts = 0.0f;
69+
70+
bool m_VideoClockInitialized = false;
71+
double m_VideoStartPTS = 0.f;
72+
std::chrono::time_point<std::chrono::steady_clock> m_SystemStartTime;
6273
};
6374

6475

Runtime/VGEngine/Include/Resource/Video/VideoDecoder.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace VisionGal {
4040

4141
bool Open(const std::string& filePath);
4242

43-
bool ReadFrame(uint8_t*& frameData, int& linesize, int& width, int& height, double& videoPts, double& audioPts);
43+
//bool ReadFrame(uint8_t*& frameData, int& linesize, int& width, int& height, double& videoPts, double& audioPts);
4444
void Close();
4545
int GetWidth() const { return width; }
4646
int GetHeight() const { return height; }
@@ -52,15 +52,23 @@ namespace VisionGal {
5252

5353
void DemuxLoop();
5454
void AudioDecodeLoop();
55+
bool VideoDecodeLoop();
5556

5657
bool GetVideoFrame(uint8_t*& frameData, int& linesize, int& width, int& height, double& pts);
5758

5859
AudioRingBuffer* GetAudioBuffer() { return audioRingBuffer.get(); }
5960
double GetAudioClock() const { return audioClock; }
61+
62+
void SetDecodeLoop(bool enable);
63+
bool IsDecodeLoop();
64+
65+
std::function<void(uint8_t* frameData, int& linesize, int width, int height, double pts)> OnVideoDataUpdate = nullptr;
6066
protected:
6167
AVCodecParameters* FindStream();
6268
bool OpenVideoDecoder(const AVCodecParameters* codecParameters);
6369
private:
70+
bool m_LoopPlay = false;
71+
6472
AVIOContext* avio_ctx = nullptr;
6573
unsigned char* io_buffer = nullptr;
6674
AVFormatContext* formatContext = nullptr;
@@ -99,6 +107,7 @@ namespace VisionGal {
99107

100108
std::thread demuxThread;
101109
std::thread audioThread;
110+
std::thread videoThread;
102111
std::atomic<bool> running = false;
103112
bool demuxFinished = false;
104113

Runtime/VGEngine/Source/Engine/Manager/AssetManager.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,29 @@ namespace VisionGal
192192

193193
auto aPath = VFS::GetInstance()->AbsolutePath(path);
194194

195+
if (aPath.empty())
196+
return false;
197+
198+
if (Horizon::HFileSystem::ExistsFile(aPath) == false)
199+
return false;
200+
195201
// remove data file
196-
Horizon::HFileSystem::RemoveFile(aPath);
202+
try {
203+
std::filesystem::remove(aPath);
204+
}
205+
catch (const std::filesystem::filesystem_error& ex) {
206+
H_LOG_ERROR("std::filesystem::remove error: %s", ex.what());
207+
return false;
208+
}
209+
catch (const std::exception& ex) {
210+
H_LOG_ERROR("Standard exception: %s", ex.what());
211+
return false;
212+
}
213+
catch (...) {
214+
H_LOG_ERROR("Unknown std::filesystem::remove exception occurred");
215+
return false;
216+
}
217+
//Horizon::HFileSystem::RemoveFile(aPath);
197218

198219
// remove meta file
199220
auto metaPath = aPath + ".meta";

Runtime/VGEngine/Source/Project/ProjectBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ namespace VisionGal
8989
// 打包引擎资源
9090
auto enginePath = VFS::GetInstance()->AbsolutePath(Core::GetEngineResourcePathVFS());
9191
H_LOG_INFO("Package engine resource: %s", enginePath.c_str());
92-
if (Horizon::HFileSystem::ExistsFile("Data/engine.pak"))
92+
if (Horizon::HFileSystem::ExistsFile("Data/engine.pak")) // 应用程序模式
9393
{
9494
Horizon::HFileSystem::CopyFile("Data/engine.pak", dataPath / "engine.pak");
9595
}

0 commit comments

Comments
 (0)