initial commit

This commit is contained in:
klein panic
2025-04-13 02:27:22 -04:00
commit cf50b5a377
18 changed files with 1920 additions and 0 deletions

37
include/audio.h Normal file
View File

@@ -0,0 +1,37 @@
#ifndef AUDIO_H
#define AUDIO_H
#include <alsa/asoundlib.h>
#include <stdint.h>
typedef struct {
snd_pcm_t *pcm_handle;
int is_recording;
int sample_rate;
int channels;
int capture_audio; // New flag for dynamic audio recording toggle (1 = enabled, 0 = disabled)
} AudioContext;
/* Initialize and configure ALSA capture */
AudioContext* audio_init();
/* Start audio capture */
int audio_start(AudioContext* ctx);
/* Stop audio capture */
int audio_stop(AudioContext* ctx);
/* Cleanup audio capture resources */
void audio_cleanup(AudioContext* ctx);
/* Capture audio into the provided buffer.
Returns the number of frames captured. */
int audio_capture(AudioContext* ctx, uint8_t *buffer, int buffer_size);
/* Set dynamic audio capture toggle.
If enabled is 1, audio will be captured; if 0, audio capture is skipped.
*/
void audio_set_capture(AudioContext* ctx, int enabled);
#endif // AUDIO_H

15
include/config.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef CONFIG_H
#define CONFIG_H
/* Define CSS colors and style values */
#define WINDOW_BG_COLOR "#735290"
#define BUTTON_BG_COLOR "#D0C5FC"
#define BUTTON_TEXT_COLOR "#FFFFFF"
#define BUTTON_BORDER_RADIUS "5px"
#define BUTTON_PADDING "5px"
#define LABEL_TEXT_COLOR "#FFFFFF"
#define COMBO_BG_COLOR "#A28CC6"
#define COMBO_TEXT_COLOR "#FFFFFF"
#endif /* CONFIG_H */

16
include/debug.h Normal file
View File

@@ -0,0 +1,16 @@
#ifndef DEBUG_H
#define DEBUG_H
#include <stdio.h>
extern int g_debug; // Global debug flag
#define DEBUG_LOG(fmt, ...) \
do { \
if (g_debug) { \
fprintf(stderr, "[DEBUG] " fmt "\n", ##__VA_ARGS__); \
} \
} while (0)
#endif // DEBUG_H

67
include/encoder.h Normal file
View File

@@ -0,0 +1,67 @@
#ifndef ENCODER_H
#define ENCODER_H
#include <stdint.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>
// Default Audio Bitrate for AAC/Opus (lossy codecs)
#define DEFAULT_AUDIO_BIT_RATE 64000
/* Video quality enumeration */
typedef enum {
QUALITY_LOW,
QUALITY_MEDIUM,
QUALITY_HIGH
} Quality;
/* Audio codec enumeration */
typedef enum {
AUDIO_CODEC_AAC,
AUDIO_CODEC_PCM,
AUDIO_CODEC_OPUS
} AudioCodec;
typedef struct {
AVFormatContext *fmt_ctx;
AVCodecContext *video_enc_ctx;
AVStream *video_stream;
AVCodecContext *audio_enc_ctx;
AVStream *audio_stream;
struct SwsContext *sws_ctx;
struct SwrContext *swr_ctx; // audio resampling context
int frame_index;
int64_t audio_pts; // running PTS (in samples) for audio
Quality quality;
char filename[512]; // The output filename
} EncoderContext;
/*
* Initializes the encoder.
* 'width' and 'height' are the recording dimensions (which may differ from native screen size).
* 'fps' is the capture framerate, and 'sample_rate' and 'channels' are audio parameters.
* 'audio_codec' selects the audio codec: AAC (lossy), PCM (lossless), or Opus (modern lossy).
* 'audio_bitrate' specifies the desired audio bitrate (e.g., DEFAULT_AUDIO_BIT_RATE for AAC/Opus).
* The output file is initially created in ~/Videos/Screenrecords/ with a generated name.
*/
EncoderContext* encoder_init(Quality quality, int width, int height, int fps, int sample_rate, int channels, AudioCodec audio_codec, int audio_bitrate);
/* Encode one video frame (input data in RGB24 format) */
int encoder_encode_video_frame(EncoderContext* ctx, uint8_t* data);
/* Encode one audio frame with PCM data.
The input data is expected to be S16 interleaved.
Internally, the data is converted to the encoders sample format.
*/
int encoder_encode_audio_frame(EncoderContext* ctx, uint8_t* data, int size);
/* Finalize the output file */
int encoder_finalize(EncoderContext* ctx);
/* Cleanup the encoder resources */
void encoder_cleanup(EncoderContext* ctx);
#endif // ENCODER_H

79
include/gui.h Normal file
View File

@@ -0,0 +1,79 @@
#ifndef GUI_H
#define GUI_H
#include <gtk/gtk.h>
#include "encoder.h" /* For Quality and AudioCodec */
/* Available recording sources */
typedef enum {
RECORD_SOURCE_ALL, /* record union of all monitors */
RECORD_SOURCE_WINDOW, /* Record a specific window */
RECORD_SOURCE_MONITOR /* Record a single monitor */
} RecordSource;
/* GUIComponents structure, extended with additional controls */
typedef struct {
GtkWidget *window;
GtkWidget *record_toggle; /* Button to start/stop recording */
GtkWidget *camera_toggle; /* Button to toggle webcam preview */
GtkWidget *audio_toggle; /* New: Toggle button for audio recording */
GtkWidget *source_combo; /* Combo box: "All", "Window", plus individual monitor names */
GtkWidget *quality_combo; /* Combo box: "Low", "Medium", "High" */
GtkWidget *resolution_combo; /* Combo box: "Full", "1080p", "720p", "480p" */
GtkWidget *audio_codec_combo; /* New: Combo box for Audio Codec (AAC, PCM, Opus) */
GtkWidget *fps_selector; /* New: Selector for FPS (e.g., SpinButton) */
GtkWidget *webcam_resolution_combo; /* New: Combo for webcam resolution (Default, 640x480) */
GtkWidget *info_label; /* Displays recording info */
GtkWidget *preview_area; /* Webcam preview area */
} GUIComponents;
/* Initialize the GUI and return main components */
GUIComponents* gui_init();
/* Update the info label with a string */
void gui_update_info(GUIComponents* gui, const char* info);
/* Update the webcam preview area with a GdkPixbuf */
void gui_update_preview(GUIComponents* gui, GdkPixbuf* pixbuf);
/* Free GUI resources */
void gui_cleanup(GUIComponents* gui);
/* Get the currently selected recording source.
Returns:
- RECORD_SOURCE_WINDOW if the selected entry equals "Window"
- RECORD_SOURCE_ALL if the selected entry equals "All"
- RECORD_SOURCE_MONITOR otherwise (i.e. if it matches a monitor name)
*/
RecordSource gui_get_record_source(GUIComponents* gui);
/* Get the quality setting (Low/Medium/High) */
Quality gui_get_quality(GUIComponents* gui);
/* Get the resolution selection string (e.g., "Full", "1080p", etc.) */
const char* gui_get_resolution(GUIComponents* gui);
/* Get the selected monitor name from the source combo.
Returns NULL if the user selected "All" or "Window".
*/
const char* gui_get_monitor_name(GUIComponents* gui);
/* Populate the source combo with available monitors in addition to "All" and "Window". */
void gui_populate_source_combo(GUIComponents *gui);
/* Get the selected audio codec from the GUI.
Returns one of AUDIO_CODEC_AAC, AUDIO_CODEC_PCM, AUDIO_CODEC_OPUS.
*/
AudioCodec gui_get_audio_codec(GUIComponents* gui);
/* Get the selected FPS value */
int gui_get_fps(GUIComponents* gui);
/* Get the selected webcam resolution option (e.g., "Default" or "640x480") */
const char* gui_get_webcam_resolution(GUIComponents* gui);
int get_monitor_geometry(const char *monitor_name, int *x, int *y, int *width, int *height);
#endif // GUI_H

57
include/recorder.h Normal file
View File

@@ -0,0 +1,57 @@
#ifndef RECORDER_H
#define RECORDER_H
#include <X11/Xlib.h>
#include <stdint.h>
#include <X11/extensions/XShm.h>
/* Recorder context now supports both full-screen and window-based capture */
typedef struct {
Display *display;
Window root;
Window target; // Target window for capture (if any)
int screen;
int x, y; // Capture region origin
int width;
int height;
int is_capturing;
int is_window_capture; // Flag: if 1, capture only the target window
int use_shm; // Flag: 1 if XShm is used
XShmSegmentInfo shm_info; // For XShm
} RecorderContext;
/*
* Initializes recorder. If target is 0, captures the full screen;
* otherwise, uses the specified target windows geometry.
*/
RecorderContext* recorder_init(Window target);
/*
* Grabs the pointer and lets the user click on a window to capture.
* This function blocks until a window is selected.
* On return, *target, *x, *y, *width, *height are filled out.
*/
void recorder_select_window(Display *display, Window *target, int *x, int *y, int *width, int *height);
/* Begin capturing (sets a flag) */
int recorder_start(RecorderContext* ctx);
/* Stop capturing */
int recorder_stop(RecorderContext* ctx);
/* Free allocated recorder context */
void recorder_cleanup(RecorderContext* ctx);
/* Capture one frame from the screen or target window.
Returns a buffer in RGB24 format (caller must free).
'linesize' returns the number of bytes per row.
*/
uint8_t* recorder_capture_frame(RecorderContext* ctx, int *linesize);
/* Update window geometry dynamically for window capture.
Re-fetches attributes of the target window.
*/
int recorder_update_window_geometry(RecorderContext *ctx);
#endif // RECORDER_H

7
include/version.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef VERSION_H
#define VERSION_H
#define APP_VERSION "0.0.1"
#endif // VERSION_H