movie_writer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include "movie_writer.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/os/time.h"
  34. #include "servers/display_server.h"
  35. #include "servers/rendering_server.h"
  36. MovieWriter *MovieWriter::writers[MovieWriter::MAX_WRITERS];
  37. uint32_t MovieWriter::writer_count = 0;
  38. void MovieWriter::add_writer(MovieWriter *p_writer) {
  39. ERR_FAIL_COND(writer_count == MAX_WRITERS);
  40. writers[writer_count++] = p_writer;
  41. }
  42. MovieWriter *MovieWriter::find_writer_for_file(const String &p_file) {
  43. for (int32_t i = writer_count - 1; i >= 0; i--) { // More recent last, to have override ability.
  44. if (writers[i]->handles_file(p_file)) {
  45. return writers[i];
  46. }
  47. }
  48. return nullptr;
  49. }
  50. uint32_t MovieWriter::get_audio_mix_rate() const {
  51. uint32_t ret = 48000;
  52. GDVIRTUAL_REQUIRED_CALL(_get_audio_mix_rate, ret);
  53. return ret;
  54. }
  55. AudioServer::SpeakerMode MovieWriter::get_audio_speaker_mode() const {
  56. AudioServer::SpeakerMode ret = AudioServer::SPEAKER_MODE_STEREO;
  57. GDVIRTUAL_REQUIRED_CALL(_get_audio_speaker_mode, ret);
  58. return ret;
  59. }
  60. Error MovieWriter::write_begin(const Size2i &p_movie_size, uint32_t p_fps, const String &p_base_path) {
  61. Error ret = ERR_UNCONFIGURED;
  62. GDVIRTUAL_REQUIRED_CALL(_write_begin, p_movie_size, p_fps, p_base_path, ret);
  63. return ret;
  64. }
  65. Error MovieWriter::write_frame(const Ref<Image> &p_image, const int32_t *p_audio_data) {
  66. Error ret = ERR_UNCONFIGURED;
  67. GDVIRTUAL_REQUIRED_CALL(_write_frame, p_image, p_audio_data, ret);
  68. return ret;
  69. }
  70. void MovieWriter::write_end() {
  71. GDVIRTUAL_REQUIRED_CALL(_write_end);
  72. }
  73. bool MovieWriter::handles_file(const String &p_path) const {
  74. bool ret = false;
  75. GDVIRTUAL_REQUIRED_CALL(_handles_file, p_path, ret);
  76. return ret;
  77. }
  78. void MovieWriter::get_supported_extensions(List<String> *r_extensions) const {
  79. Vector<String> exts;
  80. GDVIRTUAL_REQUIRED_CALL(_get_supported_extensions, exts);
  81. for (int i = 0; i < exts.size(); i++) {
  82. r_extensions->push_back(exts[i]);
  83. }
  84. }
  85. void MovieWriter::begin(const Size2i &p_movie_size, uint32_t p_fps, const String &p_base_path) {
  86. project_name = GLOBAL_GET("application/config/name");
  87. print_line(vformat("Movie Maker mode enabled, recording movie at %d FPS...", p_fps));
  88. // Check for available disk space and warn the user if needed.
  89. Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  90. String path = p_base_path.get_basename();
  91. if (path.is_relative_path()) {
  92. path = "res://" + path;
  93. }
  94. dir->open(path);
  95. if (dir->get_space_left() < 10 * Math::pow(1024.0, 3.0)) {
  96. // Less than 10 GiB available.
  97. 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())));
  98. }
  99. cpu_time = 0.0f;
  100. gpu_time = 0.0f;
  101. mix_rate = get_audio_mix_rate();
  102. AudioDriverDummy::get_dummy_singleton()->set_mix_rate(mix_rate);
  103. AudioDriverDummy::get_dummy_singleton()->set_speaker_mode(AudioDriver::SpeakerMode(get_audio_speaker_mode()));
  104. fps = p_fps;
  105. if ((mix_rate % fps) != 0) {
  106. 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.");
  107. }
  108. audio_channels = AudioDriverDummy::get_dummy_singleton()->get_channels();
  109. audio_mix_buffer.resize(mix_rate * audio_channels / fps);
  110. write_begin(p_movie_size, p_fps, p_base_path);
  111. }
  112. void MovieWriter::_bind_methods() {
  113. ClassDB::bind_static_method("MovieWriter", D_METHOD("add_writer", "writer"), &MovieWriter::add_writer);
  114. GDVIRTUAL_BIND(_get_audio_mix_rate)
  115. GDVIRTUAL_BIND(_get_audio_speaker_mode)
  116. GDVIRTUAL_BIND(_handles_file, "path")
  117. GDVIRTUAL_BIND(_write_begin, "movie_size", "fps", "base_path")
  118. GDVIRTUAL_BIND(_write_frame, "frame_image", "audio_frame_block")
  119. GDVIRTUAL_BIND(_write_end)
  120. GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/movie_writer/mix_rate", PROPERTY_HINT_RANGE, "8000,192000,1,suffix:Hz"), 48000);
  121. GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/movie_writer/speaker_mode", PROPERTY_HINT_ENUM, "Stereo,3.1,5.1,7.1"), 0);
  122. GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "editor/movie_writer/mjpeg_quality", PROPERTY_HINT_RANGE, "0.01,1.0,0.01"), 0.75);
  123. // Used by the editor.
  124. GLOBAL_DEF_BASIC("editor/movie_writer/movie_file", "");
  125. GLOBAL_DEF_BASIC("editor/movie_writer/disable_vsync", false);
  126. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "editor/movie_writer/fps", PROPERTY_HINT_RANGE, "1,300,1,suffix:FPS"), 60);
  127. }
  128. void MovieWriter::set_extensions_hint() {
  129. RBSet<String> found;
  130. for (uint32_t i = 0; i < writer_count; i++) {
  131. List<String> extensions;
  132. writers[i]->get_supported_extensions(&extensions);
  133. for (const String &ext : extensions) {
  134. found.insert(ext);
  135. }
  136. }
  137. String ext_hint;
  138. for (const String &S : found) {
  139. if (ext_hint != "") {
  140. ext_hint += ",";
  141. }
  142. ext_hint += "*." + S;
  143. }
  144. ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "editor/movie_writer/movie_file", PROPERTY_HINT_GLOBAL_SAVE_FILE, ext_hint));
  145. }
  146. void MovieWriter::add_frame() {
  147. const int movie_time_seconds = Engine::get_singleton()->get_frames_drawn() / fps;
  148. const String movie_time = vformat("%s:%s:%s",
  149. String::num(movie_time_seconds / 3600).pad_zeros(2),
  150. String::num((movie_time_seconds % 3600) / 60).pad_zeros(2),
  151. String::num(movie_time_seconds % 60).pad_zeros(2));
  152. #ifdef DEBUG_ENABLED
  153. DisplayServer::get_singleton()->window_set_title(vformat("MovieWriter: Frame %d (time: %s) - %s (DEBUG)", Engine::get_singleton()->get_frames_drawn(), movie_time, project_name));
  154. #else
  155. DisplayServer::get_singleton()->window_set_title(vformat("MovieWriter: Frame %d (time: %s) - %s", Engine::get_singleton()->get_frames_drawn(), movie_time, project_name));
  156. #endif
  157. RID main_vp_rid = RenderingServer::get_singleton()->viewport_find_from_screen_attachment(DisplayServer::MAIN_WINDOW_ID);
  158. RID main_vp_texture = RenderingServer::get_singleton()->viewport_get_texture(main_vp_rid);
  159. Ref<Image> vp_tex = RenderingServer::get_singleton()->texture_2d_get(main_vp_texture);
  160. RenderingServer::get_singleton()->viewport_set_measure_render_time(main_vp_rid, true);
  161. cpu_time += RenderingServer::get_singleton()->viewport_get_measured_render_time_cpu(main_vp_rid);
  162. cpu_time += RenderingServer::get_singleton()->get_frame_setup_time_cpu();
  163. gpu_time += RenderingServer::get_singleton()->viewport_get_measured_render_time_gpu(main_vp_rid);
  164. AudioDriverDummy::get_dummy_singleton()->mix_audio(mix_rate / fps, audio_mix_buffer.ptr());
  165. write_frame(vp_tex, audio_mix_buffer.ptr());
  166. }
  167. void MovieWriter::end() {
  168. write_end();
  169. // Print a report with various statistics.
  170. print_line("----------------");
  171. String movie_path = Engine::get_singleton()->get_write_movie_path();
  172. if (movie_path.is_relative_path()) {
  173. // Print absolute path to make finding the file easier,
  174. // and to make it clickable in terminal emulators that support this.
  175. movie_path = ProjectSettings::get_singleton()->globalize_path("res://").path_join(movie_path);
  176. }
  177. print_line(vformat("Done recording movie at path: %s", movie_path));
  178. const int movie_time_seconds = Engine::get_singleton()->get_frames_drawn() / fps;
  179. const String movie_time = vformat("%s:%s:%s",
  180. String::num(movie_time_seconds / 3600).pad_zeros(2),
  181. String::num((movie_time_seconds % 3600) / 60).pad_zeros(2),
  182. String::num(movie_time_seconds % 60).pad_zeros(2));
  183. const int real_time_seconds = Time::get_singleton()->get_ticks_msec() / 1000;
  184. const String real_time = vformat("%s:%s:%s",
  185. String::num(real_time_seconds / 3600).pad_zeros(2),
  186. String::num((real_time_seconds % 3600) / 60).pad_zeros(2),
  187. String::num(real_time_seconds % 60).pad_zeros(2));
  188. 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(movie_time_seconds) / real_time_seconds) * 100));
  189. print_line(vformat("CPU time: %.2f seconds (average: %.2f ms/frame)", cpu_time / 1000, cpu_time / Engine::get_singleton()->get_frames_drawn()));
  190. print_line(vformat("GPU time: %.2f seconds (average: %.2f ms/frame)", gpu_time / 1000, gpu_time / Engine::get_singleton()->get_frames_drawn()));
  191. print_line("----------------");
  192. }