1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
14 Audio flow has two ways:
16 * from channel to the upper layer
17 -> sound from mISDN channel
18 -> announcement from vbox channel
20 * from the upper layer to the channel
21 -> sound from remote channel
25 -> if local or remote channel is not mISDN
26 -> if call is recorded (vbox)
32 -> audio from upper layer is buffered for later transmission to channel
34 -> buffered audio from upper layer or tones are transmitted via system clock
36 -> rx-data from port to record() and upper layer
37 -> tx-data from port (dsp) to record()
39 -> streaming announcement to upper layer
40 -> recording announcement
41 * VboxPort::message_epoint
42 -> recording audio message from upper layer
50 /* enable to test conference mixing, even if only two members are bridged */
51 //#define TEST_CONFERENCE 1
53 #define SHORT_MIN -32768
54 #define SHORT_MAX 32767
56 class Port *port_first = NULL;
58 unsigned int port_serial = 1; /* must be 1, because 0== no port */
60 struct port_bridge *p_bridge_first;
62 static void remove_bridge(struct port_bridge *bridge, class Port *port);
64 /* free epointlist relation
66 void Port::free_epointlist(struct epoint_list *epointlist)
68 struct epoint_list *temp, **tempp;
71 tempp = &p_epointlist;
73 if (temp == epointlist)
80 PERROR("SOFTWARE ERROR: epointlist not in port's list.\n");
87 PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
88 FREE(temp, sizeof(struct epoint_list));
93 void Port::free_epointid(unsigned int epoint_id)
95 struct epoint_list *temp, **tempp;
98 tempp = &p_epointlist;
100 if (temp->epoint_id == epoint_id)
107 PERROR("epoint_id not in port's list.\n");
114 PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
115 FREE(temp, sizeof(struct epoint_list));
120 /* create new epointlist relation
122 struct epoint_list *Port::epointlist_new(unsigned int epoint_id)
124 struct epoint_list *epointlist, **epointlistpointer;
126 /* epointlist structure */
127 epointlist = (struct epoint_list *)MALLOC(sizeof(struct epoint_list));
129 FATAL("No memory for epointlist\n");
131 PDEBUG(DEBUG_EPOINT, "PORT(%d) allocating epoint_list.\n", p_serial);
133 /* add epoint_list to chain */
134 epointlist->next = NULL;
135 epointlistpointer = &p_epointlist;
136 while(*epointlistpointer)
137 epointlistpointer = &((*epointlistpointer)->next);
138 *epointlistpointer = epointlist;
141 epointlist->epoint_id = epoint_id;
142 epointlist->active = 1;
151 Port::Port(int type, const char *portname, struct port_settings *settings, struct interface *interface)
153 class Port *temp, **tempp;
155 /* initialize object */
157 memcpy(&p_settings, settings, sizeof(struct port_settings));
159 memset(&p_settings, 0, sizeof(p_settings));
161 SCPY(p_name, portname);
163 SCPY(p_interface_name, interface->name);
164 p_tone_dir[0] = '\0';
166 p_serial = port_serial++;
168 p_tone_fetched = NULL;
169 p_tone_name[0] = '\0';
170 p_state = PORT_STATE_IDLE;
172 memset(&p_callerinfo, 0, sizeof(p_callerinfo));
173 memset(&p_dialinginfo, 0, sizeof(p_dialinginfo));
174 memset(&p_connectinfo, 0, sizeof(p_connectinfo));
175 memset(&p_redirinfo, 0, sizeof(p_redirinfo));
176 memset(&p_capainfo, 0, sizeof(p_capainfo));
186 p_record_filename[0] = '\0';
187 p_record_buffer_readp = 0;
188 p_record_buffer_writep = 0;
189 p_record_buffer_dir = 0;
196 /* append port to chain */
208 PDEBUG(DEBUG_PORT, "new port (%d) of type 0x%x, name '%s' interface '%s'\n", p_serial, type, portname, p_interface_name);
217 class Port *temp, **tempp;
218 struct lcr_msg *message;
220 PDEBUG(DEBUG_PORT, "removing port (%d) of type 0x%x, name '%s' interface '%s'\n", p_serial, p_type, p_name, p_interface_name);
224 vootp_destroy(p_vootp);
230 PDEBUG(DEBUG_PORT, "Removing us from bridge %u\n", p_bridge->bridge_id);
231 remove_bridge(p_bridge, this);
239 /* disconnect port from endpoint */
240 while(p_epointlist) {
241 /* send disconnect */
242 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
243 message->param.disconnectinfo.cause = 16;
244 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
245 message_put(message);
246 /* remove endpoint */
247 free_epointlist(p_epointlist);
250 /* remove port from chain */
260 FATAL("PORT(%s) port not in port's list.\n", p_name);
264 /* close open tones file */
265 if (p_tone_fh >= 0) {
270 p_tone_fetched = NULL;
275 /* set new endpoint state
277 void Port::new_state(int state)
279 PDEBUG(DEBUG_PORT, "PORT(%s) new state %s --> %s\n", p_name, state_name[p_state], state_name[state]);
285 * find the port with port_id
287 class Port *find_port_id(unsigned int port_id)
289 class Port *port = port_first;
292 //printf("comparing: '%s' with '%s'\n", name, port->name);
293 if (port->p_serial == port_id)
305 void Port::set_echotest(int echotest)
307 p_echotest = echotest;
312 * set the file in the tone directory with the given name
314 void Port::set_tone(const char *dir, const char *name)
323 dir = options.tones_dir; /* just in case we have no PmISDN instance */
325 /* no counter, no eof, normal speed */
329 p_tone_codec = CODEC_LAW;
331 if (p_tone_fh >= 0) {
336 p_tone_fetched = NULL;
339 if (name[0] == '/') {
340 SPRINT(p_tone_name, "%s", name);
341 p_tone_dir[0] = '\0';
343 SCPY(p_tone_dir, dir);
344 SCPY(p_tone_name, name);
346 /* trigger playback */
349 p_tone_name[0]= '\0';
354 if (!!strncmp(name,"cause_",6))
357 /* now we check if the cause exists, otherwhise we use error tone. */
358 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, 0, 0))) {
359 p_tone_fetched = NULL;
362 SPRINT(filename, "%s_loop", p_tone_name);
363 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, 0, 0))) {
364 p_tone_fetched = NULL;
367 SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
368 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0) {
372 SPRINT(filename, "%s/%s/%s_loop", SHARE_DATA, p_tone_dir, p_tone_name);
373 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0) {
378 if (!strcmp(name,"cause_00") || !strcmp(name,"cause_10")) {
379 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using release tone\n", p_name, name+6);
380 SPRINT(p_tone_name,"release");
382 if (!strcmp(name,"cause_11")) {
383 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using busy tone\n", p_name, name+6);
384 SPRINT(p_tone_name,"busy");
386 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using error tone\n", p_name, name+6);
387 SPRINT(p_tone_name,"error");
392 void Port::set_display(const char *text)
397 * set the file in the tone directory for vbox playback
398 * also set the play_eof-flag
400 void Port::set_vbox_tone(const char *dir, const char *name)
406 p_tone_codec = CODEC_LAW;
409 if (p_tone_fh >= 0) {
414 p_tone_fetched = NULL;
416 SPRINT(p_tone_dir, dir);
417 SPRINT(p_tone_name, name);
418 /* trigger playback */
421 /* now we check if the cause exists, otherwhise we use error tone. */
423 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, &p_tone_size, &p_tone_left))) {
424 PDEBUG(DEBUG_PORT, "PORT(%s) opening fetched tone: %s\n", p_name, p_tone_name);
427 SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
428 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0) {
430 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
434 SPRINT(filename, "%s", p_tone_name);
435 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0) {
437 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
445 * set the file in the given directory for vbox playback
446 * also set the eof-flag
447 * also set the counter-flag
449 void Port::set_vbox_play(const char *name, int offset)
451 struct lcr_msg *message;
453 /* use ser_box_tone() */
454 set_vbox_tone("", name);
462 if (p_tone_name[0]) {
463 /* send message with counter value */
464 if (p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist)) {
465 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
466 message->param.counter.current = offset;
467 message->param.counter.max = p_tone_size;
468 message_put(message);
475 * set the playback speed (for recording playback with different speeds)
477 void Port::set_vbox_speed(int speed)
479 /* enable vbox play mode */
480 p_tone_speed = speed;
484 * read from the given file as specified in port_set_tone and return sample data
485 * if the tone ends, the result may be less samples than requested
487 int Port::read_audio(unsigned char *buffer, int length)
490 int nodata=0; /* to detect 0-length files and avoid endless reopen */
492 int tone_left_before; /* temp variable to determine the change in p_tone_left */
500 /* if there is no tone set, use silence */
504 /* if the file pointer is not open, we open it */
505 if (p_tone_fh<0 && p_tone_fetched==NULL) {
507 SPRINT(filename, "%s", p_tone_name);
508 /* if file does not exist */
509 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left))) {
510 SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
511 /* if file does not exist */
512 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
513 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
519 SPRINT(filename, "%s", p_tone_name);
520 /* if file does not exist */
521 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
522 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
527 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
531 /* file descriptor is open read data */
532 tone_left_before = p_tone_left;
533 if (p_tone_fh >= 0) {
534 l = read_tone(p_tone_fh, buffer, p_tone_codec, len, p_tone_size, &p_tone_left, p_tone_speed);
535 if (l<0 || l>len) /* paranoia */
540 if (p_tone_fetched) {
541 l = read_tone_fetched(&p_tone_fetched, buffer, len, p_tone_size, &p_tone_left, p_tone_speed);
542 if (l<0 || l>len) /* paranoia */
548 /* if counter is enabled, we check if we have a change */
549 if (p_tone_counter && p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist)) {
550 /* if we jumed to the next second */
551 if (((p_tone_size-p_tone_left)/8000) != (p_tone_size-tone_left_before)/8000) {
552 //printf("\nsize=%d left=%d\n\n",p_tone_size,p_tone_left);
553 struct lcr_msg *message;
554 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
555 message->param.counter.current = (p_tone_size-p_tone_left)/8000;
556 message->param.counter.max = -1;
557 message_put(message);
564 if (p_tone_fh >= 0) {
569 p_tone_fetched = NULL;
574 /* if the file has 0-length */
576 PDEBUG(DEBUG_PORT, "PORT(%s) 0-length loop: %s\n", p_name, filename);
582 /* if eof is reached, or if the normal file cannot be opened, continue with the loop file if possible */
584 if (p_tone_eof && ACTIVE_EPOINT(p_epointlist)) {
585 struct lcr_msg *message;
586 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_EOF);
587 message_put(message);
591 /* if file does not exist */
592 SPRINT(filename, "%s_loop", p_tone_name);
593 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left))) {
594 SPRINT(filename, "%s/%s/%s_loop", SHARE_DATA, p_tone_dir, p_tone_name);
595 /* if file does not exist */
596 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
597 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
598 p_tone_dir[0] = '\0';
599 p_tone_name[0] = '\0';
605 SPRINT(filename, "%s_loop", p_tone_name);
606 /* if file does not exist */
607 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
608 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
609 p_tone_dir[0] = '\0';
610 p_tone_name[0] = '\0';
616 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
618 /* now we have opened the loop */
623 /* Endpoint sends messages to the port
624 * This is called by the message_epoint, inherited by child classes.
625 * Therefor a return 1 means: "already handled here"
627 //extern struct lcr_msg *dddebug;
628 int Port::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
630 /* check if we got audio data from one remote port */
632 case MESSAGE_TONE: /* play tone */
633 PDEBUG(DEBUG_PORT, "PORT(%s) setting tone '%s' dir '%s'\n", p_name, param->tone.name, param->tone.dir);
634 set_tone(param->tone.dir,param->tone.name);
637 case MESSAGE_VBOX_TONE: /* play tone of answering machine */
638 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine tone '%s' '%s'\n", p_name, param->tone.dir, param->tone.name);
639 set_vbox_tone(param->tone.dir, param->tone.name);
642 case MESSAGE_VBOX_PLAY: /* play recording of answering machine */
643 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine file to play '%s' (offset %d seconds)\n", p_name, param->play.file, param->play.offset);
644 set_vbox_play(param->play.file, param->play.offset);
647 case MESSAGE_VBOX_PLAY_SPEED: /* set speed of playback (recording of answering machine) */
648 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine playback speed %d (times)\n", p_name, param->speed);
649 set_vbox_speed(param->speed);
652 case MESSAGE_BRIDGE: /* create / join / leave / destroy bridge */
653 PDEBUG(DEBUG_PORT, "PORT(%s) bridging to id %d\n", p_name, param->bridge_id);
654 bridge(param->bridge_id);
658 case MESSAGE_VOOTP: /* enable / disable VoOTP */
659 PDEBUG(DEBUG_PORT, "PORT(%s) VoOTP enabled: %d\n", p_name, param->vootp.enable);
660 set_vootp(¶m->vootp);
669 /* wave header structure */
671 unsigned short stereo; /* 1 = mono, 2 = stereo */
672 unsigned short channels; /* number of channels */
673 unsigned int sample_rate; /* sample rate */
674 unsigned int data_rate; /* data rate */
675 unsigned short bytes_sample; /* bytes per sample (all channels) */
676 unsigned short bits_sample; /* bits per sample (one channel) */
681 * open record file (actually a wave file with empty header which will be
682 * written before close, because we do not know the size yet)
683 * type=1 record annoucement, type=0 record audio stream, type=2 record vbox
685 int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ignore, const char *vbox_email, int vbox_email_file)
687 /* RIFFxxxxWAVEfmt xxxx(fmt-size)dataxxxx... */
688 char dummyheader[8+4+8+sizeof(fmt)+8];
692 int __attribute__((__unused__)) ret;
695 PERROR("Port(%d) not an extension\n", p_serial);
698 SCPY(p_record_extension, extension);
699 p_record_anon_ignore = anon_ignore;
700 SCPY(p_record_vbox_email, vbox_email);
701 p_record_vbox_email_file = vbox_email_file;
704 PERROR("Port(%d) already recording\n", p_serial);
709 SPRINT(filename, "%s/%s/vbox", EXTENSION_DATA, p_record_extension);
711 SPRINT(filename, "%s/%s/recordings", EXTENSION_DATA, p_record_extension);
712 if (mkdir(filename, 0755) < 0) {
713 if (errno != EEXIST) {
714 PERROR("Port(%d) cannot create directory '%s'\n", p_serial, filename);
720 UPRINT(strchr(filename,'\0'), "/announcement");
723 now_tm = localtime(&now);
724 UPRINT(strchr(filename,'\0'), "/%04d-%02d-%02d_%02d%02d%02d", now_tm->tm_year+1900, now_tm->tm_mon+1, now_tm->tm_mday, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
727 p_record_vbox_year = now_tm->tm_year;
728 p_record_vbox_mon = now_tm->tm_mon;
729 p_record_vbox_mday = now_tm->tm_mday;
730 p_record_vbox_hour = now_tm->tm_hour;
731 p_record_vbox_min = now_tm->tm_min;
734 /* check, if file exists (especially when an extension calls the same extension) */
736 if ((p_record = fopen(filename, "r"))) {
738 SCAT(filename, "_2nd");
741 p_record = fopen(filename, "w");
743 PERROR("Port(%d) cannot record because file cannot be opened '%s'\n", p_serial, filename);
749 p_record_type = type;
750 p_record_vbox = vbox;
751 p_record_skip = skip;
753 switch(p_record_type) {
757 ret = fwrite(dummyheader, sizeof(dummyheader), 1, p_record);
763 UCPY(p_record_filename, filename);
765 PDEBUG(DEBUG_PORT, "Port(%d) recording started with file name '%s'\n", p_serial, filename);
771 * close the recoding file, put header in front and rename
773 void Port::close_record(int beep, int mute)
775 static signed short beep_mono[256];
776 unsigned int size = 0, wsize = 0;
778 char filename[512], indexname[512];
781 char number[256], callerid[256];
783 struct caller_info callerinfo;
784 const char *valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_.-!$%&/()=+*;~";
785 int __attribute__((__unused__)) ret;
789 PDEBUG(DEBUG_PORT, "data still in record buffer: %d (dir %d)\n", (p_record_buffer_writep - p_record_buffer_readp) & RECORD_BUFFER_MASK, p_record_buffer_dir);
791 memcpy(&callerinfo, &p_callerinfo, sizeof(struct caller_info));
792 // apply_callerid_restriction(p_record_anon_ignore, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.extension, callerinfo.name);
794 SCPY(number, p_dialinginfo.id);
795 SCPY(callerid, numberrize_callerinfo(callerinfo.id, callerinfo.ntype, options.national, options.international));
796 if (callerid[0] == '\0') {
797 if (callerinfo.present == INFO_PRESENT_RESTRICTED)
798 UCPY(callerid,"anonymous");
800 UCPY(callerid,"unknown");
803 /* change verboten digits */
805 while((p=strchr(p,'*')))
808 while((p=strchr(p,'/')))
811 while((p=strchr(p,'*')))
814 while((p=strchr(p,'/')))
817 ii = strlen(callerid);
819 if (!strchr(valid_chars, callerid[i]))
826 if (!strchr(valid_chars, number[i]))
832 if (mute && p_record_type==CODEC_MONO) {
836 fseek(p_record, -(i<<1), SEEK_END);
837 p_record_length -= (i<<1);
839 /* add beep to the end of recording */
840 if (beep && p_record_type==CODEC_MONO) {
843 beep_mono[i] = (signed short)(sin((double)i / 5.688888888889 * 2.0 * 3.1415927) * 2000.0);
848 ret = fwrite(beep_mono, sizeof(beep_mono), 1, p_record);
849 i += sizeof(beep_mono);
850 p_record_length += sizeof(beep_mono);
854 /* complete header */
855 switch(p_record_type) {
860 fprintf(p_record, "cue %c%c%c%c%c%c%c%c", 4, 0, 0, 0, 0,0,0,0);
863 fprintf(p_record, "LIST%c%c%c%cadtl", 4, 0, 0, 0);
866 fseek(p_record, 0, SEEK_SET);
868 /* WAVEfmt xxxx(fmt-size)dataxxxx[data]cue xxxx0000LISTxxxxadtl*/
869 size = p_record_length;
870 wsize = 4+8+sizeof(fmt)+8+size+8+4+8+4;
873 fprintf(p_record, "RIFF%c%c%c%c", (unsigned char)(wsize&0xff), (unsigned char)((wsize>>8)&0xff), (unsigned char)((wsize>>16)&0xff), (unsigned char)(wsize>>24));
876 fprintf(p_record, "WAVE");
879 fprintf(p_record, "fmt %c%c%c%c", (unsigned int)sizeof(fmt), 0, 0, 0);
880 switch(p_record_type) {
884 fmt.sample_rate = 8000; /* samples/sec */
885 fmt.data_rate = 16000; /* full data rate */
886 fmt.bytes_sample = 2; /* all channels */
887 fmt.bits_sample = 16; /* one channel */
893 fmt.sample_rate = 8000; /* samples/sec */
894 fmt.data_rate = 32000; /* full data rate */
895 fmt.bytes_sample = 4; /* all channels */
896 fmt.bits_sample = 16; /* one channel */
902 fmt.sample_rate = 8000; /* samples/sec */
903 fmt.data_rate = 8000; /* full data rate */
904 fmt.bytes_sample = 1; /* all channels */
905 fmt.bits_sample = 8; /* one channel */
908 ret = fwrite(&fmt, sizeof(fmt), 1, p_record);
911 fprintf(p_record, "data%c%c%c%c", (unsigned char)(size&0xff), (unsigned char)((size>>8)&0xff), (unsigned char)((size>>16)&0xff), (unsigned char)(size>>24));
914 if (p_record_vbox == 1)
915 SPRINT(filename, "%s.wav", p_record_filename);
917 SPRINT(filename, "%s_%s-%s.wav", p_record_filename, callerid, number);
922 if (p_record_vbox == 1)
923 SPRINT(filename, "%s.isdn", p_record_filename);
925 SPRINT(filename, "%s_%s-%s.isdn", p_record_filename, callerid, number);
934 if (rename(p_record_filename, filename) < 0) {
935 PERROR("Port(%d) cannot rename from '%s' to '%s'\n", p_serial, p_record_filename, filename);
939 PDEBUG(DEBUG_PORT, "Port(%d) recording is written and renamed to '%s' and must have the following size:%lu raw:%lu samples:%lu\n", p_serial, filename, wsize+8, size, size>>1);
941 if (p_record_vbox == 2) {
942 SPRINT(indexname, "%s/%s/vbox/index", EXTENSION_DATA, p_record_extension);
943 if ((fp = fopen(indexname,"a"))) {
946 /* remove path from file name */
948 while(strchr(p, '/'))
949 p = strchr(p, '/')+1;
950 fprintf(fp, "%s %d %d %d %d %d %s\n", p, p_record_vbox_year, p_record_vbox_mon, p_record_vbox_mday, p_record_vbox_hour, p_record_vbox_min, callerid);
955 PERROR("Port(%d) cannot open index file '%s' to append.\n", p_serial, indexname);
958 /* send email with sample*/
959 if (p_record_vbox_email[0]) {
960 send_mail(p_record_vbox_email_file?filename:(char *)"", callerid, callerinfo.extension, callerinfo.name, p_record_vbox_email, p_record_vbox_year, p_record_vbox_mon, p_record_vbox_mday, p_record_vbox_hour, p_record_vbox_min, p_record_extension);
968 * Records all data from down and from up into one single stream.
969 * Both streams may have gaps or jitter.
970 * A Jitter buffer for both streams is used to compensate jitter.
972 * If one stream (dir) received packets, they are stored to a
973 * buffer to wait for the other stream (dir), so both streams can
974 * be combined. If the buffer is full, it's content is written
975 * without mixing stream. (assuming only one stram (dir) exists.)
976 * A flag is used to indicate what stream is currently in buffer.
978 * NOTE: First stereo sample (odd) is from down, second is from up.
980 void Port::record(unsigned char *data, int length, int dir_fromup)
982 unsigned char write_buffer[1024], *d;
986 int __attribute__((__unused__)) ret;
989 if (!p_record || !length)
992 /* skip data from local caller (dtmf input) */
993 if (p_record_skip && !dir_fromup) {
994 /* more to skip than we have */
995 if (p_record_skip > length) {
996 p_record_skip -= length;
1000 data += p_record_skip;
1001 length -= p_record_skip;
1005 //printf("dir=%d len=%d\n", dir_fromup, length);
1007 free = ((p_record_buffer_readp - p_record_buffer_writep - 1) & RECORD_BUFFER_MASK);
1009 //PDEBUG(DEBUG_PORT, "record(data,%d,%d): free=%d, p_record_buffer_dir=%d, p_record_buffer_readp=%d, p_record_buffer_writep=%d.\n", length, dir_fromup, free, p_record_buffer_dir, p_record_buffer_readp, p_record_buffer_writep);
1011 /* the buffer stores the same data stream */
1012 if (dir_fromup == p_record_buffer_dir) {
1015 //printf("same free=%d length=%d\n", free, length);
1016 /* first write what we can to the buffer */
1017 while(free && length) {
1018 p_record_buffer[p_record_buffer_writep] = audio_law_to_s32[*data++];
1019 p_record_buffer_writep = (p_record_buffer_writep + 1) & RECORD_BUFFER_MASK;
1023 /* all written, so we return */
1026 /* still data left, buffer is full, so we need to write a chunk to file */
1027 switch(p_record_type) {
1029 s = (signed short *)write_buffer;
1032 *s++ = p_record_buffer[p_record_buffer_readp];
1033 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1036 ret = fwrite(write_buffer, 512, 1, p_record);
1037 p_record_length += 512;
1041 s = (signed short *)write_buffer;
1042 if (p_record_buffer_dir) {
1045 *s++ = 0; /* nothing from down */
1046 *s++ = p_record_buffer[p_record_buffer_readp];
1047 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1053 *s++ = p_record_buffer[p_record_buffer_readp];
1054 *s++ = 0; /* nothing from up */
1055 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1059 ret = fwrite(write_buffer, 1024, 1, p_record);
1060 p_record_length += 1024;
1067 *d++ = ((unsigned short)(p_record_buffer[p_record_buffer_readp]+0x8000)) >> 8;
1068 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1071 ret = fwrite(write_buffer, 512, 1, p_record);
1072 p_record_length += 512;
1079 *d++ = audio_s16_to_law[p_record_buffer[p_record_buffer_readp] & 0xffff];
1080 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1083 ret = fwrite(write_buffer, 256, 1, p_record);
1084 p_record_length += 256;
1087 /* because we still have data, we write again */
1091 /* the buffer stores the other stream */
1094 /* if buffer empty, change it */
1095 if (p_record_buffer_readp == p_record_buffer_writep) {
1096 p_record_buffer_dir = dir_fromup;
1099 /* how much data can we mix ? */
1100 ii = (p_record_buffer_writep - p_record_buffer_readp) & RECORD_BUFFER_MASK;
1106 //printf("same ii=%d length=%d\n", ii, length);
1107 //PDEBUG(DEBUG_PORT, "record(data,%d,%d): free=%d, p_record_buffer_dir=%d, p_record_buffer_readp=%d, p_record_buffer_writep=%d: mixing %d bytes.\n", length, dir_fromup, free, p_record_buffer_dir, p_record_buffer_readp, p_record_buffer_writep, ii);
1109 /* write data mixed with the buffer */
1110 switch(p_record_type) {
1112 s = (signed short *)write_buffer;
1115 sample = p_record_buffer[p_record_buffer_readp]
1116 + audio_law_to_s32[*data++];
1117 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1118 if (sample < SHORT_MIN) sample = SHORT_MIN;
1119 if (sample > SHORT_MAX) sample = SHORT_MAX;
1123 ret = fwrite(write_buffer, ii<<1, 1, p_record);
1124 p_record_length += (ii<<1);
1128 s = (signed short *)write_buffer;
1129 if (p_record_buffer_dir) {
1132 *s++ = audio_law_to_s32[*data++];
1133 *s++ = p_record_buffer[p_record_buffer_readp];
1134 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1140 *s++ = p_record_buffer[p_record_buffer_readp];
1141 *s++ = audio_law_to_s32[*data++];
1142 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1146 ret = fwrite(write_buffer, ii<<2, 1, p_record);
1147 p_record_length += (ii<<2);
1154 sample = p_record_buffer[p_record_buffer_readp]
1155 + audio_law_to_s32[*data++];
1156 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1157 if (sample < SHORT_MIN) sample = SHORT_MIN;
1158 if (sample > SHORT_MAX) sample = SHORT_MAX;
1159 *d++ = (sample+0x8000) >> 8;
1162 ret = fwrite(write_buffer, ii, 1, p_record);
1163 p_record_length += ii;
1170 sample = p_record_buffer[p_record_buffer_readp]
1171 + audio_law_to_s32[*data++];
1172 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1173 if (sample < SHORT_MIN) sample = SHORT_MIN;
1174 if (sample > SHORT_MAX) sample = SHORT_MAX;
1175 *d++ = audio_s16_to_law[sample & 0xffff];
1178 ret = fwrite(write_buffer, ii, 1, p_record);
1179 p_record_length += ii;
1185 goto different_again;
1186 /* no data (maybe buffer) */
1191 void Port::tap(unsigned char *data, int length, int dir_fromup)
1195 void Port::update_rxoff(void)
1199 void Port::update_load(void)
1208 int bridge_timeout(struct lcr_timer *timer, void *instance, int index);
1210 static void remove_bridge(struct port_bridge *bridge, class Port *port)
1212 struct port_bridge **temp = &p_bridge_first;
1214 if (*temp == bridge) {
1215 struct port_bridge_member **memberp = &bridge->first, *member;
1217 /* loop until we are found */
1219 if ((*memberp)->port == port) {
1221 *memberp = member->next;
1222 FREE(member, sizeof(struct port_bridge_member));
1224 #ifndef TEST_CONFERENCE
1225 if (bridge->first && bridge->first->next && !bridge->first->next->next) {
1227 if (bridge->first && !bridge->first->next) {
1229 PDEBUG(DEBUG_PORT, "bridge %u is no conference anymore\n", bridge->bridge_id);
1230 del_timer(&bridge->timer);
1234 memberp = &((*memberp)->next);
1236 /* if bridge is empty, remove it */
1237 if (bridge->first == NULL) {
1238 PDEBUG(DEBUG_PORT, "Remove bridge %u\n", bridge->bridge_id);
1239 *temp = bridge->next;
1240 FREE(bridge, sizeof(struct port_bridge));
1245 temp = &((*temp)->next);
1247 PERROR("Bridge %p not found in list\n", bridge);
1250 void Port::bridge(unsigned int bridge_id)
1252 struct port_bridge_member **memberp;
1254 /* Remove bridge, if we leave bridge or if we join a different bridge. */
1255 if (p_bridge && bridge_id != p_bridge->bridge_id) {
1256 PDEBUG(DEBUG_PORT, "Remove port %u from bridge %u, because out new bridge is %u\n", p_serial, p_bridge->bridge_id, bridge_id);
1257 remove_bridge(p_bridge, this);
1261 /* if we leave bridge */
1267 struct port_bridge *temp = p_bridge_first;
1270 if (temp->bridge_id == bridge_id)
1276 PDEBUG(DEBUG_PORT, "Port %d found existing bridge %u.\n", p_serial, p_bridge->bridge_id);
1281 struct port_bridge **temp = &p_bridge_first;
1283 p_bridge = (struct port_bridge *) MALLOC(sizeof(struct port_bridge));
1285 p_bridge->bridge_id = bridge_id;
1287 /* attach bridge instance to list */
1289 temp = &((*temp)->next);
1291 PDEBUG(DEBUG_PORT, "Port %d creating not existing bridge %u.\n", p_serial, p_bridge->bridge_id);
1294 /* attach to bridge */
1295 memberp = &p_bridge->first;
1297 if ((*memberp)->port == this) {
1298 /* already joined */
1301 memberp = &((*memberp)->next);
1303 *memberp = (struct port_bridge_member *) MALLOC(sizeof(struct port_bridge_member));
1305 (*memberp)->port = this;
1306 /* check if bridge becomes a conference */
1307 #ifndef TEST_CONFERENCE
1308 if (p_bridge->first->next && p_bridge->first->next->next && !p_bridge->first->next->next->next) {
1309 p_bridge->first->next->next->write_p = 0;
1310 p_bridge->first->next->next->min_space = 0;
1311 memset(p_bridge->first->next->next->buffer, silence, sizeof((*memberp)->buffer));
1313 if (p_bridge->first->next && !p_bridge->first->next->next) {
1315 p_bridge->first->next->write_p = 0;
1316 p_bridge->first->next->min_space = 0;
1317 memset(p_bridge->first->next->buffer, silence, sizeof((*memberp)->buffer));
1318 p_bridge->first->write_p = 0;
1319 p_bridge->first->min_space = 0;
1320 memset(p_bridge->first->buffer, silence, sizeof((*memberp)->buffer));
1321 memset(p_bridge->sum_buffer, 0, sizeof(p_bridge->sum_buffer));
1322 p_bridge->read_p = 0;
1323 add_timer(&p_bridge->timer, bridge_timeout, p_bridge, 0);
1324 schedule_timer(&p_bridge->timer, 0, 20000); /* 20 MS */
1325 p_bridge->sample_count = 0;
1326 PDEBUG(DEBUG_PORT, "bridge %u became a conference\n", p_bridge->bridge_id);
1330 /* send data to remote Port or add to sum buffer */
1331 int Port::bridge_tx(unsigned char *data, int len)
1334 struct port_bridge_member *member;
1340 vootp_encrypt_stream(p_vootp, data, len);
1343 /* less than two ports, so drop */
1344 if (!p_bridge || !p_bridge->first || !p_bridge->first->next)
1346 #ifndef TEST_CONFERENCE
1347 /* two ports, so bridge */
1348 if (!p_bridge->first->next->next) {
1349 if (p_bridge->first->port == this)
1350 return p_bridge->first->next->port->bridge_rx(data, len);
1351 if (p_bridge->first->next->port == this)
1352 return p_bridge->first->port->bridge_rx(data, len);
1356 /* more than two ports... */
1357 member = p_bridge->first;
1359 if (member->port == this)
1361 member = member->next;
1365 write_p = member->write_p;
1366 /* calculate space, so write pointer will not overrun (or reach) read pointer in ring buffer */
1367 space = (p_bridge->read_p - write_p - 1) & (BRIDGE_BUFFER - 1);
1368 /* clip len, if it does not fit */
1371 /* apply audio samples to sum buffer */
1372 sum = p_bridge->sum_buffer;
1373 buf = member->buffer;
1375 sum[write_p] += audio_law_to_s32[*data];
1376 buf[write_p] = *data++;
1377 write_p = (write_p + 1) & (BRIDGE_BUFFER - 1);
1379 /* raise write pointer */
1380 member->write_p = write_p;
1385 int bridge_timeout(struct lcr_timer *timer, void *instance, int index)
1387 struct port_bridge *bridge = (struct port_bridge *)instance;
1388 struct port_bridge_member *member = bridge->first;
1389 unsigned long long timer_time;
1390 signed long *sum, sample;
1391 unsigned char buffer[160], *buf, *d;
1392 int i, read_p, space;
1394 bridge->sample_count += 160;
1396 /* schedule exactly 20ms from last schedule */
1397 timer_time = timer->timeout.tv_sec * MICRO_SECONDS + timer->timeout.tv_usec;
1398 timer_time += 20000; /* 20 MS */
1399 timer->timeout.tv_sec = timer_time / MICRO_SECONDS;
1400 timer->timeout.tv_usec = timer_time % MICRO_SECONDS;
1404 /* calculate transmit data */
1405 read_p = bridge->read_p;
1406 sum = bridge->sum_buffer;
1407 buf = member->buffer;
1409 for (i = 0; i < 160; i++) {
1410 sample = sum[read_p];
1411 sample -= audio_law_to_s32[buf[read_p]];
1412 buf[read_p] = silence;
1413 if (sample < SHORT_MIN) sample = SHORT_MIN;
1414 if (sample > SHORT_MAX) sample = SHORT_MAX;
1415 *d++ = audio_s16_to_law[sample & 0xffff];
1416 read_p = (read_p + 1) & (BRIDGE_BUFFER - 1);
1419 member->port->bridge_rx(buffer, 160);
1420 /* raise write pointer, if read pointer would overrun them */
1421 space = ((member->write_p - bridge->read_p) & (BRIDGE_BUFFER - 1)) - 160;
1424 member->write_p = read_p;
1425 // PDEBUG(DEBUG_PORT, "bridge %u member %d has buffer underrun\n", bridge->bridge_id, member->port->p_serial);
1427 /* find minimum delay */
1428 if (space < member->min_space)
1429 member->min_space = space;
1430 /* check if we should reduce buffer */
1431 if (bridge->sample_count >= 8000*5) {
1432 /* reduce buffer by minimum delay */
1433 // PDEBUG(DEBUG_PORT, "bridge %u member %d has min space of %d samples\n", bridge->bridge_id, member->port->p_serial, member->min_space);
1434 member->write_p = (member->write_p - member->min_space) & (BRIDGE_BUFFER - 1);
1435 member->min_space = 1000000; /* infinite */
1437 member = member->next;
1440 /* clear sample data */
1441 read_p = bridge->read_p;
1442 sum = bridge->sum_buffer;
1443 for (i = 0; i < 160; i++) {
1445 read_p = (read_p + 1) & (BRIDGE_BUFFER - 1);
1448 /* raise read pointer */
1449 bridge->read_p = read_p;
1451 if (bridge->sample_count >= 8000*5)
1452 bridge->sample_count = 0;
1458 /* receive data from remote Port */
1459 int Port::bridge_rx(unsigned char *data, int len)
1464 vootp_decrypt_stream(p_vootp, data, len);
1471 static void vootp_info(void *priv, const char *text)
1473 class Port *port = (class Port *)priv;
1474 char display[strlen(text) + 1];
1476 SCPY(display, text);
1478 display[strlen(display) - 1] = '\0';
1480 port->set_display(display);
1483 void Port::set_vootp(struct param_vootp *vootp)
1486 vootp_destroy(p_vootp);
1489 if (vootp->enable) {
1490 p_vootp = vootp_create(this, (options.law=='a'), options.otp_dir, NULL, NULL, vootp->id, vootp_info);
1491 // vootp_loglevel(VOOTP_LOGL_DEBUG);
1493 struct lcr_msg *message;
1495 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_VOOTP);
1496 message->param.vootp.failed = 1;
1497 message_put(message);