Skip to content

Commit 87e451d

Browse files
committed
utils : add VideoRecorder::close() API (to manually close recorder)
1 parent d60e2db commit 87e451d

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- utils : avoid public inclusion of `libavutil/pixfmt` header for video recorder
1313
- core : `errors.h`: make terminate_with_message a template (with formatting arguments, etc)
14+
- utils: add VideoRecorder::close() API (to manually close recorder)
1415

1516
## [0.0.7] - 2025-05-17
1617

src/candlewick/utils/VideoRecorder.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,26 @@ namespace media {
3636
void writeFrame(const Uint8 *data, Uint32 payloadSize,
3737
AVPixelFormat avPixelFormat);
3838

39-
~VideoRecorderImpl() noexcept;
39+
void close() noexcept;
40+
41+
~VideoRecorderImpl() noexcept { this->close(); }
4042
};
4143

42-
VideoRecorderImpl::~VideoRecorderImpl() noexcept {
44+
void VideoRecorderImpl::close() noexcept {
45+
if (!formatContext)
46+
return;
47+
4348
av_write_trailer(formatContext);
44-
if (codecContext)
45-
avcodec_free_context(&codecContext);
46-
if (formatContext->pb)
47-
avio_closep(&formatContext->pb);
48-
if (formatContext)
49-
avformat_free_context(formatContext);
50-
if (frame)
51-
av_frame_free(&frame);
52-
if (packet)
53-
av_packet_free(&packet);
49+
50+
// close out stream
51+
av_frame_free(&frame);
52+
// av_frame_free(&tmpFrame);
53+
av_packet_free(&packet);
54+
avcodec_free_context(&codecContext);
55+
56+
avio_closep(&formatContext->pb);
57+
avformat_free_context(formatContext);
58+
formatContext = nullptr;
5459
}
5560

5661
VideoRecorderImpl::VideoRecorderImpl(Uint32 width, Uint32 height,
@@ -226,6 +231,11 @@ namespace media {
226231
impl_->writeFrame(data, payloadSize, outputFormat);
227232
}
228233

234+
void VideoRecorder::close() noexcept {
235+
if (impl_)
236+
impl_->close();
237+
}
238+
229239
VideoRecorder::~VideoRecorder() = default;
230240

231241
} // namespace media

src/candlewick/utils/VideoRecorder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace media {
4141
Uint32 frameCounter() const;
4242
void writeFrame(const Uint8 *data, Uint32 payloadSize,
4343
SDL_GPUTextureFormat pixelFormat);
44+
void close() noexcept;
4445
~VideoRecorder();
4546
};
4647

0 commit comments

Comments
 (0)