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
22 -> sound from asterisk
26 -> if local or remote channel is not mISDN
27 -> if endpoint is linked to asterisk
28 -> if call is recorded (vbox)
34 -> audio from upper layer is buffered for later transmission to channel
36 -> buffered audio from upper layer or tones are transmitted via system clock
38 -> rx-data from port to record() and upper layer
39 -> tx-data from port (dsp) to record()
41 -> streaming announcement to upper layer
42 -> recording announcement
43 * VboxPort::message_epoint
44 -> recording audio message from upper layer
53 #include <sys/types.h>
60 class Port *port_first = NULL;
62 unsigned long port_serial = 1; /* must be 1, because 0== no port */
65 /* free epointlist relation
67 void Port::free_epointlist(struct epoint_list *epointlist)
69 struct epoint_list *temp, **tempp;
72 tempp = &p_epointlist;
75 if (temp == epointlist)
83 PERROR("SOFTWARE ERROR: epointlist not in port's list.\n");
90 PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
91 memset(temp, 0, sizeof(struct epoint_list));
97 void Port::free_epointid(unsigned long epoint_id)
99 struct epoint_list *temp, **tempp;
102 tempp = &p_epointlist;
105 if (temp->epoint_id == epoint_id)
113 PERROR("epoint_id not in port's list, exitting.\n");
120 PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
121 memset(temp, 0, sizeof(struct epoint_list));
127 /* create new epointlist relation
129 struct epoint_list *Port::epointlist_new(unsigned long epoint_id)
131 struct epoint_list *epointlist, **epointlistpointer;
133 /* epointlist structure */
134 epointlist = (struct epoint_list *)calloc(1, sizeof(struct epoint_list));
137 PERROR("no mem for allocating epoint_list\n");
141 PDEBUG(DEBUG_EPOINT, "PORT(%d) allocating epoint_list.\n", p_serial);
142 memset(epointlist, 0, sizeof(struct epoint_list));
144 /* add epoint_list to chain */
145 epointlist->next = NULL;
146 epointlistpointer = &p_epointlist;
147 while(*epointlistpointer)
148 epointlistpointer = &((*epointlistpointer)->next);
149 *epointlistpointer = epointlist;
152 epointlist->epoint_id = epoint_id;
153 epointlist->active = 1;
162 Port::Port(int type, char *portname, struct port_settings *settings)
164 class Port *temp, **tempp;
166 PDEBUG(DEBUG_PORT, "new port of type %d, name '%s'\n", type, portname);
168 /* initialize object */
170 memcpy(&p_settings, settings, sizeof(struct port_settings));
173 memset(&p_settings, 0, sizeof(p_settings));
174 SCPY(p_settings.tones_dir, options.tones_dir);
176 SCPY(p_name, portname);
177 SCPY(p_tone_dir, p_settings.tones_dir); // just to be sure
181 p_serial = port_serial++;
182 p_debug_nothingtosend = 0;
184 p_tone_fetched = NULL;
185 p_tone_name[0] = '\0';
186 p_state = PORT_STATE_IDLE;
188 memset(&p_callerinfo, 0, sizeof(p_callerinfo));
189 memset(&p_dialinginfo, 0, sizeof(p_dialinginfo));
190 memset(&p_connectinfo, 0, sizeof(p_connectinfo));
191 memset(&p_redirinfo, 0, sizeof(p_redirinfo));
192 memset(&p_capainfo, 0, sizeof(p_capainfo));
200 p_record_filename[0] = '\0';
201 p_record_buffer_readp = 0;
202 p_record_buffer_writep = 0;
203 p_record_buffer_dir = 0;
205 /* append port to chain */
225 class Port *temp, **tempp;
226 struct message *message;
233 PDEBUG(DEBUG_PORT, "removing port of type %d, name '%s'\n", p_type, p_name);
235 /* disconnect port from endpoint */
238 /* send disconnect */
239 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
240 message->param.disconnectinfo.cause = 16;
241 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
242 message_put(message);
243 /* remove endpoint */
244 free_epointlist(p_epointlist);
247 /* remove port from chain */
259 PERROR("PORT(%s) port not in port's list.\n", p_name);
265 /* close open tones file */
272 p_tone_fetched = NULL;
277 /* set new endpoint state
279 void Port::new_state(int state)
281 PDEBUG(DEBUG_PORT, "PORT(%s) new state %s --> %s\n", p_name, state_name[p_state], state_name[state]);
287 * find the port with port_id
289 class Port *find_port_id(unsigned long port_id)
291 class Port *port = port_first;
295 //printf("comparing: '%s' with '%s'\n", name, port->name);
296 if (port->p_serial == port_id)
308 void Port::set_echotest(int echotest)
310 p_echotest = echotest;
315 * set the file in the tone directory with the given name
317 void Port::set_tone(char *dir, char *name)
325 /* no counter, no eof, normal speed */
329 p_tone_codec = CODEC_LAW;
337 p_tone_fetched = NULL;
343 SPRINT(p_tone_name, "%s", name);
344 p_tone_dir[0] = '\0';
347 SCPY(p_tone_dir, dir);
348 SCPY(p_tone_name, name);
352 p_tone_name[0]= '\0';
357 if (!!strncmp(name,"cause_",6))
360 /* now we check if the cause exists, otherwhise we use error tone. */
361 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, 0, 0)))
363 p_tone_fetched = NULL;
366 SPRINT(filename, "%s_loop", p_tone_name);
367 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, 0, 0)))
369 p_tone_fetched = NULL;
372 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
373 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0)
378 SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name);
379 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0)
385 if (!strcmp(name,"cause_00") || !strcmp(name,"cause_10"))
387 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using release tone\n", p_name, name+6);
388 SPRINT(p_tone_name,"release");
390 if (!strcmp(name,"cause_11"))
392 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using busy tone\n", p_name, name+6);
393 SPRINT(p_tone_name,"busy");
396 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using error tone\n", p_name, name+6);
397 SPRINT(p_tone_name,"error");
403 * set the file in the tone directory for vbox playback
404 * also set the play_eof-flag
406 void Port::set_vbox_tone(char *dir, char *name)
412 p_tone_codec = CODEC_LAW;
421 p_tone_fetched = NULL;
423 SPRINT(p_tone_dir, dir);
424 SPRINT(p_tone_name, name);
426 /* now we check if the cause exists, otherwhise we use error tone. */
429 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, &p_tone_size, &p_tone_left)))
431 PDEBUG(DEBUG_PORT, "PORT(%s) opening fetched tone: %s\n", p_name, p_tone_name);
434 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
435 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0)
438 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
443 SPRINT(filename, "%s", p_tone_name);
444 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0)
447 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
455 * set the file in the given directory for vbox playback
456 * also set the eof-flag
457 * also set the counter-flag
459 void Port::set_vbox_play(char *name, int offset)
462 struct message *message;
464 /* use ser_box_tone() */
465 set_vbox_tone("", name);
475 /* send message with counter value */
476 if (p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist))
478 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
479 message->param.counter.current = offset;
480 message->param.counter.max = size;
481 message_put(message);
488 * set the playback speed (for recording playback with different speeds)
490 void Port::set_vbox_speed(int speed)
492 /* enable vbox play mode */
493 p_tone_speed = speed;
497 * read from the given file as specified in port_set_tone and return sample data
498 * silence is appended if sample ends, but only the number of samples with tones are returned
500 int Port::read_audio(unsigned char *buffer, int length)
503 int nodata=0; /* to detect 0-length files and avoid endless reopen */
505 int tone_left_before; /* temp variable to determine the change in p_tone_left */
513 /* if there is no tone set, use silence */
514 if (p_tone_name[0] == 0)
517 memset(buffer, (options.law=='a')?0x2a:0xff, len); /* silence */
521 /* if the file pointer is not open, we open it */
522 if (p_tone_fh<0 && p_tone_fetched==NULL)
526 SPRINT(filename, "%s", p_tone_name);
527 /* if file does not exist */
528 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left)))
530 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
531 /* if file does not exist */
532 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
534 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
541 SPRINT(filename, "%s", p_tone_name);
542 /* if file does not exist */
543 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
545 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
550 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
554 /* file descriptor is open read data */
555 tone_left_before = p_tone_left;
558 l = read_tone(p_tone_fh, buffer, p_tone_codec, len, p_tone_size, &p_tone_left, p_tone_speed);
559 if (l<0 || l>len) /* paranoia */
566 l = read_tone_fetched(&p_tone_fetched, buffer, len, p_tone_size, &p_tone_left, p_tone_speed);
567 if (l<0 || l>len) /* paranoia */
573 /* if counter is enabled, we check if we have a change */
574 if (p_tone_counter && p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist))
576 /* if we jumed to the next second */
577 if (((p_tone_size-p_tone_left)/8000) != (p_tone_size-tone_left_before)/8000)
579 //printf("\nsize=%d left=%d\n\n",p_tone_size,p_tone_left);
580 struct message *message;
581 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
582 message->param.counter.current = (p_tone_size-p_tone_left)/8000;
583 message->param.counter.max = -1;
584 message_put(message);
597 p_tone_fetched = NULL;
602 /* if the file has 0-length */
605 PDEBUG(DEBUG_PORT, "PORT(%s) 0-length loop: %s\n", p_name, filename);
608 goto rest_is_silence;
611 /* if eof is reached, or if the normal file cannot be opened, continue with the loop file if possible */
613 if (p_tone_eof && ACTIVE_EPOINT(p_epointlist))
615 struct message *message;
616 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_EOF);
617 message_put(message);
622 /* if file does not exist */
623 SPRINT(filename, "%s_loop", p_tone_name);
624 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left)))
626 SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name);
627 /* if file does not exist */
628 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
630 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
631 p_tone_dir[0] = '\0';
632 p_tone_name[0] = '\0';
633 goto rest_is_silence;
639 SPRINT(filename, "%s_loop", p_tone_name);
640 /* if file does not exist */
641 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
643 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
644 p_tone_dir[0] = '\0';
645 p_tone_name[0] = '\0';
646 goto rest_is_silence;
651 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
653 /* now we have opened the loop */
662 * process transmission clock */
663 int Port::handler(void)
668 /* endpoint sends messages to the port
669 * this is called by the message_epoint inherited by child classes
670 * therefor a return=1 means: stop, no more processing
672 //extern struct message *dddebug;
673 int Port::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
675 /* check if we got audio data from one remote port */
678 case MESSAGE_TONE: /* play tone */
679 PDEBUG(DEBUG_PORT, "PORT(%s) isdn port with (caller id %s) setting tone '%s' dir '%s'\n", p_name, p_callerinfo.id, param->tone.name, param->tone.dir);
680 set_tone(param->tone.dir,param->tone.name);
683 case MESSAGE_VBOX_TONE: /* play tone of answering machine */
684 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine tone '%s' '%s'\n", p_name, param->tone.dir, param->tone.name);
685 set_vbox_tone(param->tone.dir, param->tone.name);
688 case MESSAGE_VBOX_PLAY: /* play recording of answering machine */
689 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine file to play '%s' (offset %d seconds)\n", p_name, param->play.file, param->play.offset);
690 set_vbox_play(param->play.file, param->play.offset);
693 case MESSAGE_VBOX_PLAY_SPEED: /* set speed of playback (recording of answering machine) */
694 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine playback speed %d (times)\n", p_name, param->speed);
695 set_vbox_speed(param->speed);
704 /* wave header structure */
706 unsigned short stereo; /* 1 = mono, 2 = stereo */
707 unsigned short channels; /* number of channels */
708 unsigned long sample_rate; /* sample rate */
709 unsigned long data_rate; /* data rate */
710 unsigned short bytes_sample; /* bytes per sample (all channels) */
711 unsigned short bits_sample; /* bits per sample (one channel) */
716 * open record file (actually a wave file with empty header which will be
717 * written before close, because we do not know the size yet)
718 * type=1 record annoucement, type=0 record audio stream, type=2 record vbox
720 int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ignore, char *vbox_email, int vbox_email_file)
722 /* RIFFxxxxWAVEfmt xxxx(fmt-size)dataxxxx... */
723 char dummyheader[8+4+8+sizeof(fmt)+8];
728 PERROR("Port(%d) not an extension\n", p_serial);
731 SCPY(p_record_extension, extension);
732 p_record_anon_ignore = anon_ignore;
733 SCPY(p_record_vbox_email, vbox_email);
734 p_record_vbox_email_file = vbox_email_file;
738 PERROR("Port(%d) already recording\n", p_serial);
743 SPRINT(filename, "%s/%s/%s/vbox", INSTALL_DATA, options.extensions_dir, p_record_extension);
745 SPRINT(filename, "%s/%s/%s/recordings", INSTALL_DATA, options.extensions_dir, p_record_extension);
746 if (mkdir(filename, 0755) < 0)
750 PERROR("Port(%d) cannot create directory '%s'\n", p_serial, filename);
756 UPRINT(strchr(filename,'\0'), "/announcement");
758 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);
761 p_record_vbox_year = now_tm->tm_year;
762 p_record_vbox_mon = now_tm->tm_mon;
763 p_record_vbox_mday = now_tm->tm_mday;
764 p_record_vbox_hour = now_tm->tm_hour;
765 p_record_vbox_min = now_tm->tm_min;
768 /* check, if file exists (especially when an extension calls the same extension) */
770 if ((p_record = fopen(filename, "r")))
773 SCAT(filename, "_2nd");
776 p_record = fopen(filename, "w");
779 PERROR("Port(%d) cannot record because file cannot be opened '%s'\n", p_serial, filename);
784 p_record_type = type;
785 p_record_vbox = vbox;
786 p_record_skip = skip;
788 switch(p_record_type)
793 fwrite(dummyheader, sizeof(dummyheader), 1, p_record);
799 UCPY(p_record_filename, filename);
801 PDEBUG(DEBUG_PORT, "Port(%d) recording started with file name '%s'\n", p_serial, filename);
807 * close the recoding file, put header in front and rename
809 void Port::close_record(int beep)
811 static signed long beep_mono[] = {-10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000};
812 static unsigned char beep_8bit[] = {48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208};
813 unsigned long size, wsize;
815 char filename[512], indexname[512];
818 char number[256], callerid[256];
820 struct caller_info callerinfo;
821 char *valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_.-!$%&/()=+*;~";
825 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);
827 memcpy(&callerinfo, &p_callerinfo, sizeof(struct caller_info));
828 apply_callerid_restriction(p_record_anon_ignore, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.extension, callerinfo.name);
830 SCPY(number, p_dialinginfo.id);
831 SCPY(callerid, numberrize_callerinfo(callerinfo.id, callerinfo.ntype));
832 if (callerid[0] == '\0')
834 if (callerinfo.present == INFO_PRESENT_RESTRICTED)
835 UCPY(callerid,"anonymous");
837 UCPY(callerid,"unknown");
840 /* change verboten digits */
842 while((p=strchr(p,'*')))
845 while((p=strchr(p,'/')))
848 while((p=strchr(p,'*')))
851 while((p=strchr(p,'/')))
854 ii = strlen(callerid);
857 if (!strchr(valid_chars, callerid[i]))
865 if (!strchr(valid_chars, number[i]))
870 /* add beep to the end of recording */
872 switch(p_record_type)
878 fwrite(beep_mono, sizeof(beep_mono), 1, p_record);
879 i += sizeof(beep_mono);
880 p_record_length += sizeof(beep_mono);
887 fwrite(beep_8bit, sizeof(beep_8bit), 1, p_record);
888 i += sizeof(beep_8bit);
889 p_record_length += sizeof(beep_8bit);
897 fwrite(beep_law, sizeof(beep_law), 1, p_record);
898 i += sizeof(beep_law);
899 p_record_length += sizeof(beep_law);
904 PERROR("codec %d not supported for beep adding\n", p_record_type);
907 /* complete header */
908 switch(p_record_type)
914 fprintf(p_record, "cue %c%c%c%c%c%c%c%c", 4, 0, 0, 0, 0,0,0,0);
917 fprintf(p_record, "LIST%c%c%c%cadtl", 4, 0, 0, 0);
920 fseek(p_record, 0, SEEK_SET);
922 /* WAVEfmt xxxx(fmt-size)dataxxxx[data]cue xxxx0000LISTxxxxadtl*/
923 size = p_record_length;
924 wsize = 4+8+sizeof(fmt)+8+size+8+4+8+4;
927 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));
930 fprintf(p_record, "WAVE");
933 fprintf(p_record, "fmt %c%c%c%c", sizeof(fmt), 0, 0, 0);
934 switch(p_record_type)
939 fmt.sample_rate = 8000; /* samples/sec */
940 fmt.data_rate = 16000; /* full data rate */
941 fmt.bytes_sample = 2; /* all channels */
942 fmt.bits_sample = 16; /* one channel */
948 fmt.sample_rate = 8000; /* samples/sec */
949 fmt.data_rate = 32000; /* full data rate */
950 fmt.bytes_sample = 4; /* all channels */
951 fmt.bits_sample = 16; /* one channel */
957 fmt.sample_rate = 8000; /* samples/sec */
958 fmt.data_rate = 8000; /* full data rate */
959 fmt.bytes_sample = 1; /* all channels */
960 fmt.bits_sample = 8; /* one channel */
963 fwrite(&fmt, sizeof(fmt), 1, p_record);
966 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));
969 if (p_record_vbox == 1)
970 SPRINT(filename, "%s.wav", p_record_filename);
972 SPRINT(filename, "%s_%s-%s.wav", p_record_filename, callerid, number);
977 if (p_record_vbox == 1)
978 SPRINT(filename, "%s.isdn", p_record_filename);
980 SPRINT(filename, "%s_%s-%s.isdn", p_record_filename, callerid, number);
988 if (rename(p_record_filename, filename) < 0)
990 PERROR("Port(%d) cannot rename from '%s' to '%s'\n", p_serial, p_record_filename, filename);
994 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);
996 if (p_record_vbox == 2)
998 SPRINT(indexname, "%s/%s/%s/vbox/index", INSTALL_DATA, options.extensions_dir, p_record_extension);
999 if ((fp = fopen(indexname,"a")))
1003 /* remove path from file name */
1005 while(strchr(p, '/'))
1006 p = strchr(p, '/')+1;
1007 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);
1013 PERROR("Port(%d) cannot open index file '%s' to append.\n", p_serial, indexname);
1016 /* send email with sample*/
1017 if (p_record_vbox_email[0])
1019 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);
1026 * recording function
1027 * Records all data from down and from up into one single stream.
1028 * Both streams may have gaps or jitter.
1029 * A Jitter buffer for both streams is used to compensate jitter.
1031 * If one stream (dir) received packets, they are stored to a
1032 * buffer to wait for the other stream (dir), so both streams can
1033 * be combined. If the buffer is full, it's content is written
1034 * without mixing stream. (assuming only one stram (dir) exists.)
1035 * A flag is used to indicate what stream is currently in buffer.
1037 * NOTE: First stereo sample (odd) is from down, second is from up.
1039 void Port::record(unsigned char *data, int length, int dir_fromup)
1041 unsigned char write_buffer[1024], *d;
1047 if (!p_record || !length)
1053 /* more than we have */
1054 if (p_record_skip > length)
1056 p_record_skip -= length;
1059 data += p_record_skip;
1060 length -= p_record_skip;
1063 free = ((p_record_buffer_readp - p_record_buffer_writep - 1) & RECORD_BUFFER_MASK);
1065 /* the buffer stores the same data stream */
1066 if (dir_fromup == p_record_buffer_dir)
1070 /* first write what we can to the buffer */
1071 while(free && length)
1073 p_record_buffer[p_record_buffer_writep] = audio_law_to_s32[*data++];
1074 p_record_buffer_writep = (p_record_buffer_writep + 1) & RECORD_BUFFER_MASK;
1078 /* all written, so we return */
1081 /* still data left, buffer is full, so we need to write to file */
1082 switch(p_record_type)
1085 s = (signed short *)write_buffer;
1089 *s++ = p_record_buffer[p_record_buffer_readp];
1090 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1093 fwrite(write_buffer, 512, 1, p_record);
1097 s = (signed short *)write_buffer;
1098 if (p_record_buffer_dir)
1103 *s++ = 0; /* nothing from down */
1104 *s++ = p_record_buffer[p_record_buffer_readp];
1105 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1113 *s++ = p_record_buffer[p_record_buffer_readp];
1114 *s++ = 0; /* nothing from up */
1115 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1119 fwrite(write_buffer, 1024, 1, p_record);
1127 *d++ = (p_record_buffer[p_record_buffer_readp]+0x8000) >> 8;
1128 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1131 fwrite(write_buffer, 512, 1, p_record);
1139 *d++ = audio_s16_to_law[p_record_buffer[p_record_buffer_readp] & 0xffff];
1140 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1143 fwrite(write_buffer, 256, 1, p_record);
1146 /* because we still have data, we write again */
1147 free += sizeof(write_buffer);
1150 /* the buffer stores the other stream */
1152 /* if buffer empty, change it */
1153 if (p_record_buffer_readp == p_record_buffer_writep)
1155 p_record_buffer_dir = dir_fromup;
1158 /* how much data can we mix ? */
1159 ii = (p_record_buffer_writep - p_record_buffer_readp) & RECORD_BUFFER_MASK;
1162 /* write data mixed with the buffer */
1163 switch(p_record_type)
1166 s = (signed short *)write_buffer;
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;
1180 fwrite(write_buffer, ii<<1, 1, p_record);
1184 s = (signed short *)write_buffer;
1185 if (p_record_buffer_dir)
1190 *s++ = audio_law_to_s32[*data++];
1191 *s++ = p_record_buffer[p_record_buffer_readp];
1192 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1200 *s++ = p_record_buffer[p_record_buffer_readp];
1201 *s++ = audio_law_to_s32[*data++];
1205 fwrite(write_buffer, ii<<2, 1, p_record);
1213 sample = p_record_buffer[p_record_buffer_readp]
1214 + audio_law_to_s32[*data++];
1215 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1220 *d++ = (sample+0x8000) >> 8;
1223 fwrite(write_buffer, ii, 1, p_record);
1231 sample = p_record_buffer[p_record_buffer_readp]
1232 + audio_law_to_s32[*data++];
1233 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1238 *d++ = audio_s16_to_law[sample & 0xffff];
1241 fwrite(write_buffer, ii, 1, p_record);
1245 /* data, but buffer empty */
1248 p_record_buffer_dir = dir_fromup;
1251 /* no data (maybe buffer) */