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 */
58 /* free epointlist relation
60 void Port::free_epointlist(struct epoint_list *epointlist)
62 struct epoint_list *temp, **tempp;
65 tempp = &p_epointlist;
67 if (temp == epointlist)
74 PERROR("SOFTWARE ERROR: epointlist not in port's list.\n");
81 PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
82 FREE(temp, sizeof(struct epoint_list));
87 void Port::free_epointid(unsigned int epoint_id)
89 struct epoint_list *temp, **tempp;
92 tempp = &p_epointlist;
94 if (temp->epoint_id == epoint_id)
101 PERROR("epoint_id not in port's list.\n");
108 PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
109 FREE(temp, sizeof(struct epoint_list));
114 /* create new epointlist relation
116 struct epoint_list *Port::epointlist_new(unsigned int epoint_id)
118 struct epoint_list *epointlist, **epointlistpointer;
120 /* epointlist structure */
121 epointlist = (struct epoint_list *)MALLOC(sizeof(struct epoint_list));
123 FATAL("No memory for epointlist\n");
125 PDEBUG(DEBUG_EPOINT, "PORT(%d) allocating epoint_list.\n", p_serial);
127 /* add epoint_list to chain */
128 epointlist->next = NULL;
129 epointlistpointer = &p_epointlist;
130 while(*epointlistpointer)
131 epointlistpointer = &((*epointlistpointer)->next);
132 *epointlistpointer = epointlist;
135 epointlist->epoint_id = epoint_id;
136 epointlist->active = 1;
145 Port::Port(int type, const char *portname, struct port_settings *settings)
147 class Port *temp, **tempp;
149 PDEBUG(DEBUG_PORT, "new port of type %d, name '%s'\n", type, portname);
151 /* initialize object */
153 memcpy(&p_settings, settings, sizeof(struct port_settings));
155 memset(&p_settings, 0, sizeof(p_settings));
157 SCPY(p_name, portname);
158 p_tone_dir[0] = '\0';
160 p_serial = port_serial++;
162 p_tone_fetched = NULL;
163 p_tone_name[0] = '\0';
164 p_state = PORT_STATE_IDLE;
166 memset(&p_callerinfo, 0, sizeof(p_callerinfo));
167 memset(&p_dialinginfo, 0, sizeof(p_dialinginfo));
168 memset(&p_connectinfo, 0, sizeof(p_connectinfo));
169 memset(&p_redirinfo, 0, sizeof(p_redirinfo));
170 memset(&p_capainfo, 0, sizeof(p_capainfo));
178 p_record_filename[0] = '\0';
179 p_record_buffer_readp = 0;
180 p_record_buffer_writep = 0;
181 p_record_buffer_dir = 0;
183 /* append port to chain */
202 class Port *temp, **tempp;
203 struct lcr_msg *message;
210 PDEBUG(DEBUG_PORT, "removing port of type %d, name '%s'\n", p_type, p_name);
212 /* disconnect port from endpoint */
213 while(p_epointlist) {
214 /* send disconnect */
215 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
216 message->param.disconnectinfo.cause = 16;
217 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
218 message_put(message);
219 /* remove endpoint */
220 free_epointlist(p_epointlist);
223 /* remove port from chain */
233 FATAL("PORT(%s) port not in port's list.\n", p_name);
237 /* close open tones file */
238 if (p_tone_fh >= 0) {
243 p_tone_fetched = NULL;
248 /* set new endpoint state
250 void Port::new_state(int state)
252 PDEBUG(DEBUG_PORT, "PORT(%s) new state %s --> %s\n", p_name, state_name[p_state], state_name[state]);
258 * find the port with port_id
260 class Port *find_port_id(unsigned int port_id)
262 class Port *port = port_first;
265 //printf("comparing: '%s' with '%s'\n", name, port->name);
266 if (port->p_serial == port_id)
278 void Port::set_echotest(int echotest)
280 p_echotest = echotest;
285 * set the file in the tone directory with the given name
287 void Port::set_tone(const char *dir, const char *name)
296 dir = options.tones_dir; /* just in case we have no PmISDN instance */
298 /* no counter, no eof, normal speed */
302 p_tone_codec = CODEC_LAW;
304 if (p_tone_fh >= 0) {
309 p_tone_fetched = NULL;
312 if (name[0] == '/') {
313 SPRINT(p_tone_name, "%s", name);
314 p_tone_dir[0] = '\0';
316 SCPY(p_tone_dir, dir);
317 SCPY(p_tone_name, name);
319 /* trigger playback */
322 p_tone_name[0]= '\0';
327 if (!!strncmp(name,"cause_",6))
330 /* now we check if the cause exists, otherwhise we use error tone. */
331 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, 0, 0))) {
332 p_tone_fetched = NULL;
335 SPRINT(filename, "%s_loop", p_tone_name);
336 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, 0, 0))) {
337 p_tone_fetched = NULL;
340 SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
341 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0) {
345 SPRINT(filename, "%s/%s/%s_loop", SHARE_DATA, p_tone_dir, p_tone_name);
346 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0) {
351 if (!strcmp(name,"cause_00") || !strcmp(name,"cause_10")) {
352 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using release tone\n", p_name, name+6);
353 SPRINT(p_tone_name,"release");
355 if (!strcmp(name,"cause_11")) {
356 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using busy tone\n", p_name, name+6);
357 SPRINT(p_tone_name,"busy");
359 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using error tone\n", p_name, name+6);
360 SPRINT(p_tone_name,"error");
366 * set the file in the tone directory for vbox playback
367 * also set the play_eof-flag
369 void Port::set_vbox_tone(const char *dir, const char *name)
375 p_tone_codec = CODEC_LAW;
378 if (p_tone_fh >= 0) {
383 p_tone_fetched = NULL;
385 SPRINT(p_tone_dir, dir);
386 SPRINT(p_tone_name, name);
387 /* trigger playback */
390 /* now we check if the cause exists, otherwhise we use error tone. */
392 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, &p_tone_size, &p_tone_left))) {
393 PDEBUG(DEBUG_PORT, "PORT(%s) opening fetched tone: %s\n", p_name, p_tone_name);
396 SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
397 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0) {
399 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
403 SPRINT(filename, "%s", p_tone_name);
404 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0) {
406 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
414 * set the file in the given directory for vbox playback
415 * also set the eof-flag
416 * also set the counter-flag
418 void Port::set_vbox_play(const char *name, int offset)
420 struct lcr_msg *message;
422 /* use ser_box_tone() */
423 set_vbox_tone("", name);
431 if (p_tone_name[0]) {
432 /* send message with counter value */
433 if (p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist)) {
434 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
435 message->param.counter.current = offset;
436 message->param.counter.max = p_tone_size;
437 message_put(message);
444 * set the playback speed (for recording playback with different speeds)
446 void Port::set_vbox_speed(int speed)
448 /* enable vbox play mode */
449 p_tone_speed = speed;
453 * read from the given file as specified in port_set_tone and return sample data
454 * if the tone ends, the result may be less samples than requested
456 int Port::read_audio(unsigned char *buffer, int length)
459 int nodata=0; /* to detect 0-length files and avoid endless reopen */
461 int tone_left_before; /* temp variable to determine the change in p_tone_left */
469 /* if there is no tone set, use silence */
473 /* if the file pointer is not open, we open it */
474 if (p_tone_fh<0 && p_tone_fetched==NULL) {
476 SPRINT(filename, "%s", p_tone_name);
477 /* if file does not exist */
478 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left))) {
479 SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
480 /* if file does not exist */
481 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
482 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
488 SPRINT(filename, "%s", 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);
496 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
500 /* file descriptor is open read data */
501 tone_left_before = p_tone_left;
502 if (p_tone_fh >= 0) {
503 l = read_tone(p_tone_fh, buffer, p_tone_codec, len, p_tone_size, &p_tone_left, p_tone_speed);
504 if (l<0 || l>len) /* paranoia */
509 if (p_tone_fetched) {
510 l = read_tone_fetched(&p_tone_fetched, buffer, len, p_tone_size, &p_tone_left, p_tone_speed);
511 if (l<0 || l>len) /* paranoia */
517 /* if counter is enabled, we check if we have a change */
518 if (p_tone_counter && p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist)) {
519 /* if we jumed to the next second */
520 if (((p_tone_size-p_tone_left)/8000) != (p_tone_size-tone_left_before)/8000) {
521 //printf("\nsize=%d left=%d\n\n",p_tone_size,p_tone_left);
522 struct lcr_msg *message;
523 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
524 message->param.counter.current = (p_tone_size-p_tone_left)/8000;
525 message->param.counter.max = -1;
526 message_put(message);
533 if (p_tone_fh >= 0) {
538 p_tone_fetched = NULL;
543 /* if the file has 0-length */
545 PDEBUG(DEBUG_PORT, "PORT(%s) 0-length loop: %s\n", p_name, filename);
551 /* if eof is reached, or if the normal file cannot be opened, continue with the loop file if possible */
553 if (p_tone_eof && ACTIVE_EPOINT(p_epointlist)) {
554 struct lcr_msg *message;
555 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_EOF);
556 message_put(message);
560 /* if file does not exist */
561 SPRINT(filename, "%s_loop", p_tone_name);
562 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left))) {
563 SPRINT(filename, "%s/%s/%s_loop", SHARE_DATA, p_tone_dir, p_tone_name);
564 /* if file does not exist */
565 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
566 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
567 p_tone_dir[0] = '\0';
568 p_tone_name[0] = '\0';
574 SPRINT(filename, "%s_loop", p_tone_name);
575 /* if file does not exist */
576 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
577 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
578 p_tone_dir[0] = '\0';
579 p_tone_name[0] = '\0';
585 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
587 /* now we have opened the loop */
592 /* endpoint sends messages to the port
593 * this is called by the message_epoint inherited by child classes
594 * therefor a return=1 means: stop, no more processing
596 //extern struct lcr_msg *dddebug;
597 int Port::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
599 /* check if we got audio data from one remote port */
601 case MESSAGE_TONE: /* play tone */
602 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);
603 set_tone(param->tone.dir,param->tone.name);
606 case MESSAGE_VBOX_TONE: /* play tone of answering machine */
607 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine tone '%s' '%s'\n", p_name, param->tone.dir, param->tone.name);
608 set_vbox_tone(param->tone.dir, param->tone.name);
611 case MESSAGE_VBOX_PLAY: /* play recording of answering machine */
612 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine file to play '%s' (offset %d seconds)\n", p_name, param->play.file, param->play.offset);
613 set_vbox_play(param->play.file, param->play.offset);
616 case MESSAGE_VBOX_PLAY_SPEED: /* set speed of playback (recording of answering machine) */
617 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine playback speed %d (times)\n", p_name, param->speed);
618 set_vbox_speed(param->speed);
627 /* wave header structure */
629 unsigned short stereo; /* 1 = mono, 2 = stereo */
630 unsigned short channels; /* number of channels */
631 unsigned int sample_rate; /* sample rate */
632 unsigned int data_rate; /* data rate */
633 unsigned short bytes_sample; /* bytes per sample (all channels) */
634 unsigned short bits_sample; /* bits per sample (one channel) */
639 * open record file (actually a wave file with empty header which will be
640 * written before close, because we do not know the size yet)
641 * type=1 record annoucement, type=0 record audio stream, type=2 record vbox
643 int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ignore, const char *vbox_email, int vbox_email_file)
645 /* RIFFxxxxWAVEfmt xxxx(fmt-size)dataxxxx... */
646 char dummyheader[8+4+8+sizeof(fmt)+8];
653 PERROR("Port(%d) not an extension\n", p_serial);
656 SCPY(p_record_extension, extension);
657 p_record_anon_ignore = anon_ignore;
658 SCPY(p_record_vbox_email, vbox_email);
659 p_record_vbox_email_file = vbox_email_file;
662 PERROR("Port(%d) already recording\n", p_serial);
667 SPRINT(filename, "%s/%s/vbox", EXTENSION_DATA, p_record_extension);
669 SPRINT(filename, "%s/%s/recordings", EXTENSION_DATA, p_record_extension);
670 if (mkdir(filename, 0755) < 0) {
671 if (errno != EEXIST) {
672 PERROR("Port(%d) cannot create directory '%s'\n", p_serial, filename);
678 UPRINT(strchr(filename,'\0'), "/announcement");
681 now_tm = localtime(&now);
682 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);
685 p_record_vbox_year = now_tm->tm_year;
686 p_record_vbox_mon = now_tm->tm_mon;
687 p_record_vbox_mday = now_tm->tm_mday;
688 p_record_vbox_hour = now_tm->tm_hour;
689 p_record_vbox_min = now_tm->tm_min;
692 /* check, if file exists (especially when an extension calls the same extension) */
694 if ((p_record = fopen(filename, "r"))) {
696 SCAT(filename, "_2nd");
699 p_record = fopen(filename, "w");
701 PERROR("Port(%d) cannot record because file cannot be opened '%s'\n", p_serial, filename);
707 p_record_type = type;
708 p_record_vbox = vbox;
709 p_record_skip = skip;
711 switch(p_record_type) {
715 ret = fwrite(dummyheader, sizeof(dummyheader), 1, p_record);
721 UCPY(p_record_filename, filename);
723 PDEBUG(DEBUG_PORT, "Port(%d) recording started with file name '%s'\n", p_serial, filename);
729 * close the recoding file, put header in front and rename
731 void Port::close_record(int beep, int mute)
733 static signed short beep_mono[256];
734 unsigned int size = 0, wsize = 0;
736 char filename[512], indexname[512];
739 char number[256], callerid[256];
741 struct caller_info callerinfo;
742 const char *valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_.-!$%&/()=+*;~";
747 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);
749 memcpy(&callerinfo, &p_callerinfo, sizeof(struct caller_info));
750 // apply_callerid_restriction(p_record_anon_ignore, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.extension, callerinfo.name);
752 SCPY(number, p_dialinginfo.id);
753 SCPY(callerid, numberrize_callerinfo(callerinfo.id, callerinfo.ntype, options.national, options.international));
754 if (callerid[0] == '\0') {
755 if (callerinfo.present == INFO_PRESENT_RESTRICTED)
756 UCPY(callerid,"anonymous");
758 UCPY(callerid,"unknown");
761 /* change verboten digits */
763 while((p=strchr(p,'*')))
766 while((p=strchr(p,'/')))
769 while((p=strchr(p,'*')))
772 while((p=strchr(p,'/')))
775 ii = strlen(callerid);
777 if (!strchr(valid_chars, callerid[i]))
784 if (!strchr(valid_chars, number[i]))
790 if (mute && p_record_type==CODEC_MONO) {
794 fseek(p_record, -(i<<1), SEEK_END);
795 p_record_length -= (i<<1);
797 /* add beep to the end of recording */
798 if (beep && p_record_type==CODEC_MONO) {
801 beep_mono[i] = (signed short)(sin((double)i / 5.688888888889 * 2.0 * 3.1415927) * 2000.0);
806 ret = fwrite(beep_mono, sizeof(beep_mono), 1, p_record);
807 i += sizeof(beep_mono);
808 p_record_length += sizeof(beep_mono);
812 /* complete header */
813 switch(p_record_type) {
818 fprintf(p_record, "cue %c%c%c%c%c%c%c%c", 4, 0, 0, 0, 0,0,0,0);
821 fprintf(p_record, "LIST%c%c%c%cadtl", 4, 0, 0, 0);
824 fseek(p_record, 0, SEEK_SET);
826 /* WAVEfmt xxxx(fmt-size)dataxxxx[data]cue xxxx0000LISTxxxxadtl*/
827 size = p_record_length;
828 wsize = 4+8+sizeof(fmt)+8+size+8+4+8+4;
831 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));
834 fprintf(p_record, "WAVE");
837 fprintf(p_record, "fmt %c%c%c%c", (unsigned int)sizeof(fmt), 0, 0, 0);
838 switch(p_record_type) {
842 fmt.sample_rate = 8000; /* samples/sec */
843 fmt.data_rate = 16000; /* full data rate */
844 fmt.bytes_sample = 2; /* all channels */
845 fmt.bits_sample = 16; /* one channel */
851 fmt.sample_rate = 8000; /* samples/sec */
852 fmt.data_rate = 32000; /* full data rate */
853 fmt.bytes_sample = 4; /* all channels */
854 fmt.bits_sample = 16; /* one channel */
860 fmt.sample_rate = 8000; /* samples/sec */
861 fmt.data_rate = 8000; /* full data rate */
862 fmt.bytes_sample = 1; /* all channels */
863 fmt.bits_sample = 8; /* one channel */
866 ret = fwrite(&fmt, sizeof(fmt), 1, p_record);
869 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));
872 if (p_record_vbox == 1)
873 SPRINT(filename, "%s.wav", p_record_filename);
875 SPRINT(filename, "%s_%s-%s.wav", p_record_filename, callerid, number);
880 if (p_record_vbox == 1)
881 SPRINT(filename, "%s.isdn", p_record_filename);
883 SPRINT(filename, "%s_%s-%s.isdn", p_record_filename, callerid, number);
892 if (rename(p_record_filename, filename) < 0) {
893 PERROR("Port(%d) cannot rename from '%s' to '%s'\n", p_serial, p_record_filename, filename);
897 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);
899 if (p_record_vbox == 2) {
900 SPRINT(indexname, "%s/%s/vbox/index", EXTENSION_DATA, p_record_extension);
901 if ((fp = fopen(indexname,"a"))) {
904 /* remove path from file name */
906 while(strchr(p, '/'))
907 p = strchr(p, '/')+1;
908 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);
913 PERROR("Port(%d) cannot open index file '%s' to append.\n", p_serial, indexname);
916 /* send email with sample*/
917 if (p_record_vbox_email[0]) {
918 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);
926 * Records all data from down and from up into one single stream.
927 * Both streams may have gaps or jitter.
928 * A Jitter buffer for both streams is used to compensate jitter.
930 * If one stream (dir) received packets, they are stored to a
931 * buffer to wait for the other stream (dir), so both streams can
932 * be combined. If the buffer is full, it's content is written
933 * without mixing stream. (assuming only one stram (dir) exists.)
934 * A flag is used to indicate what stream is currently in buffer.
936 * NOTE: First stereo sample (odd) is from down, second is from up.
938 void Port::record(unsigned char *data, int length, int dir_fromup)
940 unsigned char write_buffer[1024], *d;
947 if (!p_record || !length)
950 /* skip data from local caller (dtmf input) */
951 if (p_record_skip && !dir_fromup) {
952 /* more to skip than we have */
953 if (p_record_skip > length) {
954 p_record_skip -= length;
958 data += p_record_skip;
959 length -= p_record_skip;
963 //printf("dir=%d len=%d\n", dir_fromup, length);
965 free = ((p_record_buffer_readp - p_record_buffer_writep - 1) & RECORD_BUFFER_MASK);
967 //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);
969 /* the buffer stores the same data stream */
970 if (dir_fromup == p_record_buffer_dir) {
973 //printf("same free=%d length=%d\n", free, length);
974 /* first write what we can to the buffer */
975 while(free && length) {
976 p_record_buffer[p_record_buffer_writep] = audio_law_to_s32[*data++];
977 p_record_buffer_writep = (p_record_buffer_writep + 1) & RECORD_BUFFER_MASK;
981 /* all written, so we return */
984 /* still data left, buffer is full, so we need to write a chunk to file */
985 switch(p_record_type) {
987 s = (signed short *)write_buffer;
990 *s++ = p_record_buffer[p_record_buffer_readp];
991 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
994 ret = fwrite(write_buffer, 512, 1, p_record);
995 p_record_length += 512;
999 s = (signed short *)write_buffer;
1000 if (p_record_buffer_dir) {
1003 *s++ = 0; /* nothing from down */
1004 *s++ = p_record_buffer[p_record_buffer_readp];
1005 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1011 *s++ = p_record_buffer[p_record_buffer_readp];
1012 *s++ = 0; /* nothing from up */
1013 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1017 ret = fwrite(write_buffer, 1024, 1, p_record);
1018 p_record_length += 1024;
1025 *d++ = ((unsigned short)(p_record_buffer[p_record_buffer_readp]+0x8000)) >> 8;
1026 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1029 ret = fwrite(write_buffer, 512, 1, p_record);
1030 p_record_length += 512;
1037 *d++ = audio_s16_to_law[p_record_buffer[p_record_buffer_readp] & 0xffff];
1038 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1041 ret = fwrite(write_buffer, 256, 1, p_record);
1042 p_record_length += 256;
1045 /* because we still have data, we write again */
1049 /* the buffer stores the other stream */
1052 /* if buffer empty, change it */
1053 if (p_record_buffer_readp == p_record_buffer_writep) {
1054 p_record_buffer_dir = dir_fromup;
1057 /* how much data can we mix ? */
1058 ii = (p_record_buffer_writep - p_record_buffer_readp) & RECORD_BUFFER_MASK;
1064 //printf("same ii=%d length=%d\n", ii, length);
1065 //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);
1067 /* write data mixed with the buffer */
1068 switch(p_record_type) {
1070 s = (signed short *)write_buffer;
1073 sample = p_record_buffer[p_record_buffer_readp]
1074 + audio_law_to_s32[*data++];
1075 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1076 if (sample < SHORT_MIN) sample = SHORT_MIN;
1077 if (sample > SHORT_MAX) sample = SHORT_MAX;
1081 ret = fwrite(write_buffer, ii<<1, 1, p_record);
1082 p_record_length += (ii<<1);
1086 s = (signed short *)write_buffer;
1087 if (p_record_buffer_dir) {
1090 *s++ = audio_law_to_s32[*data++];
1091 *s++ = p_record_buffer[p_record_buffer_readp];
1092 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1098 *s++ = p_record_buffer[p_record_buffer_readp];
1099 *s++ = audio_law_to_s32[*data++];
1100 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1104 ret = fwrite(write_buffer, ii<<2, 1, p_record);
1105 p_record_length += (ii<<2);
1112 sample = p_record_buffer[p_record_buffer_readp]
1113 + audio_law_to_s32[*data++];
1114 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1115 if (sample < SHORT_MIN) sample = SHORT_MIN;
1116 if (sample > SHORT_MAX) sample = SHORT_MAX;
1117 *d++ = (sample+0x8000) >> 8;
1120 ret = fwrite(write_buffer, ii, 1, p_record);
1121 p_record_length += ii;
1128 sample = p_record_buffer[p_record_buffer_readp]
1129 + audio_law_to_s32[*data++];
1130 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1131 if (sample < SHORT_MIN) sample = SHORT_MIN;
1132 if (sample > SHORT_MAX) sample = SHORT_MAX;
1133 *d++ = audio_s16_to_law[sample & 0xffff];
1136 ret = fwrite(write_buffer, ii, 1, p_record);
1137 p_record_length += ii;
1143 goto different_again;
1144 /* no data (maybe buffer) */
1149 void Port::update_rxoff(void)
1153 void Port::update_load(void)