Skip to content

Commit

Permalink
Adding proper error codes + documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
voylin committed Aug 8, 2024
1 parent 01af614 commit eef4b55
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 69 deletions.
60 changes: 60 additions & 0 deletions ERROR_CODES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# GDE GoZen error codes

## Video class

### get_video_file_meta

No error code but returns an empty dictionary when nothing was able to be read.

### open_video

- 0: OK;
- -1: Couldn't open video file;
- -2: Failed setting up of audio decoder;
- -3: Failed setting up of video decoder;
- -4: Failed setting up of SWS;
- -5: Video file is not usable due to limitations;
- -6: Invalid frame-rate found in video;
- -7: Could not establish the total amount of frames in video files;
- -8: Failed setting up of SWR;
- -9: Audio seeking error;

### seek_frame and next_frame

No error code but returns an empty picture when an error occured.

## Renderer class

### open

- 0: OK;
- -1: Renderer not fully ready (some variables aren't set);
- -2: Failed to open video context;
- -3: Failed to setup video encoder;
- -4: failed to setup audio encoder;
- -5: Failed to setup video stream;
- -6: Failed to setup audio stream;
- -7: Couldn't create packet;
- -8: Couldn't create frame;
- -9: Couldn't create SWS;
- -10: Couldn't create SWR;
- -11: Failed to open video file;
- -12: Failed to write stream header;

### send_frame

- 0: OK;
- -1: Video codec isn't open;
- -2: Frame is not write-able;
- -3: Couldn't send frame to encoder;

Any other response is an FFmpeg error code which has to do with sending, receiving, and writing frames.


### send_audio

- 0: OK;
- -1: Audio rendering is not enabled;
- -2: Audio codec isn't open;

###
44 changes: 21 additions & 23 deletions src/renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "renderer.hpp"
#include <libavcodec/packet.h>

// TODO: Set proper return errors and document them!

Renderer::~Renderer() {
close();
Expand Down Expand Up @@ -119,7 +118,7 @@ int Renderer::open() {
av_stream_video = avformat_new_stream(av_format_ctx, NULL);
if (!av_stream_video) {
UtilityFunctions::printerr("Couldn't create stream!");
return -3;
return -7;
}

av_codec_ctx_video->codec_id = av_codec_id_video;
Expand All @@ -137,25 +136,25 @@ int Renderer::open() {
av_codec_audio = avcodec_find_encoder(av_codec_id_audio);
if (!av_codec_audio) {
UtilityFunctions::printerr("Audio codec not found!");
return -3;
return -4;
}

av_codec_ctx_audio = avcodec_alloc_context3(av_codec_audio);
if (!av_codec_ctx_audio) {
UtilityFunctions::printerr("Couldn't allocate audio codec context!");
return -3;
return -4;
}

av_packet_audio = av_packet_alloc();
if (!av_packet_audio) {
UtilityFunctions::printerr("Couldn't allocate packet!");
return -3;
return -7;
}

av_stream_audio = avformat_new_stream(av_format_ctx, NULL);
if (!av_stream_audio) {
UtilityFunctions::printerr("Couldn't create new stream!");
return -3;
return -6;
}

av_codec_ctx_audio->sample_fmt = (*av_codec_audio).sample_fmts ? (*av_codec_audio).sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
Expand All @@ -179,8 +178,8 @@ int Renderer::open() {
}

// TODO: Add options in render profile for these type of things
// if (codec->id == AV_CODEC_ID_H264)
// av_opt_set(p_codec_context->priv_data, "preset", "slow", 0);
if (av_codec_video->id == AV_CODEC_ID_H264)
av_opt_set(p_codec_context->priv_data, "preset", "slow", 0);

// Opening the video encoder codec
response = avcodec_open2(av_codec_ctx_video, av_codec_video, NULL);
Expand All @@ -201,19 +200,19 @@ int Renderer::open() {
av_packet_video = av_packet_alloc();
if (!av_packet_video) {
UtilityFunctions::printerr("Couldn't allocate packet!");
return -3;
return -7;
}
av_frame_video = av_frame_alloc();
if (!av_frame_video) {
UtilityFunctions::printerr("Couldn't allocate frame!");
return -3;
return -8;
}
av_frame_video->format = AV_PIX_FMT_YUV420P;
av_frame_video->width = resolution.x;
av_frame_video->height = resolution.y;
if (av_frame_get_buffer(av_frame_video, 0)) {
UtilityFunctions::printerr("Couldn't allocate frame data!");
return -3;
return -8;
}

sws_ctx = sws_getContext(
Expand All @@ -222,13 +221,13 @@ int Renderer::open() {
SWS_BILINEAR, NULL, NULL, NULL); // TODO: Option to change SWS_BILINEAR
if (!sws_ctx) {
UtilityFunctions::printerr("Couldn't get sws context!");
return -7;
return -9;
}

// Copy video stream params to muxer
if (avcodec_parameters_from_context(av_stream_video->codecpar, av_codec_ctx_video) < 0) {
UtilityFunctions::printerr("Couldn't copy video stream params!");
return -3;
return -5;
}

if (render_audio) {
Expand Down Expand Up @@ -260,7 +259,7 @@ int Renderer::open() {
swr_ctx = swr_alloc();
if (!swr_ctx) {
UtilityFunctions::printerr("Couldn't allocate swr!");
return -4;
return -10;
}

// Setting audio options
Expand All @@ -274,7 +273,7 @@ int Renderer::open() {
// Initialize resampling context
if ((response = swr_init(swr_ctx)) < 0) {
UtilityFunctions::printerr("Failed to initialize resampling context!");
return -4;
return -10;
}
}

Expand All @@ -285,22 +284,21 @@ int Renderer::open() {
response = avio_open(&av_format_ctx->pb, file_path.utf8(), AVIO_FLAG_WRITE);
if (response < 0) {
UtilityFunctions::printerr("Couldn't open output file!", get_av_error());
return -5;
return -11;
}
}

// Write stream header - if any
response = avformat_write_header(av_format_ctx, NULL);
if (response < 0) {
UtilityFunctions::printerr("Error when writing header!", get_av_error());
return -6;
return -12;
}
av_packet_free(&av_packet_video);
i = 0; // Reset i for send_frame
return 0;
return OK;
}

// TODO: Make argument int frame_nr, this could allow for multi-threaded rendering ... maybe
int Renderer::send_frame(Ref<Image> a_frame_image) {
if (!av_codec_ctx_video) {
UtilityFunctions::printerr("Video codec isn't open!");
Expand All @@ -323,7 +321,7 @@ int Renderer::send_frame(Ref<Image> a_frame_image) {
response = avcodec_send_frame(av_codec_ctx_video, av_frame_video);
if (response < 0) {
UtilityFunctions::printerr("Error sending video frame!", get_av_error());
return -1;
return -3;
}

av_packet_video = av_packet_alloc();
Expand Down Expand Up @@ -379,12 +377,12 @@ int Renderer::send_audio(Ref<AudioStreamWAV> a_wav) {
// i += av_frame_audio->nb_samples;
// while loop end to repeat

return 0;
return OK;
}

int Renderer::close() {
if (av_codec_ctx_video == nullptr)
return 1;
return -1;

av_write_trailer(av_format_ctx);

Expand All @@ -404,5 +402,5 @@ int Renderer::close() {
avio_closep(&av_format_ctx->pb);
avformat_free_context(av_format_ctx);

return 0;
return OK;
}
Loading

0 comments on commit eef4b55

Please sign in to comment.