Skip to content

Commit

Permalink
Merge pull request #34 from lars18th/in-dev3
Browse files Browse the repository at this point in the history
Add selection of the output filename
  • Loading branch information
gfto authored Mar 13, 2024
2 parents 634791e + 7f38487 commit 6a59e47
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
29 changes: 22 additions & 7 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ void config_free(CONFIG **pconf) {
FREE(conf->epg_conf);
FREE(conf->network_name);
FREE(conf->provider_name);
FREE(conf->output_filename);
FREE(*pconf);
}
}
Expand Down Expand Up @@ -444,7 +445,8 @@ static void show_usage(void) {
puts("\t-q\t\tQuiet");
puts("\t-D\t\tDebug");
puts("");
puts("\t-W\t\tWrite output file");
puts("\t-W\t\tWrite output file (recommended to use with -N)");
puts("\t-f\t\tThe output filename (default: mptsd-output.ts)");
puts("\t-E\t\tWrite input file");
puts("");
}
Expand All @@ -462,7 +464,7 @@ void config_load(CONFIG *conf, int argc, char **argv) {
conf->server_socket = -1;
conf->write_output_network = 1;

while ((j = getopt(argc, argv, "i:b:p:g:c:n:e:d:t:o:O:P:l:L:B:m:s:qDHhEWN")) != -1) {
while ((j = getopt(argc, argv, "i:b:p:g:c:n:e:d:t:o:O:P:l:L:B:m:s:f:qDHhEWN")) != -1) {
switch (j) {
case 'i':
conf->ident = strdup(optarg);
Expand Down Expand Up @@ -545,11 +547,13 @@ void config_load(CONFIG *conf, int argc, char **argv) {
break;
case 'W':
conf->write_output_file = 1;
output_open_file(conf->output);
break;
case 'E':
conf->write_input_file = 1;
break;
case 'f':
conf->output_filename = strdup(optarg);
break;
case 'D':
conf->debug = 1;
break;
Expand All @@ -564,7 +568,7 @@ void config_load(CONFIG *conf, int argc, char **argv) {
}
}
if (conf->write_output_network && !conf->output->out_host.s_addr) {
fprintf(stderr, "ERROR: Output address is not set (use -O x.x.x.x)\n");
fprintf(stderr, "ERROR: Output address is not set (use -O x.x.x.x)\n\n");
show_usage();
goto ERR;
}
Expand All @@ -585,13 +589,21 @@ void config_load(CONFIG *conf, int argc, char **argv) {
if (!conf->epg_conf) {
conf->epg_conf = strdup("mptsd_epg.conf");
}
if (!conf->output_filename) {
conf->output_filename = strdup("mptsd-output.ts");
}

// Align bitrate to 1 packet (1316 bytes)
conf->output_bitrate *= 1000000; // In bytes
conf->output_packets_per_sec = ceil(conf->output_bitrate / (1316 * 8));
conf->output_bitrate = conf->output_packets_per_sec * (1316 * 8);
conf->output_tmout = 1000000 / conf->output_packets_per_sec;

// Open the filename if we want to write to a file
if(conf->write_output_file) {
output_open_file(conf->output_filename, conf->output);
}

if (conf->server_port)
init_server_socket(conf->server_addr, conf->server_port, &conf->server, &conf->server_socket);

Expand All @@ -601,7 +613,10 @@ void config_load(CONFIG *conf, int argc, char **argv) {
printf("\tGlobal config : %s\n", conf->global_conf);
printf("\tChannels config : %s\n", conf->channels_conf);
printf("\tNIT config : %s\n", conf->nit_conf);
printf("\tOutput addr : %s://%s:%d\n", (conf->output->rtp_ssrc != 0 ? "rtp" : "udp"), inet_ntoa(conf->output->out_host), conf->output->out_port);
if (conf->write_output_network)
printf("\tOutput addr : %s://%s:%d\n", (conf->output->rtp_ssrc != 0 ? "rtp" : "udp"), inet_ntoa(conf->output->out_host), conf->output->out_port);
else
printf("\tOutput addr : disabled\n");
if (conf->output_intf.s_addr)
printf("\tOutput iface addr : %s\n", inet_ntoa(conf->output_intf));
printf("\tMulticast ttl : %d\n", conf->multicast_ttl);
Expand All @@ -610,7 +625,7 @@ void config_load(CONFIG *conf, int argc, char **argv) {
printf("\tSyslog host : %s\n", conf->loghost);
printf("\tSyslog port : %d\n", conf->logport);
} else {
printf("\tSyslog disabled.\n");
printf("\tSyslog : disabled\n");
}
printf("\tOutput bitrate : %.0f bps, %.2f Kbps, %.2f Mbps\n", conf->output_bitrate, conf->output_bitrate / 1000, conf->output_bitrate / 1000000);
printf("\tOutput pkt tmout : %ld us\n", conf->output_tmout);
Expand All @@ -622,7 +637,7 @@ void config_load(CONFIG *conf, int argc, char **argv) {
conf->pcr_mode == 3 ? "Move PCRs and rewrite them" : "???"
);
if (conf->write_output_file)
printf("\tWrite output file\n");
printf("\tWrite output file : %s\n", conf->output_filename);
if (conf->write_input_file)
printf("\tWrite input file(s)\n");
}
Expand Down
4 changes: 3 additions & 1 deletion config.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ typedef struct {

int write_input_file;
int write_output_network;
int write_output_file;
int write_output_file; // 0 - do not write an output file
// 1 - write output files
char *output_filename; // filename to write the output
int pcr_mode; // 0 - do not touch PCRs
// 1 - move PCRs to their calculated place
// 2 - rewrite PCRs using output bitrate
Expand Down
9 changes: 7 additions & 2 deletions data.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,13 @@ OUTPUT *output_new() {
return o;
}

void output_open_file(OUTPUT *o) {
o->ofd = open("mptsd-output.ts", O_CREAT | O_WRONLY | O_TRUNC, 0644);
void output_open_file(char *filename, OUTPUT *o) {
int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660);
if (fd == -1) {
perror("Cannot open output file\n");
exit(1);
}
o->ofd = fd;
}

void obuf_reset(OBUF *ob) {
Expand Down
2 changes: 1 addition & 1 deletion data.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void input_stream_reset (INPUT *input);

OUTPUT * output_new ();
void output_free (OUTPUT **output);
void output_open_file (OUTPUT *o);
void output_open_file (char *filename, OUTPUT *o);
void output_buffer_alloc (OUTPUT *o, double output_bitrate);
void obuf_reset (OBUF *ob);

Expand Down
12 changes: 8 additions & 4 deletions output_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,20 @@ void ts_frame_process(CONFIG *conf, OUTPUT *o, uint8_t *data) {

// write plain UDP packets
ssize_t ts_frame_write(OUTPUT *o, uint8_t *data) {
ssize_t written;
ssize_t written, written2;

written = fdwrite(o->out_sock, (char *)data, FRAME_PACKET_SIZE);
if (o->ofd)
written2 = write(o->ofd, data, FRAME_PACKET_SIZE);
else written2 = 0;

if (written2 > written)
written = written2;
if (written >= 0) {
o->traffic += written;
o->traffic_period += written;
}

if (o->ofd)
write(o->ofd, data, FRAME_PACKET_SIZE);

return written;
}

Expand Down

0 comments on commit 6a59e47

Please sign in to comment.