diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/args_parser.h | 106 | ||||
-rw-r--r-- | include/color_conversion.h | 10 | ||||
-rw-r--r-- | include/defs.h | 75 | ||||
-rw-r--r-- | include/encoder/encoder.h | 44 | ||||
-rw-r--r-- | include/encoder/video/video.h | 7 | ||||
-rw-r--r-- | include/replay_buffer.h | 41 |
6 files changed, 271 insertions, 12 deletions
diff --git a/include/args_parser.h b/include/args_parser.h new file mode 100644 index 0000000..d1b9713 --- /dev/null +++ b/include/args_parser.h @@ -0,0 +1,106 @@ +#ifndef GSR_ARGS_PARSER_H +#define GSR_ARGS_PARSER_H + +#include <stdbool.h> +#include <stdint.h> +#include "defs.h" +#include "vec2.h" + +typedef struct gsr_egl gsr_egl; + +#define NUM_ARGS 29 +#define WINDOW_STR_MAX_SIZE 128 + +typedef enum { + ARG_TYPE_STRING, + ARG_TYPE_BOOLEAN, + ARG_TYPE_ENUM, + ARG_TYPE_I64, + ARG_TYPE_DOUBLE, +} ArgType; + +typedef struct { + const char *name; + int value; +} ArgEnum; + +typedef struct { + ArgType type; + const char **values; + int capacity_num_values; + int num_values; + + const char *key; + bool optional; + bool list; + + const ArgEnum *enum_values; + int num_enum_values; + + int64_t integer_value_min; + int64_t integer_value_max; + + union { + bool boolean; + int enum_value; + int64_t i64_value; + double d_value; + } typed_value; +} Arg; + +typedef struct { + void (*version)(void *userdata); + void (*info)(void *userdata); + void (*list_audio_devices)(void *userdata); + void (*list_application_audio)(void *userdata); + void (*list_capture_options)(const char *card_path, void *userdata); +} args_handlers; + +typedef struct { + Arg args[NUM_ARGS]; + + gsr_video_encoder_hardware video_encoder; + gsr_pixel_format pixel_format; + gsr_framerate_mode framerate_mode; + gsr_color_range color_range; + gsr_tune tune; + gsr_video_codec video_codec; + gsr_audio_codec audio_codec; + gsr_bitrate_mode bitrate_mode; + gsr_video_quality video_quality; + char window[WINDOW_STR_MAX_SIZE]; + const char *container_format; + const char *filename; + const char *replay_recording_directory; + const char *portal_session_token_filepath; + const char *recording_saved_script; + bool verbose; + bool gl_debug; + bool record_cursor; + bool date_folders; + bool restore_portal_session; + bool restart_replay_on_save; + bool overclock; + bool is_livestream; + bool is_output_piped; + bool low_latency_recording; + bool very_old_gpu; + int64_t video_bitrate; + int64_t audio_bitrate; + int64_t fps; + int64_t replay_buffer_size_secs; + double keyint; + vec2i output_resolution; + vec2i region_size; + vec2i region_position; +} args_parser; + +/* |argv| is stored as a reference */ +bool args_parser_parse(args_parser *self, int argc, char **argv, const args_handlers *args_handlers, void *userdata); +void args_parser_deinit(args_parser *self); + +bool args_parser_validate_with_gl_info(args_parser *self, gsr_egl *egl); +void args_parser_print_usage(void); +Arg* args_parser_get_arg(args_parser *self, const char *arg_name); + +#endif /* GSR_ARGS_PARSER_H */ diff --git a/include/color_conversion.h b/include/color_conversion.h index a8462c0..76d8be5 100644 --- a/include/color_conversion.h +++ b/include/color_conversion.h @@ -11,16 +11,6 @@ #define GSR_COLOR_CONVERSION_MAX_FRAMEBUFFERS 2 typedef enum { - GSR_COLOR_RANGE_LIMITED, - GSR_COLOR_RANGE_FULL -} gsr_color_range; - -typedef enum { - GSR_COLOR_DEPTH_8_BITS, - GSR_COLOR_DEPTH_10_BITS -} gsr_color_depth; - -typedef enum { GSR_SOURCE_COLOR_RGB, GSR_SOURCE_COLOR_BGR } gsr_source_color; diff --git a/include/defs.h b/include/defs.h index cbb5618..def5ed5 100644 --- a/include/defs.h +++ b/include/defs.h @@ -3,6 +3,9 @@ #include <stdbool.h> +#define GSR_VIDEO_CODEC_AUTO -1 +#define GSR_BITRATE_MODE_AUTO -1 + typedef enum { GSR_GPU_VENDOR_AMD, GSR_GPU_VENDOR_INTEL, @@ -29,4 +32,76 @@ typedef enum { GSR_CONNECTION_DRM } gsr_connection_type; +typedef enum { + GSR_VIDEO_QUALITY_MEDIUM, + GSR_VIDEO_QUALITY_HIGH, + GSR_VIDEO_QUALITY_VERY_HIGH, + GSR_VIDEO_QUALITY_ULTRA, +} gsr_video_quality; + +typedef enum { + GSR_VIDEO_CODEC_H264, + GSR_VIDEO_CODEC_HEVC, + GSR_VIDEO_CODEC_HEVC_HDR, + GSR_VIDEO_CODEC_HEVC_10BIT, + GSR_VIDEO_CODEC_AV1, + GSR_VIDEO_CODEC_AV1_HDR, + GSR_VIDEO_CODEC_AV1_10BIT, + GSR_VIDEO_CODEC_VP8, + GSR_VIDEO_CODEC_VP9, + GSR_VIDEO_CODEC_H264_VULKAN, + GSR_VIDEO_CODEC_HEVC_VULKAN, +} gsr_video_codec; + +typedef enum { + GSR_AUDIO_CODEC_AAC, + GSR_AUDIO_CODEC_OPUS, + GSR_AUDIO_CODEC_FLAC, +} gsr_audio_codec; + +typedef enum { + GSR_PIXEL_FORMAT_YUV420, + GSR_PIXEL_FORMAT_YUV444, +} gsr_pixel_format; + +typedef enum { + GSR_FRAMERATE_MODE_CONSTANT, + GSR_FRAMERATE_MODE_VARIABLE, + GSR_FRAMERATE_MODE_CONTENT, +} gsr_framerate_mode; + +typedef enum { + GSR_BITRATE_MODE_QP, + GSR_BITRATE_MODE_VBR, + GSR_BITRATE_MODE_CBR, +} gsr_bitrate_mode; + +typedef enum { + GSR_TUNE_PERFORMANCE, + GSR_TUNE_QUALITY, +} gsr_tune; + +typedef enum { + GSR_VIDEO_ENCODER_HW_GPU, + GSR_VIDEO_ENCODER_HW_CPU, +} gsr_video_encoder_hardware; + +typedef enum { + GSR_COLOR_RANGE_LIMITED, + GSR_COLOR_RANGE_FULL +} gsr_color_range; + +typedef enum { + GSR_COLOR_DEPTH_8_BITS, + GSR_COLOR_DEPTH_10_BITS +} gsr_color_depth; + +bool video_codec_is_hdr(gsr_video_codec video_codec); +gsr_video_codec hdr_video_codec_to_sdr_video_codec(gsr_video_codec video_codec); +gsr_color_depth video_codec_to_bit_depth(gsr_video_codec video_codec); +const char* video_codec_to_string(gsr_video_codec video_codec); +bool video_codec_is_av1(gsr_video_codec video_codec); +bool video_codec_is_vulkan(gsr_video_codec video_codec); +const char* audio_codec_get_name(gsr_audio_codec audio_codec); + #endif /* GSR_DEFS_H */ diff --git a/include/encoder/encoder.h b/include/encoder/encoder.h new file mode 100644 index 0000000..8f03149 --- /dev/null +++ b/include/encoder/encoder.h @@ -0,0 +1,44 @@ +#ifndef GSR_ENCODER_H +#define GSR_ENCODER_H + +#include "../replay_buffer.h" +#include <stdbool.h> +#include <stdint.h> +#include <stddef.h> +#include <pthread.h> + +#define GSR_MAX_RECORDING_DESTINATIONS 128 + +typedef struct AVCodecContext AVCodecContext; +typedef struct AVFormatContext AVFormatContext; +typedef struct AVStream AVStream; + +typedef struct { + size_t id; + AVCodecContext *codec_context; + AVFormatContext *format_context; + AVStream *stream; + int64_t start_pts; + bool has_received_keyframe; +} gsr_encoder_recording_destination; + +typedef struct { + gsr_replay_buffer replay_buffer; + bool has_replay_buffer; + pthread_mutex_t file_write_mutex; + bool mutex_created; + + gsr_encoder_recording_destination recording_destinations[GSR_MAX_RECORDING_DESTINATIONS]; + size_t num_recording_destinations; + size_t recording_destination_id_counter; +} gsr_encoder; + +bool gsr_encoder_init(gsr_encoder *self, size_t replay_buffer_num_packets); +void gsr_encoder_deinit(gsr_encoder *self); + +void gsr_encoder_receive_packets(gsr_encoder *self, AVCodecContext *codec_context, int64_t pts, int stream_index); +/* Returns the id to the recording destination, or -1 on error */ +size_t gsr_encoder_add_recording_destination(gsr_encoder *self, AVCodecContext *codec_context, AVFormatContext *format_context, AVStream *stream, int64_t start_pts); +bool gsr_encoder_remove_recording_destination(gsr_encoder *self, size_t id); + +#endif /* GSR_ENCODER_H */ diff --git a/include/encoder/video/video.h b/include/encoder/video/video.h index 49f48bd..7a706b5 100644 --- a/include/encoder/video/video.h +++ b/include/encoder/video/video.h @@ -4,24 +4,27 @@ #include "../../color_conversion.h" #include <stdbool.h> +#define GSR_MAX_RECORDING_DESTINATIONS 128 + typedef struct gsr_video_encoder gsr_video_encoder; typedef struct AVCodecContext AVCodecContext; typedef struct AVFrame AVFrame; struct gsr_video_encoder { bool (*start)(gsr_video_encoder *encoder, AVCodecContext *video_codec_context, AVFrame *frame); + void (*destroy)(gsr_video_encoder *encoder, AVCodecContext *video_codec_context); void (*copy_textures_to_frame)(gsr_video_encoder *encoder, AVFrame *frame, gsr_color_conversion *color_conversion); /* Can be NULL */ /* |textures| should be able to fit 2 elements */ void (*get_textures)(gsr_video_encoder *encoder, unsigned int *textures, int *num_textures, gsr_destination_color *destination_color); - void (*destroy)(gsr_video_encoder *encoder, AVCodecContext *video_codec_context); void *priv; bool started; }; +/* Set |replay_buffer_time_seconds| and |fps| to 0 to disable replay buffer */ bool gsr_video_encoder_start(gsr_video_encoder *encoder, AVCodecContext *video_codec_context, AVFrame *frame); +void gsr_video_encoder_destroy(gsr_video_encoder *encoder, AVCodecContext *video_codec_context); void gsr_video_encoder_copy_textures_to_frame(gsr_video_encoder *encoder, AVFrame *frame, gsr_color_conversion *color_conversion); void gsr_video_encoder_get_textures(gsr_video_encoder *encoder, unsigned int *textures, int *num_textures, gsr_destination_color *destination_color); -void gsr_video_encoder_destroy(gsr_video_encoder *encoder, AVCodecContext *video_codec_context); #endif /* GSR_ENCODER_VIDEO_H */ diff --git a/include/replay_buffer.h b/include/replay_buffer.h new file mode 100644 index 0000000..e99b844 --- /dev/null +++ b/include/replay_buffer.h @@ -0,0 +1,41 @@ +#ifndef GSR_REPLAY_BUFFER_H +#define GSR_REPLAY_BUFFER_H + +#include <pthread.h> +#include <stdbool.h> +#include <libavcodec/packet.h> + +typedef struct { + AVPacket packet; + int ref_counter; + double timestamp; +} gsr_av_packet; + +gsr_av_packet* gsr_av_packet_create(const AVPacket *av_packet, double timestamp); +gsr_av_packet* gsr_av_packet_ref(gsr_av_packet *self); +void gsr_av_packet_unref(gsr_av_packet *self); + +typedef struct { + gsr_av_packet **packets; + size_t capacity_num_packets; + size_t num_packets; + size_t index; + pthread_mutex_t mutex; + bool mutex_initialized; + bool owns_mutex; +} gsr_replay_buffer; + +bool gsr_replay_buffer_init(gsr_replay_buffer *self, size_t replay_buffer_num_packets); +void gsr_replay_buffer_deinit(gsr_replay_buffer *self); + +bool gsr_replay_buffer_append(gsr_replay_buffer *self, const AVPacket *av_packet, double timestamp); +void gsr_replay_buffer_clear(gsr_replay_buffer *self); +gsr_av_packet* gsr_replay_buffer_get_packet_at_index(gsr_replay_buffer *self, size_t index); +/* The clone has to be deinitialized before the replay buffer it clones */ +bool gsr_replay_buffer_clone(const gsr_replay_buffer *self, gsr_replay_buffer *destination); +/* Returns 0 if replay buffer is empty */ +size_t gsr_replay_buffer_find_packet_index_by_time_passed(gsr_replay_buffer *self, int seconds); +/* Returns -1 if not found */ +size_t gsr_replay_buffer_find_keyframe(gsr_replay_buffer *self, size_t start_index, int stream_index, bool invert_stream_index); + +#endif /* GSR_REPLAY_BUFFER_H */
\ No newline at end of file |