Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/movie_writer/movie_writer.cpp
10278 views
1
/**************************************************************************/
2
/* movie_writer.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "movie_writer.h"
32
#include "core/config/project_settings.h"
33
#include "core/io/dir_access.h"
34
#include "core/os/time.h"
35
#include "scene/main/window.h"
36
#include "servers/audio/audio_driver_dummy.h"
37
#include "servers/display_server.h"
38
#include "servers/rendering_server.h"
39
40
MovieWriter *MovieWriter::writers[MovieWriter::MAX_WRITERS];
41
uint32_t MovieWriter::writer_count = 0;
42
43
void MovieWriter::add_writer(MovieWriter *p_writer) {
44
ERR_FAIL_COND(writer_count == MAX_WRITERS);
45
writers[writer_count++] = p_writer;
46
}
47
48
MovieWriter *MovieWriter::find_writer_for_file(const String &p_file) {
49
for (int32_t i = writer_count - 1; i >= 0; i--) { // More recent last, to have override ability.
50
if (writers[i]->handles_file(p_file)) {
51
return writers[i];
52
}
53
}
54
return nullptr;
55
}
56
57
uint32_t MovieWriter::get_audio_mix_rate() const {
58
uint32_t ret = 48000;
59
GDVIRTUAL_CALL(_get_audio_mix_rate, ret);
60
return ret;
61
}
62
AudioServer::SpeakerMode MovieWriter::get_audio_speaker_mode() const {
63
AudioServer::SpeakerMode ret = AudioServer::SPEAKER_MODE_STEREO;
64
GDVIRTUAL_CALL(_get_audio_speaker_mode, ret);
65
return ret;
66
}
67
68
Error MovieWriter::write_begin(const Size2i &p_movie_size, uint32_t p_fps, const String &p_base_path) {
69
Error ret = ERR_UNCONFIGURED;
70
GDVIRTUAL_CALL(_write_begin, p_movie_size, p_fps, p_base_path, ret);
71
return ret;
72
}
73
74
Error MovieWriter::write_frame(const Ref<Image> &p_image, const int32_t *p_audio_data) {
75
Error ret = ERR_UNCONFIGURED;
76
GDVIRTUAL_CALL(_write_frame, p_image, p_audio_data, ret);
77
return ret;
78
}
79
80
void MovieWriter::write_end() {
81
GDVIRTUAL_CALL(_write_end);
82
}
83
84
bool MovieWriter::handles_file(const String &p_path) const {
85
bool ret = false;
86
GDVIRTUAL_CALL(_handles_file, p_path, ret);
87
return ret;
88
}
89
90
void MovieWriter::get_supported_extensions(List<String> *r_extensions) const {
91
Vector<String> exts;
92
GDVIRTUAL_CALL(_get_supported_extensions, exts);
93
for (int i = 0; i < exts.size(); i++) {
94
r_extensions->push_back(exts[i]);
95
}
96
}
97
98
void MovieWriter::begin(const Size2i &p_movie_size, uint32_t p_fps, const String &p_base_path) {
99
project_name = GLOBAL_GET("application/config/name");
100
101
print_line(vformat("Movie Maker mode enabled, recording movie at %d FPS...", p_fps));
102
103
// When using Display/Window/Stretch/Mode = Viewport, use the project's
104
// configured viewport size instead of the size of the window in the OS
105
Size2i actual_movie_size = p_movie_size;
106
String stretch_mode = GLOBAL_GET("display/window/stretch/mode");
107
if (stretch_mode == "viewport") {
108
actual_movie_size.width = GLOBAL_GET("display/window/size/viewport_width");
109
actual_movie_size.height = GLOBAL_GET("display/window/size/viewport_height");
110
111
print_line(vformat("Movie Maker mode using project viewport size: %dx%d",
112
actual_movie_size.width, actual_movie_size.height));
113
}
114
115
// Check for available disk space and warn the user if needed.
116
Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
117
String path = p_base_path.get_basename();
118
if (path.is_relative_path()) {
119
path = "res://" + path;
120
}
121
dir->open(path);
122
if (dir->get_space_left() < 10 * Math::pow(1024.0, 3.0)) {
123
// Less than 10 GiB available.
124
WARN_PRINT(vformat("Current available space on disk is low (%s). MovieWriter will fail during movie recording if the disk runs out of available space.", String::humanize_size(dir->get_space_left())));
125
}
126
127
cpu_time = 0.0f;
128
gpu_time = 0.0f;
129
encoding_time_usec = 0;
130
131
mix_rate = get_audio_mix_rate();
132
AudioDriverDummy::get_dummy_singleton()->set_mix_rate(mix_rate);
133
AudioDriverDummy::get_dummy_singleton()->set_speaker_mode(AudioDriver::SpeakerMode(get_audio_speaker_mode()));
134
fps = p_fps;
135
if ((mix_rate % fps) != 0) {
136
WARN_PRINT("MovieWriter's audio mix rate (" + itos(mix_rate) + ") can not be divided by the recording FPS (" + itos(fps) + "). Audio may go out of sync over time.");
137
}
138
139
audio_channels = AudioDriverDummy::get_dummy_singleton()->get_channels();
140
audio_mix_buffer.resize(mix_rate * audio_channels / fps);
141
142
write_begin(actual_movie_size, p_fps, p_base_path);
143
}
144
145
void MovieWriter::_bind_methods() {
146
ClassDB::bind_static_method("MovieWriter", D_METHOD("add_writer", "writer"), &MovieWriter::add_writer);
147
148
GDVIRTUAL_BIND(_get_audio_mix_rate)
149
GDVIRTUAL_BIND(_get_audio_speaker_mode)
150
151
GDVIRTUAL_BIND(_handles_file, "path")
152
153
GDVIRTUAL_BIND(_write_begin, "movie_size", "fps", "base_path")
154
GDVIRTUAL_BIND(_write_frame, "frame_image", "audio_frame_block")
155
GDVIRTUAL_BIND(_write_end)
156
157
GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/movie_writer/mix_rate", PROPERTY_HINT_RANGE, "8000,192000,1,suffix:Hz"), 48000);
158
GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/movie_writer/speaker_mode", PROPERTY_HINT_ENUM, "Stereo,3.1,5.1,7.1"), 0);
159
GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "editor/movie_writer/video_quality", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), 0.75);
160
GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "editor/movie_writer/ogv/audio_quality", PROPERTY_HINT_RANGE, "-0.1,1.0,0.01"), 0.5);
161
GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/movie_writer/ogv/encoding_speed", PROPERTY_HINT_ENUM, "Fastest (Lowest Efficiency):4,Fast (Low Efficiency):3,Slow (High Efficiency):2,Slowest (Highest Efficiency):1"), 4);
162
GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/movie_writer/ogv/keyframe_interval", PROPERTY_HINT_RANGE, "1,1024,1"), 64);
163
164
// Used by the editor.
165
GLOBAL_DEF_BASIC("editor/movie_writer/movie_file", "");
166
GLOBAL_DEF_BASIC("editor/movie_writer/disable_vsync", false);
167
GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "editor/movie_writer/fps", PROPERTY_HINT_RANGE, "1,300,1,suffix:FPS"), 60);
168
}
169
170
void MovieWriter::set_extensions_hint() {
171
RBSet<String> found;
172
for (uint32_t i = 0; i < writer_count; i++) {
173
List<String> extensions;
174
writers[i]->get_supported_extensions(&extensions);
175
for (const String &ext : extensions) {
176
found.insert(ext);
177
}
178
}
179
180
String ext_hint;
181
182
for (const String &S : found) {
183
if (ext_hint != "") {
184
ext_hint += ",";
185
}
186
ext_hint += "*." + S;
187
}
188
ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "editor/movie_writer/movie_file", PROPERTY_HINT_GLOBAL_SAVE_FILE, ext_hint));
189
}
190
191
void MovieWriter::add_frame() {
192
const int movie_time_seconds = Engine::get_singleton()->get_frames_drawn() / fps;
193
const int frame_remainder = Engine::get_singleton()->get_frames_drawn() % fps;
194
const String movie_time = vformat("%s:%s:%s:%s",
195
String::num(movie_time_seconds / 3600, 0).pad_zeros(2),
196
String::num((movie_time_seconds % 3600) / 60, 0).pad_zeros(2),
197
String::num(movie_time_seconds % 60, 0).pad_zeros(2),
198
String::num(frame_remainder, 0).pad_zeros(2));
199
200
Window *main_window = Window::get_from_id(DisplayServer::MAIN_WINDOW_ID);
201
if (main_window) {
202
main_window->set_title(vformat("MovieWriter: Frame %d (time: %s) - %s", Engine::get_singleton()->get_frames_drawn(), movie_time, project_name));
203
}
204
205
RID main_vp_rid = RenderingServer::get_singleton()->viewport_find_from_screen_attachment(DisplayServer::MAIN_WINDOW_ID);
206
RID main_vp_texture = RenderingServer::get_singleton()->viewport_get_texture(main_vp_rid);
207
Ref<Image> vp_tex = RenderingServer::get_singleton()->texture_2d_get(main_vp_texture);
208
if (RenderingServer::get_singleton()->viewport_is_using_hdr_2d(main_vp_rid)) {
209
vp_tex->convert(Image::FORMAT_RGBA8);
210
vp_tex->linear_to_srgb();
211
}
212
213
RenderingServer::get_singleton()->viewport_set_measure_render_time(main_vp_rid, true);
214
cpu_time += RenderingServer::get_singleton()->viewport_get_measured_render_time_cpu(main_vp_rid);
215
cpu_time += RenderingServer::get_singleton()->get_frame_setup_time_cpu();
216
gpu_time += RenderingServer::get_singleton()->viewport_get_measured_render_time_gpu(main_vp_rid);
217
218
AudioDriverDummy::get_dummy_singleton()->mix_audio(mix_rate / fps, audio_mix_buffer.ptr());
219
220
uint64_t encoding_start_usec = Time::get_singleton()->get_ticks_usec();
221
write_frame(vp_tex, audio_mix_buffer.ptr());
222
uint64_t encoding_end_usec = Time::get_singleton()->get_ticks_usec();
223
encoding_time_usec += encoding_end_usec - encoding_start_usec;
224
}
225
226
void MovieWriter::end() {
227
uint64_t encoding_start_usec = Time::get_singleton()->get_ticks_usec();
228
write_end();
229
uint64_t encoding_end_usec = Time::get_singleton()->get_ticks_usec();
230
encoding_time_usec += encoding_end_usec - encoding_start_usec;
231
232
// Print a report with various statistics.
233
print_line("--------------------------------------------------------------------------------");
234
String movie_path = Engine::get_singleton()->get_write_movie_path();
235
if (movie_path.is_relative_path()) {
236
// Print absolute path to make finding the file easier,
237
// and to make it clickable in terminal emulators that support this.
238
movie_path = ProjectSettings::get_singleton()->globalize_path("res://").path_join(movie_path);
239
}
240
print_line(vformat("Done recording movie at path: %s", movie_path));
241
242
const int movie_time_seconds = Engine::get_singleton()->get_frames_drawn() / fps;
243
const int frame_remainder = Engine::get_singleton()->get_frames_drawn() % fps;
244
const String movie_time = vformat("%s:%s:%s:%s",
245
String::num(movie_time_seconds / 3600, 0).pad_zeros(2),
246
String::num((movie_time_seconds % 3600) / 60, 0).pad_zeros(2),
247
String::num(movie_time_seconds % 60, 0).pad_zeros(2),
248
String::num(frame_remainder, 0).pad_zeros(2));
249
250
const int real_time_seconds = Time::get_singleton()->get_ticks_msec() / 1000;
251
const String real_time = vformat("%s:%s:%s",
252
String::num(real_time_seconds / 3600, 0).pad_zeros(2),
253
String::num((real_time_seconds % 3600) / 60, 0).pad_zeros(2),
254
String::num(real_time_seconds % 60, 0).pad_zeros(2));
255
256
print_line(vformat("%d frames at %d FPS (movie length: %s), recorded in %s (%d%% of real-time speed).", Engine::get_singleton()->get_frames_drawn(), fps, movie_time, real_time, (float(MAX(1, movie_time_seconds)) / MAX(1, real_time_seconds)) * 100));
257
print_line(vformat("CPU render time: %.2f seconds (average: %.2f ms/frame)", cpu_time / 1000, cpu_time / Engine::get_singleton()->get_frames_drawn()));
258
print_line(vformat("GPU render time: %.2f seconds (average: %.2f ms/frame)", gpu_time / 1000, gpu_time / Engine::get_singleton()->get_frames_drawn()));
259
print_line(vformat("Encoding time: %.2f seconds (average: %.2f ms/frame)", encoding_time_usec / 1000000.f, encoding_time_usec / 1000.f / Engine::get_singleton()->get_frames_drawn()));
260
print_line("--------------------------------------------------------------------------------");
261
}
262
263