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>
59 #define SHORT_MIN -32768
60 #define SHORT_MAX 32767
62 class Port *port_first = NULL;
64 unsigned long port_serial = 1; /* must be 1, because 0== no port */
67 /* free epointlist relation
69 void Port::free_epointlist(struct epoint_list *epointlist)
71 struct epoint_list *temp, **tempp;
74 tempp = &p_epointlist;
77 if (temp == epointlist)
85 PERROR("SOFTWARE ERROR: epointlist not in port's list.\n");
92 PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
93 FREE(temp, sizeof(struct epoint_list));
98 void Port::free_epointid(unsigned long epoint_id)
100 struct epoint_list *temp, **tempp;
103 tempp = &p_epointlist;
106 if (temp->epoint_id == epoint_id)
114 PERROR("epoint_id not in port's list.\n");
121 PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
122 FREE(temp, sizeof(struct epoint_list));
127 /* create new epointlist relation
129 struct epoint_list *Port::epointlist_new(unsigned long epoint_id)
131 struct epoint_list *epointlist, **epointlistpointer;
133 /* epointlist structure */
134 epointlist = (struct epoint_list *)MALLOC(sizeof(struct epoint_list));
136 FATAL("No memory for epointlist\n");
138 PDEBUG(DEBUG_EPOINT, "PORT(%d) allocating epoint_list.\n", p_serial);
140 /* add epoint_list to chain */
141 epointlist->next = NULL;
142 epointlistpointer = &p_epointlist;
143 while(*epointlistpointer)
144 epointlistpointer = &((*epointlistpointer)->next);
145 *epointlistpointer = epointlist;
148 epointlist->epoint_id = epoint_id;
149 epointlist->active = 1;
158 Port::Port(int type, char *portname, struct port_settings *settings)
160 class Port *temp, **tempp;
162 PDEBUG(DEBUG_PORT, "new port of type %d, name '%s'\n", type, portname);
164 /* initialize object */
166 memcpy(&p_settings, settings, sizeof(struct port_settings));
169 memset(&p_settings, 0, sizeof(p_settings));
170 SCPY(p_settings.tones_dir, options.tones_dir);
172 SCPY(p_name, portname);
173 SCPY(p_tone_dir, p_settings.tones_dir); // just to be sure
175 p_serial = port_serial++;
177 p_tone_fetched = NULL;
178 p_tone_name[0] = '\0';
179 p_state = PORT_STATE_IDLE;
181 memset(&p_callerinfo, 0, sizeof(p_callerinfo));
182 memset(&p_dialinginfo, 0, sizeof(p_dialinginfo));
183 memset(&p_connectinfo, 0, sizeof(p_connectinfo));
184 memset(&p_redirinfo, 0, sizeof(p_redirinfo));
185 memset(&p_capainfo, 0, sizeof(p_capainfo));
193 p_record_filename[0] = '\0';
194 p_record_buffer_readp = 0;
195 p_record_buffer_writep = 0;
196 p_record_buffer_dir = 0;
198 /* append port to chain */
218 class Port *temp, **tempp;
219 struct message *message;
226 PDEBUG(DEBUG_PORT, "removing port of type %d, name '%s'\n", p_type, p_name);
228 /* disconnect port from endpoint */
231 /* send disconnect */
232 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
233 message->param.disconnectinfo.cause = 16;
234 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
235 message_put(message);
236 /* remove endpoint */
237 free_epointlist(p_epointlist);
240 /* remove port from chain */
251 FATAL("PORT(%s) port not in port's list.\n", p_name);
255 /* close open tones file */
262 p_tone_fetched = NULL;
267 /* set new endpoint state
269 void Port::new_state(int state)
271 PDEBUG(DEBUG_PORT, "PORT(%s) new state %s --> %s\n", p_name, state_name[p_state], state_name[state]);
277 * find the port with port_id
279 class Port *find_port_id(unsigned long port_id)
281 class Port *port = port_first;
285 //printf("comparing: '%s' with '%s'\n", name, port->name);
286 if (port->p_serial == port_id)
298 void Port::set_echotest(int echotest)
300 p_echotest = echotest;
305 * set the file in the tone directory with the given name
307 void Port::set_tone(char *dir, char *name)
315 /* no counter, no eof, normal speed */
319 p_tone_codec = CODEC_LAW;
327 p_tone_fetched = NULL;
333 SPRINT(p_tone_name, "%s", name);
334 p_tone_dir[0] = '\0';
337 SCPY(p_tone_dir, dir);
338 SCPY(p_tone_name, name);
342 p_tone_name[0]= '\0';
347 if (!!strncmp(name,"cause_",6))
350 /* now we check if the cause exists, otherwhise we use error tone. */
351 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, 0, 0)))
353 p_tone_fetched = NULL;
356 SPRINT(filename, "%s_loop", p_tone_name);
357 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, 0, 0)))
359 p_tone_fetched = NULL;
362 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
363 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0)
368 SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name);
369 if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0)
375 if (!strcmp(name,"cause_00") || !strcmp(name,"cause_10"))
377 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using release tone\n", p_name, name+6);
378 SPRINT(p_tone_name,"release");
380 if (!strcmp(name,"cause_11"))
382 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using busy tone\n", p_name, name+6);
383 SPRINT(p_tone_name,"busy");
386 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using error tone\n", p_name, name+6);
387 SPRINT(p_tone_name,"error");
393 * set the file in the tone directory for vbox playback
394 * also set the play_eof-flag
396 void Port::set_vbox_tone(char *dir, char *name)
402 p_tone_codec = CODEC_LAW;
411 p_tone_fetched = NULL;
413 SPRINT(p_tone_dir, dir);
414 SPRINT(p_tone_name, name);
416 /* now we check if the cause exists, otherwhise we use error tone. */
419 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, &p_tone_size, &p_tone_left)))
421 PDEBUG(DEBUG_PORT, "PORT(%s) opening fetched tone: %s\n", p_name, p_tone_name);
424 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
425 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0)
428 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
433 SPRINT(filename, "%s", p_tone_name);
434 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0)
437 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
445 * set the file in the given directory for vbox playback
446 * also set the eof-flag
447 * also set the counter-flag
449 void Port::set_vbox_play(char *name, int offset)
452 struct message *message;
454 /* use ser_box_tone() */
455 set_vbox_tone("", name);
465 /* send message with counter value */
466 if (p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist))
468 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
469 message->param.counter.current = offset;
470 message->param.counter.max = size;
471 message_put(message);
478 * set the playback speed (for recording playback with different speeds)
480 void Port::set_vbox_speed(int speed)
482 /* enable vbox play mode */
483 p_tone_speed = speed;
487 * read from the given file as specified in port_set_tone and return sample data
488 * if the tone ends, the result may be less samples than requested
490 int Port::read_audio(unsigned char *buffer, int length)
493 int nodata=0; /* to detect 0-length files and avoid endless reopen */
495 int tone_left_before; /* temp variable to determine the change in p_tone_left */
503 /* if there is no tone set, use silence */
507 /* if the file pointer is not open, we open it */
508 if (p_tone_fh<0 && p_tone_fetched==NULL)
512 SPRINT(filename, "%s", p_tone_name);
513 /* if file does not exist */
514 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left)))
516 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
517 /* if file does not exist */
518 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
520 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
527 SPRINT(filename, "%s", p_tone_name);
528 /* if file does not exist */
529 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
531 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
536 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
540 /* file descriptor is open read data */
541 tone_left_before = p_tone_left;
544 l = read_tone(p_tone_fh, buffer, p_tone_codec, len, p_tone_size, &p_tone_left, p_tone_speed);
545 if (l<0 || l>len) /* paranoia */
552 l = read_tone_fetched(&p_tone_fetched, buffer, len, p_tone_size, &p_tone_left, p_tone_speed);
553 if (l<0 || l>len) /* paranoia */
559 /* if counter is enabled, we check if we have a change */
560 if (p_tone_counter && p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist))
562 /* if we jumed to the next second */
563 if (((p_tone_size-p_tone_left)/8000) != (p_tone_size-tone_left_before)/8000)
565 //printf("\nsize=%d left=%d\n\n",p_tone_size,p_tone_left);
566 struct message *message;
567 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
568 message->param.counter.current = (p_tone_size-p_tone_left)/8000;
569 message->param.counter.max = -1;
570 message_put(message);
583 p_tone_fetched = NULL;
588 /* if the file has 0-length */
591 PDEBUG(DEBUG_PORT, "PORT(%s) 0-length loop: %s\n", p_name, filename);
597 /* if eof is reached, or if the normal file cannot be opened, continue with the loop file if possible */
599 if (p_tone_eof && ACTIVE_EPOINT(p_epointlist))
601 struct message *message;
602 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_EOF);
603 message_put(message);
608 /* if file does not exist */
609 SPRINT(filename, "%s_loop", p_tone_name);
610 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left)))
612 SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name);
613 /* if file does not exist */
614 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
616 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
617 p_tone_dir[0] = '\0';
618 p_tone_name[0] = '\0';
625 SPRINT(filename, "%s_loop", p_tone_name);
626 /* if file does not exist */
627 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
629 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
630 p_tone_dir[0] = '\0';
631 p_tone_name[0] = '\0';
637 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
639 /* now we have opened the loop */
645 * process transmission clock */
646 int Port::handler(void)
651 /* endpoint sends messages to the port
652 * this is called by the message_epoint inherited by child classes
653 * therefor a return=1 means: stop, no more processing
655 //extern struct message *dddebug;
656 int Port::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
658 /* check if we got audio data from one remote port */
661 case MESSAGE_TONE: /* play tone */
662 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);
663 set_tone(param->tone.dir,param->tone.name);
666 case MESSAGE_VBOX_TONE: /* play tone of answering machine */
667 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine tone '%s' '%s'\n", p_name, param->tone.dir, param->tone.name);
668 set_vbox_tone(param->tone.dir, param->tone.name);
671 case MESSAGE_VBOX_PLAY: /* play recording of answering machine */
672 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine file to play '%s' (offset %d seconds)\n", p_name, param->play.file, param->play.offset);
673 set_vbox_play(param->play.file, param->play.offset);
676 case MESSAGE_VBOX_PLAY_SPEED: /* set speed of playback (recording of answering machine) */
677 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine playback speed %d (times)\n", p_name, param->speed);
678 set_vbox_speed(param->speed);
687 /* wave header structure */
689 unsigned short stereo; /* 1 = mono, 2 = stereo */
690 unsigned short channels; /* number of channels */
691 unsigned long sample_rate; /* sample rate */
692 unsigned long data_rate; /* data rate */
693 unsigned short bytes_sample; /* bytes per sample (all channels) */
694 unsigned short bits_sample; /* bits per sample (one channel) */
699 * open record file (actually a wave file with empty header which will be
700 * written before close, because we do not know the size yet)
701 * type=1 record annoucement, type=0 record audio stream, type=2 record vbox
703 int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ignore, char *vbox_email, int vbox_email_file)
705 /* RIFFxxxxWAVEfmt xxxx(fmt-size)dataxxxx... */
706 char dummyheader[8+4+8+sizeof(fmt)+8];
711 PERROR("Port(%d) not an extension\n", p_serial);
714 SCPY(p_record_extension, extension);
715 p_record_anon_ignore = anon_ignore;
716 SCPY(p_record_vbox_email, vbox_email);
717 p_record_vbox_email_file = vbox_email_file;
721 PERROR("Port(%d) already recording\n", p_serial);
726 SPRINT(filename, "%s/%s/%s/vbox", INSTALL_DATA, options.extensions_dir, p_record_extension);
728 SPRINT(filename, "%s/%s/%s/recordings", INSTALL_DATA, options.extensions_dir, p_record_extension);
729 if (mkdir(filename, 0755) < 0)
733 PERROR("Port(%d) cannot create directory '%s'\n", p_serial, filename);
739 UPRINT(strchr(filename,'\0'), "/announcement");
741 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);
744 p_record_vbox_year = now_tm->tm_year;
745 p_record_vbox_mon = now_tm->tm_mon;
746 p_record_vbox_mday = now_tm->tm_mday;
747 p_record_vbox_hour = now_tm->tm_hour;
748 p_record_vbox_min = now_tm->tm_min;
751 /* check, if file exists (especially when an extension calls the same extension) */
753 if ((p_record = fopen(filename, "r")))
756 SCAT(filename, "_2nd");
759 p_record = fopen(filename, "w");
762 PERROR("Port(%d) cannot record because file cannot be opened '%s'\n", p_serial, filename);
767 p_record_type = type;
768 p_record_vbox = vbox;
769 p_record_skip = skip;
771 switch(p_record_type)
776 fwrite(dummyheader, sizeof(dummyheader), 1, p_record);
782 UCPY(p_record_filename, filename);
784 PDEBUG(DEBUG_PORT, "Port(%d) recording started with file name '%s'\n", p_serial, filename);
790 * close the recoding file, put header in front and rename
792 void Port::close_record(int beep, int mute)
794 static signed short beep_mono[256];
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]))
853 if (mute && p_record_type==CODEC_MONO)
858 fseek(p_record, -(i<<1), SEEK_END);
859 p_record_length -= (i<<1);
861 /* add beep to the end of recording */
862 if (beep && p_record_type==CODEC_MONO)
867 beep_mono[i] = (signed short)(sin((double)i / 5.688888888889 * 2.0 * 3.1415927) * 2000.0);
873 fwrite(beep_mono, sizeof(beep_mono), 1, p_record);
874 i += sizeof(beep_mono);
875 p_record_length += sizeof(beep_mono);
879 /* complete header */
880 switch(p_record_type)
886 fprintf(p_record, "cue %c%c%c%c%c%c%c%c", 4, 0, 0, 0, 0,0,0,0);
889 fprintf(p_record, "LIST%c%c%c%cadtl", 4, 0, 0, 0);
892 fseek(p_record, 0, SEEK_SET);
894 /* WAVEfmt xxxx(fmt-size)dataxxxx[data]cue xxxx0000LISTxxxxadtl*/
895 size = p_record_length;
896 wsize = 4+8+sizeof(fmt)+8+size+8+4+8+4;
899 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));
902 fprintf(p_record, "WAVE");
905 fprintf(p_record, "fmt %c%c%c%c", sizeof(fmt), 0, 0, 0);
906 switch(p_record_type)
911 fmt.sample_rate = 8000; /* samples/sec */
912 fmt.data_rate = 16000; /* full data rate */
913 fmt.bytes_sample = 2; /* all channels */
914 fmt.bits_sample = 16; /* one channel */
920 fmt.sample_rate = 8000; /* samples/sec */
921 fmt.data_rate = 32000; /* full data rate */
922 fmt.bytes_sample = 4; /* all channels */
923 fmt.bits_sample = 16; /* one channel */
929 fmt.sample_rate = 8000; /* samples/sec */
930 fmt.data_rate = 8000; /* full data rate */
931 fmt.bytes_sample = 1; /* all channels */
932 fmt.bits_sample = 8; /* one channel */
935 fwrite(&fmt, sizeof(fmt), 1, p_record);
938 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));
941 if (p_record_vbox == 1)
942 SPRINT(filename, "%s.wav", p_record_filename);
944 SPRINT(filename, "%s_%s-%s.wav", p_record_filename, callerid, number);
949 if (p_record_vbox == 1)
950 SPRINT(filename, "%s.isdn", p_record_filename);
952 SPRINT(filename, "%s_%s-%s.isdn", p_record_filename, callerid, number);
960 if (rename(p_record_filename, filename) < 0)
962 PERROR("Port(%d) cannot rename from '%s' to '%s'\n", p_serial, p_record_filename, filename);
966 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);
968 if (p_record_vbox == 2)
970 SPRINT(indexname, "%s/%s/%s/vbox/index", INSTALL_DATA, options.extensions_dir, p_record_extension);
971 if ((fp = fopen(indexname,"a")))
975 /* remove path from file name */
977 while(strchr(p, '/'))
978 p = strchr(p, '/')+1;
979 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);
985 PERROR("Port(%d) cannot open index file '%s' to append.\n", p_serial, indexname);
988 /* send email with sample*/
989 if (p_record_vbox_email[0])
991 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);
999 * Records all data from down and from up into one single stream.
1000 * Both streams may have gaps or jitter.
1001 * A Jitter buffer for both streams is used to compensate jitter.
1003 * If one stream (dir) received packets, they are stored to a
1004 * buffer to wait for the other stream (dir), so both streams can
1005 * be combined. If the buffer is full, it's content is written
1006 * without mixing stream. (assuming only one stram (dir) exists.)
1007 * A flag is used to indicate what stream is currently in buffer.
1009 * NOTE: First stereo sample (odd) is from down, second is from up.
1011 void Port::record(unsigned char *data, int length, int dir_fromup)
1013 unsigned char write_buffer[1024], *d;
1019 if (!p_record || !length)
1022 /* skip data from local caller (dtmf input) */
1023 if (p_record_skip && !dir_fromup)
1025 /* more to skip than we have */
1026 if (p_record_skip > length)
1028 p_record_skip -= length;
1032 data += p_record_skip;
1033 length -= p_record_skip;
1037 //printf("dir=%d len=%d\n", dir_fromup, length);
1039 free = ((p_record_buffer_readp - p_record_buffer_writep - 1) & RECORD_BUFFER_MASK);
1041 //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);
1043 /* the buffer stores the same data stream */
1044 if (dir_fromup == p_record_buffer_dir)
1048 //printf("same free=%d length=%d\n", free, length);
1049 /* first write what we can to the buffer */
1050 while(free && length)
1052 p_record_buffer[p_record_buffer_writep] = audio_law_to_s32[*data++];
1053 p_record_buffer_writep = (p_record_buffer_writep + 1) & RECORD_BUFFER_MASK;
1057 /* all written, so we return */
1060 /* still data left, buffer is full, so we need to write a chunk to file */
1061 switch(p_record_type)
1064 s = (signed short *)write_buffer;
1068 *s++ = p_record_buffer[p_record_buffer_readp];
1069 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1072 fwrite(write_buffer, 512, 1, p_record);
1073 p_record_length += 512;
1077 s = (signed short *)write_buffer;
1078 if (p_record_buffer_dir)
1083 *s++ = 0; /* nothing from down */
1084 *s++ = p_record_buffer[p_record_buffer_readp];
1085 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1093 *s++ = p_record_buffer[p_record_buffer_readp];
1094 *s++ = 0; /* nothing from up */
1095 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1099 fwrite(write_buffer, 1024, 1, p_record);
1100 p_record_length += 1024;
1108 *d++ = ((unsigned short)(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);
1113 p_record_length += 512;
1121 *d++ = audio_s16_to_law[p_record_buffer[p_record_buffer_readp] & 0xffff];
1122 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1125 fwrite(write_buffer, 256, 1, p_record);
1126 p_record_length += 256;
1129 /* because we still have data, we write again */
1133 /* the buffer stores the other stream */
1136 /* if buffer empty, change it */
1137 if (p_record_buffer_readp == p_record_buffer_writep)
1139 p_record_buffer_dir = dir_fromup;
1142 /* how much data can we mix ? */
1143 ii = (p_record_buffer_writep - p_record_buffer_readp) & RECORD_BUFFER_MASK;
1149 //printf("same ii=%d length=%d\n", ii, length);
1150 //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);
1152 /* write data mixed with the buffer */
1153 switch(p_record_type)
1156 s = (signed short *)write_buffer;
1160 sample = p_record_buffer[p_record_buffer_readp]
1161 + audio_law_to_s32[*data++];
1162 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1163 if (sample < SHORT_MIN) sample = SHORT_MIN;
1164 if (sample > SHORT_MAX) sample = SHORT_MAX;
1168 fwrite(write_buffer, ii<<1, 1, p_record);
1169 p_record_length += (ii<<1);
1173 s = (signed short *)write_buffer;
1174 if (p_record_buffer_dir)
1179 *s++ = audio_law_to_s32[*data++];
1180 *s++ = p_record_buffer[p_record_buffer_readp];
1181 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1189 *s++ = p_record_buffer[p_record_buffer_readp];
1190 *s++ = audio_law_to_s32[*data++];
1191 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1195 fwrite(write_buffer, ii<<2, 1, p_record);
1196 p_record_length += (ii<<2);
1204 sample = p_record_buffer[p_record_buffer_readp]
1205 + audio_law_to_s32[*data++];
1206 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1207 if (sample < SHORT_MIN) sample = SHORT_MIN;
1208 if (sample > SHORT_MAX) sample = SHORT_MAX;
1209 *d++ = (sample+0x8000) >> 8;
1212 fwrite(write_buffer, ii, 1, p_record);
1213 p_record_length += ii;
1221 sample = p_record_buffer[p_record_buffer_readp]
1222 + audio_law_to_s32[*data++];
1223 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1224 if (sample < SHORT_MIN) sample = SHORT_MIN;
1225 if (sample > SHORT_MAX) sample = SHORT_MAX;
1226 *d++ = audio_s16_to_law[sample & 0xffff];
1229 fwrite(write_buffer, ii, 1, p_record);
1230 p_record_length += ii;
1236 goto different_again;
1237 /* no data (maybe buffer) */