reserve 100 video stashes temporary fix

This commit is contained in:
Frank DeMarco 2019-06-16 19:12:21 -04:00
parent 30e6c970d3
commit d7f1877cbe
2 changed files with 27 additions and 40 deletions

View File

@ -7,6 +7,7 @@ Recorder::Recorder(Node* parent) : Node(parent)
get_delegate().subscribe(&Recorder::respond, this); get_delegate().subscribe(&Recorder::respond, this);
animation.play(); animation.play();
Mix_SetPostMix(&process_audio, this); Mix_SetPostMix(&process_audio, this);
video_stashes.reserve(100);
} }
float Recorder::get_frame_length() float Recorder::get_frame_length()
@ -86,7 +87,9 @@ void Recorder::grab_stash()
void Recorder::write_most_recent_frames() void Recorder::write_most_recent_frames()
{ {
make_directory(); make_directory();
write_stash_frames(false, 0); write_stash_frames(&most_recent_stash.pixel_buffers,
&most_recent_stash.flipped,
most_recent_stash.frame_offset);
open_audio_file(); open_audio_file();
while (not most_recent_stash.audio_buffers.empty()) while (not most_recent_stash.audio_buffers.empty())
{ {
@ -154,10 +157,13 @@ void Recorder::add_frame()
video_stashes.back().flipped.push_back(get_root()->is_gl_context); video_stashes.back().flipped.push_back(get_root()->is_gl_context);
if (video_stashes.back().pixel_buffers.size() * get_frame_length() > max_length) if (video_stashes.back().pixel_buffers.size() * get_frame_length() > max_length)
{ {
std::function<void(bool, int)> f = std::function<void(std::vector<unsigned char*>*, std::vector<bool>*, int)> f =
std::bind(&Recorder::write_stash_frames, this, std::bind(&Recorder::write_stash_frames, this,
std::placeholders::_1, std::placeholders::_2); std::placeholders::_1, std::placeholders::_2,
std::thread writing(f, true, video_stashes.size() - 1); std::placeholders::_3);
std::thread writing(f, &video_stashes.back().pixel_buffers,
&video_stashes.back().flipped,
video_stashes.back().frame_offset);
writing.detach(); writing.detach();
video_stashes.push_back( video_stashes.push_back(
Stash(video_stashes.back().frame_offset + Stash(video_stashes.back().frame_offset +
@ -206,32 +212,23 @@ void Recorder::make_directory()
current_video_directory = directory; current_video_directory = directory;
} }
void Recorder::write_stash_frames(bool is_video, int index) void Recorder::write_stash_frames(
std::vector<unsigned char*>* pixel_buffers, std::vector<bool>* flipped,
int frame_offset)
{ {
int frame_offset = is_video ? video_stashes[index].frame_offset : 0; SDL_Log("Writing stash offset %i to %s...", frame_offset, current_video_directory.c_str());
SDL_Log("Writing stash index %i to %s...", index, current_video_directory.c_str());
SDL_Surface* frame; SDL_Surface* frame;
GifWriter gif_writer; GifWriter gif_writer;
int gif_frame_length = get_configuration()["recording"]["gif-frame-length"]; int gif_frame_length = get_configuration()["recording"]["gif-frame-length"];
fs::path gif_path = sfw::get_next_file_name( fs::path gif_path = sfw::get_next_file_name(
current_video_directory, 3, "gif-", ".gif"); current_video_directory, 3, "gif-", ".gif");
float elapsed = 0, last_gif_write = 0, gif_write_overflow = 0; float elapsed = 0, last_gif_write = 0, gif_write_overflow = 0;
for (int ii = frame_offset; SDL_Log("Pixel buffers size %zu", pixel_buffers->size());
not (is_video ? video_stashes[index].pixel_buffers.empty() : for (int ii = frame_offset; not pixel_buffers->empty(); ii++)
most_recent_stash.pixel_buffers.empty()); ii++)
{ {
if (is_video) frame = get_display().get_screen_surface_from_pixels(
{ pixel_buffers->front(), flipped->front());
frame = get_display().get_screen_surface_from_pixels( get_root()->log_surface_format(frame, "got");
video_stashes[index].pixel_buffers.front(),
video_stashes[index].flipped.front());
}
else
{
frame = get_display().get_screen_surface_from_pixels(
most_recent_stash.pixel_buffers.front(),
most_recent_stash.flipped.front());
}
std::stringstream name; std::stringstream name;
name << sfw::pad(ii, 5) << ".png"; name << sfw::pad(ii, 5) << ".png";
fs::path path = current_video_directory / name.str(); fs::path path = current_video_directory / name.str();
@ -255,22 +252,9 @@ void Recorder::write_stash_frames(bool is_video, int index)
frame->w, frame->h, gif_frame_length / 10); frame->w, frame->h, gif_frame_length / 10);
} }
elapsed += get_frame_length(); elapsed += get_frame_length();
if (is_video) delete[] pixel_buffers->front();
{ pixel_buffers->erase(pixel_buffers->begin());
delete[] video_stashes[index].pixel_buffers.front(); flipped->erase(flipped->begin());
video_stashes[index].pixel_buffers.erase(
video_stashes[index].pixel_buffers.begin());
video_stashes[index].flipped.erase(
video_stashes[index].flipped.begin());
}
else
{
delete[] most_recent_stash.pixel_buffers.front();
most_recent_stash.pixel_buffers.erase(
most_recent_stash.pixel_buffers.begin());
most_recent_stash.flipped.erase(
most_recent_stash.flipped.begin());
}
SDL_FreeSurface(frame); SDL_FreeSurface(frame);
} }
GifEnd(&gif_writer); GifEnd(&gif_writer);
@ -307,7 +291,9 @@ void Recorder::end_recording()
void Recorder::finish_writing_video() void Recorder::finish_writing_video()
{ {
write_stash_frames(true, video_stashes.size() - 1); write_stash_frames(
&video_stashes.back().pixel_buffers, &video_stashes.back().flipped,
video_stashes.back().frame_offset);
int count; int count;
while (true) while (true)
{ {

View File

@ -54,7 +54,8 @@ struct Recorder : Node
void add_frame(); void add_frame();
int get_memory_size(); int get_memory_size();
void make_directory(); void make_directory();
void write_stash_frames(bool, int); void write_stash_frames(
std::vector<unsigned char*>*, std::vector<bool>*, int);
void keep_stash(); void keep_stash();
void end_recording(); void end_recording();
void finish_writing_video(); void finish_writing_video();