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 #define SHORT_MIN -32768
61 #define SHORT_MAX 32767
63 class Port *port_first = NULL;
65 unsigned long port_serial = 1; /* must be 1, because 0== no port */
68 /* free epointlist relation
70 void Port::free_epointlist(struct epoint_list *epointlist)
72 struct epoint_list *temp, **tempp;
75 tempp = &p_epointlist;
78 if (temp == epointlist)
86 PERROR("SOFTWARE ERROR: epointlist not in port's list.\n");
93 PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
94 FREE(temp, sizeof(struct epoint_list));
99 void Port::free_epointid(unsigned long epoint_id)
101 struct epoint_list *temp, **tempp;
104 tempp = &p_epointlist;
107 if (temp->epoint_id == epoint_id)
115 PERROR("epoint_id not in port's list.\n");
122 PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
123 FREE(temp, sizeof(struct epoint_list));
128 /* create new epointlist relation
130 struct epoint_list *Port::epointlist_new(unsigned long epoint_id)
132 struct epoint_list *epointlist, **epointlistpointer;
134 /* epointlist structure */
135 epointlist = (struct epoint_list *)MALLOC(sizeof(struct epoint_list));
137 FATAL("No memory for epointlist\n");
139 PDEBUG(DEBUG_EPOINT, "PORT(%d) allocating epoint_list.\n", p_serial);
141 /* add epoint_list to chain */
142 epointlist->next = NULL;
143 epointlistpointer = &p_epointlist;
144 while(*epointlistpointer)
145 epointlistpointer = &((*epointlistpointer)->next);
146 *epointlistpointer = epointlist;
149 epointlist->epoint_id = epoint_id;
150 epointlist->active = 1;
159 Port::Port(int type, char *portname, struct port_settings *settings)
161 class Port *temp, **tempp;
163 PDEBUG(DEBUG_PORT, "new port of type %d, name '%s'\n", type, portname);
165 /* initialize object */
167 memcpy(&p_settings, settings, sizeof(struct port_settings));
170 memset(&p_settings, 0, sizeof(p_settings));
171 SCPY(p_settings.tones_dir, options.tones_dir);
173 SCPY(p_name, portname);
174 SCPY(p_tone_dir, p_settings.tones_dir); // just to be sure
176 p_serial = port_serial++;
178 p_tone_fetched = NULL;
179 p_tone_name[0] = '\0';
180 p_state = PORT_STATE_IDLE;
182 memset(&p_callerinfo, 0, sizeof(p_callerinfo));
183 memset(&p_dialinginfo, 0, sizeof(p_dialinginfo));
184 memset(&p_connectinfo, 0, sizeof(p_connectinfo));
185 memset(&p_redirinfo, 0, sizeof(p_redirinfo));
186 memset(&p_capainfo, 0, sizeof(p_capainfo));
194 p_record_filename[0] = '\0';
195 p_record_buffer_readp = 0;
196 p_record_buffer_writep = 0;
197 p_record_buffer_dir = 0;
199 /* append port to chain */
219 class Port *temp, **tempp;
220 struct message *message;
227 PDEBUG(DEBUG_PORT, "removing port of type %d, name '%s'\n", p_type, p_name);
229 /* disconnect port from endpoint */
232 /* send disconnect */
233 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
234 message->param.disconnectinfo.cause = 16;
235 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
236 message_put(message);
237 /* remove endpoint */
238 free_epointlist(p_epointlist);
241 /* remove port from chain */
252 FATAL("PORT(%s) port not in port's list.\n", p_name);
256 /* close open tones file */
263 p_tone_fetched = NULL;
268 /* set new endpoint state
270 void Port::new_state(int state)
272 PDEBUG(DEBUG_PORT, "PORT(%s) new state %s --> %s\n", p_name, state_name[p_state], state_name[state]);
278 * find the port with port_id
280 class Port *find_port_id(unsigned long port_id)
282 class Port *port = port_first;
286 //printf("comparing: '%s' with '%s'\n", name, port->name);
287 if (port->p_serial == port_id)
299 void Port::set_echotest(int echotest)
301 p_echotest = echotest;
306 * set the file in the tone directory with the given name
308 void Port::set_tone(char *dir, char *name)
316 /* no counter, no eof, normal speed */
320 p_tone_codec = CODEC_LAW;
328 p_tone_fetched = NULL;
334 SPRINT(p_tone_name, "%s", name);
335 p_tone_dir[0] = '\0';
338 SCPY(p_tone_dir, dir);
339 SCPY(p_tone_name, name);
343 p_tone_name[0]= '\0';
348 if (!!strncmp(name,"cause_",6))
351 /* now we check if the cause exists, otherwhise we use error tone. */
352 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, 0, 0)))
354 p_tone_fetched = NULL;
357 SPRINT(filename, "%s_loop", p_tone_name);
358 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, 0, 0)))
360 p_tone_fetched = NULL;
363 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
364 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0)
369 SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name);
370 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0)
376 if (!strcmp(name,"cause_00") || !strcmp(name,"cause_10"))
378 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using release tone\n", p_name, name+6);
379 SPRINT(p_tone_name,"release");
381 if (!strcmp(name,"cause_11"))
383 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using busy tone\n", p_name, name+6);
384 SPRINT(p_tone_name,"busy");
387 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using error tone\n", p_name, name+6);
388 SPRINT(p_tone_name,"error");
394 * set the file in the tone directory for vbox playback
395 * also set the play_eof-flag
397 void Port::set_vbox_tone(char *dir, char *name)
403 p_tone_codec = CODEC_LAW;
412 p_tone_fetched = NULL;
414 SPRINT(p_tone_dir, dir);
415 SPRINT(p_tone_name, name);
417 /* now we check if the cause exists, otherwhise we use error tone. */
420 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, &p_tone_size, &p_tone_left)))
422 PDEBUG(DEBUG_PORT, "PORT(%s) opening fetched tone: %s\n", p_name, p_tone_name);
425 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
426 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0)
429 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
434 SPRINT(filename, "%s", p_tone_name);
435 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0)
438 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
446 * set the file in the given directory for vbox playback
447 * also set the eof-flag
448 * also set the counter-flag
450 void Port::set_vbox_play(char *name, int offset)
453 struct message *message;
455 /* use ser_box_tone() */
456 set_vbox_tone("", name);
466 /* send message with counter value */
467 if (p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist))
469 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
470 message->param.counter.current = offset;
471 message->param.counter.max = size;
472 message_put(message);
479 * set the playback speed (for recording playback with different speeds)
481 void Port::set_vbox_speed(int speed)
483 /* enable vbox play mode */
484 p_tone_speed = speed;
488 * read from the given file as specified in port_set_tone and return sample data
489 * if the tone ends, the result may be less samples than requested
491 int Port::read_audio(unsigned char *buffer, int length)
494 int nodata=0; /* to detect 0-length files and avoid endless reopen */
496 int tone_left_before; /* temp variable to determine the change in p_tone_left */
504 /* if there is no tone set, use silence */
508 /* if the file pointer is not open, we open it */
509 if (p_tone_fh<0 && p_tone_fetched==NULL)
513 SPRINT(filename, "%s", p_tone_name);
514 /* if file does not exist */
515 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left)))
517 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
518 /* if file does not exist */
519 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
521 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
528 SPRINT(filename, "%s", p_tone_name);
529 /* if file does not exist */
530 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
532 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
537 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
541 /* file descriptor is open read data */
542 tone_left_before = p_tone_left;
545 l = read_tone(p_tone_fh, buffer, p_tone_codec, len, p_tone_size, &p_tone_left, p_tone_speed);
546 if (l<0 || l>len) /* paranoia */
553 l = read_tone_fetched(&p_tone_fetched, buffer, len, p_tone_size, &p_tone_left, p_tone_speed);
554 if (l<0 || l>len) /* paranoia */
560 /* if counter is enabled, we check if we have a change */
561 if (p_tone_counter && p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist))
563 /* if we jumed to the next second */
564 if (((p_tone_size-p_tone_left)/8000) != (p_tone_size-tone_left_before)/8000)
566 //printf("\nsize=%d left=%d\n\n",p_tone_size,p_tone_left);
567 struct message *message;
568 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
569 message->param.counter.current = (p_tone_size-p_tone_left)/8000;
570 message->param.counter.max = -1;
571 message_put(message);
584 p_tone_fetched = NULL;
589 /* if the file has 0-length */
592 PDEBUG(DEBUG_PORT, "PORT(%s) 0-length loop: %s\n", p_name, filename);
598 /* if eof is reached, or if the normal file cannot be opened, continue with the loop file if possible */
600 if (p_tone_eof && ACTIVE_EPOINT(p_epointlist))
602 struct message *message;
603 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_EOF);
604 message_put(message);
609 /* if file does not exist */
610 SPRINT(filename, "%s_loop", p_tone_name);
611 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left)))
613 SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name);
614 /* if file does not exist */
615 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
617 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
618 p_tone_dir[0] = '\0';
619 p_tone_name[0] = '\0';
626 SPRINT(filename, "%s_loop", 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';
638 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
640 /* now we have opened the loop */
646 * process transmission clock */
647 int Port::handler(void)
652 /* endpoint sends messages to the port
653 * this is called by the message_epoint inherited by child classes
654 * therefor a return=1 means: stop, no more processing
656 //extern struct message *dddebug;
657 int Port::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
659 /* check if we got audio data from one remote port */
662 case MESSAGE_TONE: /* play tone */
663 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);
664 set_tone(param->tone.dir,param->tone.name);
667 case MESSAGE_VBOX_TONE: /* play tone of answering machine */
668 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine tone '%s' '%s'\n", p_name, param->tone.dir, param->tone.name);
669 set_vbox_tone(param->tone.dir, param->tone.name);
672 case MESSAGE_VBOX_PLAY: /* play recording of answering machine */
673 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine file to play '%s' (offset %d seconds)\n", p_name, param->play.file, param->play.offset);
674 set_vbox_play(param->play.file, param->play.offset);
677 case MESSAGE_VBOX_PLAY_SPEED: /* set speed of playback (recording of answering machine) */
678 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine playback speed %d (times)\n", p_name, param->speed);
679 set_vbox_speed(param->speed);
688 /* wave header structure */
690 unsigned short stereo; /* 1 = mono, 2 = stereo */
691 unsigned short channels; /* number of channels */
692 unsigned long sample_rate; /* sample rate */
693 unsigned long data_rate; /* data rate */
694 unsigned short bytes_sample; /* bytes per sample (all channels) */
695 unsigned short bits_sample; /* bits per sample (one channel) */
700 * open record file (actually a wave file with empty header which will be
701 * written before close, because we do not know the size yet)
702 * type=1 record annoucement, type=0 record audio stream, type=2 record vbox
704 int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ignore, char *vbox_email, int vbox_email_file)
706 /* RIFFxxxxWAVEfmt xxxx(fmt-size)dataxxxx... */
707 char dummyheader[8+4+8+sizeof(fmt)+8];
712 PERROR("Port(%d) not an extension\n", p_serial);
715 SCPY(p_record_extension, extension);
716 p_record_anon_ignore = anon_ignore;
717 SCPY(p_record_vbox_email, vbox_email);
718 p_record_vbox_email_file = vbox_email_file;
722 PERROR("Port(%d) already recording\n", p_serial);
727 SPRINT(filename, "%s/%s/%s/vbox", INSTALL_DATA, options.extensions_dir, p_record_extension);
729 SPRINT(filename, "%s/%s/%s/recordings", INSTALL_DATA, options.extensions_dir, p_record_extension);
730 if (mkdir(filename, 0755) < 0)
734 PERROR("Port(%d) cannot create directory '%s'\n", p_serial, filename);
740 UPRINT(strchr(filename,'\0'), "/announcement");
742 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);
745 p_record_vbox_year = now_tm->tm_year;
746 p_record_vbox_mon = now_tm->tm_mon;
747 p_record_vbox_mday = now_tm->tm_mday;
748 p_record_vbox_hour = now_tm->tm_hour;
749 p_record_vbox_min = now_tm->tm_min;
752 /* check, if file exists (especially when an extension calls the same extension) */
754 if ((p_record = fopen(filename, "r")))
757 SCAT(filename, "_2nd");
760 p_record = fopen(filename, "w");
763 PERROR("Port(%d) cannot record because file cannot be opened '%s'\n", p_serial, filename);
768 p_record_type = type;
769 p_record_vbox = vbox;
770 p_record_skip = skip;
772 switch(p_record_type)
777 fwrite(dummyheader, sizeof(dummyheader), 1, p_record);
783 UCPY(p_record_filename, filename);
785 PDEBUG(DEBUG_PORT, "Port(%d) recording started with file name '%s'\n", p_serial, filename);
791 * close the recoding file, put header in front and rename
793 void Port::close_record(int beep)
795 static signed long beep_mono[] = {-10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000};
796 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};
797 unsigned long size, wsize;
799 char filename[512], indexname[512];
802 char number[256], callerid[256];
804 struct caller_info callerinfo;
805 char *valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_.-!$%&/()=+*;~";
809 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);
811 memcpy(&callerinfo, &p_callerinfo, sizeof(struct caller_info));
812 apply_callerid_restriction(p_record_anon_ignore, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.extension, callerinfo.name);
814 SCPY(number, p_dialinginfo.id);
815 SCPY(callerid, numberrize_callerinfo(callerinfo.id, callerinfo.ntype));
816 if (callerid[0] == '\0')
818 if (callerinfo.present == INFO_PRESENT_RESTRICTED)
819 UCPY(callerid,"anonymous");
821 UCPY(callerid,"unknown");
824 /* change verboten digits */
826 while((p=strchr(p,'*')))
829 while((p=strchr(p,'/')))
832 while((p=strchr(p,'*')))
835 while((p=strchr(p,'/')))
838 ii = strlen(callerid);
841 if (!strchr(valid_chars, callerid[i]))
849 if (!strchr(valid_chars, number[i]))
854 /* add beep to the end of recording */
856 switch(p_record_type)
862 fwrite(beep_mono, sizeof(beep_mono), 1, p_record);
863 i += sizeof(beep_mono);
864 p_record_length += sizeof(beep_mono);
871 fwrite(beep_8bit, sizeof(beep_8bit), 1, p_record);
872 i += sizeof(beep_8bit);
873 p_record_length += sizeof(beep_8bit);
881 fwrite(beep_law, sizeof(beep_law), 1, p_record);
882 i += sizeof(beep_law);
883 p_record_length += sizeof(beep_law);
888 PERROR("codec %d not supported for beep adding\n", p_record_type);
891 /* complete header */
892 switch(p_record_type)
898 fprintf(p_record, "cue %c%c%c%c%c%c%c%c", 4, 0, 0, 0, 0,0,0,0);
901 fprintf(p_record, "LIST%c%c%c%cadtl", 4, 0, 0, 0);
904 fseek(p_record, 0, SEEK_SET);
906 /* WAVEfmt xxxx(fmt-size)dataxxxx[data]cue xxxx0000LISTxxxxadtl*/
907 size = p_record_length;
908 wsize = 4+8+sizeof(fmt)+8+size+8+4+8+4;
911 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));
914 fprintf(p_record, "WAVE");
917 fprintf(p_record, "fmt %c%c%c%c", sizeof(fmt), 0, 0, 0);
918 switch(p_record_type)
923 fmt.sample_rate = 8000; /* samples/sec */
924 fmt.data_rate = 16000; /* full data rate */
925 fmt.bytes_sample = 2; /* all channels */
926 fmt.bits_sample = 16; /* one channel */
932 fmt.sample_rate = 8000; /* samples/sec */
933 fmt.data_rate = 32000; /* full data rate */
934 fmt.bytes_sample = 4; /* all channels */
935 fmt.bits_sample = 16; /* one channel */
941 fmt.sample_rate = 8000; /* samples/sec */
942 fmt.data_rate = 8000; /* full data rate */
943 fmt.bytes_sample = 1; /* all channels */
944 fmt.bits_sample = 8; /* one channel */
947 fwrite(&fmt, sizeof(fmt), 1, p_record);
950 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));
953 if (p_record_vbox == 1)
954 SPRINT(filename, "%s.wav", p_record_filename);
956 SPRINT(filename, "%s_%s-%s.wav", p_record_filename, callerid, number);
961 if (p_record_vbox == 1)
962 SPRINT(filename, "%s.isdn", p_record_filename);
964 SPRINT(filename, "%s_%s-%s.isdn", p_record_filename, callerid, number);
972 if (rename(p_record_filename, filename) < 0)
974 PERROR("Port(%d) cannot rename from '%s' to '%s'\n", p_serial, p_record_filename, filename);
978 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);
980 if (p_record_vbox == 2)
982 SPRINT(indexname, "%s/%s/%s/vbox/index", INSTALL_DATA, options.extensions_dir, p_record_extension);
983 if ((fp = fopen(indexname,"a")))
987 /* remove path from file name */
989 while(strchr(p, '/'))
990 p = strchr(p, '/')+1;
991 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);
997 PERROR("Port(%d) cannot open index file '%s' to append.\n", p_serial, indexname);
1000 /* send email with sample*/
1001 if (p_record_vbox_email[0])
1003 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);
1010 * recording function
1011 * Records all data from down and from up into one single stream.
1012 * Both streams may have gaps or jitter.
1013 * A Jitter buffer for both streams is used to compensate jitter.
1015 * If one stream (dir) received packets, they are stored to a
1016 * buffer to wait for the other stream (dir), so both streams can
1017 * be combined. If the buffer is full, it's content is written
1018 * without mixing stream. (assuming only one stram (dir) exists.)
1019 * A flag is used to indicate what stream is currently in buffer.
1021 * NOTE: First stereo sample (odd) is from down, second is from up.
1023 void Port::record(unsigned char *data, int length, int dir_fromup)
1025 unsigned char write_buffer[1024], *d;
1031 if (!p_record || !length)
1037 /* more than we have */
1038 if (p_record_skip > length)
1040 p_record_skip -= length;
1043 data += p_record_skip;
1044 length -= p_record_skip;
1047 free = ((p_record_buffer_readp - p_record_buffer_writep - 1) & RECORD_BUFFER_MASK);
1049 //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);
1051 /* the buffer stores the same data stream */
1052 if (dir_fromup == p_record_buffer_dir)
1056 /* first write what we can to the buffer */
1057 while(free && length)
1059 p_record_buffer[p_record_buffer_writep] = audio_law_to_s32[*data++];
1060 p_record_buffer_writep = (p_record_buffer_writep + 1) & RECORD_BUFFER_MASK;
1064 /* all written, so we return */
1067 /* still data left, buffer is full, so we need to write to file */
1068 switch(p_record_type)
1071 s = (signed short *)write_buffer;
1075 *s++ = p_record_buffer[p_record_buffer_readp];
1076 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1079 fwrite(write_buffer, 512, 1, p_record);
1083 s = (signed short *)write_buffer;
1084 if (p_record_buffer_dir)
1089 *s++ = 0; /* nothing from down */
1090 *s++ = p_record_buffer[p_record_buffer_readp];
1091 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1099 *s++ = p_record_buffer[p_record_buffer_readp];
1100 *s++ = 0; /* nothing from up */
1101 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1105 fwrite(write_buffer, 1024, 1, p_record);
1113 *d++ = (p_record_buffer[p_record_buffer_readp]+0x8000) >> 8;
1114 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1117 fwrite(write_buffer, 512, 1, p_record);
1125 *d++ = audio_s16_to_law[p_record_buffer[p_record_buffer_readp] & 0xffff];
1126 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1129 fwrite(write_buffer, 256, 1, p_record);
1132 /* because we still have data, we write again */
1133 free += sizeof(write_buffer);
1136 /* the buffer stores the other stream */
1138 /* if buffer empty, change it */
1139 if (p_record_buffer_readp == p_record_buffer_writep)
1141 p_record_buffer_dir = dir_fromup;
1144 /* how much data can we mix ? */
1145 ii = (p_record_buffer_writep - p_record_buffer_readp) & RECORD_BUFFER_MASK;
1148 //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);
1150 /* write data mixed with the buffer */
1151 switch(p_record_type)
1154 s = (signed short *)write_buffer;
1158 sample = p_record_buffer[p_record_buffer_readp]
1159 + audio_law_to_s32[*data++];
1160 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1161 if (sample < SHORT_MIN) sample = SHORT_MIN;
1162 if (sample > SHORT_MAX) sample = SHORT_MAX;
1166 fwrite(write_buffer, ii<<1, 1, p_record);
1170 s = (signed short *)write_buffer;
1171 if (p_record_buffer_dir)
1176 *s++ = audio_law_to_s32[*data++];
1177 *s++ = p_record_buffer[p_record_buffer_readp];
1178 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1186 *s++ = p_record_buffer[p_record_buffer_readp];
1187 *s++ = audio_law_to_s32[*data++];
1188 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1192 fwrite(write_buffer, ii<<2, 1, p_record);
1200 sample = p_record_buffer[p_record_buffer_readp]
1201 + audio_law_to_s32[*data++];
1202 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1203 if (sample < SHORT_MIN) sample = SHORT_MIN;
1204 if (sample > SHORT_MAX) sample = SHORT_MAX;
1205 *d++ = (sample+0x8000) >> 8;
1208 fwrite(write_buffer, ii, 1, p_record);
1216 sample = p_record_buffer[p_record_buffer_readp]
1217 + audio_law_to_s32[*data++];
1218 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1219 if (sample < SHORT_MIN) sample = SHORT_MIN;
1220 if (sample > SHORT_MAX) sample = SHORT_MAX;
1221 *d++ = audio_s16_to_law[sample & 0xffff];
1224 fwrite(write_buffer, ii, 1, p_record);
1228 /* data, but buffer empty */
1231 p_record_buffer_dir = dir_fromup;
1234 /* no data (maybe buffer) */