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;
191 /* append port to chain */
203 PDEBUG(DEBUG_PORT, "new port (%d) of type 0x%x, name '%s' interface '%s'\n", p_serial, type, portname, p_interface_name);
212 class Port *temp, **tempp;
213 struct lcr_msg *message;
215 PDEBUG(DEBUG_PORT, "removing port (%d) of type 0x%x, name '%s' interface '%s'\n", p_serial, p_type, p_name, p_interface_name);
218 PDEBUG(DEBUG_PORT, "Removing us from bridge %u\n", p_bridge->bridge_id);
219 remove_bridge(p_bridge, this);
227 /* disconnect port from endpoint */
228 while(p_epointlist) {
229 /* send disconnect */
230 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
231 message->param.disconnectinfo.cause = 16;
232 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
233 message_put(message);
234 /* remove endpoint */
235 free_epointlist(p_epointlist);
238 /* remove port from chain */
248 FATAL("PORT(%s) port not in port's list.\n", p_name);
252 /* close open tones file */
253 if (p_tone_fh >= 0) {
258 p_tone_fetched = NULL;
263 /* set new endpoint state
265 void Port::new_state(int state)
267 PDEBUG(DEBUG_PORT, "PORT(%s) new state %s --> %s\n", p_name, state_name[p_state], state_name[state]);
273 * find the port with port_id
275 class Port *find_port_id(unsigned int port_id)
277 class Port *port = port_first;
280 //printf("comparing: '%s' with '%s'\n", name, port->name);
281 if (port->p_serial == port_id)
293 void Port::set_echotest(int echotest)
295 p_echotest = echotest;
300 * set the file in the tone directory with the given name
302 void Port::set_tone(const char *dir, const char *name)
311 dir = options.tones_dir; /* just in case we have no PmISDN instance */
313 /* no counter, no eof, normal speed */
317 p_tone_codec = CODEC_LAW;
319 if (p_tone_fh >= 0) {
324 p_tone_fetched = NULL;
327 if (name[0] == '/') {
328 SPRINT(p_tone_name, "%s", name);
329 p_tone_dir[0] = '\0';
331 SCPY(p_tone_dir, dir);
332 SCPY(p_tone_name, name);
334 /* trigger playback */
337 p_tone_name[0]= '\0';
342 if (!!strncmp(name,"cause_",6))
345 /* now we check if the cause exists, otherwhise we use error tone. */
346 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, 0, 0))) {
347 p_tone_fetched = NULL;
350 SPRINT(filename, "%s_loop", p_tone_name);
351 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, 0, 0))) {
352 p_tone_fetched = NULL;
355 SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
356 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0) {
360 SPRINT(filename, "%s/%s/%s_loop", SHARE_DATA, p_tone_dir, p_tone_name);
361 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0) {
366 if (!strcmp(name,"cause_00") || !strcmp(name,"cause_10")) {
367 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using release tone\n", p_name, name+6);
368 SPRINT(p_tone_name,"release");
370 if (!strcmp(name,"cause_11")) {
371 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using busy tone\n", p_name, name+6);
372 SPRINT(p_tone_name,"busy");
374 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using error tone\n", p_name, name+6);
375 SPRINT(p_tone_name,"error");
381 * set the file in the tone directory for vbox playback
382 * also set the play_eof-flag
384 void Port::set_vbox_tone(const char *dir, const char *name)
390 p_tone_codec = CODEC_LAW;
393 if (p_tone_fh >= 0) {
398 p_tone_fetched = NULL;
400 SPRINT(p_tone_dir, dir);
401 SPRINT(p_tone_name, name);
402 /* trigger playback */
405 /* now we check if the cause exists, otherwhise we use error tone. */
407 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, &p_tone_size, &p_tone_left))) {
408 PDEBUG(DEBUG_PORT, "PORT(%s) opening fetched tone: %s\n", p_name, p_tone_name);
411 SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
412 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0) {
414 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
418 SPRINT(filename, "%s", p_tone_name);
419 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0) {
421 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
429 * set the file in the given directory for vbox playback
430 * also set the eof-flag
431 * also set the counter-flag
433 void Port::set_vbox_play(const char *name, int offset)
435 struct lcr_msg *message;
437 /* use ser_box_tone() */
438 set_vbox_tone("", name);
446 if (p_tone_name[0]) {
447 /* send message with counter value */
448 if (p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist)) {
449 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
450 message->param.counter.current = offset;
451 message->param.counter.max = p_tone_size;
452 message_put(message);
459 * set the playback speed (for recording playback with different speeds)
461 void Port::set_vbox_speed(int speed)
463 /* enable vbox play mode */
464 p_tone_speed = speed;
468 * read from the given file as specified in port_set_tone and return sample data
469 * if the tone ends, the result may be less samples than requested
471 int Port::read_audio(unsigned char *buffer, int length)
474 int nodata=0; /* to detect 0-length files and avoid endless reopen */
476 int tone_left_before; /* temp variable to determine the change in p_tone_left */
484 /* if there is no tone set, use silence */
488 /* if the file pointer is not open, we open it */
489 if (p_tone_fh<0 && p_tone_fetched==NULL) {
491 SPRINT(filename, "%s", p_tone_name);
492 /* if file does not exist */
493 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left))) {
494 SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
495 /* if file does not exist */
496 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
497 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
503 SPRINT(filename, "%s", p_tone_name);
504 /* if file does not exist */
505 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
506 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
511 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
515 /* file descriptor is open read data */
516 tone_left_before = p_tone_left;
517 if (p_tone_fh >= 0) {
518 l = read_tone(p_tone_fh, buffer, p_tone_codec, len, p_tone_size, &p_tone_left, p_tone_speed);
519 if (l<0 || l>len) /* paranoia */
524 if (p_tone_fetched) {
525 l = read_tone_fetched(&p_tone_fetched, buffer, len, p_tone_size, &p_tone_left, p_tone_speed);
526 if (l<0 || l>len) /* paranoia */
532 /* if counter is enabled, we check if we have a change */
533 if (p_tone_counter && p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist)) {
534 /* if we jumed to the next second */
535 if (((p_tone_size-p_tone_left)/8000) != (p_tone_size-tone_left_before)/8000) {
536 //printf("\nsize=%d left=%d\n\n",p_tone_size,p_tone_left);
537 struct lcr_msg *message;
538 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
539 message->param.counter.current = (p_tone_size-p_tone_left)/8000;
540 message->param.counter.max = -1;
541 message_put(message);
548 if (p_tone_fh >= 0) {
553 p_tone_fetched = NULL;
558 /* if the file has 0-length */
560 PDEBUG(DEBUG_PORT, "PORT(%s) 0-length loop: %s\n", p_name, filename);
566 /* if eof is reached, or if the normal file cannot be opened, continue with the loop file if possible */
568 if (p_tone_eof && ACTIVE_EPOINT(p_epointlist)) {
569 struct lcr_msg *message;
570 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_EOF);
571 message_put(message);
575 /* if file does not exist */
576 SPRINT(filename, "%s_loop", p_tone_name);
577 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left))) {
578 SPRINT(filename, "%s/%s/%s_loop", SHARE_DATA, p_tone_dir, p_tone_name);
579 /* if file does not exist */
580 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
581 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
582 p_tone_dir[0] = '\0';
583 p_tone_name[0] = '\0';
589 SPRINT(filename, "%s_loop", p_tone_name);
590 /* if file does not exist */
591 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
592 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
593 p_tone_dir[0] = '\0';
594 p_tone_name[0] = '\0';
600 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
602 /* now we have opened the loop */
607 /* Endpoint sends messages to the port
608 * This is called by the message_epoint, inherited by child classes.
609 * Therefor a return 1 means: "already handled here"
611 //extern struct lcr_msg *dddebug;
612 int Port::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
614 /* check if we got audio data from one remote port */
616 case MESSAGE_TONE: /* play tone */
617 PDEBUG(DEBUG_PORT, "PORT(%s) setting tone '%s' dir '%s'\n", p_name, param->tone.name, param->tone.dir);
618 set_tone(param->tone.dir,param->tone.name);
621 case MESSAGE_VBOX_TONE: /* play tone of answering machine */
622 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine tone '%s' '%s'\n", p_name, param->tone.dir, param->tone.name);
623 set_vbox_tone(param->tone.dir, param->tone.name);
626 case MESSAGE_VBOX_PLAY: /* play recording of answering machine */
627 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine file to play '%s' (offset %d seconds)\n", p_name, param->play.file, param->play.offset);
628 set_vbox_play(param->play.file, param->play.offset);
631 case MESSAGE_VBOX_PLAY_SPEED: /* set speed of playback (recording of answering machine) */
632 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine playback speed %d (times)\n", p_name, param->speed);
633 set_vbox_speed(param->speed);
636 case MESSAGE_BRIDGE: /* create / join / leave / destroy bridge */
637 PDEBUG(DEBUG_PORT, "PORT(%s) bridging to id %d\n", p_name, param->bridge_id);
638 bridge(param->bridge_id);
646 /* wave header structure */
648 unsigned short stereo; /* 1 = mono, 2 = stereo */
649 unsigned short channels; /* number of channels */
650 unsigned int sample_rate; /* sample rate */
651 unsigned int data_rate; /* data rate */
652 unsigned short bytes_sample; /* bytes per sample (all channels) */
653 unsigned short bits_sample; /* bits per sample (one channel) */
658 * open record file (actually a wave file with empty header which will be
659 * written before close, because we do not know the size yet)
660 * type=1 record annoucement, type=0 record audio stream, type=2 record vbox
662 int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ignore, const char *vbox_email, int vbox_email_file)
664 /* RIFFxxxxWAVEfmt xxxx(fmt-size)dataxxxx... */
665 char dummyheader[8+4+8+sizeof(fmt)+8];
672 PERROR("Port(%d) not an extension\n", p_serial);
675 SCPY(p_record_extension, extension);
676 p_record_anon_ignore = anon_ignore;
677 SCPY(p_record_vbox_email, vbox_email);
678 p_record_vbox_email_file = vbox_email_file;
681 PERROR("Port(%d) already recording\n", p_serial);
686 SPRINT(filename, "%s/%s/vbox", EXTENSION_DATA, p_record_extension);
688 SPRINT(filename, "%s/%s/recordings", EXTENSION_DATA, p_record_extension);
689 if (mkdir(filename, 0755) < 0) {
690 if (errno != EEXIST) {
691 PERROR("Port(%d) cannot create directory '%s'\n", p_serial, filename);
697 UPRINT(strchr(filename,'\0'), "/announcement");
700 now_tm = localtime(&now);
701 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);
704 p_record_vbox_year = now_tm->tm_year;
705 p_record_vbox_mon = now_tm->tm_mon;
706 p_record_vbox_mday = now_tm->tm_mday;
707 p_record_vbox_hour = now_tm->tm_hour;
708 p_record_vbox_min = now_tm->tm_min;
711 /* check, if file exists (especially when an extension calls the same extension) */
713 if ((p_record = fopen(filename, "r"))) {
715 SCAT(filename, "_2nd");
718 p_record = fopen(filename, "w");
720 PERROR("Port(%d) cannot record because file cannot be opened '%s'\n", p_serial, filename);
726 p_record_type = type;
727 p_record_vbox = vbox;
728 p_record_skip = skip;
730 switch(p_record_type) {
734 ret = fwrite(dummyheader, sizeof(dummyheader), 1, p_record);
740 UCPY(p_record_filename, filename);
742 PDEBUG(DEBUG_PORT, "Port(%d) recording started with file name '%s'\n", p_serial, filename);
748 * close the recoding file, put header in front and rename
750 void Port::close_record(int beep, int mute)
752 static signed short beep_mono[256];
753 unsigned int size = 0, wsize = 0;
755 char filename[512], indexname[512];
758 char number[256], callerid[256];
760 struct caller_info callerinfo;
761 const char *valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_.-!$%&/()=+*;~";
766 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);
768 memcpy(&callerinfo, &p_callerinfo, sizeof(struct caller_info));
769 // apply_callerid_restriction(p_record_anon_ignore, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.extension, callerinfo.name);
771 SCPY(number, p_dialinginfo.id);
772 SCPY(callerid, numberrize_callerinfo(callerinfo.id, callerinfo.ntype, options.national, options.international));
773 if (callerid[0] == '\0') {
774 if (callerinfo.present == INFO_PRESENT_RESTRICTED)
775 UCPY(callerid,"anonymous");
777 UCPY(callerid,"unknown");
780 /* change verboten digits */
782 while((p=strchr(p,'*')))
785 while((p=strchr(p,'/')))
788 while((p=strchr(p,'*')))
791 while((p=strchr(p,'/')))
794 ii = strlen(callerid);
796 if (!strchr(valid_chars, callerid[i]))
803 if (!strchr(valid_chars, number[i]))
809 if (mute && p_record_type==CODEC_MONO) {
813 fseek(p_record, -(i<<1), SEEK_END);
814 p_record_length -= (i<<1);
816 /* add beep to the end of recording */
817 if (beep && p_record_type==CODEC_MONO) {
820 beep_mono[i] = (signed short)(sin((double)i / 5.688888888889 * 2.0 * 3.1415927) * 2000.0);
825 ret = fwrite(beep_mono, sizeof(beep_mono), 1, p_record);
826 i += sizeof(beep_mono);
827 p_record_length += sizeof(beep_mono);
831 /* complete header */
832 switch(p_record_type) {
837 fprintf(p_record, "cue %c%c%c%c%c%c%c%c", 4, 0, 0, 0, 0,0,0,0);
840 fprintf(p_record, "LIST%c%c%c%cadtl", 4, 0, 0, 0);
843 fseek(p_record, 0, SEEK_SET);
845 /* WAVEfmt xxxx(fmt-size)dataxxxx[data]cue xxxx0000LISTxxxxadtl*/
846 size = p_record_length;
847 wsize = 4+8+sizeof(fmt)+8+size+8+4+8+4;
850 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));
853 fprintf(p_record, "WAVE");
856 fprintf(p_record, "fmt %c%c%c%c", (unsigned int)sizeof(fmt), 0, 0, 0);
857 switch(p_record_type) {
861 fmt.sample_rate = 8000; /* samples/sec */
862 fmt.data_rate = 16000; /* full data rate */
863 fmt.bytes_sample = 2; /* all channels */
864 fmt.bits_sample = 16; /* one channel */
870 fmt.sample_rate = 8000; /* samples/sec */
871 fmt.data_rate = 32000; /* full data rate */
872 fmt.bytes_sample = 4; /* all channels */
873 fmt.bits_sample = 16; /* one channel */
879 fmt.sample_rate = 8000; /* samples/sec */
880 fmt.data_rate = 8000; /* full data rate */
881 fmt.bytes_sample = 1; /* all channels */
882 fmt.bits_sample = 8; /* one channel */
885 ret = fwrite(&fmt, sizeof(fmt), 1, p_record);
888 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));
891 if (p_record_vbox == 1)
892 SPRINT(filename, "%s.wav", p_record_filename);
894 SPRINT(filename, "%s_%s-%s.wav", p_record_filename, callerid, number);
899 if (p_record_vbox == 1)
900 SPRINT(filename, "%s.isdn", p_record_filename);
902 SPRINT(filename, "%s_%s-%s.isdn", p_record_filename, callerid, number);
911 if (rename(p_record_filename, filename) < 0) {
912 PERROR("Port(%d) cannot rename from '%s' to '%s'\n", p_serial, p_record_filename, filename);
916 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);
918 if (p_record_vbox == 2) {
919 SPRINT(indexname, "%s/%s/vbox/index", EXTENSION_DATA, p_record_extension);
920 if ((fp = fopen(indexname,"a"))) {
923 /* remove path from file name */
925 while(strchr(p, '/'))
926 p = strchr(p, '/')+1;
927 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);
932 PERROR("Port(%d) cannot open index file '%s' to append.\n", p_serial, indexname);
935 /* send email with sample*/
936 if (p_record_vbox_email[0]) {
937 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);
945 * Records all data from down and from up into one single stream.
946 * Both streams may have gaps or jitter.
947 * A Jitter buffer for both streams is used to compensate jitter.
949 * If one stream (dir) received packets, they are stored to a
950 * buffer to wait for the other stream (dir), so both streams can
951 * be combined. If the buffer is full, it's content is written
952 * without mixing stream. (assuming only one stram (dir) exists.)
953 * A flag is used to indicate what stream is currently in buffer.
955 * NOTE: First stereo sample (odd) is from down, second is from up.
957 void Port::record(unsigned char *data, int length, int dir_fromup)
959 unsigned char write_buffer[1024], *d;
966 if (!p_record || !length)
969 /* skip data from local caller (dtmf input) */
970 if (p_record_skip && !dir_fromup) {
971 /* more to skip than we have */
972 if (p_record_skip > length) {
973 p_record_skip -= length;
977 data += p_record_skip;
978 length -= p_record_skip;
982 //printf("dir=%d len=%d\n", dir_fromup, length);
984 free = ((p_record_buffer_readp - p_record_buffer_writep - 1) & RECORD_BUFFER_MASK);
986 //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);
988 /* the buffer stores the same data stream */
989 if (dir_fromup == p_record_buffer_dir) {
992 //printf("same free=%d length=%d\n", free, length);
993 /* first write what we can to the buffer */
994 while(free && length) {
995 p_record_buffer[p_record_buffer_writep] = audio_law_to_s32[*data++];
996 p_record_buffer_writep = (p_record_buffer_writep + 1) & RECORD_BUFFER_MASK;
1000 /* all written, so we return */
1003 /* still data left, buffer is full, so we need to write a chunk to file */
1004 switch(p_record_type) {
1006 s = (signed short *)write_buffer;
1009 *s++ = p_record_buffer[p_record_buffer_readp];
1010 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1013 ret = fwrite(write_buffer, 512, 1, p_record);
1014 p_record_length += 512;
1018 s = (signed short *)write_buffer;
1019 if (p_record_buffer_dir) {
1022 *s++ = 0; /* nothing from down */
1023 *s++ = p_record_buffer[p_record_buffer_readp];
1024 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1030 *s++ = p_record_buffer[p_record_buffer_readp];
1031 *s++ = 0; /* nothing from up */
1032 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1036 ret = fwrite(write_buffer, 1024, 1, p_record);
1037 p_record_length += 1024;
1044 *d++ = ((unsigned short)(p_record_buffer[p_record_buffer_readp]+0x8000)) >> 8;
1045 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1048 ret = fwrite(write_buffer, 512, 1, p_record);
1049 p_record_length += 512;
1056 *d++ = audio_s16_to_law[p_record_buffer[p_record_buffer_readp] & 0xffff];
1057 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1060 ret = fwrite(write_buffer, 256, 1, p_record);
1061 p_record_length += 256;
1064 /* because we still have data, we write again */
1068 /* the buffer stores the other stream */
1071 /* if buffer empty, change it */
1072 if (p_record_buffer_readp == p_record_buffer_writep) {
1073 p_record_buffer_dir = dir_fromup;
1076 /* how much data can we mix ? */
1077 ii = (p_record_buffer_writep - p_record_buffer_readp) & RECORD_BUFFER_MASK;
1083 //printf("same ii=%d length=%d\n", ii, length);
1084 //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);
1086 /* write data mixed with the buffer */
1087 switch(p_record_type) {
1089 s = (signed short *)write_buffer;
1092 sample = p_record_buffer[p_record_buffer_readp]
1093 + audio_law_to_s32[*data++];
1094 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1095 if (sample < SHORT_MIN) sample = SHORT_MIN;
1096 if (sample > SHORT_MAX) sample = SHORT_MAX;
1100 ret = fwrite(write_buffer, ii<<1, 1, p_record);
1101 p_record_length += (ii<<1);
1105 s = (signed short *)write_buffer;
1106 if (p_record_buffer_dir) {
1109 *s++ = audio_law_to_s32[*data++];
1110 *s++ = p_record_buffer[p_record_buffer_readp];
1111 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1117 *s++ = p_record_buffer[p_record_buffer_readp];
1118 *s++ = audio_law_to_s32[*data++];
1119 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1123 ret = fwrite(write_buffer, ii<<2, 1, p_record);
1124 p_record_length += (ii<<2);
1131 sample = p_record_buffer[p_record_buffer_readp]
1132 + audio_law_to_s32[*data++];
1133 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1134 if (sample < SHORT_MIN) sample = SHORT_MIN;
1135 if (sample > SHORT_MAX) sample = SHORT_MAX;
1136 *d++ = (sample+0x8000) >> 8;
1139 ret = fwrite(write_buffer, ii, 1, p_record);
1140 p_record_length += ii;
1147 sample = p_record_buffer[p_record_buffer_readp]
1148 + audio_law_to_s32[*data++];
1149 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1150 if (sample < SHORT_MIN) sample = SHORT_MIN;
1151 if (sample > SHORT_MAX) sample = SHORT_MAX;
1152 *d++ = audio_s16_to_law[sample & 0xffff];
1155 ret = fwrite(write_buffer, ii, 1, p_record);
1156 p_record_length += ii;
1162 goto different_again;
1163 /* no data (maybe buffer) */
1168 void Port::tap(unsigned char *data, int length, int dir_fromup)
1172 void Port::update_rxoff(void)
1176 void Port::update_load(void)
1185 int bridge_timeout(struct lcr_timer *timer, void *instance, int index);
1187 static void remove_bridge(struct port_bridge *bridge, class Port *port)
1189 struct port_bridge **temp = &p_bridge_first;
1191 if (*temp == bridge) {
1192 struct port_bridge_member **memberp = &bridge->first, *member;
1194 /* loop until we are found */
1196 if ((*memberp)->port == port) {
1198 *memberp = member->next;
1199 FREE(member, sizeof(struct port_bridge_member));
1201 #ifndef TEST_CONFERENCE
1202 if (bridge->first && bridge->first->next && !bridge->first->next->next) {
1204 if (bridge->first && !bridge->first->next) {
1206 PDEBUG(DEBUG_PORT, "bridge %u is no conference anymore\n", bridge->bridge_id);
1207 del_timer(&bridge->timer);
1211 memberp = &((*memberp)->next);
1213 /* if bridge is empty, remove it */
1214 if (bridge->first == NULL) {
1215 PDEBUG(DEBUG_PORT, "Remove bridge %u\n", bridge->bridge_id);
1216 *temp = bridge->next;
1217 FREE(bridge, sizeof(struct port_bridge));
1222 temp = &((*temp)->next);
1224 PERROR("Bridge %p not found in list\n", bridge);
1227 void Port::bridge(unsigned int bridge_id)
1229 struct port_bridge_member **memberp;
1231 /* Remove bridge, if we leave bridge or if we join a different bridge. */
1232 if (p_bridge && bridge_id != p_bridge->bridge_id) {
1233 PDEBUG(DEBUG_PORT, "Remove port %u from bridge %u, because out new bridge is %u\n", p_serial, p_bridge->bridge_id, bridge_id);
1234 remove_bridge(p_bridge, this);
1238 /* if we leave bridge */
1244 struct port_bridge *temp = p_bridge_first;
1247 if (temp->bridge_id == bridge_id)
1253 PDEBUG(DEBUG_PORT, "Port %d found existing bridge %u.\n", p_serial, p_bridge->bridge_id);
1258 struct port_bridge **temp = &p_bridge_first;
1260 p_bridge = (struct port_bridge *) MALLOC(sizeof(struct port_bridge));
1262 p_bridge->bridge_id = bridge_id;
1264 /* attach bridge instance to list */
1266 temp = &((*temp)->next);
1268 PDEBUG(DEBUG_PORT, "Port %d creating not existing bridge %u.\n", p_serial, p_bridge->bridge_id);
1271 /* attach to bridge */
1272 memberp = &p_bridge->first;
1274 if ((*memberp)->port == this) {
1275 /* already joined */
1278 memberp = &((*memberp)->next);
1280 *memberp = (struct port_bridge_member *) MALLOC(sizeof(struct port_bridge_member));
1282 (*memberp)->port = this;
1283 /* check if bridge becomes a conference */
1284 #ifndef TEST_CONFERENCE
1285 if (p_bridge->first->next && p_bridge->first->next->next && !p_bridge->first->next->next->next) {
1286 p_bridge->first->next->next->write_p = 0;
1287 p_bridge->first->next->next->min_space = 0;
1288 memset(p_bridge->first->next->next->buffer, silence, sizeof((*memberp)->buffer));
1290 if (p_bridge->first->next && !p_bridge->first->next->next) {
1292 p_bridge->first->next->write_p = 0;
1293 p_bridge->first->next->min_space = 0;
1294 memset(p_bridge->first->next->buffer, silence, sizeof((*memberp)->buffer));
1295 p_bridge->first->write_p = 0;
1296 p_bridge->first->min_space = 0;
1297 memset(p_bridge->first->buffer, silence, sizeof((*memberp)->buffer));
1298 memset(p_bridge->sum_buffer, 0, sizeof(p_bridge->sum_buffer));
1299 p_bridge->read_p = 0;
1300 add_timer(&p_bridge->timer, bridge_timeout, p_bridge, 0);
1301 schedule_timer(&p_bridge->timer, 0, 20000); /* 20 MS */
1302 p_bridge->sample_count = 0;
1303 PDEBUG(DEBUG_PORT, "bridge %u became a conference\n", p_bridge->bridge_id);
1307 /* send data to remote Port or add to sum buffer */
1308 int Port::bridge_tx(unsigned char *data, int len)
1311 struct port_bridge_member *member;
1315 /* less than two ports, so drop */
1316 if (!p_bridge || !p_bridge->first || !p_bridge->first->next)
1318 #ifndef TEST_CONFERENCE
1319 /* two ports, so bridge */
1320 if (!p_bridge->first->next->next) {
1321 if (p_bridge->first->port == this)
1322 return p_bridge->first->next->port->bridge_rx(data, len);
1323 if (p_bridge->first->next->port == this)
1324 return p_bridge->first->port->bridge_rx(data, len);
1328 /* more than two ports... */
1329 member = p_bridge->first;
1331 if (member->port == this)
1333 member = member->next;
1337 write_p = member->write_p;
1338 /* calculate space, so write pointer will not overrun (or reach) read pointer in ring buffer */
1339 space = (p_bridge->read_p - write_p - 1) & (BRIDGE_BUFFER - 1);
1340 /* clip len, if it does not fit */
1343 /* apply audio samples to sum buffer */
1344 sum = p_bridge->sum_buffer;
1345 buf = member->buffer;
1347 sum[write_p] += audio_law_to_s32[*data];
1348 buf[write_p] = *data++;
1349 write_p = (write_p + 1) & (BRIDGE_BUFFER - 1);
1351 /* raise write pointer */
1352 member->write_p = write_p;
1357 int bridge_timeout(struct lcr_timer *timer, void *instance, int index)
1359 struct port_bridge *bridge = (struct port_bridge *)instance;
1360 struct port_bridge_member *member = bridge->first;
1361 unsigned long long timer_time;
1362 signed long *sum, sample;
1363 unsigned char buffer[160], *buf, *d;
1364 int i, read_p, space;
1366 bridge->sample_count += 160;
1368 /* schedule exactly 20ms from last schedule */
1369 timer_time = timer->timeout.tv_sec * MICRO_SECONDS + timer->timeout.tv_usec;
1370 timer_time += 20000; /* 20 MS */
1371 timer->timeout.tv_sec = timer_time / MICRO_SECONDS;
1372 timer->timeout.tv_usec = timer_time % MICRO_SECONDS;
1376 /* calculate transmit data */
1377 read_p = bridge->read_p;
1378 sum = bridge->sum_buffer;
1379 buf = member->buffer;
1381 for (i = 0; i < 160; i++) {
1382 sample = sum[read_p];
1383 sample -= audio_law_to_s32[buf[read_p]];
1384 buf[read_p] = silence;
1385 if (sample < SHORT_MIN) sample = SHORT_MIN;
1386 if (sample > SHORT_MAX) sample = SHORT_MAX;
1387 *d++ = audio_s16_to_law[sample & 0xffff];
1388 read_p = (read_p + 1) & (BRIDGE_BUFFER - 1);
1391 member->port->bridge_rx(buffer, 160);
1392 /* raise write pointer, if read pointer would overrun them */
1393 space = ((member->write_p - bridge->read_p) & (BRIDGE_BUFFER - 1)) - 160;
1396 member->write_p = read_p;
1397 // PDEBUG(DEBUG_PORT, "bridge %u member %d has buffer underrun\n", bridge->bridge_id, member->port->p_serial);
1399 /* find minimum delay */
1400 if (space < member->min_space)
1401 member->min_space = space;
1402 /* check if we should reduce buffer */
1403 if (bridge->sample_count >= 8000*5) {
1404 /* reduce buffer by minimum delay */
1405 // PDEBUG(DEBUG_PORT, "bridge %u member %d has min space of %d samples\n", bridge->bridge_id, member->port->p_serial, member->min_space);
1406 member->write_p = (member->write_p - member->min_space) & (BRIDGE_BUFFER - 1);
1407 member->min_space = 1000000; /* infinite */
1409 member = member->next;
1412 /* clear sample data */
1413 read_p = bridge->read_p;
1414 sum = bridge->sum_buffer;
1415 for (i = 0; i < 160; i++) {
1417 read_p = (read_p + 1) & (BRIDGE_BUFFER - 1);
1420 /* raise read pointer */
1421 bridge->read_p = read_p;
1423 if (bridge->sample_count >= 8000*5)
1424 bridge->sample_count = 0;
1430 /* receive data from remote Port (dummy, needs to be inherited) */
1431 int Port::bridge_rx(unsigned char *data, int len)
1433 return 0; /* datenklo */