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 #define SHORT_MIN -32768
51 #define SHORT_MAX 32767
53 class Port *port_first = NULL;
55 unsigned int port_serial = 1; /* must be 1, because 0== no port */
57 struct port_bridge *p_bridge_first;
59 static void remove_bridge(struct port_bridge *bridge, class Port *port);
61 /* free epointlist relation
63 void Port::free_epointlist(struct epoint_list *epointlist)
65 struct epoint_list *temp, **tempp;
68 tempp = &p_epointlist;
70 if (temp == epointlist)
77 PERROR("SOFTWARE ERROR: epointlist not in port's list.\n");
84 PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
85 FREE(temp, sizeof(struct epoint_list));
90 void Port::free_epointid(unsigned int epoint_id)
92 struct epoint_list *temp, **tempp;
95 tempp = &p_epointlist;
97 if (temp->epoint_id == epoint_id)
104 PERROR("epoint_id not in port's list.\n");
111 PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
112 FREE(temp, sizeof(struct epoint_list));
117 /* create new epointlist relation
119 struct epoint_list *Port::epointlist_new(unsigned int epoint_id)
121 struct epoint_list *epointlist, **epointlistpointer;
123 /* epointlist structure */
124 epointlist = (struct epoint_list *)MALLOC(sizeof(struct epoint_list));
126 FATAL("No memory for epointlist\n");
128 PDEBUG(DEBUG_EPOINT, "PORT(%d) allocating epoint_list.\n", p_serial);
130 /* add epoint_list to chain */
131 epointlist->next = NULL;
132 epointlistpointer = &p_epointlist;
133 while(*epointlistpointer)
134 epointlistpointer = &((*epointlistpointer)->next);
135 *epointlistpointer = epointlist;
138 epointlist->epoint_id = epoint_id;
139 epointlist->active = 1;
148 Port::Port(int type, const char *portname, struct port_settings *settings)
150 class Port *temp, **tempp;
152 /* initialize object */
154 memcpy(&p_settings, settings, sizeof(struct port_settings));
156 memset(&p_settings, 0, sizeof(p_settings));
158 SCPY(p_name, portname);
159 p_tone_dir[0] = '\0';
161 p_serial = port_serial++;
163 p_tone_fetched = NULL;
164 p_tone_name[0] = '\0';
165 p_state = PORT_STATE_IDLE;
167 memset(&p_callerinfo, 0, sizeof(p_callerinfo));
168 memset(&p_dialinginfo, 0, sizeof(p_dialinginfo));
169 memset(&p_connectinfo, 0, sizeof(p_connectinfo));
170 memset(&p_redirinfo, 0, sizeof(p_redirinfo));
171 memset(&p_capainfo, 0, sizeof(p_capainfo));
180 p_record_filename[0] = '\0';
181 p_record_buffer_readp = 0;
182 p_record_buffer_writep = 0;
183 p_record_buffer_dir = 0;
185 /* append port to chain */
197 PDEBUG(DEBUG_PORT, "new port (%d) of type 0x%x, name '%s'\n", p_serial, type, portname);
206 class Port *temp, **tempp;
207 struct lcr_msg *message;
209 PDEBUG(DEBUG_PORT, "removing port (%d) of type 0x%x, name '%s'\n", p_serial, p_type, p_name);
212 PDEBUG(DEBUG_PORT, "Removing us from bridge %u\n", p_bridge->bridge_id);
213 remove_bridge(p_bridge, this);
221 /* disconnect port from endpoint */
222 while(p_epointlist) {
223 /* send disconnect */
224 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
225 message->param.disconnectinfo.cause = 16;
226 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
227 message_put(message);
228 /* remove endpoint */
229 free_epointlist(p_epointlist);
232 /* remove port from chain */
242 FATAL("PORT(%s) port not in port's list.\n", p_name);
246 /* close open tones file */
247 if (p_tone_fh >= 0) {
252 p_tone_fetched = NULL;
257 /* set new endpoint state
259 void Port::new_state(int state)
261 PDEBUG(DEBUG_PORT, "PORT(%s) new state %s --> %s\n", p_name, state_name[p_state], state_name[state]);
267 * find the port with port_id
269 class Port *find_port_id(unsigned int port_id)
271 class Port *port = port_first;
274 //printf("comparing: '%s' with '%s'\n", name, port->name);
275 if (port->p_serial == port_id)
287 void Port::set_echotest(int echotest)
289 p_echotest = echotest;
294 * set the file in the tone directory with the given name
296 void Port::set_tone(const char *dir, const char *name)
305 dir = options.tones_dir; /* just in case we have no PmISDN instance */
307 /* no counter, no eof, normal speed */
311 p_tone_codec = CODEC_LAW;
313 if (p_tone_fh >= 0) {
318 p_tone_fetched = NULL;
321 if (name[0] == '/') {
322 SPRINT(p_tone_name, "%s", name);
323 p_tone_dir[0] = '\0';
325 SCPY(p_tone_dir, dir);
326 SCPY(p_tone_name, name);
328 /* trigger playback */
331 p_tone_name[0]= '\0';
336 if (!!strncmp(name,"cause_",6))
339 /* now we check if the cause exists, otherwhise we use error tone. */
340 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, 0, 0))) {
341 p_tone_fetched = NULL;
344 SPRINT(filename, "%s_loop", p_tone_name);
345 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, 0, 0))) {
346 p_tone_fetched = NULL;
349 SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
350 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0) {
354 SPRINT(filename, "%s/%s/%s_loop", SHARE_DATA, p_tone_dir, p_tone_name);
355 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0) {
360 if (!strcmp(name,"cause_00") || !strcmp(name,"cause_10")) {
361 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using release tone\n", p_name, name+6);
362 SPRINT(p_tone_name,"release");
364 if (!strcmp(name,"cause_11")) {
365 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using busy tone\n", p_name, name+6);
366 SPRINT(p_tone_name,"busy");
368 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using error tone\n", p_name, name+6);
369 SPRINT(p_tone_name,"error");
375 * set the file in the tone directory for vbox playback
376 * also set the play_eof-flag
378 void Port::set_vbox_tone(const char *dir, const char *name)
384 p_tone_codec = CODEC_LAW;
387 if (p_tone_fh >= 0) {
392 p_tone_fetched = NULL;
394 SPRINT(p_tone_dir, dir);
395 SPRINT(p_tone_name, name);
396 /* trigger playback */
399 /* now we check if the cause exists, otherwhise we use error tone. */
401 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, &p_tone_size, &p_tone_left))) {
402 PDEBUG(DEBUG_PORT, "PORT(%s) opening fetched tone: %s\n", p_name, p_tone_name);
405 SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
406 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0) {
408 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
412 SPRINT(filename, "%s", p_tone_name);
413 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0) {
415 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
423 * set the file in the given directory for vbox playback
424 * also set the eof-flag
425 * also set the counter-flag
427 void Port::set_vbox_play(const char *name, int offset)
429 struct lcr_msg *message;
431 /* use ser_box_tone() */
432 set_vbox_tone("", name);
440 if (p_tone_name[0]) {
441 /* send message with counter value */
442 if (p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist)) {
443 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
444 message->param.counter.current = offset;
445 message->param.counter.max = p_tone_size;
446 message_put(message);
453 * set the playback speed (for recording playback with different speeds)
455 void Port::set_vbox_speed(int speed)
457 /* enable vbox play mode */
458 p_tone_speed = speed;
462 * read from the given file as specified in port_set_tone and return sample data
463 * if the tone ends, the result may be less samples than requested
465 int Port::read_audio(unsigned char *buffer, int length)
468 int nodata=0; /* to detect 0-length files and avoid endless reopen */
470 int tone_left_before; /* temp variable to determine the change in p_tone_left */
478 /* if there is no tone set, use silence */
482 /* if the file pointer is not open, we open it */
483 if (p_tone_fh<0 && p_tone_fetched==NULL) {
485 SPRINT(filename, "%s", p_tone_name);
486 /* if file does not exist */
487 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left))) {
488 SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
489 /* if file does not exist */
490 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
491 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
497 SPRINT(filename, "%s", p_tone_name);
498 /* if file does not exist */
499 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
500 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
505 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
509 /* file descriptor is open read data */
510 tone_left_before = p_tone_left;
511 if (p_tone_fh >= 0) {
512 l = read_tone(p_tone_fh, buffer, p_tone_codec, len, p_tone_size, &p_tone_left, p_tone_speed);
513 if (l<0 || l>len) /* paranoia */
518 if (p_tone_fetched) {
519 l = read_tone_fetched(&p_tone_fetched, buffer, len, p_tone_size, &p_tone_left, p_tone_speed);
520 if (l<0 || l>len) /* paranoia */
526 /* if counter is enabled, we check if we have a change */
527 if (p_tone_counter && p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist)) {
528 /* if we jumed to the next second */
529 if (((p_tone_size-p_tone_left)/8000) != (p_tone_size-tone_left_before)/8000) {
530 //printf("\nsize=%d left=%d\n\n",p_tone_size,p_tone_left);
531 struct lcr_msg *message;
532 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
533 message->param.counter.current = (p_tone_size-p_tone_left)/8000;
534 message->param.counter.max = -1;
535 message_put(message);
542 if (p_tone_fh >= 0) {
547 p_tone_fetched = NULL;
552 /* if the file has 0-length */
554 PDEBUG(DEBUG_PORT, "PORT(%s) 0-length loop: %s\n", p_name, filename);
560 /* if eof is reached, or if the normal file cannot be opened, continue with the loop file if possible */
562 if (p_tone_eof && ACTIVE_EPOINT(p_epointlist)) {
563 struct lcr_msg *message;
564 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_EOF);
565 message_put(message);
569 /* if file does not exist */
570 SPRINT(filename, "%s_loop", p_tone_name);
571 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left))) {
572 SPRINT(filename, "%s/%s/%s_loop", SHARE_DATA, p_tone_dir, p_tone_name);
573 /* if file does not exist */
574 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
575 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
576 p_tone_dir[0] = '\0';
577 p_tone_name[0] = '\0';
583 SPRINT(filename, "%s_loop", p_tone_name);
584 /* if file does not exist */
585 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
586 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
587 p_tone_dir[0] = '\0';
588 p_tone_name[0] = '\0';
594 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
596 /* now we have opened the loop */
601 /* Endpoint sends messages to the port
602 * This is called by the message_epoint, inherited by child classes.
603 * Therefor a return 1 means: "already handled here"
605 //extern struct lcr_msg *dddebug;
606 int Port::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
608 /* check if we got audio data from one remote port */
610 case MESSAGE_TONE: /* play tone */
611 PDEBUG(DEBUG_PORT, "PORT(%s) setting tone '%s' dir '%s'\n", p_name, param->tone.name, param->tone.dir);
612 set_tone(param->tone.dir,param->tone.name);
615 case MESSAGE_VBOX_TONE: /* play tone of answering machine */
616 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine tone '%s' '%s'\n", p_name, param->tone.dir, param->tone.name);
617 set_vbox_tone(param->tone.dir, param->tone.name);
620 case MESSAGE_VBOX_PLAY: /* play recording of answering machine */
621 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine file to play '%s' (offset %d seconds)\n", p_name, param->play.file, param->play.offset);
622 set_vbox_play(param->play.file, param->play.offset);
625 case MESSAGE_VBOX_PLAY_SPEED: /* set speed of playback (recording of answering machine) */
626 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine playback speed %d (times)\n", p_name, param->speed);
627 set_vbox_speed(param->speed);
630 case MESSAGE_BRIDGE: /* create / join / leave / destroy bridge */
631 PDEBUG(DEBUG_PORT, "PORT(%s) bridging to id %d\n", p_name, param->bridge_id);
632 bridge(param->bridge_id);
640 /* wave header structure */
642 unsigned short stereo; /* 1 = mono, 2 = stereo */
643 unsigned short channels; /* number of channels */
644 unsigned int sample_rate; /* sample rate */
645 unsigned int data_rate; /* data rate */
646 unsigned short bytes_sample; /* bytes per sample (all channels) */
647 unsigned short bits_sample; /* bits per sample (one channel) */
652 * open record file (actually a wave file with empty header which will be
653 * written before close, because we do not know the size yet)
654 * type=1 record annoucement, type=0 record audio stream, type=2 record vbox
656 int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ignore, const char *vbox_email, int vbox_email_file)
658 /* RIFFxxxxWAVEfmt xxxx(fmt-size)dataxxxx... */
659 char dummyheader[8+4+8+sizeof(fmt)+8];
666 PERROR("Port(%d) not an extension\n", p_serial);
669 SCPY(p_record_extension, extension);
670 p_record_anon_ignore = anon_ignore;
671 SCPY(p_record_vbox_email, vbox_email);
672 p_record_vbox_email_file = vbox_email_file;
675 PERROR("Port(%d) already recording\n", p_serial);
680 SPRINT(filename, "%s/%s/vbox", EXTENSION_DATA, p_record_extension);
682 SPRINT(filename, "%s/%s/recordings", EXTENSION_DATA, p_record_extension);
683 if (mkdir(filename, 0755) < 0) {
684 if (errno != EEXIST) {
685 PERROR("Port(%d) cannot create directory '%s'\n", p_serial, filename);
691 UPRINT(strchr(filename,'\0'), "/announcement");
694 now_tm = localtime(&now);
695 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);
698 p_record_vbox_year = now_tm->tm_year;
699 p_record_vbox_mon = now_tm->tm_mon;
700 p_record_vbox_mday = now_tm->tm_mday;
701 p_record_vbox_hour = now_tm->tm_hour;
702 p_record_vbox_min = now_tm->tm_min;
705 /* check, if file exists (especially when an extension calls the same extension) */
707 if ((p_record = fopen(filename, "r"))) {
709 SCAT(filename, "_2nd");
712 p_record = fopen(filename, "w");
714 PERROR("Port(%d) cannot record because file cannot be opened '%s'\n", p_serial, filename);
720 p_record_type = type;
721 p_record_vbox = vbox;
722 p_record_skip = skip;
724 switch(p_record_type) {
728 ret = fwrite(dummyheader, sizeof(dummyheader), 1, p_record);
734 UCPY(p_record_filename, filename);
736 PDEBUG(DEBUG_PORT, "Port(%d) recording started with file name '%s'\n", p_serial, filename);
742 * close the recoding file, put header in front and rename
744 void Port::close_record(int beep, int mute)
746 static signed short beep_mono[256];
747 unsigned int size = 0, wsize = 0;
749 char filename[512], indexname[512];
752 char number[256], callerid[256];
754 struct caller_info callerinfo;
755 const char *valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_.-!$%&/()=+*;~";
760 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);
762 memcpy(&callerinfo, &p_callerinfo, sizeof(struct caller_info));
763 // apply_callerid_restriction(p_record_anon_ignore, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.extension, callerinfo.name);
765 SCPY(number, p_dialinginfo.id);
766 SCPY(callerid, numberrize_callerinfo(callerinfo.id, callerinfo.ntype, options.national, options.international));
767 if (callerid[0] == '\0') {
768 if (callerinfo.present == INFO_PRESENT_RESTRICTED)
769 UCPY(callerid,"anonymous");
771 UCPY(callerid,"unknown");
774 /* change verboten digits */
776 while((p=strchr(p,'*')))
779 while((p=strchr(p,'/')))
782 while((p=strchr(p,'*')))
785 while((p=strchr(p,'/')))
788 ii = strlen(callerid);
790 if (!strchr(valid_chars, callerid[i]))
797 if (!strchr(valid_chars, number[i]))
803 if (mute && p_record_type==CODEC_MONO) {
807 fseek(p_record, -(i<<1), SEEK_END);
808 p_record_length -= (i<<1);
810 /* add beep to the end of recording */
811 if (beep && p_record_type==CODEC_MONO) {
814 beep_mono[i] = (signed short)(sin((double)i / 5.688888888889 * 2.0 * 3.1415927) * 2000.0);
819 ret = fwrite(beep_mono, sizeof(beep_mono), 1, p_record);
820 i += sizeof(beep_mono);
821 p_record_length += sizeof(beep_mono);
825 /* complete header */
826 switch(p_record_type) {
831 fprintf(p_record, "cue %c%c%c%c%c%c%c%c", 4, 0, 0, 0, 0,0,0,0);
834 fprintf(p_record, "LIST%c%c%c%cadtl", 4, 0, 0, 0);
837 fseek(p_record, 0, SEEK_SET);
839 /* WAVEfmt xxxx(fmt-size)dataxxxx[data]cue xxxx0000LISTxxxxadtl*/
840 size = p_record_length;
841 wsize = 4+8+sizeof(fmt)+8+size+8+4+8+4;
844 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));
847 fprintf(p_record, "WAVE");
850 fprintf(p_record, "fmt %c%c%c%c", (unsigned int)sizeof(fmt), 0, 0, 0);
851 switch(p_record_type) {
855 fmt.sample_rate = 8000; /* samples/sec */
856 fmt.data_rate = 16000; /* full data rate */
857 fmt.bytes_sample = 2; /* all channels */
858 fmt.bits_sample = 16; /* one channel */
864 fmt.sample_rate = 8000; /* samples/sec */
865 fmt.data_rate = 32000; /* full data rate */
866 fmt.bytes_sample = 4; /* all channels */
867 fmt.bits_sample = 16; /* one channel */
873 fmt.sample_rate = 8000; /* samples/sec */
874 fmt.data_rate = 8000; /* full data rate */
875 fmt.bytes_sample = 1; /* all channels */
876 fmt.bits_sample = 8; /* one channel */
879 ret = fwrite(&fmt, sizeof(fmt), 1, p_record);
882 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));
885 if (p_record_vbox == 1)
886 SPRINT(filename, "%s.wav", p_record_filename);
888 SPRINT(filename, "%s_%s-%s.wav", p_record_filename, callerid, number);
893 if (p_record_vbox == 1)
894 SPRINT(filename, "%s.isdn", p_record_filename);
896 SPRINT(filename, "%s_%s-%s.isdn", p_record_filename, callerid, number);
905 if (rename(p_record_filename, filename) < 0) {
906 PERROR("Port(%d) cannot rename from '%s' to '%s'\n", p_serial, p_record_filename, filename);
910 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);
912 if (p_record_vbox == 2) {
913 SPRINT(indexname, "%s/%s/vbox/index", EXTENSION_DATA, p_record_extension);
914 if ((fp = fopen(indexname,"a"))) {
917 /* remove path from file name */
919 while(strchr(p, '/'))
920 p = strchr(p, '/')+1;
921 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);
926 PERROR("Port(%d) cannot open index file '%s' to append.\n", p_serial, indexname);
929 /* send email with sample*/
930 if (p_record_vbox_email[0]) {
931 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);
939 * Records all data from down and from up into one single stream.
940 * Both streams may have gaps or jitter.
941 * A Jitter buffer for both streams is used to compensate jitter.
943 * If one stream (dir) received packets, they are stored to a
944 * buffer to wait for the other stream (dir), so both streams can
945 * be combined. If the buffer is full, it's content is written
946 * without mixing stream. (assuming only one stram (dir) exists.)
947 * A flag is used to indicate what stream is currently in buffer.
949 * NOTE: First stereo sample (odd) is from down, second is from up.
951 void Port::record(unsigned char *data, int length, int dir_fromup)
953 unsigned char write_buffer[1024], *d;
960 if (!p_record || !length)
963 /* skip data from local caller (dtmf input) */
964 if (p_record_skip && !dir_fromup) {
965 /* more to skip than we have */
966 if (p_record_skip > length) {
967 p_record_skip -= length;
971 data += p_record_skip;
972 length -= p_record_skip;
976 //printf("dir=%d len=%d\n", dir_fromup, length);
978 free = ((p_record_buffer_readp - p_record_buffer_writep - 1) & RECORD_BUFFER_MASK);
980 //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);
982 /* the buffer stores the same data stream */
983 if (dir_fromup == p_record_buffer_dir) {
986 //printf("same free=%d length=%d\n", free, length);
987 /* first write what we can to the buffer */
988 while(free && length) {
989 p_record_buffer[p_record_buffer_writep] = audio_law_to_s32[*data++];
990 p_record_buffer_writep = (p_record_buffer_writep + 1) & RECORD_BUFFER_MASK;
994 /* all written, so we return */
997 /* still data left, buffer is full, so we need to write a chunk to file */
998 switch(p_record_type) {
1000 s = (signed short *)write_buffer;
1003 *s++ = p_record_buffer[p_record_buffer_readp];
1004 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1007 ret = fwrite(write_buffer, 512, 1, p_record);
1008 p_record_length += 512;
1012 s = (signed short *)write_buffer;
1013 if (p_record_buffer_dir) {
1016 *s++ = 0; /* nothing from down */
1017 *s++ = p_record_buffer[p_record_buffer_readp];
1018 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1024 *s++ = p_record_buffer[p_record_buffer_readp];
1025 *s++ = 0; /* nothing from up */
1026 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1030 ret = fwrite(write_buffer, 1024, 1, p_record);
1031 p_record_length += 1024;
1038 *d++ = ((unsigned short)(p_record_buffer[p_record_buffer_readp]+0x8000)) >> 8;
1039 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1042 ret = fwrite(write_buffer, 512, 1, p_record);
1043 p_record_length += 512;
1050 *d++ = audio_s16_to_law[p_record_buffer[p_record_buffer_readp] & 0xffff];
1051 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1054 ret = fwrite(write_buffer, 256, 1, p_record);
1055 p_record_length += 256;
1058 /* because we still have data, we write again */
1062 /* the buffer stores the other stream */
1065 /* if buffer empty, change it */
1066 if (p_record_buffer_readp == p_record_buffer_writep) {
1067 p_record_buffer_dir = dir_fromup;
1070 /* how much data can we mix ? */
1071 ii = (p_record_buffer_writep - p_record_buffer_readp) & RECORD_BUFFER_MASK;
1077 //printf("same ii=%d length=%d\n", ii, length);
1078 //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);
1080 /* write data mixed with the buffer */
1081 switch(p_record_type) {
1083 s = (signed short *)write_buffer;
1086 sample = p_record_buffer[p_record_buffer_readp]
1087 + audio_law_to_s32[*data++];
1088 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1089 if (sample < SHORT_MIN) sample = SHORT_MIN;
1090 if (sample > SHORT_MAX) sample = SHORT_MAX;
1094 ret = fwrite(write_buffer, ii<<1, 1, p_record);
1095 p_record_length += (ii<<1);
1099 s = (signed short *)write_buffer;
1100 if (p_record_buffer_dir) {
1103 *s++ = audio_law_to_s32[*data++];
1104 *s++ = p_record_buffer[p_record_buffer_readp];
1105 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1111 *s++ = p_record_buffer[p_record_buffer_readp];
1112 *s++ = audio_law_to_s32[*data++];
1113 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1117 ret = fwrite(write_buffer, ii<<2, 1, p_record);
1118 p_record_length += (ii<<2);
1125 sample = p_record_buffer[p_record_buffer_readp]
1126 + audio_law_to_s32[*data++];
1127 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1128 if (sample < SHORT_MIN) sample = SHORT_MIN;
1129 if (sample > SHORT_MAX) sample = SHORT_MAX;
1130 *d++ = (sample+0x8000) >> 8;
1133 ret = fwrite(write_buffer, ii, 1, p_record);
1134 p_record_length += ii;
1141 sample = p_record_buffer[p_record_buffer_readp]
1142 + audio_law_to_s32[*data++];
1143 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1144 if (sample < SHORT_MIN) sample = SHORT_MIN;
1145 if (sample > SHORT_MAX) sample = SHORT_MAX;
1146 *d++ = audio_s16_to_law[sample & 0xffff];
1149 ret = fwrite(write_buffer, ii, 1, p_record);
1150 p_record_length += ii;
1156 goto different_again;
1157 /* no data (maybe buffer) */
1162 void Port::update_rxoff(void)
1166 void Port::update_load(void)
1175 static void remove_bridge(struct port_bridge *bridge, class Port *port)
1177 struct port_bridge **temp = &p_bridge_first;
1179 if (*temp == bridge) {
1182 /* Remove us from bridge. If bridge is empty, remove it completely. */
1183 if (bridge->sunrise == port) {
1184 bridge->sunrise = NULL;
1185 if (!bridge->sunset)
1188 if (bridge->sunset == port) {
1189 bridge->sunset = NULL;
1190 if (!bridge->sunrise)
1194 PDEBUG(DEBUG_PORT, "Remove bridge %u\n", bridge->bridge_id);
1195 *temp = bridge->next;
1196 FREE(bridge, sizeof(struct port_bridge));
1201 temp = &((*temp)->next);
1203 PERROR("Bridge %p not found in list\n", bridge);
1206 void Port::bridge(unsigned int bridge_id)
1208 /* Remove bridge, if we leave bridge or if we join a different bridge. */
1209 if (p_bridge && bridge_id != p_bridge->bridge_id) {
1210 PDEBUG(DEBUG_PORT, "Remove port %u from bridge %u, because out new bridge is %u\n", p_serial, p_bridge->bridge_id, bridge_id);
1211 remove_bridge(p_bridge, this);
1215 /* if we leave bridge */
1221 struct port_bridge *temp = p_bridge_first;
1224 if (temp->bridge_id == bridge_id)
1230 PDEBUG(DEBUG_PORT, "Port %d found existing bridge %u.\n", p_serial, p_bridge->bridge_id);
1235 struct port_bridge **temp = &p_bridge_first;
1237 p_bridge = (struct port_bridge *) MALLOC(sizeof(struct port_bridge));
1239 p_bridge->bridge_id = bridge_id;
1240 p_bridge->sunrise = this;
1242 /* attach bridge instance to list */
1244 temp = &((*temp)->next);
1246 PDEBUG(DEBUG_PORT, "Port %d creating not existing bridge %u.\n", p_serial, p_bridge->bridge_id);
1249 /* already joined */
1250 if (p_bridge->sunrise == this || p_bridge->sunset == this)
1254 if (!p_bridge->sunrise) {
1255 p_bridge->sunrise = this;
1258 if (!p_bridge->sunset) {
1259 p_bridge->sunset = this;
1263 PERROR("Bridge ID %u cannot be joined by port %u, because it is already occupied by ports %u and %u.\n", p_bridge->bridge_id, p_serial, p_bridge->sunrise->p_serial, p_bridge->sunset->p_serial);
1267 class Port *Port::bridge_remote(void)
1269 class Port *remote = NULL;
1271 /* get remote port from bridge */
1274 if (p_bridge->sunrise == this)
1275 remote = p_bridge->sunset;
1276 if (p_bridge->sunset == this)
1277 remote = p_bridge->sunrise;
1282 /* send data to remote Port */
1283 int Port::bridge_tx(unsigned char *data, int len)
1285 class Port *remote = bridge_remote();
1290 // printf("Traffic: %u -> %u (bridge %u)\n", p_serial, to_port->p_serial, p_bridge->bridge_id);
1291 return remote->bridge_rx(data, len);
1294 /* receive data from remote Port (dummy, needs to be inherited) */
1295 int Port::bridge_rx(unsigned char *data, int len)
1297 return 0; /* datenklo */