Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix png fonts breaking recording #146

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Makefile.dji
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ goggle_ipk: install
cp -r ipk/goggle/data ipk/goggle/build/
echo "2.0" > ipk/goggle/build/debian-binary
cp -r ipk/goggle/control ipk/goggle/build/
cd ipk/goggle/build/control && tar czvf ../control.tar.gz .
cd ipk/goggle/build/data && tar czvf ../data.tar.gz .
cd ipk/goggle/build && tar czvf "../../${IPK_NAME}" ./control.tar.gz ./data.tar.gz ./debian-binary
cd ipk/goggle/build/control && gtar czvf ../control.tar.gz .
cd ipk/goggle/build/data && gtar czvf ../data.tar.gz .
cd ipk/goggle/build && gtar czvf "../../${IPK_NAME}" ./control.tar.gz ./data.tar.gz ./debian-binary

airunit_ipk: install
$(eval PKG_NAME := $(shell cat ./ipk/airunit/control/control | grep Package | cut -d" " -f2))
Expand Down
2 changes: 1 addition & 1 deletion ipk/goggle/control/control
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: msp-osd
Version: 0.12.1
Version: 0.12.2
Maintainer: bri3d
Description: MSP OSD service for the DJI HD FPV goggles.
Architecture: pigeon-glasses
Expand Down
17 changes: 9 additions & 8 deletions jni/font/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
// #include <ctype.h>

#include "../libspng/spng.h"
#include "font.h"
Expand All @@ -14,7 +15,7 @@

/* Font helper methods */

void get_font_path_with_extension(char *font_path_dest, const char *font_path, const char *extension, uint8_t len, uint8_t is_hd, char *font_variant)
void get_font_path_with_extension(char *font_path_dest, const char *font_path, const char *extension, uint8_t len, uint8_t is_hd, const char *font_variant)
{
char name_buf[len];
char res_buf[len];
Expand All @@ -37,7 +38,7 @@ void get_font_path_with_extension(char *font_path_dest, const char *font_path, c
DEBUG_PRINT("Font path: %s\n", font_path_dest);
}

static int open_font(const char *filename, display_info_t *display_info, char *font_variant)
static int open_font(const char *filename, display_info_t *display_info, const char *font_variant)
{
char file_path[255];
int is_hd = (display_info->font_width == HD_FONT_WIDTH) ? 1 : 0;
Expand Down Expand Up @@ -135,19 +136,19 @@ static int open_font(const char *filename, display_info_t *display_info, char *f
return -1;
}

void load_font(display_info_t *display_info, char *font_variant) {
void load_font(display_info_t *display_info, const char *font_variant) {

// Note: load_font will not replace an existing font.
if(display_info->fonts[0] == NULL) {
int loaded_font = 0;
DEBUG_PRINT("IN LOAD_FONT\n");
// create a copy of font_variant
char font_variant_lower[5] = "";
if (font_variant != NULL)
if (font_variant != NULL)
{
DEBUG_PRINT("Lowercasing variant\n");
int length = sizeof(font_variant) / sizeof(char);
for (int i = 0; i < length; i++)
size_t length = strlen(font_variant);
for (size_t i = 0; i < length && i < 4; i++) // Ensure not to exceed array bounds
{
font_variant_lower[i] = tolower(font_variant[i]);
}
Expand All @@ -160,12 +161,12 @@ void load_font(display_info_t *display_info, char *font_variant) {
DEBUG_PRINT("Loading font %s\n", font_variant_lower);

char *fallback_font_variant = "";
if (font_variant_lower!=NULL && strcmp(font_variant_lower, "btfl") == 0)
if (strcmp(font_variant_lower, "btfl") == 0)
{
DEBUG_PRINT("Setting fallback font variant to bf\n");
fallback_font_variant = "bf";
}
else if (font_variant_lower != NULL && strcmp(font_variant_lower, "ultr") == 0)
else if (strcmp(font_variant_lower, "ultr") == 0)
{
DEBUG_PRINT("Setting fallback font variant to ultra\n");
fallback_font_variant = "ultra";
Expand Down
6 changes: 4 additions & 2 deletions jni/font/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#define ENTWARE_FONT_PATH "/opt/fonts/font"
#define SDCARD_FONT_PATH "/storage/sdcard0/font"

#define FALLBACK_FONT NULL

typedef enum
{
FONT_VARIANT_GENERIC,
Expand All @@ -19,6 +21,6 @@ typedef enum
FONT_VARIANT_COUNT
} font_variant_e;

void load_font(display_info_t *display_info, char *font_variant);
void load_font(display_info_t *display_info, const char *font_variant);
void close_font(display_info_t *display_info);
void get_font_path_with_extension(char *font_path_dest, const char *font_path, const char *extension, uint8_t len, uint8_t is_hd, char *font_variant);
void get_font_path_with_extension(char *font_path_dest, const char *font_path, const char *extension, uint8_t len, uint8_t is_hd, const char *font_variant);
12 changes: 6 additions & 6 deletions jni/osd_dji_overlay_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ static void msp_callback(msp_msg_t *msp_message)
displayport_process_message(display_driver, msp_message);
}

static void load_fonts(font_variant_e font_variant) {
static void load_fonts(char* font_variant) {
char file_path[255];
get_font_path_with_extension(file_path, "font", ".png", 255, 0, font_variant);
toast(file_path);
Expand Down Expand Up @@ -346,7 +346,7 @@ static void process_data_packet(uint8_t *buf, int len, dji_shm_state_t *radio_sh
close_all_fonts();
load_fonts(current_fc_variant);
// This is not a typo - fill in any missing fonts for the current variant with the generic one.
load_fonts(FONT_VARIANT_GENERIC);
load_fonts(FALLBACK_FONT);
}
}
}
Expand Down Expand Up @@ -400,9 +400,9 @@ static void rec_msp_draw_complete_hook()
.font_width = current_display_info->font_width,
.font_height = current_display_info->font_height,
.x_offset = current_display_info->x_offset,
.y_offset = current_display_info->y_offset,
.font_variant = current_fc_variant,
.y_offset = current_display_info->y_offset
};
strcpy(config.font_variant, current_fc_variant);

rec_start(&config);
}
Expand Down Expand Up @@ -480,7 +480,7 @@ static void rec_pb_timeout_hook()
DEBUG_PRINT("msp_osd: playback config, x_offset: %d\n", osd_display_info->x_offset);
DEBUG_PRINT("msp_osd: playback config, y_offset: %d\n", osd_display_info->y_offset);

DEBUG_PRINT("msp_osd: gls playing dvr, loading font variant %d\n", rec_config->font_variant);
DEBUG_PRINT("msp_osd: gls playing dvr, loading font variant %s\n", rec_config->font_variant);
uint8_t is_hd = osd_display_info->font_width != sd_display_info.font_width;
load_font(osd_display_info, rec_config->font_variant);

Expand Down Expand Up @@ -577,7 +577,7 @@ void osd_directfb(duss_disp_instance_handle_t *disp, duss_hal_obj_handle_t ion_h
memset(&last_render, 0, sizeof(last_render));
memset(&now, 0, sizeof(now));

load_fonts(FONT_VARIANT_GENERIC);
load_fonts(FALLBACK_FONT);
open_dji_radio_shm(&radio_shm);
start_display(is_v2_goggles, disp, ion_handle);

Expand Down
9 changes: 9 additions & 0 deletions jni/rec/rec.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ void rec_start(rec_config_t *config)
sizeof(rec_file_name));
DEBUG_PRINT("rec_file_name: %s", rec_file_name);

DEBUG_PRINT("Config:\n");
DEBUG_PRINT(" Char Width: %u\n", config->char_width);
DEBUG_PRINT(" Char Height: %u\n", config->char_height);
DEBUG_PRINT(" Font Width: %u\n", config->font_width);
DEBUG_PRINT(" Font Height: %u\n", config->font_height);
DEBUG_PRINT(" X Offset: %u\n", config->x_offset);
DEBUG_PRINT(" Y Offset: %u\n", config->y_offset);
DEBUG_PRINT(" Font Variant: %.5s\n", config->font_variant);

rec_fd = fopen(rec_file_name, "wb");
if (rec_fd == NULL)
{
Expand Down
22 changes: 20 additions & 2 deletions jni/rec/rec.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stdint.h>

#define REC_MAGIC "MSPOSD"
#define REC_VERSION 1
#define REC_VERSION 2

typedef struct rec_config_t
{
Expand All @@ -14,7 +14,7 @@ typedef struct rec_config_t
uint8_t font_height;
uint16_t x_offset;
uint16_t y_offset;
uint8_t font_variant;
char font_variant[5];
} __attribute__((packed)) rec_config_t;

typedef struct rec_file_header_t
Expand All @@ -24,6 +24,24 @@ typedef struct rec_file_header_t
rec_config_t config;
} __attribute__((packed)) rec_file_header_t;

typedef struct rec_config_v1_t
{
uint8_t char_width;
uint8_t char_height;
uint8_t font_width;
uint8_t font_height;
uint16_t x_offset;
uint16_t y_offset;
uint8_t font_variant;
} __attribute__((packed)) rec_config_v1_t;

typedef struct rec_file_header_v1_t
{
char magic[7];
uint16_t version;
rec_config_v1_t config;
} __attribute__((packed)) rec_file_header_v1_t;

typedef struct rec_frame_header_t
{
uint32_t frame_idx;
Expand Down
60 changes: 53 additions & 7 deletions jni/rec/rec_pb.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "rec_pb.h"

#include "../font/font.h"

#define REC_PB_CONFIG_ENABLED_KEY "rec_pb_enabled"

#define MAX_X 60
Expand All @@ -34,6 +36,7 @@ static bool rec_pb_enabled = false;
static FILE *osd_fd = NULL;
static rec_config_t osd_config = {0};

static uint32_t header_size = 0;
static int64_t frame_counter = 0;

static uint32_t *frame_idxs;
Expand Down Expand Up @@ -90,22 +93,64 @@ int rec_pb_start()
return 1;
}

if (file_header.version != REC_VERSION)
if (file_header.version == REC_VERSION)
{
DEBUG_PRINT("header ok!");
memcpy(&osd_config, &file_header.config, sizeof(rec_config_t));
header_size = sizeof(rec_file_header_t);
}
else if (file_header.version == 1)
{
DEBUG_PRINT("header is v1");
rec_file_header_v1_t file_header_v1;
fseek(osd_fd, 0, SEEK_SET);

fread(&file_header_v1, sizeof(rec_file_header_v1_t), 1, osd_fd);
if (strncmp(file_header_v1.magic, REC_MAGIC, sizeof(REC_MAGIC)) != 0)
{
DEBUG_PRINT("invalid osd file");
fclose(osd_fd);
osd_fd = NULL;
return 1;
}

switch (file_header_v1.config.font_variant)
{
case FONT_VARIANT_BETAFLIGHT:
strcpy(file_header.config.font_variant, "BTFL");
break;
case FONT_VARIANT_INAV:
strcpy(file_header.config.font_variant, "INAV");
break;
case FONT_VARIANT_ARDUPILOT:
strcpy(file_header.config.font_variant, "ARDU");
break;
case FONT_VARIANT_KISS_ULTRA:
strcpy(file_header.config.font_variant, "ULTR");
break;
case FONT_VARIANT_QUICKSILVER:
strcpy(file_header.config.font_variant, "QUIC");
break;
default:
file_header.config.font_variant[0] = '\0'; // Empty string
}

memcpy(&osd_config, &file_header.config, sizeof(rec_config_t));
header_size = sizeof(rec_file_header_v1_t);
}
else
{
DEBUG_PRINT("invalid osd file version! expected: %d, got: %d", REC_VERSION, file_header.version);
fclose(osd_fd);
osd_fd = NULL;
return 1;
}

DEBUG_PRINT("header ok!");
memcpy(&osd_config, &file_header.config, sizeof(rec_config_t));

DEBUG_PRINT("loading frame indexes");

fseek(osd_fd, 0, SEEK_END);
uint32_t file_size = ftell(osd_fd);
fseek(osd_fd, sizeof(rec_file_header_t), SEEK_SET);
fseek(osd_fd, header_size, SEEK_SET);
DEBUG_PRINT("file size: %d", file_size);

frame_idx_len = file_size / FRAME_SIZE;
Expand All @@ -121,7 +166,7 @@ int rec_pb_start()
fseek(osd_fd, sizeof(uint16_t) * MAX_T, SEEK_CUR);
}

fseek(osd_fd, sizeof(rec_file_header_t), SEEK_SET);
fseek(osd_fd, header_size, SEEK_SET);

current_frame_idx = 0;
frame_counter = rec_pb_cp_vdec->frames_sent;
Expand Down Expand Up @@ -179,7 +224,7 @@ int rec_pb_do_next_frame(uint16_t *map_out)

fseek(
osd_fd,
sizeof(rec_file_header_t) + (closest_frame_idx * FRAME_SIZE) + sizeof(rec_frame_header_t),
header_size + (closest_frame_idx * FRAME_SIZE) + sizeof(rec_frame_header_t),
SEEK_SET);
fread(map_out, sizeof(uint16_t), MAX_T, osd_fd);

Expand All @@ -203,3 +248,4 @@ bool rec_pb_is_ready()
{
return rec_pb_cp_vdec != NULL && rec_pb_vdec_local_player != NULL;
}