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 FREE(temp, sizeof(struct epoint_list));
96 void Port::free_epointid(unsigned long epoint_id)
98 struct epoint_list *temp, **tempp;
101 tempp = &p_epointlist;
104 if (temp->epoint_id == epoint_id)
112 PERROR("epoint_id not in port's list.\n");
119 PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
120 FREE(temp, sizeof(struct epoint_list));
125 /* create new epointlist relation
127 struct epoint_list *Port::epointlist_new(unsigned long epoint_id)
129 struct epoint_list *epointlist, **epointlistpointer;
131 /* epointlist structure */
132 epointlist = (struct epoint_list *)MALLOC(sizeof(struct epoint_list));
134 FATAL("No memory for epointlist\n");
136 PDEBUG(DEBUG_EPOINT, "PORT(%d) allocating epoint_list.\n", p_serial);
138 /* add epoint_list to chain */
139 epointlist->next = NULL;
140 epointlistpointer = &p_epointlist;
141 while(*epointlistpointer)
142 epointlistpointer = &((*epointlistpointer)->next);
143 *epointlistpointer = epointlist;
146 epointlist->epoint_id = epoint_id;
147 epointlist->active = 1;
156 Port::Port(int type, char *portname, struct port_settings *settings)
158 class Port *temp, **tempp;
160 PDEBUG(DEBUG_PORT, "new port of type %d, name '%s'\n", type, portname);
162 /* initialize object */
164 memcpy(&p_settings, settings, sizeof(struct port_settings));
167 memset(&p_settings, 0, sizeof(p_settings));
168 SCPY(p_settings.tones_dir, options.tones_dir);
170 SCPY(p_name, portname);
171 SCPY(p_tone_dir, p_settings.tones_dir); // just to be sure
173 p_serial = port_serial++;
175 p_tone_fetched = NULL;
176 p_tone_name[0] = '\0';
177 p_state = PORT_STATE_IDLE;
179 memset(&p_callerinfo, 0, sizeof(p_callerinfo));
180 memset(&p_dialinginfo, 0, sizeof(p_dialinginfo));
181 memset(&p_connectinfo, 0, sizeof(p_connectinfo));
182 memset(&p_redirinfo, 0, sizeof(p_redirinfo));
183 memset(&p_capainfo, 0, sizeof(p_capainfo));
191 p_record_filename[0] = '\0';
192 p_record_buffer_readp = 0;
193 p_record_buffer_writep = 0;
194 p_record_buffer_dir = 0;
196 /* append port to chain */
216 class Port *temp, **tempp;
217 struct message *message;
224 PDEBUG(DEBUG_PORT, "removing port of type %d, name '%s'\n", p_type, p_name);
226 /* disconnect port from endpoint */
229 /* send disconnect */
230 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
231 message->param.disconnectinfo.cause = 16;
232 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
233 message_put(message);
234 /* remove endpoint */
235 free_epointlist(p_epointlist);
238 /* remove port from chain */
249 FATAL("PORT(%s) port not in port's list.\n", p_name);
253 /* close open tones file */
260 p_tone_fetched = NULL;
265 /* set new endpoint state
267 void Port::new_state(int state)
269 PDEBUG(DEBUG_PORT, "PORT(%s) new state %s --> %s\n", p_name, state_name[p_state], state_name[state]);
275 * find the port with port_id
277 class Port *find_port_id(unsigned long port_id)
279 class Port *port = port_first;
283 //printf("comparing: '%s' with '%s'\n", name, port->name);
284 if (port->p_serial == port_id)
296 void Port::set_echotest(int echotest)
298 p_echotest = echotest;
303 * set the file in the tone directory with the given name
305 void Port::set_tone(char *dir, char *name)
313 /* no counter, no eof, normal speed */
317 p_tone_codec = CODEC_LAW;
325 p_tone_fetched = NULL;
331 SPRINT(p_tone_name, "%s", name);
332 p_tone_dir[0] = '\0';
335 SCPY(p_tone_dir, dir);
336 SCPY(p_tone_name, name);
340 p_tone_name[0]= '\0';
345 if (!!strncmp(name,"cause_",6))
348 /* now we check if the cause exists, otherwhise we use error tone. */
349 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, 0, 0)))
351 p_tone_fetched = NULL;
354 SPRINT(filename, "%s_loop", p_tone_name);
355 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, 0, 0)))
357 p_tone_fetched = NULL;
360 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
361 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0)
366 SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name);
367 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0)
373 if (!strcmp(name,"cause_00") || !strcmp(name,"cause_10"))
375 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using release tone\n", p_name, name+6);
376 SPRINT(p_tone_name,"release");
378 if (!strcmp(name,"cause_11"))
380 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using busy tone\n", p_name, name+6);
381 SPRINT(p_tone_name,"busy");
384 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using error tone\n", p_name, name+6);
385 SPRINT(p_tone_name,"error");
391 * set the file in the tone directory for vbox playback
392 * also set the play_eof-flag
394 void Port::set_vbox_tone(char *dir, char *name)
400 p_tone_codec = CODEC_LAW;
409 p_tone_fetched = NULL;
411 SPRINT(p_tone_dir, dir);
412 SPRINT(p_tone_name, name);
414 /* now we check if the cause exists, otherwhise we use error tone. */
417 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, &p_tone_size, &p_tone_left)))
419 PDEBUG(DEBUG_PORT, "PORT(%s) opening fetched tone: %s\n", p_name, p_tone_name);
422 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
423 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0)
426 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
431 SPRINT(filename, "%s", p_tone_name);
432 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0)
435 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
443 * set the file in the given directory for vbox playback
444 * also set the eof-flag
445 * also set the counter-flag
447 void Port::set_vbox_play(char *name, int offset)
450 struct message *message;
452 /* use ser_box_tone() */
453 set_vbox_tone("", name);
463 /* send message with counter value */
464 if (p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist))
466 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
467 message->param.counter.current = offset;
468 message->param.counter.max = size;
469 message_put(message);
476 * set the playback speed (for recording playback with different speeds)
478 void Port::set_vbox_speed(int speed)
480 /* enable vbox play mode */
481 p_tone_speed = speed;
485 * read from the given file as specified in port_set_tone and return sample data
486 * if the tone ends, the result may be less samples than requested
488 int Port::read_audio(unsigned char *buffer, int length)
491 int nodata=0; /* to detect 0-length files and avoid endless reopen */
493 int tone_left_before; /* temp variable to determine the change in p_tone_left */
501 /* if there is no tone set, use silence */
505 /* if the file pointer is not open, we open it */
506 if (p_tone_fh<0 && p_tone_fetched==NULL)
510 SPRINT(filename, "%s", p_tone_name);
511 /* if file does not exist */
512 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left)))
514 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
515 /* if file does not exist */
516 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
518 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
525 SPRINT(filename, "%s", p_tone_name);
526 /* if file does not exist */
527 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
529 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
534 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
538 /* file descriptor is open read data */
539 tone_left_before = p_tone_left;
542 l = read_tone(p_tone_fh, buffer, p_tone_codec, len, p_tone_size, &p_tone_left, p_tone_speed);
543 if (l<0 || l>len) /* paranoia */
550 l = read_tone_fetched(&p_tone_fetched, buffer, len, p_tone_size, &p_tone_left, p_tone_speed);
551 if (l<0 || l>len) /* paranoia */
557 /* if counter is enabled, we check if we have a change */
558 if (p_tone_counter && p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist))
560 /* if we jumed to the next second */
561 if (((p_tone_size-p_tone_left)/8000) != (p_tone_size-tone_left_before)/8000)
563 //printf("\nsize=%d left=%d\n\n",p_tone_size,p_tone_left);
564 struct message *message;
565 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
566 message->param.counter.current = (p_tone_size-p_tone_left)/8000;
567 message->param.counter.max = -1;
568 message_put(message);
581 p_tone_fetched = NULL;
586 /* if the file has 0-length */
589 PDEBUG(DEBUG_PORT, "PORT(%s) 0-length loop: %s\n", p_name, filename);
595 /* if eof is reached, or if the normal file cannot be opened, continue with the loop file if possible */
597 if (p_tone_eof && ACTIVE_EPOINT(p_epointlist))
599 struct message *message;
600 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_EOF);
601 message_put(message);
606 /* if file does not exist */
607 SPRINT(filename, "%s_loop", p_tone_name);
608 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left)))
610 SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name);
611 /* if file does not exist */
612 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
614 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
615 p_tone_dir[0] = '\0';
616 p_tone_name[0] = '\0';
623 SPRINT(filename, "%s_loop", p_tone_name);
624 /* if file does not exist */
625 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
627 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
628 p_tone_dir[0] = '\0';
629 p_tone_name[0] = '\0';
635 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
637 /* now we have opened the loop */
643 * process transmission clock */
644 int Port::handler(void)
649 /* endpoint sends messages to the port
650 * this is called by the message_epoint inherited by child classes
651 * therefor a return=1 means: stop, no more processing
653 //extern struct message *dddebug;
654 int Port::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
656 /* check if we got audio data from one remote port */
659 case MESSAGE_TONE: /* play tone */
660 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);
661 set_tone(param->tone.dir,param->tone.name);
664 case MESSAGE_VBOX_TONE: /* play tone of answering machine */
665 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine tone '%s' '%s'\n", p_name, param->tone.dir, param->tone.name);
666 set_vbox_tone(param->tone.dir, param->tone.name);
669 case MESSAGE_VBOX_PLAY: /* play recording of answering machine */
670 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine file to play '%s' (offset %d seconds)\n", p_name, param->play.file, param->play.offset);
671 set_vbox_play(param->play.file, param->play.offset);
674 case MESSAGE_VBOX_PLAY_SPEED: /* set speed of playback (recording of answering machine) */
675 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine playback speed %d (times)\n", p_name, param->speed);
676 set_vbox_speed(param->speed);
685 /* wave header structure */
687 unsigned short stereo; /* 1 = mono, 2 = stereo */
688 unsigned short channels; /* number of channels */
689 unsigned long sample_rate; /* sample rate */
690 unsigned long data_rate; /* data rate */
691 unsigned short bytes_sample; /* bytes per sample (all channels) */
692 unsigned short bits_sample; /* bits per sample (one channel) */
697 * open record file (actually a wave file with empty header which will be
698 * written before close, because we do not know the size yet)
699 * type=1 record annoucement, type=0 record audio stream, type=2 record vbox
701 int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ignore, char *vbox_email, int vbox_email_file)
703 /* RIFFxxxxWAVEfmt xxxx(fmt-size)dataxxxx... */
704 char dummyheader[8+4+8+sizeof(fmt)+8];
709 PERROR("Port(%d) not an extension\n", p_serial);
712 SCPY(p_record_extension, extension);
713 p_record_anon_ignore = anon_ignore;
714 SCPY(p_record_vbox_email, vbox_email);
715 p_record_vbox_email_file = vbox_email_file;
719 PERROR("Port(%d) already recording\n", p_serial);
724 SPRINT(filename, "%s/%s/%s/vbox", INSTALL_DATA, options.extensions_dir, p_record_extension);
726 SPRINT(filename, "%s/%s/%s/recordings", INSTALL_DATA, options.extensions_dir, p_record_extension);
727 if (mkdir(filename, 0755) < 0)
731 PERROR("Port(%d) cannot create directory '%s'\n", p_serial, filename);
737 UPRINT(strchr(filename,'\0'), "/announcement");
739 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);
742 p_record_vbox_year = now_tm->tm_year;
743 p_record_vbox_mon = now_tm->tm_mon;
744 p_record_vbox_mday = now_tm->tm_mday;
745 p_record_vbox_hour = now_tm->tm_hour;
746 p_record_vbox_min = now_tm->tm_min;
749 /* check, if file exists (especially when an extension calls the same extension) */
751 if ((p_record = fopen(filename, "r")))
754 SCAT(filename, "_2nd");
757 p_record = fopen(filename, "w");
760 PERROR("Port(%d) cannot record because file cannot be opened '%s'\n", p_serial, filename);
765 p_record_type = type;
766 p_record_vbox = vbox;
767 p_record_skip = skip;
769 switch(p_record_type)
774 fwrite(dummyheader, sizeof(dummyheader), 1, p_record);
780 UCPY(p_record_filename, filename);
782 PDEBUG(DEBUG_PORT, "Port(%d) recording started with file name '%s'\n", p_serial, filename);
788 * close the recoding file, put header in front and rename
790 void Port::close_record(int beep)
792 static signed long beep_mono[] = {-10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000};
793 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};
794 unsigned long size, wsize;
796 char filename[512], indexname[512];
799 char number[256], callerid[256];
801 struct caller_info callerinfo;
802 char *valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_.-!$%&/()=+*;~";
806 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);
808 memcpy(&callerinfo, &p_callerinfo, sizeof(struct caller_info));
809 apply_callerid_restriction(p_record_anon_ignore, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.extension, callerinfo.name);
811 SCPY(number, p_dialinginfo.id);
812 SCPY(callerid, numberrize_callerinfo(callerinfo.id, callerinfo.ntype));
813 if (callerid[0] == '\0')
815 if (callerinfo.present == INFO_PRESENT_RESTRICTED)
816 UCPY(callerid,"anonymous");
818 UCPY(callerid,"unknown");
821 /* change verboten digits */
823 while((p=strchr(p,'*')))
826 while((p=strchr(p,'/')))
829 while((p=strchr(p,'*')))
832 while((p=strchr(p,'/')))
835 ii = strlen(callerid);
838 if (!strchr(valid_chars, callerid[i]))
846 if (!strchr(valid_chars, number[i]))
851 /* add beep to the end of recording */
853 switch(p_record_type)
859 fwrite(beep_mono, sizeof(beep_mono), 1, p_record);
860 i += sizeof(beep_mono);
861 p_record_length += sizeof(beep_mono);
868 fwrite(beep_8bit, sizeof(beep_8bit), 1, p_record);
869 i += sizeof(beep_8bit);
870 p_record_length += sizeof(beep_8bit);
878 fwrite(beep_law, sizeof(beep_law), 1, p_record);
879 i += sizeof(beep_law);
880 p_record_length += sizeof(beep_law);
885 PERROR("codec %d not supported for beep adding\n", p_record_type);
888 /* complete header */
889 switch(p_record_type)
895 fprintf(p_record, "cue %c%c%c%c%c%c%c%c", 4, 0, 0, 0, 0,0,0,0);
898 fprintf(p_record, "LIST%c%c%c%cadtl", 4, 0, 0, 0);
901 fseek(p_record, 0, SEEK_SET);
903 /* WAVEfmt xxxx(fmt-size)dataxxxx[data]cue xxxx0000LISTxxxxadtl*/
904 size = p_record_length;
905 wsize = 4+8+sizeof(fmt)+8+size+8+4+8+4;
908 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));
911 fprintf(p_record, "WAVE");
914 fprintf(p_record, "fmt %c%c%c%c", sizeof(fmt), 0, 0, 0);
915 switch(p_record_type)
920 fmt.sample_rate = 8000; /* samples/sec */
921 fmt.data_rate = 16000; /* full data rate */
922 fmt.bytes_sample = 2; /* all channels */
923 fmt.bits_sample = 16; /* one channel */
929 fmt.sample_rate = 8000; /* samples/sec */
930 fmt.data_rate = 32000; /* full data rate */
931 fmt.bytes_sample = 4; /* all channels */
932 fmt.bits_sample = 16; /* one channel */
938 fmt.sample_rate = 8000; /* samples/sec */
939 fmt.data_rate = 8000; /* full data rate */
940 fmt.bytes_sample = 1; /* all channels */
941 fmt.bits_sample = 8; /* one channel */
944 fwrite(&fmt, sizeof(fmt), 1, p_record);
947 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));
950 if (p_record_vbox == 1)
951 SPRINT(filename, "%s.wav", p_record_filename);
953 SPRINT(filename, "%s_%s-%s.wav", p_record_filename, callerid, number);
958 if (p_record_vbox == 1)
959 SPRINT(filename, "%s.isdn", p_record_filename);
961 SPRINT(filename, "%s_%s-%s.isdn", p_record_filename, callerid, number);
969 if (rename(p_record_filename, filename) < 0)
971 PERROR("Port(%d) cannot rename from '%s' to '%s'\n", p_serial, p_record_filename, filename);
975 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);
977 if (p_record_vbox == 2)
979 SPRINT(indexname, "%s/%s/%s/vbox/index", INSTALL_DATA, options.extensions_dir, p_record_extension);
980 if ((fp = fopen(indexname,"a")))
984 /* remove path from file name */
986 while(strchr(p, '/'))
987 p = strchr(p, '/')+1;
988 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);
994 PERROR("Port(%d) cannot open index file '%s' to append.\n", p_serial, indexname);
997 /* send email with sample*/
998 if (p_record_vbox_email[0])
1000 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);
1007 * recording function
1008 * Records all data from down and from up into one single stream.
1009 * Both streams may have gaps or jitter.
1010 * A Jitter buffer for both streams is used to compensate jitter.
1012 * If one stream (dir) received packets, they are stored to a
1013 * buffer to wait for the other stream (dir), so both streams can
1014 * be combined. If the buffer is full, it's content is written
1015 * without mixing stream. (assuming only one stram (dir) exists.)
1016 * A flag is used to indicate what stream is currently in buffer.
1018 * NOTE: First stereo sample (odd) is from down, second is from up.
1020 void Port::record(unsigned char *data, int length, int dir_fromup)
1022 unsigned char write_buffer[1024], *d;
1028 if (!p_record || !length)
1034 /* more than we have */
1035 if (p_record_skip > length)
1037 p_record_skip -= length;
1040 data += p_record_skip;
1041 length -= p_record_skip;
1044 free = ((p_record_buffer_readp - p_record_buffer_writep - 1) & RECORD_BUFFER_MASK);
1046 /* the buffer stores the same data stream */
1047 if (dir_fromup == p_record_buffer_dir)
1051 /* first write what we can to the buffer */
1052 while(free && length)
1054 p_record_buffer[p_record_buffer_writep] = audio_law_to_s32[*data++];
1055 p_record_buffer_writep = (p_record_buffer_writep + 1) & RECORD_BUFFER_MASK;
1059 /* all written, so we return */
1062 /* still data left, buffer is full, so we need to write to file */
1063 switch(p_record_type)
1066 s = (signed short *)write_buffer;
1070 *s++ = p_record_buffer[p_record_buffer_readp];
1071 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1074 fwrite(write_buffer, 512, 1, p_record);
1078 s = (signed short *)write_buffer;
1079 if (p_record_buffer_dir)
1084 *s++ = 0; /* nothing from down */
1085 *s++ = p_record_buffer[p_record_buffer_readp];
1086 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1094 *s++ = p_record_buffer[p_record_buffer_readp];
1095 *s++ = 0; /* nothing from up */
1096 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1100 fwrite(write_buffer, 1024, 1, p_record);
1108 *d++ = (p_record_buffer[p_record_buffer_readp]+0x8000) >> 8;
1109 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1112 fwrite(write_buffer, 512, 1, p_record);
1120 *d++ = audio_s16_to_law[p_record_buffer[p_record_buffer_readp] & 0xffff];
1121 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1124 fwrite(write_buffer, 256, 1, p_record);
1127 /* because we still have data, we write again */
1128 free += sizeof(write_buffer);
1131 /* the buffer stores the other stream */
1133 /* if buffer empty, change it */
1134 if (p_record_buffer_readp == p_record_buffer_writep)
1136 p_record_buffer_dir = dir_fromup;
1139 /* how much data can we mix ? */
1140 ii = (p_record_buffer_writep - p_record_buffer_readp) & RECORD_BUFFER_MASK;
1143 /* write data mixed with the buffer */
1144 switch(p_record_type)
1147 s = (signed short *)write_buffer;
1151 sample = p_record_buffer[p_record_buffer_readp]
1152 + audio_law_to_s32[*data++];
1153 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1161 fwrite(write_buffer, ii<<1, 1, p_record);
1165 s = (signed short *)write_buffer;
1166 if (p_record_buffer_dir)
1171 *s++ = audio_law_to_s32[*data++];
1172 *s++ = p_record_buffer[p_record_buffer_readp];
1173 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1181 *s++ = p_record_buffer[p_record_buffer_readp];
1182 *s++ = audio_law_to_s32[*data++];
1186 fwrite(write_buffer, ii<<2, 1, p_record);
1194 sample = p_record_buffer[p_record_buffer_readp]
1195 + audio_law_to_s32[*data++];
1196 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1201 *d++ = (sample+0x8000) >> 8;
1204 fwrite(write_buffer, ii, 1, p_record);
1212 sample = p_record_buffer[p_record_buffer_readp]
1213 + audio_law_to_s32[*data++];
1214 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1219 *d++ = audio_s16_to_law[sample & 0xffff];
1222 fwrite(write_buffer, ii, 1, p_record);
1226 /* data, but buffer empty */
1229 p_record_buffer_dir = dir_fromup;
1232 /* no data (maybe buffer) */