00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "standard.h"
00018 #include "al.h"
00019 #include "config.h"
00020 #include "ansi.h"
00021 #include "input.h"
00022 #include "video_codec.h"
00023 #include "channel.h"
00024 #include "mux.h"
00025 #include "demux.h"
00026 #include "audio.h"
00027 #include "vid_wrapper.h"
00028 #include "bch.h"
00029
00030 extern _session session;
00031 extern int debug;
00032
00033 #define STRLEN 256
00034 #define DEF_CONFIG_FILE "sim.cfg"
00035
00036 #define DEF_NUM_CHANNELS 3
00037
00038 static void usage(void)
00039 {
00040 printf("\n");
00041 printf("Usage: sim [-d debug_level] [-c config_file]\n");
00042 printf(" sim -h\n");
00043 printf("\n");
00044 printf("-c config_file = set config file [%s]\n", DEF_CONFIG_FILE);
00045 printf("-d debug_level = set debug_level\n");
00046 printf("-h = print this message\n");
00047 printf("\n");
00048 }
00049
00050
00051 static void add_alse(mux *m, al_sending_entity *alse)
00052 {
00053 m->al_senders[m->num_al_senders++] = alse;
00054 }
00055
00056 static void add_alre(demux *d, al_receiving_entity *alre)
00057 {
00058 d->al_receivers[d->num_al_receivers++] = alre;
00059 }
00060
00061
00062 control *new_control(void)
00063 {
00064 control *bob = malloc(sizeof(control));
00065 if (bob != NULL) {
00066
00067 }
00068 return(bob);
00069 }
00070
00071
00072 static void parse_line(char *s, char *name, char *arg)
00073 {
00074 int i,n,a;
00075
00076 i=n=a=0;
00077
00078
00079 while (s[i] && (s[i]==' ' || s[i]=='\t'))
00080 i++;
00081
00082 while (s[i] && !(s[i]==' ' || s[i]=='\t'))
00083 name[n++]=s[i++];
00084 name[n]=0;
00085
00086 while (s[i] && (s[i]==' ' || s[i]=='\t'))
00087 i++;
00088
00089 while (s[i])
00090 arg[a++]=s[i++];
00091 arg[a]=0;
00092 }
00093
00094
00095 void video_config(video_codec *video)
00096 {
00097 char str[STRLEN];
00098 char name[STRLEN];
00099 char arg[STRLEN];
00100 int done=0;
00101 FILE *fp;
00102
00103 while (!done && !eof_input_file()) {
00104
00105 get_str(str);
00106
00107 parse_line(str, name, arg);
00108
00109 if (equals(name, "end")) {
00110 done = 1;
00111 }
00112 else if (equals(name, "inputfile")) {
00113 strcpy(video->input_file, arg);
00114 }
00115 else if (equals(name, "no_coding")) {
00116 video->no_coding = 1;
00117 }
00118 else if (equals(name, "outputfile")) {
00119 strcpy(video->output_file, arg);
00120
00121
00122 fp = fopen(video->output_file,"w");
00123 if (fp==NULL) error("video_config","Can't open output file");
00124 fclose(fp);
00125 }
00126 else if (equals(name, "streamfile")) {
00127 strcpy(video->streamname, arg);
00128 }
00129 else if (equals(name, "bitrate")) {
00130 video->targetrate = atoi(arg);
00131 }
00132
00133
00134
00135 else if (equals(name, "sequenced")) {
00136 video->sequenced=1;
00137 }
00138 else if (equals(name, "retransmit")) {
00139 video->retransmit=1;
00140 }
00141 else if (equals(name, "tmndecode")) {
00142 video->tmndecode=1;
00143 }
00144 }
00145 }
00146
00147
00148 void audio_config(audio_codec *A)
00149 {
00150 char str[STRLEN];
00151 char name[STRLEN];
00152 char arg[STRLEN];
00153 int done=0;
00154 FILE *fp;
00155
00156 while (!done && !eof_input_file()) {
00157
00158 get_str(str);
00159
00160 parse_line(str, name, arg);
00161
00162 if (equals(name, "end")) {
00163 done = 1;
00164 }
00165 else if (equals(name, "inputfile")) {
00166 strcpy(A->input_filename, arg);
00167 }
00168 else if (equals(name, "outputfile")) {
00169 strcpy(A->output_filename, arg);
00170
00171
00172 fp = fopen(A->output_filename,"w");
00173 if (fp==NULL) error("audio_config","Can't open output file");
00174 fclose(fp);
00175 }
00176 }
00177 }
00178
00179
00180 void control_config(control *C)
00181 {
00182 char str[STRLEN];
00183 char name[STRLEN];
00184 char arg[STRLEN];
00185 int done=0;
00186
00187 while (!done && !eof_input_file()) {
00188
00189 get_str(str);
00190
00191 parse_line(str, name, arg);
00192
00193 if (equals(name, "end")) {
00194 done = 1;
00195 }
00196 else if (equals(name, "")) {
00197
00198 }
00199 }
00200 }
00201
00202
00203 void mux_table_config(mux *M)
00204 {
00205 char str[STRLEN];
00206 char name[STRLEN];
00207 char arg[STRLEN];
00208 int done=0;
00209
00210 while (!done && !eof_input_file()) {
00211 get_str(str);
00212 parse_line(str, name, arg);
00213 if (equals(name, "end")) {
00214 done = 1;
00215 }
00216 else if (equals(name, "entry")) {
00217 if (M->params->mux_table_size < MAX_MUX_TABLE_SIZE)
00218 add_to_mux_table(M->params->mux_table,
00219 &(M->params->mux_table_size), arg);
00220 }
00221 }
00222 }
00223
00224
00225 void mux_config(mux *M)
00226 {
00227 char str[STRLEN];
00228 char name[STRLEN];
00229 char arg[STRLEN];
00230 int done=0;
00231
00232 FILE *fp;
00233 while (!done && !eof_input_file()) {
00234 get_str(str);
00235 parse_line(str, name, arg);
00236
00237 if (equals(name, "end")) {
00238 done = 1;
00239 }
00240 else if (equals(name, "table")) {
00241 mux_table_config(M);
00242 }
00243 else if (equals(name, "sync_flag_length")) {
00244 if (equals(arg,"16d"))
00245 M->params->sync_flag_length = 32;
00246 else
00247 M->params->sync_flag_length = atoi(arg);
00248 }
00249 else if (equals(name, "sync_threshold")) {
00250 M->params->sync_threshold = atoi(arg);
00251 }
00252 else if (equals(name, "header_code")) {
00253 M->params->header_code = strtobch_type(arg);
00254 }
00255 else if (equals(name, "level2")) {
00256 M->params->use_mpl = 1;
00257 M->params->level = 2;
00258
00259 M->params->header_code = GOLAY23_12;
00260 M->params->use_mpl = 0;
00261 }
00262 else if (equals(name, "level1")) {
00263 M->params->use_mpl = 0;
00264 M->params->level = 1;
00265 }
00266 else if (equals(name, "level0")) {
00267 M->params->use_mpl = 0;
00268 M->params->level = 1;
00269 }
00270 else if (equals(name, "mpl_code")) {
00271 M->params->mpl_code = strtobch_type(arg);
00272 }
00273 else if (equals(name, "payload_min")) {
00274 M->params->payload_min = atoi(arg);
00275 }
00276 else if (equals(name, "payload_max")) {
00277 M->params->payload_max = atoi(arg);
00278 }
00279 else if (equals(name, "stat_file")) {
00280 strcpy(M->params->stat_file,arg);
00281 fp = fopen(M->params->stat_file,"w");
00282 assert(fp!=NULL);
00283 fclose(fp);
00284 }
00285 else if (equals(name, "mpl_t")) {
00286 M->params->mpl_t = atoi(arg);
00287 }
00288 else if (equals(name, "head_t")) {
00289 M->params->head_t = atoi(arg);
00290 }
00291 else if (equals(name, "mpl_sync_threshold")) {
00292 M->params->mpl_sync_threshold = atoi(arg);
00293 }
00294 }
00295 }
00296
00297
00298 void channel_config(channel *C)
00299 {
00300 char str[STRLEN];
00301 char name[STRLEN];
00302 char arg[STRLEN];
00303 int done=0;
00304
00305 while (!done && !eof_input_file()) {
00306 get_str(str);
00307 parse_line(str, name, arg);
00308 if (equals(name, "end")) {
00309 done = 1;
00310 }
00311 else if (equals(name, "errortype")) {
00312 if (equals(arg, "none"))
00313 C->error=NoErrors;
00314 else if (equals(arg, "random"))
00315 C->error=Random;
00316 else if (equals(arg, "file"))
00317 C->error=File;
00318 }
00319 else if (equals(name, "errorrate")) {
00320 C->bit_error_rate = atof(arg);
00321 }
00322 else if (equals(name, "errorfile")) {
00323 strcpy(C->filename, arg);
00324 }
00325 else if (equals(name, "speed")) {
00326 C->transmission_speed = atoi(arg);
00327 }
00328 else if (equals(name, "delay")) {
00329 C->transmission_delay = atoi(arg);
00330 }
00331 else if (equals(name, "error_offset")) {
00332 C->error_offset = atoi(arg);
00333 }
00334 }
00335 }
00336
00337
00338 void side_config(_side *side)
00339 {
00340 char str[STRLEN];
00341 char name[STRLEN];
00342 char arg[STRLEN];
00343
00344 int done=0;
00345
00346 while (!done && !eof_input_file()) {
00347
00348 get_str(str);
00349 parse_line(str, name, arg);
00350
00351 if (equals(name, "end")) {
00352 done = 1;
00353 }
00354 else if (equals(name, "video")) {
00355 if (debug)
00356 printf("\t# entering %s section\n", name);
00357 video_config(side->Video);
00358 }
00359 else if (equals(name, "audio")) {
00360 if (debug)
00361 printf("\t# entering %s section\n", name);
00362 audio_config(side->Audio);
00363 }
00364 else if (equals(name, "control")) {
00365 if (debug)
00366 printf("\t# entering %s section\n", name);
00367 control_config(side->Control);
00368 }
00369 else if (equals(name, "mux")) {
00370 if (debug)
00371 printf("\t# entering %s section\n", name);
00372 mux_config(side->Mux);
00373 }
00374 else if (equals(name, "channel")) {
00375 if (debug)
00376 printf("\t# entering %s section\n", name);
00377 channel_config(side->Channel);
00378 }
00379 }
00380 }
00381
00382
00383 void get_config()
00384 {
00385 char str[STRLEN];
00386 char name[STRLEN];
00387 char arg[STRLEN];
00388 void *data;
00389
00390 while (!eof_input_file()) {
00391
00392 get_str(str);
00393
00394 parse_line(str, name, arg);
00395
00396 if (equals(name, "local")) {
00397 if (debug) {
00398 printf("# %s side config \n", name);
00399 }
00400 side_config(&session.local);
00401 }
00402 else if (equals(name, "remote")) {
00403 if (debug) {
00404 printf("# %s side config\n", name);
00405 }
00406 side_config(&session.remote);
00407 }
00408 }
00409 }
00410
00411
00412 void start_config()
00413 {
00414
00415
00416
00417 session.local.Video = new_video_codec("Local Video");
00418 session.local.Audio = new_audio_codec("Local Audio");
00419 session.local.Control = new_control();
00420 session.local.Mux = new_mux("Local Mux");
00421 session.local.Mux->params = new_mux_parameters("local -> remote");
00422 session.local.Demux = new_demux("Local Demux");
00423 session.local.Channel = new_channel("Local -> Remote channel");
00424
00425 session.local.ciControl.type = AL1;
00426 session.local.ciAudio.type = AL2;
00427 session.local.ciVideo.type = AL3;
00428
00429
00430
00431
00432 session.remote.Video = new_video_codec("Remote Video");
00433 session.remote.Audio = new_audio_codec("Remote Audio");
00434 session.remote.Control = new_control();
00435 session.remote.Mux = new_mux("Remote Mux");
00436 session.remote.Mux->params = new_mux_parameters("remote -> local");
00437 session.remote.Demux = new_demux("Remote Demux");
00438 session.remote.Channel = new_channel("Remote -> Local channel");
00439
00440 session.remote.ciControl.type = AL1;
00441 session.remote.ciAudio.type = AL2;
00442 session.remote.ciVideo.type = AL3;
00443
00444
00445
00446
00447
00448 session.local.Demux->params = session.remote.Mux->params;
00449 session.remote.Demux->params = session.local.Mux->params;
00450
00451
00452 session.local.Mux->output = session.local.Channel;
00453 session.local.Demux->input = session.remote.Channel;
00454 session.remote.Mux->output = session.remote.Channel;
00455 session.remote.Demux->input = session.local.Channel;
00456
00457 }
00458
00459 void finish_config()
00460
00461
00462
00463
00464 {
00465 al_sending_entity *alse;
00466 al_receiving_entity *alre;
00467
00468
00469
00470 alre = new_al_receiving_entity(&session.local.ciControl);
00471 alse = new_al_sending_entity(&session.local.ciControl);
00472 alre->back_channel = alse;
00473 session.local.Control->output = alse;
00474 session.local.Control->input = alre;
00475 add_alse(session.local.Mux, alse);
00476 add_alre(session.local.Demux, alre);
00477
00478 alre = new_al_receiving_entity(&session.local.ciAudio);
00479 alse = new_al_sending_entity(&session.local.ciAudio);
00480 alre->back_channel = alse;
00481 session.local.Audio->output = alse;
00482 session.local.Audio->input = alre;
00483 add_alse(session.local.Mux, alse);
00484 add_alre(session.local.Demux, alre);
00485
00486 alre = new_al_receiving_entity(&session.local.ciVideo);
00487 alse = new_al_sending_entity(&session.local.ciVideo);
00488 alre->back_channel = alse;
00489 session.local.Video->output = alse;
00490 session.local.Video->input = alre;
00491 add_alse(session.local.Mux, alse);
00492 add_alre(session.local.Demux, alre);
00493
00494
00495
00496 alre = new_al_receiving_entity(&session.remote.ciControl);
00497 alse = new_al_sending_entity(&session.remote.ciControl);
00498 alre->back_channel = alse;
00499 session.remote.Control->output = alse;
00500 session.remote.Control->input = alre;
00501 add_alse(session.remote.Mux, alse);
00502 add_alre(session.remote.Demux, alre);
00503
00504 alre = new_al_receiving_entity(&session.remote.ciAudio);
00505 alse = new_al_sending_entity(&session.remote.ciAudio);
00506 alre->back_channel = alse;
00507 session.remote.Audio->output = alse;
00508 session.remote.Audio->input = alre;
00509 add_alse(session.remote.Mux, alse);
00510 add_alre(session.remote.Demux, alre);
00511
00512 alre = new_al_receiving_entity(&session.remote.ciVideo);
00513 alse = new_al_sending_entity(&session.remote.ciVideo);
00514 alre->back_channel = alse;
00515 session.remote.Video->output = alse;
00516 session.remote.Video->input = alre;
00517 add_alse(session.remote.Mux, alse);
00518 add_alre(session.remote.Demux, alre);
00519
00520
00521 set_frames(session.local.Video);
00522 set_frames(session.remote.Video);
00523 }
00524
00525
00526 void set_config(int argc, char *argv[])
00527
00528
00529
00530
00531 {
00532 int i;
00533 char config_file[STRLEN];
00534
00535 strcpy(config_file, DEF_CONFIG_FILE);
00536
00537
00538
00539
00540
00541 if (argc > 1 && prefix("-h", argv[1])) {
00542 usage();
00543 exit(0);
00544 }
00545
00546 if (argc > 1 && strcmp(argv[1], "-advice")==0) {
00547 printf("\aDon't Panic!\n");
00548 exit(42);
00549 }
00550
00551
00552
00553
00554
00555 for (i = 1; i < argc; i++) {
00556 if (argv[i][0]=='-') {
00557 if (strcmp(argv[i], "-c")==0) {
00558 i++;
00559 if (i < argc) {
00560 config_file[STRLEN-1]='\0';
00561 strncpy(config_file, argv[i], STRLEN-1);
00562 }
00563 }
00564 else if (equals(argv[i], "-d")) {
00565 if (i < (argc - 1)) {
00566 if (argv[i+1][0]!='-') {
00567 i++;
00568 debug = atoi(argv[i]);
00569 }
00570 }
00571 else {
00572 debug = 0;
00573 }
00574 }
00575 }
00576 else {
00577 fprintf(stderr, "Invalid command line option: %s\n", argv[i]);
00578 exit(1);
00579 }
00580 }
00581
00582
00583
00584
00585
00586 open_input_file(config_file);
00587
00588 start_config();
00589 get_config();
00590 finish_config();
00591
00592 close_input_file();
00593 }
00594
00595
00596