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
51 #include <sys/types.h>
58 #define SHORT_MIN -32768
59 #define SHORT_MAX 32767
61 class Port *port_first = NULL;
63 unsigned long port_serial = 1; /* must be 1, because 0== no port */
66 /* free epointlist relation
68 void Port::free_epointlist(struct epoint_list *epointlist)
70 struct epoint_list *temp, **tempp;
73 tempp = &p_epointlist;
76 if (temp == epointlist)
84 PERROR("SOFTWARE ERROR: epointlist not in port's list.\n");
91 PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
92 FREE(temp, 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.\n");
120 PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
121 FREE(temp, sizeof(struct epoint_list));
126 /* create new epointlist relation
128 struct epoint_list *Port::epointlist_new(unsigned long epoint_id)
130 struct epoint_list *epointlist, **epointlistpointer;
132 /* epointlist structure */
133 epointlist = (struct epoint_list *)MALLOC(sizeof(struct epoint_list));
135 FATAL("No memory for epointlist\n");
137 PDEBUG(DEBUG_EPOINT, "PORT(%d) allocating epoint_list.\n", p_serial);
139 /* add epoint_list to chain */
140 epointlist->next = NULL;
141 epointlistpointer = &p_epointlist;
142 while(*epointlistpointer)
143 epointlistpointer = &((*epointlistpointer)->next);
144 *epointlistpointer = epointlist;
147 epointlist->epoint_id = epoint_id;
148 epointlist->active = 1;
157 Port::Port(int type, char *portname, struct port_settings *settings)
159 class Port *temp, **tempp;
161 PDEBUG(DEBUG_PORT, "new port of type %d, name '%s'\n", type, portname);
163 /* initialize object */
165 memcpy(&p_settings, settings, sizeof(struct port_settings));
168 memset(&p_settings, 0, sizeof(p_settings));
169 SCPY(p_settings.tones_dir, options.tones_dir);
171 SCPY(p_name, portname);
172 SCPY(p_tone_dir, p_settings.tones_dir); // just to be sure
174 p_serial = port_serial++;
176 p_tone_fetched = NULL;
177 p_tone_name[0] = '\0';
178 p_state = PORT_STATE_IDLE;
180 memset(&p_callerinfo, 0, sizeof(p_callerinfo));
181 memset(&p_dialinginfo, 0, sizeof(p_dialinginfo));
182 memset(&p_connectinfo, 0, sizeof(p_connectinfo));
183 memset(&p_redirinfo, 0, sizeof(p_redirinfo));
184 memset(&p_capainfo, 0, sizeof(p_capainfo));
192 p_record_filename[0] = '\0';
193 p_record_buffer_readp = 0;
194 p_record_buffer_writep = 0;
195 p_record_buffer_dir = 0;
197 /* append port to chain */
217 class Port *temp, **tempp;
218 struct message *message;
225 PDEBUG(DEBUG_PORT, "removing port of type %d, name '%s'\n", p_type, p_name);
227 /* disconnect port from endpoint */
230 /* send disconnect */
231 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
232 message->param.disconnectinfo.cause = 16;
233 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
234 message_put(message);
235 /* remove endpoint */
236 free_epointlist(p_epointlist);
239 /* remove port from chain */
250 FATAL("PORT(%s) port not in port's list.\n", p_name);
254 /* close open tones file */
261 p_tone_fetched = NULL;
266 /* set new endpoint state
268 void Port::new_state(int state)
270 PDEBUG(DEBUG_PORT, "PORT(%s) new state %s --> %s\n", p_name, state_name[p_state], state_name[state]);
276 * find the port with port_id
278 class Port *find_port_id(unsigned long port_id)
280 class Port *port = port_first;
284 //printf("comparing: '%s' with '%s'\n", name, port->name);
285 if (port->p_serial == port_id)
297 void Port::set_echotest(int echotest)
299 p_echotest = echotest;
304 * set the file in the tone directory with the given name
306 void Port::set_tone(char *dir, char *name)
314 /* no counter, no eof, normal speed */
318 p_tone_codec = CODEC_LAW;
326 p_tone_fetched = NULL;
332 SPRINT(p_tone_name, "%s", name);
333 p_tone_dir[0] = '\0';
336 SCPY(p_tone_dir, dir);
337 SCPY(p_tone_name, name);
341 p_tone_name[0]= '\0';
346 if (!!strncmp(name,"cause_",6))
349 /* now we check if the cause exists, otherwhise we use error tone. */
350 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, 0, 0)))
352 p_tone_fetched = NULL;
355 SPRINT(filename, "%s_loop", p_tone_name);
356 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, 0, 0)))
358 p_tone_fetched = NULL;
361 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
362 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0)
367 SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name);
368 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0)
374 if (!strcmp(name,"cause_00") || !strcmp(name,"cause_10"))
376 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using release tone\n", p_name, name+6);
377 SPRINT(p_tone_name,"release");
379 if (!strcmp(name,"cause_11"))
381 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using busy tone\n", p_name, name+6);
382 SPRINT(p_tone_name,"busy");
385 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using error tone\n", p_name, name+6);
386 SPRINT(p_tone_name,"error");
392 * set the file in the tone directory for vbox playback
393 * also set the play_eof-flag
395 void Port::set_vbox_tone(char *dir, char *name)
401 p_tone_codec = CODEC_LAW;
410 p_tone_fetched = NULL;
412 SPRINT(p_tone_dir, dir);
413 SPRINT(p_tone_name, name);
415 /* now we check if the cause exists, otherwhise we use error tone. */
418 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, &p_tone_size, &p_tone_left)))
420 PDEBUG(DEBUG_PORT, "PORT(%s) opening fetched tone: %s\n", p_name, p_tone_name);
423 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
424 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0)
427 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
432 SPRINT(filename, "%s", p_tone_name);
433 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0)
436 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
444 * set the file in the given directory for vbox playback
445 * also set the eof-flag
446 * also set the counter-flag
448 void Port::set_vbox_play(char *name, int offset)
451 struct message *message;
453 /* use ser_box_tone() */
454 set_vbox_tone("", name);
464 /* send message with counter value */
465 if (p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist))
467 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
468 message->param.counter.current = offset;
469 message->param.counter.max = size;
470 message_put(message);
477 * set the playback speed (for recording playback with different speeds)
479 void Port::set_vbox_speed(int speed)
481 /* enable vbox play mode */
482 p_tone_speed = speed;
486 * read from the given file as specified in port_set_tone and return sample data
487 * if the tone ends, the result may be less samples than requested
489 int Port::read_audio(unsigned char *buffer, int length)
492 int nodata=0; /* to detect 0-length files and avoid endless reopen */
494 int tone_left_before; /* temp variable to determine the change in p_tone_left */
502 /* if there is no tone set, use silence */
506 /* if the file pointer is not open, we open it */
507 if (p_tone_fh<0 && p_tone_fetched==NULL)
511 SPRINT(filename, "%s", p_tone_name);
512 /* if file does not exist */
513 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left)))
515 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
516 /* if file does not exist */
517 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
519 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
526 SPRINT(filename, "%s", p_tone_name);
527 /* if file does not exist */
528 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
530 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
535 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
539 /* file descriptor is open read data */
540 tone_left_before = p_tone_left;
543 l = read_tone(p_tone_fh, buffer, p_tone_codec, len, p_tone_size, &p_tone_left, p_tone_speed);
544 if (l<0 || l>len) /* paranoia */
551 l = read_tone_fetched(&p_tone_fetched, buffer, len, p_tone_size, &p_tone_left, p_tone_speed);
552 if (l<0 || l>len) /* paranoia */
558 /* if counter is enabled, we check if we have a change */
559 if (p_tone_counter && p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist))
561 /* if we jumed to the next second */
562 if (((p_tone_size-p_tone_left)/8000) != (p_tone_size-tone_left_before)/8000)
564 //printf("\nsize=%d left=%d\n\n",p_tone_size,p_tone_left);
565 struct message *message;
566 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
567 message->param.counter.current = (p_tone_size-p_tone_left)/8000;
568 message->param.counter.max = -1;
569 message_put(message);
582 p_tone_fetched = NULL;
587 /* if the file has 0-length */
590 PDEBUG(DEBUG_PORT, "PORT(%s) 0-length loop: %s\n", p_name, filename);
596 /* if eof is reached, or if the normal file cannot be opened, continue with the loop file if possible */
598 if (p_tone_eof && ACTIVE_EPOINT(p_epointlist))
600 struct message *message;
601 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_EOF);
602 message_put(message);
607 /* if file does not exist */
608 SPRINT(filename, "%s_loop", p_tone_name);
609 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left)))
611 SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name);
612 /* if file does not exist */
613 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
615 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
616 p_tone_dir[0] = '\0';
617 p_tone_name[0] = '\0';
624 SPRINT(filename, "%s_loop", p_tone_name);
625 /* if file does not exist */
626 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
628 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
629 p_tone_dir[0] = '\0';
630 p_tone_name[0] = '\0';
636 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
638 /* now we have opened the loop */
644 * process transmission clock */
645 int Port::handler(void)
650 /* endpoint sends messages to the port
651 * this is called by the message_epoint inherited by child classes
652 * therefor a return=1 means: stop, no more processing
654 //extern struct message *dddebug;
655 int Port::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
657 /* check if we got audio data from one remote port */
660 case MESSAGE_TONE: /* play tone */
661 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);
662 set_tone(param->tone.dir,param->tone.name);
665 case MESSAGE_VBOX_TONE: /* play tone of answering machine */
666 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine tone '%s' '%s'\n", p_name, param->tone.dir, param->tone.name);
667 set_vbox_tone(param->tone.dir, param->tone.name);
670 case MESSAGE_VBOX_PLAY: /* play recording of answering machine */
671 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine file to play '%s' (offset %d seconds)\n", p_name, param->play.file, param->play.offset);
672 set_vbox_play(param->play.file, param->play.offset);
675 case MESSAGE_VBOX_PLAY_SPEED: /* set speed of playback (recording of answering machine) */
676 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine playback speed %d (times)\n", p_name, param->speed);
677 set_vbox_speed(param->speed);
686 /* wave header structure */
688 unsigned short stereo; /* 1 = mono, 2 = stereo */
689 unsigned short channels; /* number of channels */
690 unsigned long sample_rate; /* sample rate */
691 unsigned long data_rate; /* data rate */
692 unsigned short bytes_sample; /* bytes per sample (all channels) */
693 unsigned short bits_sample; /* bits per sample (one channel) */
698 * open record file (actually a wave file with empty header which will be
699 * written before close, because we do not know the size yet)
700 * type=1 record annoucement, type=0 record audio stream, type=2 record vbox
702 int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ignore, char *vbox_email, int vbox_email_file)
704 /* RIFFxxxxWAVEfmt xxxx(fmt-size)dataxxxx... */
705 char dummyheader[8+4+8+sizeof(fmt)+8];
710 PERROR("Port(%d) not an extension\n", p_serial);
713 SCPY(p_record_extension, extension);
714 p_record_anon_ignore = anon_ignore;
715 SCPY(p_record_vbox_email, vbox_email);
716 p_record_vbox_email_file = vbox_email_file;
720 PERROR("Port(%d) already recording\n", p_serial);
725 SPRINT(filename, "%s/%s/%s/vbox", INSTALL_DATA, options.extensions_dir, p_record_extension);
727 SPRINT(filename, "%s/%s/%s/recordings", INSTALL_DATA, options.extensions_dir, p_record_extension);
728 if (mkdir(filename, 0755) < 0)
732 PERROR("Port(%d) cannot create directory '%s'\n", p_serial, filename);
738 UPRINT(strchr(filename,'\0'), "/announcement");
740 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);
743 p_record_vbox_year = now_tm->tm_year;
744 p_record_vbox_mon = now_tm->tm_mon;
745 p_record_vbox_mday = now_tm->tm_mday;
746 p_record_vbox_hour = now_tm->tm_hour;
747 p_record_vbox_min = now_tm->tm_min;
750 /* check, if file exists (especially when an extension calls the same extension) */
752 if ((p_record = fopen(filename, "r")))
755 SCAT(filename, "_2nd");
758 p_record = fopen(filename, "w");
761 PERROR("Port(%d) cannot record because file cannot be opened '%s'\n", p_serial, filename);
766 p_record_type = type;
767 p_record_vbox = vbox;
768 p_record_skip = skip;
770 switch(p_record_type)
775 fwrite(dummyheader, sizeof(dummyheader), 1, p_record);
781 UCPY(p_record_filename, filename);
783 PDEBUG(DEBUG_PORT, "Port(%d) recording started with file name '%s'\n", p_serial, filename);
789 * close the recoding file, put header in front and rename
791 void Port::close_record(int beep)
793 static signed long beep_mono[] = {-10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000};
794 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};
795 unsigned long size, wsize;
797 char filename[512], indexname[512];
800 char number[256], callerid[256];
802 struct caller_info callerinfo;
803 char *valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_.-!$%&/()=+*;~";
807 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);
809 memcpy(&callerinfo, &p_callerinfo, sizeof(struct caller_info));
810 apply_callerid_restriction(p_record_anon_ignore, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.extension, callerinfo.name);
812 SCPY(number, p_dialinginfo.id);
813 SCPY(callerid, numberrize_callerinfo(callerinfo.id, callerinfo.ntype));
814 if (callerid[0] == '\0')
816 if (callerinfo.present == INFO_PRESENT_RESTRICTED)
817 UCPY(callerid,"anonymous");
819 UCPY(callerid,"unknown");
822 /* change verboten digits */
824 while((p=strchr(p,'*')))
827 while((p=strchr(p,'/')))
830 while((p=strchr(p,'*')))
833 while((p=strchr(p,'/')))
836 ii = strlen(callerid);
839 if (!strchr(valid_chars, callerid[i]))
847 if (!strchr(valid_chars, number[i]))
852 /* add beep to the end of recording */
854 switch(p_record_type)
860 fwrite(beep_mono, sizeof(beep_mono), 1, p_record);
861 i += sizeof(beep_mono);
862 p_record_length += sizeof(beep_mono);
869 fwrite(beep_8bit, sizeof(beep_8bit), 1, p_record);
870 i += sizeof(beep_8bit);
871 p_record_length += sizeof(beep_8bit);
879 fwrite(beep_law, sizeof(beep_law), 1, p_record);
880 i += sizeof(beep_law);
881 p_record_length += sizeof(beep_law);
886 PERROR("codec %d not supported for beep adding\n", p_record_type);
889 /* complete header */
890 switch(p_record_type)
896 fprintf(p_record, "cue %c%c%c%c%c%c%c%c", 4, 0, 0, 0, 0,0,0,0);
899 fprintf(p_record, "LIST%c%c%c%cadtl", 4, 0, 0, 0);
902 fseek(p_record, 0, SEEK_SET);
904 /* WAVEfmt xxxx(fmt-size)dataxxxx[data]cue xxxx0000LISTxxxxadtl*/
905 size = p_record_length;
906 wsize = 4+8+sizeof(fmt)+8+size+8+4+8+4;
909 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));
912 fprintf(p_record, "WAVE");
915 fprintf(p_record, "fmt %c%c%c%c", sizeof(fmt), 0, 0, 0);
916 switch(p_record_type)
921 fmt.sample_rate = 8000; /* samples/sec */
922 fmt.data_rate = 16000; /* full data rate */
923 fmt.bytes_sample = 2; /* all channels */
924 fmt.bits_sample = 16; /* one channel */
930 fmt.sample_rate = 8000; /* samples/sec */
931 fmt.data_rate = 32000; /* full data rate */
932 fmt.bytes_sample = 4; /* all channels */
933 fmt.bits_sample = 16; /* one channel */
939 fmt.sample_rate = 8000; /* samples/sec */
940 fmt.data_rate = 8000; /* full data rate */
941 fmt.bytes_sample = 1; /* all channels */
942 fmt.bits_sample = 8; /* one channel */
945 fwrite(&fmt, sizeof(fmt), 1, p_record);
948 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));
951 if (p_record_vbox == 1)
952 SPRINT(filename, "%s.wav", p_record_filename);
954 SPRINT(filename, "%s_%s-%s.wav", p_record_filename, callerid, number);
959 if (p_record_vbox == 1)
960 SPRINT(filename, "%s.isdn", p_record_filename);
962 SPRINT(filename, "%s_%s-%s.isdn", p_record_filename, callerid, number);
970 if (rename(p_record_filename, filename) < 0)
972 PERROR("Port(%d) cannot rename from '%s' to '%s'\n", p_serial, p_record_filename, filename);
976 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);
978 if (p_record_vbox == 2)
980 SPRINT(indexname, "%s/%s/%s/vbox/index", INSTALL_DATA, options.extensions_dir, p_record_extension);
981 if ((fp = fopen(indexname,"a")))
985 /* remove path from file name */
987 while(strchr(p, '/'))
988 p = strchr(p, '/')+1;
989 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);
995 PERROR("Port(%d) cannot open index file '%s' to append.\n", p_serial, indexname);
998 /* send email with sample*/
999 if (p_record_vbox_email[0])
1001 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);
1008 * recording function
1009 * Records all data from down and from up into one single stream.
1010 * Both streams may have gaps or jitter.
1011 * A Jitter buffer for both streams is used to compensate jitter.
1013 * If one stream (dir) received packets, they are stored to a
1014 * buffer to wait for the other stream (dir), so both streams can
1015 * be combined. If the buffer is full, it's content is written
1016 * without mixing stream. (assuming only one stram (dir) exists.)
1017 * A flag is used to indicate what stream is currently in buffer.
1019 * NOTE: First stereo sample (odd) is from down, second is from up.
1021 void Port::record(unsigned char *data, int length, int dir_fromup)
1023 unsigned char write_buffer[1024], *d;
1029 if (!p_record || !length)
1032 /* skip data from local caller (dtmf input) */
1033 if (p_record_skip && !dir_fromup)
1035 /* more to skip than we have */
1036 if (p_record_skip > length)
1038 p_record_skip -= length;
1042 data += p_record_skip;
1043 length -= p_record_skip;
1047 //printf("dir=%d len=%d\n", dir_fromup, length);
1049 free = ((p_record_buffer_readp - p_record_buffer_writep - 1) & RECORD_BUFFER_MASK);
1051 //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);
1053 /* the buffer stores the same data stream */
1054 if (dir_fromup == p_record_buffer_dir)
1058 //printf("same free=%d length=%d\n", free, length);
1059 /* first write what we can to the buffer */
1060 while(free && length)
1062 p_record_buffer[p_record_buffer_writep] = audio_law_to_s32[*data++];
1063 p_record_buffer_writep = (p_record_buffer_writep + 1) & RECORD_BUFFER_MASK;
1067 /* all written, so we return */
1070 /* still data left, buffer is full, so we need to write a chunk to file */
1071 switch(p_record_type)
1074 s = (signed short *)write_buffer;
1078 *s++ = p_record_buffer[p_record_buffer_readp];
1079 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1082 fwrite(write_buffer, 512, 1, p_record);
1083 p_record_length += 512;
1087 s = (signed short *)write_buffer;
1088 if (p_record_buffer_dir)
1093 *s++ = 0; /* nothing from down */
1094 *s++ = p_record_buffer[p_record_buffer_readp];
1095 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1103 *s++ = p_record_buffer[p_record_buffer_readp];
1104 *s++ = 0; /* nothing from up */
1105 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1109 fwrite(write_buffer, 1024, 1, p_record);
1110 p_record_length += 1024;
1118 *d++ = ((unsigned short)(p_record_buffer[p_record_buffer_readp]+0x8000)) >> 8;
1119 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1122 fwrite(write_buffer, 512, 1, p_record);
1123 p_record_length += 512;
1131 *d++ = audio_s16_to_law[p_record_buffer[p_record_buffer_readp] & 0xffff];
1132 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1135 fwrite(write_buffer, 256, 1, p_record);
1136 p_record_length += 256;
1139 /* because we still have data, we write again */
1143 /* the buffer stores the other stream */
1146 /* if buffer empty, change it */
1147 if (p_record_buffer_readp == p_record_buffer_writep)
1149 p_record_buffer_dir = dir_fromup;
1152 /* how much data can we mix ? */
1153 ii = (p_record_buffer_writep - p_record_buffer_readp) & RECORD_BUFFER_MASK;
1159 //printf("same ii=%d length=%d\n", ii, length);
1160 //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);
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;
1173 if (sample < SHORT_MIN) sample = SHORT_MIN;
1174 if (sample > SHORT_MAX) sample = SHORT_MAX;
1178 fwrite(write_buffer, ii<<1, 1, p_record);
1179 p_record_length += (ii<<1);
1183 s = (signed short *)write_buffer;
1184 if (p_record_buffer_dir)
1189 *s++ = audio_law_to_s32[*data++];
1190 *s++ = p_record_buffer[p_record_buffer_readp];
1191 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1199 *s++ = p_record_buffer[p_record_buffer_readp];
1200 *s++ = audio_law_to_s32[*data++];
1201 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1205 fwrite(write_buffer, ii<<2, 1, p_record);
1206 p_record_length += (ii<<2);
1214 sample = p_record_buffer[p_record_buffer_readp]
1215 + audio_law_to_s32[*data++];
1216 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1217 if (sample < SHORT_MIN) sample = SHORT_MIN;
1218 if (sample > SHORT_MAX) sample = SHORT_MAX;
1219 *d++ = (sample+0x8000) >> 8;
1222 fwrite(write_buffer, ii, 1, p_record);
1223 p_record_length += ii;
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;
1234 if (sample < SHORT_MIN) sample = SHORT_MIN;
1235 if (sample > SHORT_MAX) sample = SHORT_MAX;
1236 *d++ = audio_s16_to_law[sample & 0xffff];
1239 fwrite(write_buffer, ii, 1, p_record);
1240 p_record_length += ii;
1246 goto different_again;
1247 /* no data (maybe buffer) */