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

[FEAT] Port all the common_* files, teletext and networking portions of libccx to rust #1495

Closed
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ src/rust/CMakeCache.txt
src/rust/Makefile
src/rust/cmake_install.cmake
src/rust/target/
src/rust/lib_ccxr/target/
windows/ccx_rust.lib
windows/*/debug/*
windows/*/CACHEDIR.TAG
windows/.rustc_info.json
windows/.rustc_info.json
48 changes: 48 additions & 0 deletions src/lib_ccx/ccx_common_timing.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ int gop_rollover = 0;

struct ccx_common_timing_settings_t ccx_common_timing_settings;

#ifndef DISABLE_RUST
void ccxr_add_current_pts(struct ccx_common_timing_ctx *ctx, LLONG pts);
void ccxr_set_current_pts(struct ccx_common_timing_ctx *ctx, LLONG pts);
int ccxr_set_fts(struct ccx_common_timing_ctx *ctx);
LLONG ccxr_get_fts(struct ccx_common_timing_ctx *ctx, int current_field);
LLONG ccxr_get_fts_max(struct ccx_common_timing_ctx *ctx);
char *ccxr_print_mstime_static(LLONG mstime, char *buf);
void ccxr_print_debug_timing(struct ccx_common_timing_ctx *ctx);
void ccxr_calculate_ms_gop_time(struct gop_time_code *g);
int ccxr_gop_accepted(struct gop_time_code *g);
#endif

void ccx_common_timing_init(LLONG *file_position, int no_sync)
{
ccx_common_timing_settings.disable_sync_check = 0;
Expand Down Expand Up @@ -73,11 +85,19 @@ struct ccx_common_timing_ctx *init_timing_ctx(struct ccx_common_timing_settings_

void add_current_pts(struct ccx_common_timing_ctx *ctx, LLONG pts)
{
#ifndef DISABLE_RUST
return ccxr_add_current_pts(ctx, pts);
#endif

set_current_pts(ctx, ctx->current_pts + pts);
}

void set_current_pts(struct ccx_common_timing_ctx *ctx, LLONG pts)
{
#ifndef DISABLE_RUST
return ccxr_set_current_pts(ctx, pts);
#endif

LLONG prev_pts = ctx->current_pts;
ctx->current_pts = pts;
if (ctx->pts_set == 0)
Expand All @@ -95,6 +115,10 @@ void set_current_pts(struct ccx_common_timing_ctx *ctx, LLONG pts)

int set_fts(struct ccx_common_timing_ctx *ctx)
{
#ifndef DISABLE_RUST
return ccxr_set_fts(ctx);
#endif

int pts_jump = 0;

// ES don't have PTS unless GOP timing is used
Expand Down Expand Up @@ -266,6 +290,10 @@ int set_fts(struct ccx_common_timing_ctx *ctx)

LLONG get_fts(struct ccx_common_timing_ctx *ctx, int current_field)
{
#ifndef DISABLE_RUST
return ccxr_get_fts(ctx, current_field);
#endif

LLONG fts;

switch (current_field)
Expand All @@ -290,6 +318,10 @@ LLONG get_fts(struct ccx_common_timing_ctx *ctx, int current_field)

LLONG get_fts_max(struct ccx_common_timing_ctx *ctx)
{
#ifndef DISABLE_RUST
return ccxr_get_fts_max(ctx);
#endif

// This returns the maximum FTS that belonged to a frame. Caption block
// counters are not applicable.
return ctx->fts_max + ctx->fts_global;
Expand Down Expand Up @@ -322,13 +354,21 @@ size_t print_mstime_buff(LLONG mstime, char *fmt, char *buf)
char *print_mstime_static(LLONG mstime)
{
static char buf[15]; // 14 should be long enough
#ifndef DISABLE_RUST
return ccxr_print_mstime_static(mstime, buf);
#endif

print_mstime_buff(mstime, "%02u:%02u:%02u:%03u", buf);
return buf;
}

/* Helper function for to display debug timing info. */
void print_debug_timing(struct ccx_common_timing_ctx *ctx)
{
#ifndef DISABLE_RUST
return ccxr_print_debug_timing(ctx);
#endif

// Avoid wrong "Calc. difference" and "Asynchronous by" numbers
// for uninitialized min_pts
LLONG tempmin_pts = (ctx->min_pts == 0x01FFFFFFFFLL ? ctx->sync_pts : ctx->min_pts);
Expand All @@ -355,6 +395,10 @@ void print_debug_timing(struct ccx_common_timing_ctx *ctx)

void calculate_ms_gop_time(struct gop_time_code *g)
{
#ifndef DISABLE_RUST
return ccxr_calculate_ms_gop_time(g);
#endif

int seconds = (g->time_code_hours * 3600) + (g->time_code_minutes * 60) + g->time_code_seconds;
g->ms = (LLONG)(1000 * (seconds + g->time_code_pictures / current_fps));
if (gop_rollover)
Expand All @@ -363,6 +407,10 @@ void calculate_ms_gop_time(struct gop_time_code *g)

int gop_accepted(struct gop_time_code *g)
{
#ifndef DISABLE_RUST
return ccxr_gop_accepted(g);
#endif

if (!((g->time_code_hours <= 23) && (g->time_code_minutes <= 59) && (g->time_code_seconds <= 59) && (g->time_code_pictures <= 59)))
return 0;

Expand Down
8 changes: 8 additions & 0 deletions src/lib_ccx/lib_ccx.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include "ccx_decoders_708.h"
#include "ccx_decoders_isdb.h"

#ifndef DISABLE_RUST
extern void ccxr_init_basic_logger();
#endif

struct ccx_common_logging_t ccx_common_logging;
static struct ccx_decoders_common_settings_t *init_decoder_setting(
struct ccx_s_options *opt)
Expand Down Expand Up @@ -100,6 +104,10 @@ struct lib_ccx_ctx *init_libraries(struct ccx_s_options *opt)
ccx_common_logging.log_ftn = &mprint;
ccx_common_logging.gui_ftn = &activity_library_process;

#ifndef DISABLE_RUST
ccxr_init_basic_logger();
#endif

struct lib_ccx_ctx *ctx = malloc(sizeof(struct lib_ccx_ctx));
if (!ctx)
ccx_common_logging.fatal_ftn(EXIT_NOT_ENOUGH_MEMORY, "init_libraries: Not enough memory allocating lib_ccx_ctx context.");
Expand Down
54 changes: 54 additions & 0 deletions src/lib_ccx/networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@
#define NO_RESPONCE_INTERVAL 20
#define PING_INTERVAL 3

#ifndef DISABLE_RUST
extern void ccxr_connect_to_srv(const char *addr, const char *port, const char *cc_desc, const char *pwd);
extern void ccxr_net_send_header(const unsigned char *data, size_t len);
extern int ccxr_net_send_cc(const unsigned char *data, int length, void *private_data, struct cc_subtitle *sub);
extern void ccxr_net_check_conn();
extern void ccxr_net_send_epg(
const char *start,
const char *stop,
const char *title,
const char *desc,
const char *lang,
const char *category);
extern int ccxr_net_tcp_read(int socket, void *buffer, size_t length);
extern int ccxr_net_udp_read(int socket, void *buffer, size_t length, const char *src_str, const char *addr_str);
extern int ccxr_start_tcp_srv(const char *port, const char *pwd);
extern int ccxr_start_udp_srv(const char *src, const char *addr, unsigned port);
#endif

int srv_sd = -1; /* Server socket descriptor */

const char *srv_addr;
Expand Down Expand Up @@ -84,6 +102,10 @@ int set_nonblocking(int fd);

void connect_to_srv(const char *addr, const char *port, const char *cc_desc, const char *pwd)
{
#ifndef DISABLE_RUST
return ccxr_connect_to_srv(addr, port, cc_desc, pwd);
#endif

if (NULL == addr)
{
mprint("Server address is not set\n");
Expand Down Expand Up @@ -115,6 +137,10 @@ void connect_to_srv(const char *addr, const char *port, const char *cc_desc, con

void net_send_header(const unsigned char *data, size_t len)
{
#ifndef DISABLE_RUST
return ccxr_net_send_header(data, len);
#endif

assert(srv_sd > 0);

#if DEBUG_OUT
Expand All @@ -141,6 +167,10 @@ void net_send_header(const unsigned char *data, size_t len)

int net_send_cc(const unsigned char *data, int len, void *private_data, struct cc_subtitle *sub)
{
#ifndef DISABLE_RUST
return ccxr_net_send_cc(data, len, private_data, sub);
#endif

assert(srv_sd > 0);

#if DEBUG_OUT
Expand All @@ -160,6 +190,10 @@ int net_send_cc(const unsigned char *data, int len, void *private_data, struct c

void net_check_conn()
{
#ifndef DISABLE_RUST
return ccxr_net_check_conn();
#endif

time_t now;
static time_t last_ping = 0;
char c = 0;
Expand Down Expand Up @@ -221,6 +255,10 @@ void net_send_epg(
const char *lang,
const char *category)
{
#ifndef DISABLE_RUST
return ccxr_net_send_epg(start, stop, title, desc, lang, category);
#endif

size_t st;
size_t sp;
size_t t;
Expand Down Expand Up @@ -301,6 +339,10 @@ void net_send_epg(

int net_tcp_read(int socket, void *buffer, size_t length)
{
#ifndef DISABLE_RUST
return ccxr_net_tcp_read(socket, buffer, length);
#endif

assert(buffer != NULL);
assert(length > 0);

Expand Down Expand Up @@ -333,6 +375,10 @@ int net_tcp_read(int socket, void *buffer, size_t length)

int net_udp_read(int socket, void *buffer, size_t length, const char *src_str, const char *addr_str)
{
#ifndef DISABLE_RUST
return ccxr_net_udp_read(socket, buffer, length, src_str, addr_str);
#endif

assert(buffer != NULL);
assert(length > 0);

Expand Down Expand Up @@ -519,6 +565,10 @@ int tcp_connect(const char *host, const char *port)

int start_tcp_srv(const char *port, const char *pwd)
{
#ifndef DISABLE_RUST
return ccxr_start_tcp_srv(port, pwd);
#endif

if (NULL == port)
port = DFT_PORT;

Expand Down Expand Up @@ -974,6 +1024,10 @@ ssize_t read_byte(int fd, char *ch)

int start_upd_srv(const char *src_str, const char *addr_str, unsigned port)
{
#ifndef DISABLE_RUST
return ccxr_start_udp_srv(src_str, addr_str, port);
#endif

init_sockets();

in_addr_t src;
Expand Down
48 changes: 48 additions & 0 deletions src/lib_ccx/utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
int temp_debug = 0; // This is a convenience variable used to enable/disable debug on variable conditions. Find references to understand.
volatile sig_atomic_t change_filename_requested = 0;

#ifndef DISABLE_RUST
extern int ccxr_verify_crc32(uint8_t *buf, int len);
extern void ccxr_timestamp_to_srttime(uint64_t timestamp, char *buffer);
extern void ccxr_timestamp_to_vtttime(uint64_t timestamp, char *buffer);
extern void ccxr_millis_to_date(uint64_t timestamp, char *buffer, enum ccx_output_date_format date_format, char millis_separator);
extern int ccxr_stringztoms(const char *s, struct ccx_boundary_time *bt);
extern int ccxr_levenshtein_dist(const uint64_t *s1, const uint64_t *s2, unsigned s1len, unsigned s2len);
extern int ccxr_levenshtein_dist_char(const char *s1, const char *s2, unsigned s1len, unsigned s2len);
#endif

static uint32_t crc32_table[] = {
0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9,
0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
Expand Down Expand Up @@ -77,6 +87,10 @@ static uint32_t crc32_table[] = {

int verify_crc32(uint8_t *buf, int len)
{
#ifndef DISABLE_RUST
return ccxr_verify_crc32(buf, len);
#endif

int i = 0;
int32_t crc = -1;
for (i = 0; i < len; i++)
Expand All @@ -86,6 +100,10 @@ int verify_crc32(uint8_t *buf, int len)

int stringztoms(const char *s, struct ccx_boundary_time *bt)
{
#ifndef DISABLE_RUST
return ccxr_stringztoms(s, bt);
#endif

unsigned ss = 0, mm = 0, hh = 0;
int value = -1;
int colons = 0;
Expand Down Expand Up @@ -130,6 +148,10 @@ int stringztoms(const char *s, struct ccx_boundary_time *bt)
}
void timestamp_to_srttime(uint64_t timestamp, char *buffer)
{
#ifndef DISABLE_RUST
return ccxr_timestamp_to_srttime(timestamp, buffer);
#endif

uint64_t p = timestamp;
uint8_t h = (uint8_t)(p / 3600000);
uint8_t m = (uint8_t)(p / 60000 - 60 * h);
Expand All @@ -139,6 +161,10 @@ void timestamp_to_srttime(uint64_t timestamp, char *buffer)
}
void timestamp_to_vtttime(uint64_t timestamp, char *buffer)
{
#ifndef DISABLE_RUST
return ccxr_timestamp_to_vtttime(timestamp, buffer);
#endif

uint64_t p = timestamp;
uint8_t h = (uint8_t)(p / 3600000);
uint8_t m = (uint8_t)(p / 60000 - 60 * h);
Expand All @@ -151,6 +177,10 @@ void timestamp_to_vtttime(uint64_t timestamp, char *buffer)

int levenshtein_dist(const uint64_t *s1, const uint64_t *s2, unsigned s1len, unsigned s2len)
{
#ifndef DISABLE_RUST
return ccxr_levenshtein_dist(s1, s2, s1len, s2len);
#endif

unsigned int x, y, v, lastdiag, olddiag;
unsigned int *column = (unsigned *)malloc((s1len + 1) * sizeof(unsigned int));
for (y = 1; y <= s1len; y++)
Expand All @@ -172,6 +202,10 @@ int levenshtein_dist(const uint64_t *s1, const uint64_t *s2, unsigned s1len, uns

int levenshtein_dist_char(const char *s1, const char *s2, unsigned s1len, unsigned s2len)
{
#ifndef DISABLE_RUST
return ccxr_levenshtein_dist_char(s1, s2, s1len, s2len);
#endif

unsigned int x, y, v, lastdiag, olddiag;
unsigned int *column = (unsigned *)malloc((s1len + 1) * sizeof(unsigned int));
for (y = 1; y <= s1len; y++)
Expand All @@ -193,6 +227,20 @@ int levenshtein_dist_char(const char *s1, const char *s2, unsigned s1len, unsign

void millis_to_date(uint64_t timestamp, char *buffer, enum ccx_output_date_format date_format, char millis_separator)
{
#ifndef DISABLE_RUST
switch (date_format)
{
case ODF_NONE:
case ODF_HHMMSS:
case ODF_HHMMSSMS:
case ODF_SECONDS:
case ODF_DATE:
return ccxr_millis_to_date(timestamp, buffer, date_format, millis_separator);
default:
fatal(CCX_COMMON_EXIT_BUG_BUG, "Invalid value for date_format in millis_to_date()\n");
}
#endif

time_t secs;
unsigned int millis;
char c_temp[80];
Expand Down
Loading
Loading