From a880c32cc2501eb7b3fecdc74e64904491473d48 Mon Sep 17 00:00:00 2001 From: cat Date: Sun, 26 Oct 2025 21:53:25 +0200 Subject: [PATCH] Preserving sound is added MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Working on less mem allocs if possible 🥺 --- src/Video.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Video.cpp b/src/Video.cpp index 3f8929a..f66e715 100644 --- a/src/Video.cpp +++ b/src/Video.cpp @@ -218,9 +218,7 @@ void Video::continueVideo() { // ======================== HELPERS ======================== std::pair> Video::loadNextFrame() { av_frame_unref(convertedFrame); - av_frame_unref(convertedAudioFrame); av_frame_unref(frame); - av_frame_unref(audioFrame); // A hard stop if we are out of frames to read while (av_read_frame(ctx, packet) >= 0) { @@ -230,6 +228,10 @@ std::pair> Video::loadNextFrame() { avcodec_receive_frame(aCodecCtx, audioFrame); if (audioFrame->format != -1 && audioEngine) { + // Putting a pin on here, shouldn't we be able to rewrite on top of the + // same buffer? + // + // At least with image frames the size is always constant convertedAudioFrame->format = sampleFormat; convertedAudioFrame->sample_rate = audioEngine->GetSampleRate(); convertedAudioFrame->ch_layout = outLayout; @@ -241,6 +243,10 @@ std::pair> Video::loadNextFrame() { Sound->WriteToRingBuffer(convertedAudioFrame->data[0], convertedAudioFrame->linesize[0]); + + // Just due to a small memory leak this needs to be here + av_frame_unref(convertedAudioFrame); + av_frame_unref(audioFrame); } } @@ -261,6 +267,9 @@ std::pair> Video::loadNextFrame() { av_packet_unref(packet); } + // Definetly need to calculate this once and then make a list of + // preallocated frames in the framebuffer that we can constantly + // cycle size_t dataSize = av_image_get_buffer_size( static_cast(convertedFrame->format), Dimensions.x(), Dimensions.y(), 3); @@ -389,7 +398,15 @@ void Video::dumpAndRefillBuffer() { void Video::reinitSound() { if (audioStreamNum != -1) { - delete Sound.release(); + // TO DO: we need a way to easily pass sound configs between two instances + auto oldSound = Sound.release(); + oldSound->Pause(); Sound = std::move(audioEngine->CreateSound(10)); + + // This is very much temparory + Sound->SetVolume(oldSound->GetVolume()); + Sound->SetPosition(oldSound->GetPosition()); + + delete oldSound; } }