forked from EIRTeam/EIRTeam.FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeg_video_stream.h
140 lines (121 loc) · 5.46 KB
/
ffmpeg_video_stream.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**************************************************************************/
/* ffmpeg_video_stream.h */
/**************************************************************************/
/* This file is part of: */
/* EIRTeam.FFmpeg */
/* https://ph.eirteam.moe */
/**************************************************************************/
/* Copyright (c) 2023-present Álex Román (EIRTeam) & contributors. */
/* */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef ET_VIDEO_STREAM_H
#define ET_VIDEO_STREAM_H
#ifdef GDEXTENSION
// Headers for building as GDExtension plug-in.
#include <godot_cpp/classes/video_stream.hpp>
#include <godot_cpp/classes/video_stream_playback.hpp>
#include <godot_cpp/godot.hpp>
#include <godot_cpp/templates/list.hpp>
#include <godot_cpp/templates/vector.hpp>
using namespace godot;
#else
#include "core/object/ref_counted.h"
#include "scene/resources/video_stream.h"
#endif
#include "video_decoder.h"
// We have to use this function redirection system for GDExtension because the naming conventions
// for the functions we are supposed to override are different there
#include "gdextension_build/func_redirect.h"
class VideoStreamPlaybackFFMPEG : public VideoStreamPlayback {
GDCLASS(VideoStreamPlaybackFFMPEG, VideoStreamPlayback);
const int LENIENCE_BEFORE_SEEK = 2500;
double playback_position = 0.0f;
Ref<VideoDecoder> decoder;
List<Ref<DecodedFrame>> available_frames;
List<Ref<DecodedAudioFrame>> available_audio_frames;
Ref<DecodedFrame> last_frame;
#ifndef FFMPEG_MT_GPU_UPLOAD
Ref<ImageTexture> last_frame_texture;
#endif
Ref<Image> last_frame_image;
Ref<ImageTexture> texture;
bool looping = false;
bool buffering = false;
int frames_processed = 0;
void seek_into_sync();
double get_current_frame_time();
bool check_next_frame_valid(Ref<DecodedFrame> p_decoded_frame);
bool check_next_audio_frame_valid(Ref<DecodedAudioFrame> p_decoded_frame);
bool paused = false;
bool playing = false;
private:
bool is_paused_internal() const;
void update_internal(double p_delta);
bool is_playing_internal() const;
void set_paused_internal(bool p_paused);
void play_internal();
void stop_internal();
void seek_internal(double p_time);
double get_length_internal() const;
Ref<Texture2D> get_texture_internal() const;
double get_playback_position_internal() const;
int get_mix_rate_internal() const;
int get_channels_internal() const;
protected:
void clear();
static void _bind_methods(){}; // Required by GDExtension, do not remove
public:
Error load(Ref<FileAccess> p_file_access);
STREAM_FUNC_REDIRECT_0_CONST(bool, is_paused);
STREAM_FUNC_REDIRECT_1(void, update, double, p_delta);
STREAM_FUNC_REDIRECT_0_CONST(bool, is_playing);
STREAM_FUNC_REDIRECT_1(void, set_paused, bool, p_paused);
STREAM_FUNC_REDIRECT_0(void, play);
STREAM_FUNC_REDIRECT_0(void, stop);
STREAM_FUNC_REDIRECT_1(void, seek, double, p_time);
STREAM_FUNC_REDIRECT_0_CONST(double, get_length);
STREAM_FUNC_REDIRECT_0_CONST(Ref<Texture2D>, get_texture);
STREAM_FUNC_REDIRECT_0_CONST(double, get_playback_position);
STREAM_FUNC_REDIRECT_0_CONST(int, get_mix_rate);
STREAM_FUNC_REDIRECT_0_CONST(int, get_channels);
VideoStreamPlaybackFFMPEG();
};
class VideoStreamFFMPEG : public VideoStream {
GDCLASS(VideoStreamFFMPEG, VideoStream);
protected:
static void _bind_methods(){}; // Required by GDExtension, do not remove
Ref<VideoStreamPlayback> instantiate_playback_internal() {
Ref<FileAccess> fa = FileAccess::open(get_file(), FileAccess::READ);
if (!fa.is_valid()) {
return Ref<VideoStreamPlayback>();
}
Ref<VideoStreamPlaybackFFMPEG> pb;
pb.instantiate();
if (pb->load(fa) != OK) {
return nullptr;
}
return pb;
}
public:
STREAM_FUNC_REDIRECT_0(Ref<VideoStreamPlayback>, instantiate_playback);
};
#endif // ET_VIDEO_STREAM_H