X-Git-Url: http://git.eversberg.eu/gitweb.cgi?p=lcr.git;a=blobdiff_plain;f=port.cpp;h=0a5687c1d6fcd5120e959119a4d050ae6cdaa22a;hp=a68ca59768a7e2ebb2dce9a4364a85c29b767350;hb=79bd731c0db3e3202cfeed2af3fb217ae744b70f;hpb=2ed0fee489c37a6e2d4473f6185ebbe3e746ac11 diff --git a/port.cpp b/port.cpp index a68ca59..0a5687c 100644 --- a/port.cpp +++ b/port.cpp @@ -9,24 +9,57 @@ ** ** \*****************************************************************************/ -#include -#include -#include -#include -#include -#include -#include -#include +/* HOW TO audio? + +Audio flow has two ways: + +* from channel to the upper layer + -> sound from mISDN channel + -> announcement from vbox channel + +* from the upper layer to the channel + -> sound from remote channel + +Audio is required: + + -> if local or remote channel is not mISDN + -> if call is recorded (vbox) + + +Functions: + +* PmISDN::txfromup + -> audio from upper layer is buffered for later transmission to channel +* PmISDN::handler + -> buffered audio from upper layer or tones are transmitted via system clock +* mISDN_handler + -> rx-data from port to record() and upper layer + -> tx-data from port (dsp) to record() +* VboxPort::handler + -> streaming announcement to upper layer + -> recording announcement +* VboxPort::message_epoint + -> recording audio message from upper layer + + + +*/ + #include "main.h" -#define BETTERDELAY +/* enable to test conference mixing, even if only two members are bridged */ +//#define TEST_CONFERENCE 1 -//#define MIXER_DEBUG /* debug mixer buffer overflow and underrun */ +#define SHORT_MIN -32768 +#define SHORT_MAX 32767 class Port *port_first = NULL; -unsigned long port_serial = 1; /* must be 1, because 0== no port */ +unsigned int port_serial = 1; /* must be 1, because 0== no port */ +struct port_bridge *p_bridge_first; + +static void remove_bridge(struct port_bridge *bridge, class Port *port); /* free epointlist relation */ @@ -36,16 +69,14 @@ void Port::free_epointlist(struct epoint_list *epointlist) temp = p_epointlist; tempp = &p_epointlist; - while(temp) - { + while(temp) { if (temp == epointlist) break; tempp = &temp->next; temp = temp->next; } - if (temp == 0) - { + if (temp == 0) { PERROR("SOFTWARE ERROR: epointlist not in port's list.\n"); return; } @@ -54,29 +85,26 @@ void Port::free_epointlist(struct epoint_list *epointlist) /* free */ PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial); - memset(temp, 0, sizeof(struct epoint_list)); - free(temp); + FREE(temp, sizeof(struct epoint_list)); ememuse--; } -void Port::free_epointid(unsigned long epoint_id) +void Port::free_epointid(unsigned int epoint_id) { struct epoint_list *temp, **tempp; temp = p_epointlist; tempp = &p_epointlist; - while(temp) - { + while(temp) { if (temp->epoint_id == epoint_id) break; tempp = &temp->next; temp = temp->next; } - if (temp == 0) - { - PERROR("epoint_id not in port's list, exitting.\n"); + if (temp == 0) { + PERROR("epoint_id not in port's list.\n"); return; } /* detach */ @@ -84,28 +112,23 @@ void Port::free_epointid(unsigned long epoint_id) /* free */ PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial); - memset(temp, 0, sizeof(struct epoint_list)); - free(temp); + FREE(temp, sizeof(struct epoint_list)); ememuse--; } /* create new epointlist relation */ -struct epoint_list *Port::epointlist_new(unsigned long epoint_id) +struct epoint_list *Port::epointlist_new(unsigned int epoint_id) { struct epoint_list *epointlist, **epointlistpointer; /* epointlist structure */ - epointlist = (struct epoint_list *)calloc(1, sizeof(struct epoint_list)); + epointlist = (struct epoint_list *)MALLOC(sizeof(struct epoint_list)); if (!epointlist) - { - PERROR("no mem for allocating epoint_list\n"); - return(0); - } + FATAL("No memory for epointlist\n"); ememuse++; PDEBUG(DEBUG_EPOINT, "PORT(%d) allocating epoint_list.\n", p_serial); - memset(epointlist, 0, sizeof(struct epoint_list)); /* add epoint_list to chain */ epointlist->next = NULL; @@ -125,30 +148,27 @@ struct epoint_list *Port::epointlist_new(unsigned long epoint_id) /* * port constructor */ -Port::Port(int type, char *portname, struct port_settings *settings) +Port::Port(int type, const char *portname, struct port_settings *settings, struct interface *interface) { class Port *temp, **tempp; - PDEBUG(DEBUG_PORT, "new port of type %d, name '%s'\n", type, portname); - /* initialize object */ if (settings) memcpy(&p_settings, settings, sizeof(struct port_settings)); - else - { + else { memset(&p_settings, 0, sizeof(p_settings)); - SCPY(p_settings.tones_dir, options.tones_dir); } SCPY(p_name, portname); - SCPY(p_tone_dir, p_settings.tones_dir); // just to be sure + if (interface) { + SCPY(p_interface_name, interface->name); + SCPY(p_tones_interface, interface->tones_dir); + } + p_tone_dir[0] = '\0'; p_type = type; p_serial = port_serial++; - p_debug_nothingtosend = 0; p_tone_fh = -1; p_tone_fetched = NULL; p_tone_name[0] = '\0'; -// p_knock_fh = -1; -// p_knock_fetched = NULL; p_state = PORT_STATE_IDLE; p_epointlist = NULL; memset(&p_callerinfo, 0, sizeof(p_callerinfo)); @@ -156,29 +176,40 @@ Port::Port(int type, char *portname, struct port_settings *settings) memset(&p_connectinfo, 0, sizeof(p_connectinfo)); memset(&p_redirinfo, 0, sizeof(p_redirinfo)); memset(&p_capainfo, 0, sizeof(p_capainfo)); - memset(p_mixer_buffer, 0, sizeof(p_mixer_buffer)); - memset(p_record_buffer, 0, sizeof(p_record_buffer)); - memset(p_stereo_buffer, 0, sizeof(p_stereo_buffer)); - p_mixer_rel = NULL; - p_mixer_readp = 0; p_echotest = 0; - next = NULL; + p_bridge = 0; + + /* call recording */ p_record = NULL; + p_tap = 0; p_record_type = 0; p_record_length = 0; + p_record_skip = 0; p_record_filename[0] = '\0'; + p_record_buffer_readp = 0; + p_record_buffer_writep = 0; + p_record_buffer_dir = 0; + + /* VoOTP */ +#ifdef WITH_VOOTP + p_vootp = NULL; +#endif + /* D-O-V */ + dov_init(); /* append port to chain */ + next = NULL; temp = port_first; tempp = &port_first; - while(temp) - { + while(temp) { tempp = &temp->next; temp = temp->next; } *tempp = this; classuse++; + + PDEBUG(DEBUG_PORT, "new port (%d) of type 0x%x, name '%s' interface '%s'\n", p_serial, type, portname, p_interface_name); } @@ -187,32 +218,32 @@ Port::Port(int type, char *portname, struct port_settings *settings) */ Port::~Port(void) { - struct mixer_relation *relation, *rtemp; class Port *temp, **tempp; - struct message *message; + struct lcr_msg *message; - if (p_record) - close_record(0); + PDEBUG(DEBUG_PORT, "removing port (%d) of type 0x%x, name '%s' interface '%s'\n", p_serial, p_type, p_name, p_interface_name); - classuse--; - - PDEBUG(DEBUG_PORT, "removing port of type %d, name '%s'\n", p_type, p_name); +#ifdef WITH_VOOTP + if (p_vootp) { + vootp_destroy(p_vootp); + p_vootp = NULL; + } +#endif - /* free mixer relation chain */ - relation = p_mixer_rel; - while(relation) - { - rtemp = relation; - relation = relation->next; - memset(rtemp, 0, sizeof(struct mixer_relation)); - free(rtemp); - pmemuse--; + if (p_bridge) { + PDEBUG(DEBUG_PORT, "Removing us from bridge %u\n", p_bridge->bridge_id); + remove_bridge(p_bridge, this); } - p_mixer_rel = NULL; /* beeing paranoid */ + + if (p_record) + close_record(0, 0); + + dov_exit(); + + classuse--; /* disconnect port from endpoint */ - while(p_epointlist) - { + while(p_epointlist) { /* send disconnect */ message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE); message->param.disconnectinfo.cause = 16; @@ -225,24 +256,19 @@ Port::~Port(void) /* remove port from chain */ temp=port_first; tempp=&port_first; - while(temp) - { + while(temp) { if (temp == this) break; tempp = &temp->next; temp = temp->next; } if (temp == NULL) - { - PERROR("PORT(%s) port not in port's list.\n", p_name); - exit(-1); - } + FATAL("PORT(%s) port not in port's list.\n", p_name); /* detach */ *tempp=this->next; /* close open tones file */ - if (p_tone_fh >= 0) - { + if (p_tone_fh >= 0) { close(p_tone_fh); p_tone_fh = -1; fhuse--; @@ -262,33 +288,13 @@ void Port::new_state(int state) /* - * find the port using h323 token - */ -class Port *find_port_with_token(char *name) -{ - class Port *port = port_first; - - while(port) - { -//printf("comparing: '%s' with '%s'\n", name, p_name); - if ((port->p_type==PORT_TYPE_H323_IN || port->p_type==PORT_TYPE_H323_OUT) && !strcmp(port->p_name, name)) - return(port); - port = port->next; - } - - return(NULL); -} - - -/* * find the port with port_id */ -class Port *find_port_id(unsigned long port_id) +class Port *find_port_id(unsigned int port_id) { class Port *port = port_first; - while(port) - { + while(port) { //printf("comparing: '%s' with '%s'\n", name, port->name); if (port->p_serial == port_id) return(port); @@ -298,47 +304,6 @@ class Port *find_port_id(unsigned long port_id) return(NULL); } -#if 0 -/* - * open the knock sound - */ -void Port::set_knock(char *tones_dir_epoint) -{ - char filename[128], *tones_dir; - - /* check if we have the epoint override the tones_dir of options.conf */ - tones_dir = options.tones_dir; - if (tones_dir_epoint[0]) - { - tones_dir = tones_dir_epoint; - } - - if (p_knock_fh >= 0) - { - close(p_knock_fh); - p_knock_fh = -1; - fhuse--; - } - p_knock_fetched = NULL; - - if ((p_knock_fetched=open_tone_fetched(tones_dir, "knock", &p_knock_codec, &p_knock_size, &p_knock_left))) - { - PDEBUG(DEBUG_PORT, "PORT(%s) opening fetched tone: %s\n", p_name, "knock"); - return; - } - - SPRINT(filename, "%s/%s/knock", INSTALL_DATA, tones_dir); - if ((p_knock_fh=open_tone(filename, &p_knock_codec, &p_knock_size, &p_knock_left)) >= 0) - { - PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename); - fhuse++; - } else - { - p_knock_fh = -1; - } -} -#endif - /* * set echotest @@ -348,10 +313,11 @@ void Port::set_echotest(int echotest) p_echotest = echotest; } + /* * set the file in the tone directory with the given name */ -void Port::set_tone(char *dir, char *name) +void Port::set_tone(const char *dir, const char *name) { int fh; char filename[128]; @@ -359,14 +325,12 @@ void Port::set_tone(char *dir, char *name) if (name == NULL) name = ""; -#if 0 - /* knocking ? */ - if (!strcmp(name, "knock")) - { - set_knock(tones_dir_epoint); - return; + if (!dir || !dir[0]) { + if (p_tones_interface[0]) + dir = p_tones_interface; + else + dir = options.tones_dir; /* just in case we have no PmISDN instance */ } -#endif /* no counter, no eof, normal speed */ p_tone_counter = 0; @@ -374,27 +338,24 @@ void Port::set_tone(char *dir, char *name) p_tone_speed = 1; p_tone_codec = CODEC_LAW; - if (p_tone_fh >= 0) - { + if (p_tone_fh >= 0) { close(p_tone_fh); p_tone_fh = -1; fhuse--; } p_tone_fetched = NULL; - if (name[0]) - { - if (name[0] == '/') - { + if (name[0]) { + if (name[0] == '/') { SPRINT(p_tone_name, "%s", name); p_tone_dir[0] = '\0'; - } else - { + } else { SCPY(p_tone_dir, dir); SCPY(p_tone_name, name); } - } else - { + /* trigger playback */ + update_load(); + } else { p_tone_name[0]= '\0'; p_tone_dir[0]= '\0'; return; @@ -404,52 +365,49 @@ void Port::set_tone(char *dir, char *name) return; /* now we check if the cause exists, otherwhise we use error tone. */ - if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, 0, 0))) - { + if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, 0, 0))) { p_tone_fetched = NULL; return; } SPRINT(filename, "%s_loop", p_tone_name); - if ((p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, 0, 0))) - { + if ((p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, 0, 0))) { p_tone_fetched = NULL; return; } - SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name); - if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0) - { + SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name); + if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0) { close(fh); return; } - SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name); - if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0) - { + SPRINT(filename, "%s/%s/%s_loop", SHARE_DATA, p_tone_dir, p_tone_name); + if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0) { close(fh); return; } - if (!strcmp(name,"cause_00") || !strcmp(name,"cause_10")) - { + if (!strcmp(name,"cause_00") || !strcmp(name,"cause_10")) { PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using release tone\n", p_name, name+6); SPRINT(p_tone_name,"release"); } else - if (!strcmp(name,"cause_11")) - { + if (!strcmp(name,"cause_11")) { PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using busy tone\n", p_name, name+6); SPRINT(p_tone_name,"busy"); - } else - { + } else { PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using error tone\n", p_name, name+6); SPRINT(p_tone_name,"error"); } } +void Port::set_display(const char *text) +{ +} + /* * set the file in the tone directory for vbox playback * also set the play_eof-flag */ -void Port::set_vbox_tone(char *dir, char *name) +void Port::set_vbox_tone(const char *dir, const char *name) { char filename[256]; @@ -458,8 +416,7 @@ void Port::set_vbox_tone(char *dir, char *name) p_tone_codec = CODEC_LAW; p_tone_eof = 1; - if (p_tone_fh >= 0) - { + if (p_tone_fh >= 0) { close(p_tone_fh); p_tone_fh = -1; fhuse--; @@ -468,27 +425,24 @@ void Port::set_vbox_tone(char *dir, char *name) SPRINT(p_tone_dir, dir); SPRINT(p_tone_name, name); + /* trigger playback */ + update_load(); /* now we check if the cause exists, otherwhise we use error tone. */ - if (p_tone_dir[0]) - { - if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, &p_tone_size, &p_tone_left))) - { + if (p_tone_dir[0]) { + if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, &p_tone_size, &p_tone_left))) { PDEBUG(DEBUG_PORT, "PORT(%s) opening fetched tone: %s\n", p_name, p_tone_name); return; } - SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name); - if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0) - { + SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name); + if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0) { fhuse++; PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename); return; } - } else - { + } else { SPRINT(filename, "%s", p_tone_name); - if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0) - { + if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0) { fhuse++; PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename); return; @@ -502,10 +456,9 @@ void Port::set_vbox_tone(char *dir, char *name) * also set the eof-flag * also set the counter-flag */ -void Port::set_vbox_play(char *name, int offset) +void Port::set_vbox_play(const char *name, int offset) { - signed long size; - struct message *message; + struct lcr_msg *message; /* use ser_box_tone() */ set_vbox_tone("", name); @@ -516,53 +469,12 @@ void Port::set_vbox_play(char *name, int offset) p_tone_counter = 1; /* seek */ - if (p_tone_name[0]) - { -//printf("\n\n\n tone_codec = %d\n\n\n\n",p_tone_codec); -#if 0 - switch(p_tone_codec) - { - case CODEC_LAW: - case CODEC_8BIT: - lseek(p_tone_fh, offset*8000L, SEEK_SET); - size = p_tone_size / 8000L; - if (offset*8000L <= p_tone_left) - p_tone_left -= offset*8000L; - break; - - case CODEC_MONO: - lseek(p_tone_fh, offset*16000L, SEEK_SET); - size = p_tone_size / 16000L; - if (offset*16000L <= p_tone_left) - p_tone_left -= offset*16000L; -//printf("\n\n\n size = %d\n\n\n\n",size); - break; - - case CODEC_STEREO: - lseek(p_tone_fh, offset*32000L, SEEK_SET); - size = p_tone_size / 32000L; - if (offset*32000L <= p_tone_left) - p_tone_left -= offset*32000L; - break; - - default: - PERROR("no codec specified! exitting...\n"); - exit(-1); - } -#else - lseek(p_tone_fh, offset*8000L, SEEK_SET); - size = p_tone_size / 8000L; - if (offset*8000L <= p_tone_left) - p_tone_left -= offset*8000L; -#endif - - + if (p_tone_name[0]) { /* send message with counter value */ - if (p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist)) - { + if (p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist)) { message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER); message->param.counter.current = offset; - message->param.counter.max = size; + message->param.counter.max = p_tone_size; message_put(message); } } @@ -578,388 +490,77 @@ void Port::set_vbox_speed(int speed) p_tone_speed = speed; } -/* write data to port's mixer buffer - * it will be read by the port using read_audio - * synchronisation is also done when writing is too fast and buffer is full - * the mixer buffer will only mix what cannot be mixed by kernel mixer - * - * also write data to the record buffer. the record buffer will mix everything. - */ -void Port::mixer(union parameter *param) -{ - struct mixer_relation *relation, **relationpointer; - int len; - unsigned char *data; - signed short *data_16; - int writep; - signed long *buffer, *record; - int must_mix = 1; /* set, if we need to mix (not done by kernel) */ - - /* we do not mix if we have audio from ourself but we need to record - * unless we have a local echo enabled - */ - if (param->data.port_id==p_serial && !p_echotest) - must_mix = 0; - - if ((param->data.port_type&PORT_CLASS_MASK)==PORT_CLASS_mISDN - && (p_type&PORT_CLASS_MASK)==PORT_CLASS_mISDN) - { - must_mix = 0; - } - if (!p_record && !must_mix) /* if we do not record AND no need to mix */ - { - return; - } - - /* get the relation to the write pointer. if no relation exists - * for the given source port, we create one. - */ - relation = p_mixer_rel; - relationpointer = &(p_mixer_rel); - if (!relation) /* there is no relation at all */ - { - /* clear buffer to 0-volume and reset writep */ - memset(p_mixer_buffer, 0, sizeof(p_mixer_buffer)); - memset(p_record_buffer, 0, sizeof(p_record_buffer)); - memset(p_stereo_buffer, 0, sizeof(p_stereo_buffer)); - } else /* else because we do not need to run a 0-loop */ - while(relation) - { - if (relation->port_id == param->data.port_id) - break; - relationpointer = &(relation->next); - relation = relation->next; - } - if (!relation) - { - relation = *relationpointer = (struct mixer_relation *)calloc(1, sizeof(struct mixer_relation)); - if (!relation) - { - PERROR("fatal error: cannot alloc memory for port mixer relation\n"); - return; /* no mem */ - } - pmemuse++; - memset(relation, 0, sizeof(struct mixer_relation)); - relation->port_id = param->data.port_id; - /* put write buffer in front of read buffer */ -#ifndef BETTERDELAY - relation->mixer_writep = (p_mixer_readp+(PORT_BUFFER/2))%PORT_BUFFER; -#else - relation->mixer_writep = p_mixer_readp; -#endif -#ifdef MIXER_DEBUG - PDEBUG(DEBUG_PORT, "PORT(%s) Adding new mixer relation from port #%d (readp=%d, writep=%d, PORT_BUFFER=%d).\n", p_name, param->data.port_id, p_mixer_readp, relation->mixer_writep, PORT_BUFFER); -#endif - } - - /* adding remote's audio data to our mixer_buffer / record_buffer */ - len = param->data.len; - if (param->data.compressed == 0) /* in case of 16 bit data */ - len >>= 1; - if (len>PORT_BUFFER) - PERROR("fatal error: audio data from remote port #%d is larger than mixer buffer %d>%d\n", param->data.port_id, len, PORT_BUFFER); - writep = relation->mixer_writep; - buffer = p_mixer_buffer; - record = p_record_buffer; - /* if stereo should be recorded */ - if (param->data.port_id == p_serial) - if (p_record_type == CODEC_STEREO) - record = p_stereo_buffer; - /* NOTE: if read and write pointer are equal, the buffer is full */ - if ((p_mixer_readp-writep+PORT_BUFFER)%PORT_BUFFER < len) /* we would overrun the read pointer */ - { -#ifdef MIXER_DEBUG - PERROR("PORT(%s) buffer overrun, source port #%d is sending too fast. (dropping %d samples)\n", p_name, param->data.port_id, len); -#endif - // we do not resync since dropping causes slowing writepointer down anyway... - //relation->mixer_writep = (p_mixer_readp+(PORT_BUFFER/2))%PORT_BUFFER; - return; - } - - if (!p_record && must_mix) - { - /* WE MUST MIX BUT DO NOT RECORD */ - if (param->data.compressed) - { - /* compressed data */ - data = param->data.data; - if (len+writep >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+writep-PORT_BUFFER; /* rest to write in front of buffer */ - while(writep < PORT_BUFFER) /* write till buffer end */ - buffer[writep++] += audio_law_to_s32[*(data++)]; - writep = 0; - } - while(len--) /* write rest */ - buffer[writep++] += audio_law_to_s32[*(data++)]; - } else - { - /* uncompressed data */ - data_16 = (signed short *)param->data.data; - if (len+writep >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+writep-PORT_BUFFER; /* rest to write in front of buffer */ - while(writep < PORT_BUFFER) /* write till buffer end */ - buffer[writep++] += *(data_16++); - writep = 0; - } - while(len--) /* write rest */ - buffer[writep++] += *(data_16++); - } - } else /* else */ - if (p_record && !must_mix) - { - /* WE MUST RECORD BUT DO NOT MIX */ - if (param->data.compressed) - { - /* compressed data */ - data = param->data.data; - if (len+writep >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+writep-PORT_BUFFER; /* rest to write in front of buffer */ - while(writep < PORT_BUFFER) /* write till buffer end */ - record[writep++] += audio_law_to_s32[*(data++)]; - writep = 0; - } - while(len--) /* write rest */ - record[writep++] += audio_law_to_s32[*(data++)]; - } else - { - /* uncompressed data */ - data_16 = (signed short *)param->data.data; - if (len+writep >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+writep-PORT_BUFFER; /* rest to write in front of buffer */ - while(writep < PORT_BUFFER) /* write till buffer end */ - record[writep++] += *(data_16++); - writep = 0; - } - while(len--) /* write rest */ - record[writep++] += *(data_16++); - } - } else - { - /* WE MUST MIX AND MUST RECORD */ - if (param->data.compressed) - { - /* compressed data */ - data = param->data.data; - if (len+writep >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+writep-PORT_BUFFER; /* rest to write in front of buffer */ - while(writep < PORT_BUFFER) /* write till buffer end */ - { - buffer[writep] += audio_law_to_s32[*data]; - record[writep++] += audio_law_to_s32[*(data++)]; - } - writep = 0; - } - while(len--) /* write rest */ - { - buffer[writep] += audio_law_to_s32[*data]; - record[writep++] += audio_law_to_s32[*(data++)]; - } - } else - { - /* uncompressed data */ - data_16 = (signed short *)param->data.data; - if (len+writep >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+writep-PORT_BUFFER; /* rest to write in front of buffer */ - while(writep < PORT_BUFFER) /* write till buffer end */ - { - buffer[writep] += *data_16; - record[writep++] += *(data_16++); - } - writep = 0; - } - while(len--) /* write rest */ - { - buffer[writep] += *data_16; - record[writep++] += *(data_16++); - } - } - } - relation->mixer_writep = writep; /* new write pointer */ -// PDEBUG(DEBUG_PORT "written data len=%d port=%d (readp=%d, writep=%d, PORT_BUFFER=%d).\n", param->data.len, param->data.port_id, p_mixer_readp, relation->mixer_writep, PORT_BUFFER); -} - - /* * read from the given file as specified in port_set_tone and return sample data - * this data is mixed with the user space mixer (if anything mixed) and - * filled into the buffer as many as given by length. - * if compressed is true, the result in buffer is alaw/ulaw-compressed - * otherwhise it is 16 bit audio. in this case the buffer size must be twice the lenght. - * the length is the number of samples, not bytes in buffer! + * if the tone ends, the result may be less samples than requested */ -int Port::read_audio(unsigned char *buffer, int length, int compressed) +int Port::read_audio(unsigned char *buffer, int length) { - int l,len; - unsigned short temp_buffer[PORT_BUFFER]; /* buffer for up to 32 bit of data */ - int codec_in; /* current codec to use */ - unsigned char *buf_in8; /* buffer pointer for alaw/ulaw/8bit data */ - signed short *buf_in16; /* buffer pointer for alaw/ulaw/8bit data */ - signed short *buf_out16; /* buffer pointer for outgoing audio data */ - signed long *mix_buffer, *mix_buffer2, sample; /* pointer to mixer buffer */ - struct mixer_relation *relation, **relationpointer; - int readp; - int nodata=0; /* to detect 0-length files */ + int l = 0,len; + int nodata=0; /* to detect 0-length files and avoid endless reopen */ char filename[128]; - signed short record_buffer[PORT_BUFFER<<1], *rec_buffer; /* buffer of recorded part which will be written (*2 for stereo) */ - unsigned char *rec8_buffer; /* used to send 8-bit audio (wave-8bit, law) */ int tone_left_before; /* temp variable to determine the change in p_tone_left */ /* nothing */ if (length == 0) return(0); - /* just in case, we get too much to do */ - if (length > PORT_BUFFER-1) - length = PORT_BUFFER-1; - len = length; - buf_in8 = (unsigned char *)temp_buffer; - buf_in16 = (signed short *)temp_buffer; - - codec_in = p_tone_codec; /* if there is no tone set, use silence */ - if (p_tone_name[0] == 0) - { - codec_in = CODEC_LAW; -rest_is_silence: - switch(codec_in) - { - case CODEC_LAW: - memset(buf_in8, (options.law=='a')?0x2a:0xff, len); /* silence */ - break; - - case CODEC_MONO: - case CODEC_8BIT: - case CODEC_STEREO: - memset(buf_in16, 0, len<<1); /* silence */ - break; - - default: - PERROR("Software error: no codec during silence\n"); - exit(-1); - } - goto mix_to_buffer; - } + if (!p_tone_name[0]) + return(0); /* if the file pointer is not open, we open it */ - if (p_tone_fh<0 && p_tone_fetched==NULL) - { - if (p_tone_dir[0]) - { + if (p_tone_fh<0 && p_tone_fetched==NULL) { + if (p_tone_dir[0]) { SPRINT(filename, "%s", p_tone_name); /* if file does not exist */ - if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left))) - { - SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name); + if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left))) { + SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name); /* if file does not exist */ - if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) - { + if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) { PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename); goto try_loop; } fhuse++; } - } else - { + } else { SPRINT(filename, "%s", p_tone_name); /* if file does not exist */ - if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) - { + if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) { PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename); goto try_loop; } fhuse++; } - codec_in = p_tone_codec; -//printf("\n\ncodec=%d\n\n\n", p_tone_codec); PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename); } read_more: /* file descriptor is open read data */ tone_left_before = p_tone_left; - if (p_tone_fh >= 0) - { - switch(codec_in) - { - case CODEC_LAW: - l = read_tone(p_tone_fh, buf_in8, codec_in, len, p_tone_size, &p_tone_left, p_tone_speed); - if (l<0 || l>len) /* paranoia */ - l=0; - buf_in8 += l; - len -= l; - break; - - case CODEC_8BIT: - l = read_tone(p_tone_fh, buf_in16, codec_in, len, p_tone_size, &p_tone_left, p_tone_speed); - if (l<0 || l>len) /* paranoia */ - l=0; - buf_in16 += l; - len -= l; - break; - - case CODEC_MONO: - l = read_tone(p_tone_fh, buf_in16, codec_in, len, p_tone_size, &p_tone_left, p_tone_speed); - if (l<0 || l>len) /* paranoia */ - l=0; - buf_in16 += l; - len -= l; - break; - - case CODEC_STEREO: - l = read_tone(p_tone_fh, buf_in16, codec_in, len, p_tone_size, &p_tone_left, p_tone_speed); - if (l<0 || l>len) /* paranoia */ - l=0; - buf_in16 += l; - len -= l; - break; - - default: - PERROR("Software error: current tone reading has no codec\n"); - exit(-1); - } + if (p_tone_fh >= 0) { + l = read_tone(p_tone_fh, buffer, p_tone_codec, len, p_tone_size, &p_tone_left, p_tone_speed); + if (l<0 || l>len) /* paranoia */ + l=0; + buffer += l; + len -= l; } - if (p_tone_fetched) - { - switch(codec_in) - { - case CODEC_LAW: - l = read_tone_fetched(&p_tone_fetched, buf_in8, codec_in, len, p_tone_size, &p_tone_left, p_tone_speed); - if (l<0 || l>len) /* paranoia */ - l=0; - buf_in8 += l; - len -= l; - break; - - case CODEC_MONO: - l = read_tone_fetched(&p_tone_fetched, buf_in16, codec_in, len, p_tone_size, &p_tone_left, p_tone_speed); - if (l<0 || l>len) /* paranoia */ - l=0; - buf_in16 += l; - len -= l; - break; - - default: - PERROR("Software error: current tone reading has no codec\n"); - exit(-1); - } + if (p_tone_fetched) { + l = read_tone_fetched(&p_tone_fetched, buffer, len, p_tone_size, &p_tone_left, p_tone_speed); + if (l<0 || l>len) /* paranoia */ + l=0; + buffer += l; + len -= l; } /* if counter is enabled, we check if we have a change */ - if (p_tone_counter && p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist)) - { + if (p_tone_counter && p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist)) { /* if we jumed to the next second */ - if (((p_tone_size-p_tone_left)/8000) != (p_tone_size-tone_left_before)/8000) - { + if (((p_tone_size-p_tone_left)/8000) != (p_tone_size-tone_left_before)/8000) { //printf("\nsize=%d left=%d\n\n",p_tone_size,p_tone_left); - struct message *message; + struct lcr_msg *message; message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER); message->param.counter.current = (p_tone_size-p_tone_left)/8000; message->param.counter.max = -1; @@ -968,10 +569,9 @@ read_more: } if (len==0) - goto mix_to_buffer; + return(length-len); - if (p_tone_fh >= 0) - { + if (p_tone_fh >= 0) { close(p_tone_fh); p_tone_fh = -1; fhuse--; @@ -982,808 +582,107 @@ read_more: nodata=0; /* if the file has 0-length */ - if (nodata>1) - { + if (nodata>1) { PDEBUG(DEBUG_PORT, "PORT(%s) 0-length loop: %s\n", p_name, filename); p_tone_name[0]=0; p_tone_dir[0]=0; - goto rest_is_silence; + return(length-len); } /* if eof is reached, or if the normal file cannot be opened, continue with the loop file if possible */ try_loop: - if (p_tone_eof && ACTIVE_EPOINT(p_epointlist)) - { - struct message *message; + if (p_tone_eof && ACTIVE_EPOINT(p_epointlist)) { + struct lcr_msg *message; message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_EOF); message_put(message); } - if (p_tone_dir[0]) - { + if (p_tone_dir[0]) { /* if file does not exist */ SPRINT(filename, "%s_loop", p_tone_name); - if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left))) - { - SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name); + if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left))) { + SPRINT(filename, "%s/%s/%s_loop", SHARE_DATA, p_tone_dir, p_tone_name); /* if file does not exist */ - if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) - { + if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) { PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename); p_tone_dir[0] = '\0'; p_tone_name[0] = '\0'; - // codec_in = CODEC_LAW; - goto rest_is_silence; + return(length-len); } fhuse++; } - } else - { + } else { SPRINT(filename, "%s_loop", p_tone_name); /* if file does not exist */ - if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) - { + if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) { PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename); p_tone_dir[0] = '\0'; p_tone_name[0] = '\0'; - // codec_in = CODEC_LAW; - goto rest_is_silence; + return(length-len); } fhuse++; } - codec_in = p_tone_codec; nodata++; PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename); /* now we have opened the loop */ goto read_more; - - /* ********** - * now we mix - * we take the buffer content and mix the mixer_buffer to what we got - */ -mix_to_buffer: - /* release all writepointer which will underrun, since writing of - * remote port data is too slow - */ - relation = p_mixer_rel; - readp = p_mixer_readp; - relationpointer = &(p_mixer_rel); - while(relation) /* if no relation, this is skipped */ - { - /* NOTE: if writep and readp are equal, the distance ist max - * == PORT_BUFFER - */ - if (((relation->mixer_writep-readp-1+PORT_BUFFER)%PORT_BUFFER)+1 <= length) /* underrun */ - { - /* remove port relation in order to resync. - * this is also caused by ports which do not transmit - * anymore. - */ -#ifdef MIXER_DEBUG - PERROR("PORT(%s) Buffer underrun, source port is sending too slow or stopped sending, removing relation. (readp=%d, writep=%d, PORT_BUFFER=%d)\n", p_name, readp, relation->mixer_writep, PORT_BUFFER); -#endif - - *relationpointer = relation->next; - memset(relation, 0, sizeof(struct mixer_relation)); - free(relation); - pmemuse--; - relation = *relationpointer; - continue; - } - relationpointer = &(relation->next); - relation = relation->next; - } - - /* if we do recording, we write the record data and the buffer data to the record fp and increment record_count */ - if (p_record) - switch (p_record_type) - { - case CODEC_MONO: - /* convert from mixer to uncompressed 16 bit mono audio */ - switch(codec_in) - { - case CODEC_MONO: - case CODEC_STEREO: - case CODEC_8BIT: - len = length; - mix_buffer = p_record_buffer; - rec_buffer = record_buffer; - buf_in16 = (signed short *)temp_buffer; - if (len+readp >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+readp-PORT_BUFFER; /* rest to read from buffer start */ - while(readp < PORT_BUFFER) - { /* mix till buffer end */ - sample = mix_buffer[readp]+*(buf_in16++); - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(rec_buffer++) = sample; - } - readp = 0; - } - while(len--) /* write rest */ - { /* mix till buffer end */ - sample = mix_buffer[readp]+*(buf_in16++); - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(rec_buffer++) = sample; - } - - /* restore (changed) read pointer for further use */ - readp = p_mixer_readp; - - /* now write the rec_buffer to the file */ - if (p_record_skip) - { - p_record_skip -= length; - if (p_record_skip < 0) - p_record_skip = 0; - } else - { - fwrite(record_buffer, (length<<1), 1, p_record); - p_record_length = (length<<1) + p_record_length; - } - break; - - case CODEC_LAW: - len = length; - mix_buffer = p_record_buffer; - rec_buffer = record_buffer; - buf_in8 = (unsigned char *)temp_buffer; - if (len+readp >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+readp-PORT_BUFFER; /* rest to read from buffer start */ - while(readp < PORT_BUFFER) - { /* mix till buffer end */ - sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)]; - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(rec_buffer++) = sample; - } - readp = 0; - } - while(len--) /* write rest */ - { /* mix till buffer end */ - sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)]; - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(rec_buffer++) = sample; - } - - /* restore (changed) read pointer for further use */ - readp = p_mixer_readp; - - /* now write the rec_buffer to the file */ - if (p_record_skip) - { - p_record_skip -= length; - if (p_record_skip < 0) - p_record_skip = 0; - } else - { - fwrite(record_buffer, (length<<1), 1, p_record); - p_record_length = (length<<1) + p_record_length; - } - break; - - } - break; - - case CODEC_STEREO: - /* convert from mixer to uncompressed 16 bit stereo audio */ - switch(codec_in) - { - case CODEC_MONO: - case CODEC_STEREO: - case CODEC_8BIT: - len = length; - mix_buffer = p_record_buffer; - mix_buffer2 = p_stereo_buffer; - rec_buffer = record_buffer; - buf_in16 = (signed short *)temp_buffer; - if (len+readp >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+readp-PORT_BUFFER; /* rest to read from buffer start */ - while(readp < PORT_BUFFER) - { /* mix till buffer end */ - sample = mix_buffer[readp]+*(buf_in16++); - mix_buffer[readp] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(rec_buffer++) = sample; - - *(rec_buffer++) = mix_buffer2[readp]; - mix_buffer2[readp++] = 0; - } - readp = 0; - } - while(len--) /* write rest */ - { /* mix till buffer end */ - sample = mix_buffer[readp]+*(buf_in16++); - mix_buffer[readp] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(rec_buffer++) = sample; - *(rec_buffer++) = mix_buffer2[readp]; - mix_buffer2[readp++] = 0; - } - - /* restore (changed) read pointer for further use */ - readp = p_mixer_readp; - - /* now write the rec_buffer to the file */ - if (p_record_skip) - { - p_record_skip -= length; - if (p_record_skip < 0) - p_record_skip = 0; - } else - { - fwrite(record_buffer, (length<<2), 1, p_record); - p_record_length = (length<<2) + p_record_length; - } - break; - - case CODEC_LAW: - len = length; - mix_buffer = p_record_buffer; - mix_buffer2 = p_stereo_buffer; - rec_buffer = record_buffer; - buf_in8 = (unsigned char *)temp_buffer; - if (len+readp >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+readp-PORT_BUFFER; /* rest to read from buffer start */ - while(readp < PORT_BUFFER) - { /* mix till buffer end */ - sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)]; - mix_buffer[readp] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(rec_buffer++) = sample; - - *(rec_buffer++) = mix_buffer2[readp]; - mix_buffer2[readp++] = 0; - } - readp = 0; - } - while(len--) /* write rest */ - { /* mix till buffer end */ - sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)]; - mix_buffer[readp] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(rec_buffer++) = sample; - *(rec_buffer++) = mix_buffer2[readp]; - mix_buffer2[readp++] = 0; - } - - /* restore (changed) read pointer for further use */ - readp = p_mixer_readp; - - /* now write the rec_buffer to the file */ - if (p_record_skip) - { - p_record_skip -= length; - if (p_record_skip < 0) - p_record_skip = 0; - } else - { - fwrite(record_buffer, (length<<2), 1, p_record); - p_record_length = (length<<2) + p_record_length; - } - break; - } - break; - - case CODEC_8BIT: - /* convert from mixer to uncompressed 8 bit mono audio */ - switch(codec_in) - { - case CODEC_MONO: - case CODEC_STEREO: - case CODEC_8BIT: - len = length; - mix_buffer = p_record_buffer; - rec8_buffer = (unsigned char *)record_buffer; - buf_in16 = (signed short *)temp_buffer; - if (len+readp >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+readp-PORT_BUFFER; /* rest to read from buffer start */ - while(readp < PORT_BUFFER) - { /* mix till buffer end */ - sample = mix_buffer[readp]+*(buf_in16++); - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(rec8_buffer++) = (sample>>8)+0x80; - } - readp = 0; - } - while(len--) /* write rest */ - { /* mix till buffer end */ - sample = mix_buffer[readp]+*(buf_in16++); - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(rec8_buffer++) = (sample>>8)+0x80; - } - - /* restore (changed) read pointer for further use */ - readp = p_mixer_readp; - - /* now write the rec_buffer to the file */ - if (p_record_skip) - { - p_record_skip -= length; - if (p_record_skip < 0) - p_record_skip = 0; - } else - { - fwrite(record_buffer, (length), 1, p_record); - p_record_length = (length) + p_record_length; - } - break; - - case CODEC_LAW: - len = length; - mix_buffer = p_record_buffer; - rec8_buffer = (unsigned char *)record_buffer; - buf_in8 = (unsigned char *)temp_buffer; - if (len+readp >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+readp-PORT_BUFFER; /* rest to read from buffer start */ - while(readp < PORT_BUFFER) - { /* mix till buffer end */ - sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)]; - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(rec8_buffer++) = (sample>>8)+0x80; - } - readp = 0; - } - while(len--) /* write rest */ - { /* mix till buffer end */ - sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)]; - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(rec8_buffer++) = (sample>>8)+0x80; - } - - /* restore (changed) read pointer for further use */ - readp = p_mixer_readp; - - /* now write the rec_buffer to the file */ - if (p_record_skip) - { - p_record_skip -= length; - if (p_record_skip < 0) - p_record_skip = 0; - } else - { - fwrite(record_buffer, (length), 1, p_record); - p_record_length = (length) + p_record_length; - } - break; - - } - break; - - case CODEC_LAW: - case CODEC_OFF: /* if no codec is specified, the recorded data will be stored as LAW */ - /* convert from mixer to law */ - switch(codec_in) - { - case CODEC_MONO: - case CODEC_STEREO: - case CODEC_8BIT: - len = length; - mix_buffer = p_record_buffer; - rec8_buffer = (unsigned char *)record_buffer; - buf_in16 = (signed short *)temp_buffer; - if (len+readp >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+readp-PORT_BUFFER; /* rest to read from buffer start */ - while(readp < PORT_BUFFER) - { /* mix till buffer end */ - sample = mix_buffer[readp]+*(buf_in16++); - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(rec8_buffer++) = audio_s16_to_law[sample & 0xffff]; - } - readp = 0; - } - while(len--) /* write rest */ - { /* mix till buffer end */ - sample = mix_buffer[readp]+*(buf_in16++); - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(rec8_buffer++) = audio_s16_to_law[sample & 0xffff]; - } - - /* restore (changed) read pointer for further use */ - readp = p_mixer_readp; - - /* now write the rec_buffer to the file */ - if (p_record_skip) - { - p_record_skip -= length; - if (p_record_skip < 0) - p_record_skip = 0; - } else - { - fwrite(record_buffer, (length), 1, p_record); - p_record_length = (length) + p_record_length; - } - break; - - case CODEC_LAW: - len = length; - mix_buffer = p_record_buffer; - rec8_buffer = (unsigned char *)record_buffer; - buf_in8 = (unsigned char *)temp_buffer; - if (len+readp >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+readp-PORT_BUFFER; /* rest to read from buffer start */ - while(readp < PORT_BUFFER) - { /* mix till buffer end */ - sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)]; - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(rec8_buffer++) = audio_s16_to_law[sample & 0xffff]; - } - readp = 0; - } - while(len--) /* write rest */ - { /* mix till buffer end */ - sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)]; - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(rec8_buffer++) = audio_s16_to_law[sample & 0xffff]; - } - - /* restore (changed) read pointer for further use */ - readp = p_mixer_readp; - - /* now write the rec_buffer to the file */ - if (p_record_skip) - { - p_record_skip -= length; - if (p_record_skip < 0) - p_record_skip = 0; - } else - { - fwrite(record_buffer, (length), 1, p_record); - p_record_length = (length) + p_record_length; - } - break; - } - break; - } - - /* if we have no transmitting relation, we do not need read mixer */ - if (!p_mixer_rel) - { /* nothing mixed to(no rel), so we are just done */ - if (compressed) - { - /* compress to law */ - len = length; - buf_in8 = (unsigned char *)temp_buffer; - buf_in16 = (signed short *)temp_buffer; - buf_out16 = (signed short *)buffer; - switch(codec_in) - { - case CODEC_MONO: - case CODEC_STEREO: - case CODEC_8BIT: - while(len--) - *(buffer++) = audio_s16_to_law[*(buf_in16++) & 0xffff]; - break; - - case CODEC_LAW: - memcpy(buffer, temp_buffer, length); - break; - - default: - PERROR("Software error: current tone for unmixed & uncompressed, has no codec\n"); - exit(-1); - } - } else - { - /* uncompress law files */ - len = length; - buf_in8 = (unsigned char *)temp_buffer; - buf_in16 = (signed short *)temp_buffer; - buf_out16 = (signed short *)buffer; - switch(codec_in) - { - case CODEC_MONO: - case CODEC_STEREO: - case CODEC_8BIT: - while(len--) - (*(buf_out16++)) = *(buf_in16++); - break; - - case CODEC_LAW: - while(len--) - (*(buf_out16++)) = audio_law_to_s32[*(buf_in8++)]; - break; - - default: - PERROR("Software error: current tone for unmixed & uncompressed, has no codec\n"); - exit(-1); - } - } - return(length); - } - -// PDEBUG(DEBUG_PORT, "PORT(%s) mixing %d bytes. (readp=%d, PORT_BUFFER=%d)\n", p_name, length, readp, PORT_BUFFER); - /* now we got our stuff and we'll mix it baby */ - - len = length; - mix_buffer = p_mixer_buffer; - if (compressed) - { - /* convert from mixer to compressed law data */ - switch(codec_in) - { - case CODEC_MONO: - case CODEC_STEREO: - case CODEC_8BIT: - buf_in16 = (signed short *)temp_buffer; - if (len+readp >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+readp-PORT_BUFFER; /* rest to read from buffer start */ - while(readp < PORT_BUFFER) - { /* mix till buffer end */ - sample = mix_buffer[readp]+*(buf_in16++); - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(buffer++) = audio_s16_to_law[sample & 0xffff]; - } - readp = 0; - } - while(len--) /* write rest */ - { /* mix till buffer end */ - sample = mix_buffer[readp]+*(buf_in16++); - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(buffer++) = audio_s16_to_law[sample & 0xffff]; - } - break; - - case CODEC_LAW: - buf_in8 = (unsigned char *)temp_buffer; - if (len+readp >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+readp-PORT_BUFFER; /* rest to read from buffer start */ - while(readp < PORT_BUFFER) - { /* mix till buffer end */ - sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)]; - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(buffer++) = audio_s16_to_law[sample & 0xffff]; - } - readp = 0; - } - while(len--) /* write rest */ - { /* mix till buffer end */ - sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)]; - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(buffer++) = audio_s16_to_law[sample & 0xffff]; - } - break; - - default: - PERROR("Software error: current tone for compressed data has no codec\n"); - exit(-1); - } - } else - { - /* convert from mixer to uncompressed 16 bit audio */ - switch(codec_in) - { - case CODEC_MONO: - case CODEC_STEREO: - case CODEC_8BIT: - buf_in16 = (signed short *)temp_buffer; - buf_out16 = (signed short *)buffer; - if (len+readp >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+readp-PORT_BUFFER; /* rest to read from buffer start */ - while(readp < PORT_BUFFER) - { /* mix till buffer end */ - sample = mix_buffer[readp]+*(buf_in16++); - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(buf_out16++) = sample; - } - readp = 0; - } - while(len--) /* write rest */ - { /* mix till buffer end */ - sample = mix_buffer[readp]+*(buf_in16++); - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(buf_out16++) = sample; - } - break; - - case CODEC_LAW: - buf_in8 = (unsigned char *)temp_buffer; - buf_out16 = (signed short *)buffer; - if (len+readp >= PORT_BUFFER) /* data hits the buffer end */ - { - len = len+readp-PORT_BUFFER; /* rest to read from buffer start */ - while(readp < PORT_BUFFER) - { /* mix till buffer end */ - sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)]; - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(buf_out16++) = sample; - } - readp = 0; - } - while(len--) /* write rest */ - { /* mix till buffer end */ - sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)]; - mix_buffer[readp++] = 0; - if (sample < -32767) - sample = -32767; - if (sample > 32767) - sample = 32767; - *(buf_out16++) = sample; - } - break; - - default: - PERROR("Software error: current tone for uncompressed data has no codec\n"); - exit(-1); - } - } - - p_mixer_readp = readp; /* new read pointer */ - - return(length); } -/* dummy handler */ -int Port::handler(void) -{ - return(0); -} - -/* endpoint sends messages to the port - * this is called by the message_epoint inherited by child classes - * therefor a return=1 means: stop, no more processing +/* Endpoint sends messages to the port + * This is called by the message_epoint, inherited by child classes. + * Therefor a return 1 means: "already handled here" */ -//extern struct message *dddebug; -int Port::message_epoint(unsigned long epoint_id, int message_id, union parameter *param) +//extern struct lcr_msg *dddebug; +int Port::message_epoint(unsigned int epoint_id, int message_id, union parameter *param) { /* check if we got audio data from one remote port */ - switch(message_id) - { - case MESSAGE_TONE: /* play tone */ - 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); + switch(message_id) { + case MESSAGE_TONE: /* play tone */ + PDEBUG(DEBUG_PORT, "PORT(%s) setting tone '%s' dir '%s'\n", p_name, param->tone.name, param->tone.dir); set_tone(param->tone.dir,param->tone.name); - return(1); + return 1; - case MESSAGE_DATA: -//printf("port=%s, epoint=%d\n",p_cardname, epoint->e_serial); - mixer(param); - return(1); - - case MESSAGE_VBOX_TONE: /* play tone of answering machine */ + case MESSAGE_VBOX_TONE: /* play tone of answering machine */ PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine tone '%s' '%s'\n", p_name, param->tone.dir, param->tone.name); set_vbox_tone(param->tone.dir, param->tone.name); - return(1); + return 1; - case MESSAGE_VBOX_PLAY: /* play recording of answering machine */ + case MESSAGE_VBOX_PLAY: /* play recording of answering machine */ PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine file to play '%s' (offset %d seconds)\n", p_name, param->play.file, param->play.offset); set_vbox_play(param->play.file, param->play.offset); - return(1); + return 1; - case MESSAGE_VBOX_PLAY_SPEED: /* set speed of playback (recording of answering machine) */ + case MESSAGE_VBOX_PLAY_SPEED: /* set speed of playback (recording of answering machine) */ PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine playback speed %d (times)\n", p_name, param->speed); set_vbox_speed(param->speed); - return(1); - - } - - return(0); -} - - -/* - * special function generate individual isdn debug logs - */ -void Port::printisdn(char *fmt, ...) -{ - char buffer[4096]; - char name[128]; - va_list args; - FILE *fp; + return 1; + + case MESSAGE_BRIDGE: /* create / join / leave / destroy bridge */ + PDEBUG(DEBUG_PORT, "PORT(%s) bridging to id %d\n", p_name, param->bridge_id); + bridge(param->bridge_id); + return 1; + +#ifdef WITH_VOOTP + case MESSAGE_VOOTP: /* enable / disable VoOTP */ + PDEBUG(DEBUG_PORT, "PORT(%s) VoOTP enabled: %d\n", p_name, param->vootp.enable); + set_vootp(¶m->vootp); + return 1; +#endif - va_start(args,fmt); - VUNPRINT(buffer,sizeof(buffer)-1,fmt,args); - buffer[sizeof(buffer)-1]=0; - va_end(args); + case MESSAGE_DOV_REQUEST: /* Data-Over-Voice message */ + PDEBUG(DEBUG_PORT, "PORT(%s) sending data over voice message (len=%d)\n", p_name, param->dov.length); + dov_sendmsg(param->dov.data, param->dov.length, (enum dov_type)param->dov.type, param->dov.level); + return 1; - PDEBUG_RUNTIME(NULL, 0, DEBUG_PORT, "PORT(%s serial=%ld): %s\n", p_name, p_serial, buffer); - if (options.deb & DEBUG_LOG) - { - SPRINT(name, "%s/debug_%s.log", INSTALL_DATA, p_name); - if (!(fp = fopen(name, "a"))) - return; - - fprintf(fp, "%04d.%02d.%02d %02d:%02d:%02d %s(%ld): %s", 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, p_name, p_serial, buffer); - fclose(fp); + case MESSAGE_DOV_LISTEN: /* Data-Over-Voice listen order */ + PDEBUG(DEBUG_PORT, "PORT(%s) sending data over voice listen order\n", p_name); + dov_listen((enum dov_type)param->dov.type); + return 1; } + + return 0; } @@ -1791,8 +690,8 @@ void Port::printisdn(char *fmt, ...) struct fmt { unsigned short stereo; /* 1 = mono, 2 = stereo */ unsigned short channels; /* number of channels */ - unsigned long sample_rate; /* sample rate */ - unsigned long data_rate; /* data rate */ + unsigned int sample_rate; /* sample rate */ + unsigned int data_rate; /* data rate */ unsigned short bytes_sample; /* bytes per sample (all channels) */ unsigned short bits_sample; /* bits per sample (one channel) */ }; @@ -1803,36 +702,35 @@ struct fmt { * written before close, because we do not know the size yet) * type=1 record annoucement, type=0 record audio stream, type=2 record vbox */ -int Port::open_record(int type, int vbox, int skip, char *terminal, int anon_ignore, char *vbox_email, int vbox_email_file) +int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ignore, const char *vbox_email, int vbox_email_file) { /* RIFFxxxxWAVEfmt xxxx(fmt-size)dataxxxx... */ char dummyheader[8+4+8+sizeof(fmt)+8]; char filename[256]; + time_t now; + struct tm *now_tm; + int __attribute__((__unused__)) ret; - if (!terminal) - { - PERROR("Port(%d) not a terminal\n", p_serial); + if (!extension) { + PERROR("Port(%d) not an extension\n", p_serial); return(0); } - SCPY(p_record_extension, terminal); + SCPY(p_record_extension, extension); p_record_anon_ignore = anon_ignore; SCPY(p_record_vbox_email, vbox_email); p_record_vbox_email_file = vbox_email_file; - if (p_record) - { + if (p_record) { PERROR("Port(%d) already recording\n", p_serial); return(0); } if (vbox != 0) - SPRINT(filename, "%s/%s/%s/vbox", INSTALL_DATA, options.extensions_dir, p_record_extension); + SPRINT(filename, "%s/%s/vbox", EXTENSION_DATA, p_record_extension); else - SPRINT(filename, "%s/%s/%s/recordings", INSTALL_DATA, options.extensions_dir, p_record_extension); - if (mkdir(filename, 0755) < 0) - { - if (errno != EEXIST) - { + SPRINT(filename, "%s/%s/recordings", EXTENSION_DATA, p_record_extension); + if (mkdir(filename, 0755) < 0) { + if (errno != EEXIST) { PERROR("Port(%d) cannot create directory '%s'\n", p_serial, filename); return(0); } @@ -1840,10 +738,12 @@ int Port::open_record(int type, int vbox, int skip, char *terminal, int anon_ign if (vbox == 1) UPRINT(strchr(filename,'\0'), "/announcement"); - else + else { + time(&now); + now_tm = localtime(&now); 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); - if (vbox == 2) - { + } + if (vbox == 2) { p_record_vbox_year = now_tm->tm_year; p_record_vbox_mon = now_tm->tm_mon; p_record_vbox_mday = now_tm->tm_mday; @@ -1853,30 +753,29 @@ int Port::open_record(int type, int vbox, int skip, char *terminal, int anon_ign /* check, if file exists (especially when an extension calls the same extension) */ if (vbox != 1) - if ((p_record = fopen(filename, "r"))) - { + if ((p_record = fopen(filename, "r"))) { fclose(p_record); SCAT(filename, "_2nd"); } p_record = fopen(filename, "w"); - if (!p_record) - { + if (!p_record) { PERROR("Port(%d) cannot record because file cannot be opened '%s'\n", p_serial, filename); return(0); } + update_rxoff(); fduse++; p_record_type = type; p_record_vbox = vbox; p_record_skip = skip; p_record_length = 0; - switch(p_record_type) - { + switch(p_record_type) { case CODEC_MONO: case CODEC_STEREO: case CODEC_8BIT: - fwrite(dummyheader, sizeof(dummyheader), 1, p_record); + memset(&dummyheader, 0, sizeof(dummyheader)); + ret = fwrite(dummyheader, sizeof(dummyheader), 1, p_record); break; case CODEC_LAW: @@ -1892,11 +791,10 @@ int Port::open_record(int type, int vbox, int skip, char *terminal, int anon_ign /* * close the recoding file, put header in front and rename */ -void Port::close_record(int beep) +void Port::close_record(int beep, int mute) { - static signed long beep_mono[] = {-10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000}; - 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}; - unsigned long size, wsize; + static signed short beep_mono[256]; + unsigned int size = 0, wsize = 0; struct fmt fmt; char filename[512], indexname[512]; FILE *fp; @@ -1904,18 +802,19 @@ void Port::close_record(int beep) char number[256], callerid[256]; char *p; struct caller_info callerinfo; - char *valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_.-!$%&/()=+*;~"; + const char *valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_.-!$%&/()=+*;~"; + int __attribute__((__unused__)) ret; if (!p_record) return; + 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); memcpy(&callerinfo, &p_callerinfo, sizeof(struct caller_info)); - apply_callerid_restriction(p_record_anon_ignore, -1, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.voip, callerinfo.intern, callerinfo.name); +// apply_callerid_restriction(p_record_anon_ignore, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.extension, callerinfo.name); - SCPY(number, p_dialinginfo.number); - SCPY(callerid, numberrize_callerinfo(callerinfo.id, callerinfo.ntype)); - if (callerid[0] == '\0') - { + SCPY(number, p_dialinginfo.id); + SCPY(callerid, numberrize_callerinfo(callerinfo.id, callerinfo.ntype, options.national, options.international)); + if (callerid[0] == '\0') { if (callerinfo.present == INFO_PRESENT_RESTRICTED) UCPY(callerid,"anonymous"); else @@ -1937,61 +836,44 @@ void Port::close_record(int beep) *(p++) = 'x'; i = 0; ii = strlen(callerid); - while(i < ii) - { + while(i < ii) { if (!strchr(valid_chars, callerid[i])) callerid[i] = '_'; i++; } i = 0; ii = strlen(number); - while(i < ii) - { + while(i < ii) { if (!strchr(valid_chars, number[i])) number[i] = '_'; i++; } + /* mute */ + if (mute && p_record_type==CODEC_MONO) { + i = p_record_length; + if (i > mute) + i = mute; + fseek(p_record, -(i<<1), SEEK_END); + p_record_length -= (i<<1); + } /* add beep to the end of recording */ - if (beep) - switch(p_record_type) - { - case CODEC_MONO: + if (beep && p_record_type==CODEC_MONO) { i = 0; - while(i < beep) - { - fwrite(beep_mono, sizeof(beep_mono), 1, p_record); - i += sizeof(beep_mono); - p_record_length += sizeof(beep_mono); + while(i < 256) { + beep_mono[i] = (signed short)(sin((double)i / 5.688888888889 * 2.0 * 3.1415927) * 2000.0); + i++; } - break; - case CODEC_8BIT: - i = 0; - while(i < beep) - { - fwrite(beep_8bit, sizeof(beep_8bit), 1, p_record); - i += sizeof(beep_8bit); - p_record_length += sizeof(beep_8bit); - } - break; -#if 0 - case CODEC_LAW: i = 0; - while(i < beep) - { - fwrite(beep_law, sizeof(beep_law), 1, p_record); - i += sizeof(beep_law); - p_record_length += sizeof(beep_law); + while(i < beep) { + ret = fwrite(beep_mono, sizeof(beep_mono), 1, p_record); + i += sizeof(beep_mono); + p_record_length += sizeof(beep_mono); } - break; -#endif - default: - PERROR("codec %d not supported for beep adding\n", p_record_type); } /* complete header */ - switch(p_record_type) - { + switch(p_record_type) { case CODEC_MONO: case CODEC_STEREO: case CODEC_8BIT: @@ -2015,9 +897,8 @@ void Port::close_record(int beep) fprintf(p_record, "WAVE"); /* fmt */ - fprintf(p_record, "fmt %c%c%c%c", sizeof(fmt), 0, 0, 0); - switch(p_record_type) - { + fprintf(p_record, "fmt %c%c%c%c", (unsigned int)sizeof(fmt), 0, 0, 0); + switch(p_record_type) { case CODEC_MONO: fmt.stereo = 1; fmt.channels = 1; @@ -2045,7 +926,7 @@ void Port::close_record(int beep) fmt.bits_sample = 8; /* one channel */ break; } - fwrite(&fmt, sizeof(fmt), 1, p_record); + ret = fwrite(&fmt, sizeof(fmt), 1, p_record); /* data */ 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)); @@ -2064,31 +945,23 @@ void Port::close_record(int beep) else SPRINT(filename, "%s_%s-%s.isdn", p_record_filename, callerid, number); break; - - default: - if (p_record_vbox == 1) - SPRINT(filename, "%s.unknown", p_record_filename); - else - SPRINT(filename, "%s_%s-%s.unknown", p_record_filename, callerid, number); } fclose(p_record); fduse--; p_record = NULL; + update_rxoff(); - if (rename(p_record_filename, filename) < 0) - { + if (rename(p_record_filename, filename) < 0) { PERROR("Port(%d) cannot rename from '%s' to '%s'\n", p_serial, p_record_filename, filename); return; } 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); - if (p_record_vbox == 2) - { - SPRINT(indexname, "%s/%s/%s/vbox/index", INSTALL_DATA, options.extensions_dir, p_record_extension); - if ((fp = fopen(indexname,"a"))) - { + if (p_record_vbox == 2) { + SPRINT(indexname, "%s/%s/vbox/index", EXTENSION_DATA, p_record_extension); + if ((fp = fopen(indexname,"a"))) { fduse++; /* remove path from file name */ @@ -2099,18 +972,551 @@ void Port::close_record(int beep) fclose(fp); fduse--; - } else - { + } else { PERROR("Port(%d) cannot open index file '%s' to append.\n", p_serial, indexname); } /* send email with sample*/ - if (p_record_vbox_email[0]) - { - send_mail(p_record_vbox_email_file?filename:(char *)"", callerid, callerinfo.intern, 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); + if (p_record_vbox_email[0]) { + 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); } } } +/* + * recording function + * Records all data from down and from up into one single stream. + * Both streams may have gaps or jitter. + * A Jitter buffer for both streams is used to compensate jitter. + * + * If one stream (dir) received packets, they are stored to a + * buffer to wait for the other stream (dir), so both streams can + * be combined. If the buffer is full, it's content is written + * without mixing stream. (assuming only one stram (dir) exists.) + * A flag is used to indicate what stream is currently in buffer. + * + * NOTE: First stereo sample (odd) is from down, second is from up. + */ +void Port::record(unsigned char *data, int length, int dir_fromup) +{ + unsigned char write_buffer[1024], *d; + signed short *s; + int free, i, ii; + signed int sample; + int __attribute__((__unused__)) ret; + + /* no recording */ + if (!p_record || !length) + return; + + /* skip data from local caller (dtmf input) */ + if (p_record_skip && !dir_fromup) { + /* more to skip than we have */ + if (p_record_skip > length) { + p_record_skip -= length; + return; + } + /* less to skip */ + data += p_record_skip; + length -= p_record_skip; + p_record_skip = 0; + } + +//printf("dir=%d len=%d\n", dir_fromup, length); + + free = ((p_record_buffer_readp - p_record_buffer_writep - 1) & RECORD_BUFFER_MASK); +//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); + + /* the buffer stores the same data stream */ + if (dir_fromup == p_record_buffer_dir) { +same_again: + +//printf("same free=%d length=%d\n", free, length); + /* first write what we can to the buffer */ + while(free && length) { + p_record_buffer[p_record_buffer_writep] = audio_law_to_s32[*data++]; + p_record_buffer_writep = (p_record_buffer_writep + 1) & RECORD_BUFFER_MASK; + free--; + length--; + } + /* all written, so we return */ + if (!length) + return; + /* still data left, buffer is full, so we need to write a chunk to file */ + switch(p_record_type) { + case CODEC_MONO: + s = (signed short *)write_buffer; + i = 0; + while(i < 256) { + *s++ = p_record_buffer[p_record_buffer_readp]; + p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK; + i++; + } + ret = fwrite(write_buffer, 512, 1, p_record); + p_record_length += 512; + break; + + case CODEC_STEREO: + s = (signed short *)write_buffer; + if (p_record_buffer_dir) { + i = 0; + while(i < 256) { + *s++ = 0; /* nothing from down */ + *s++ = p_record_buffer[p_record_buffer_readp]; + p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK; + i++; + } + } else { + i = 0; + while(i < 256) { + *s++ = p_record_buffer[p_record_buffer_readp]; + *s++ = 0; /* nothing from up */ + p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK; + i++; + } + } + ret = fwrite(write_buffer, 1024, 1, p_record); + p_record_length += 1024; + break; + + case CODEC_8BIT: + d = write_buffer; + i = 0; + while(i < 256) { + *d++ = ((unsigned short)(p_record_buffer[p_record_buffer_readp]+0x8000)) >> 8; + p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK; + i++; + } + ret = fwrite(write_buffer, 512, 1, p_record); + p_record_length += 512; + break; + + case CODEC_LAW: + d = write_buffer; + i = 0; + while(i < 256) { + *d++ = audio_s16_to_law[p_record_buffer[p_record_buffer_readp] & 0xffff]; + p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK; + i++; + } + ret = fwrite(write_buffer, 256, 1, p_record); + p_record_length += 256; + break; + } + /* because we still have data, we write again */ + free += 256; + goto same_again; + } + /* the buffer stores the other stream */ + +different_again: + /* if buffer empty, change it */ + if (p_record_buffer_readp == p_record_buffer_writep) { + p_record_buffer_dir = dir_fromup; + goto same_again; + } + /* how much data can we mix ? */ + ii = (p_record_buffer_writep - p_record_buffer_readp) & RECORD_BUFFER_MASK; + if (length < ii) + ii = length; + + if (ii > 256) + ii = 256; +//printf("same ii=%d length=%d\n", ii, length); +//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); + + /* write data mixed with the buffer */ + switch(p_record_type) { + case CODEC_MONO: + s = (signed short *)write_buffer; + i = 0; + while(i < ii) { + sample = p_record_buffer[p_record_buffer_readp] + + audio_law_to_s32[*data++]; + p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK; + if (sample < SHORT_MIN) sample = SHORT_MIN; + if (sample > SHORT_MAX) sample = SHORT_MAX; + *s++ = sample; + i++; + } + ret = fwrite(write_buffer, ii<<1, 1, p_record); + p_record_length += (ii<<1); + break; + + case CODEC_STEREO: + s = (signed short *)write_buffer; + if (p_record_buffer_dir) { + i = 0; + while(i < ii) { + *s++ = audio_law_to_s32[*data++]; + *s++ = p_record_buffer[p_record_buffer_readp]; + p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK; + i++; + } + } else { + i = 0; + while(i < ii) { + *s++ = p_record_buffer[p_record_buffer_readp]; + *s++ = audio_law_to_s32[*data++]; + p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK; + i++; + } + } + ret = fwrite(write_buffer, ii<<2, 1, p_record); + p_record_length += (ii<<2); + break; + + case CODEC_8BIT: + d = write_buffer; + i = 0; + while(i < ii) { + sample = p_record_buffer[p_record_buffer_readp] + + audio_law_to_s32[*data++]; + p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK; + if (sample < SHORT_MIN) sample = SHORT_MIN; + if (sample > SHORT_MAX) sample = SHORT_MAX; + *d++ = (sample+0x8000) >> 8; + i++; + } + ret = fwrite(write_buffer, ii, 1, p_record); + p_record_length += ii; + break; + + case CODEC_LAW: + d = write_buffer; + i = 0; + while(i < ii) { + sample = p_record_buffer[p_record_buffer_readp] + + audio_law_to_s32[*data++]; + p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK; + if (sample < SHORT_MIN) sample = SHORT_MIN; + if (sample > SHORT_MAX) sample = SHORT_MAX; + *d++ = audio_s16_to_law[sample & 0xffff]; + i++; + } + ret = fwrite(write_buffer, ii, 1, p_record); + p_record_length += ii; + break; + } + length -= ii; + /* still data */ + if (length) + goto different_again; + /* no data (maybe buffer) */ + return; + +} + +void Port::tap(unsigned char *data, int length, int dir_fromup) +{ +} + +void Port::update_rxoff(void) +{ +} + +void Port::update_load(void) +{ +} + + +/* + * bridge handling + */ + +int bridge_timeout(struct lcr_timer *timer, void *instance, int index); + +static void remove_bridge(struct port_bridge *bridge, class Port *port) +{ + struct port_bridge **temp = &p_bridge_first; + while (*temp) { + if (*temp == bridge) { + struct port_bridge_member **memberp = &bridge->first, *member; + + /* loop until we are found */ + while(*memberp) { + if ((*memberp)->port == port) { + member = *memberp; + *memberp = member->next; + FREE(member, sizeof(struct port_bridge_member)); + memuse--; +#ifndef TEST_CONFERENCE + if (bridge->first && bridge->first->next && !bridge->first->next->next) { +#else + if (bridge->first && !bridge->first->next) { +#endif + PDEBUG(DEBUG_PORT, "bridge %u is no conference anymore\n", bridge->bridge_id); + del_timer(&bridge->timer); + } + break; + } + memberp = &((*memberp)->next); + } + /* if bridge is empty, remove it */ + if (bridge->first == NULL) { + PDEBUG(DEBUG_PORT, "Remove bridge %u\n", bridge->bridge_id); + *temp = bridge->next; + FREE(bridge, sizeof(struct port_bridge)); + memuse--; + } + return; + } + temp = &((*temp)->next); + } + PERROR("Bridge %p not found in list\n", bridge); +} + +void Port::bridge(unsigned int bridge_id) +{ + struct port_bridge_member **memberp; + + /* Remove bridge, if we leave bridge or if we join a different bridge. */ + if (p_bridge && bridge_id != p_bridge->bridge_id) { + PDEBUG(DEBUG_PORT, "Remove port %u from bridge %u, because out new bridge is %u\n", p_serial, p_bridge->bridge_id, bridge_id); + remove_bridge(p_bridge, this); + p_bridge = NULL; + } + + /* if we leave bridge */ + if (!bridge_id) + return; + + /* find bridge */ + if (!p_bridge) { + struct port_bridge *temp = p_bridge_first; + + while (temp) { + if (temp->bridge_id == bridge_id) + break; + temp = temp->next; + } + p_bridge = temp; + if (p_bridge) + PDEBUG(DEBUG_PORT, "Port %d found existing bridge %u.\n", p_serial, p_bridge->bridge_id); + } + + /* create bridge */ + if (!p_bridge) { + struct port_bridge **temp = &p_bridge_first; + + p_bridge = (struct port_bridge *) MALLOC(sizeof(struct port_bridge)); + memuse++; + p_bridge->bridge_id = bridge_id; + + /* attach bridge instance to list */ + while (*temp) + temp = &((*temp)->next); + *temp = p_bridge; + PDEBUG(DEBUG_PORT, "Port %d creating not existing bridge %u.\n", p_serial, p_bridge->bridge_id); + } + + /* attach to bridge */ + memberp = &p_bridge->first; + while(*memberp) { + if ((*memberp)->port == this) { + /* already joined */ + return; + } + memberp = &((*memberp)->next); + } + *memberp = (struct port_bridge_member *) MALLOC(sizeof(struct port_bridge_member)); + memuse++; + (*memberp)->port = this; + /* check if bridge becomes a conference */ +#ifndef TEST_CONFERENCE + if (p_bridge->first->next && p_bridge->first->next->next && !p_bridge->first->next->next->next) { + p_bridge->first->next->next->write_p = 0; + p_bridge->first->next->next->min_space = 0; + memset(p_bridge->first->next->next->buffer, silence, sizeof((*memberp)->buffer)); +#else + if (p_bridge->first->next && !p_bridge->first->next->next) { +#endif + p_bridge->first->next->write_p = 0; + p_bridge->first->next->min_space = 0; + memset(p_bridge->first->next->buffer, silence, sizeof((*memberp)->buffer)); + p_bridge->first->write_p = 0; + p_bridge->first->min_space = 0; + memset(p_bridge->first->buffer, silence, sizeof((*memberp)->buffer)); + memset(p_bridge->sum_buffer, 0, sizeof(p_bridge->sum_buffer)); + p_bridge->read_p = 0; + add_timer(&p_bridge->timer, bridge_timeout, p_bridge, 0); + schedule_timer(&p_bridge->timer, 0, 20000); /* 20 MS */ + p_bridge->sample_count = 0; + PDEBUG(DEBUG_PORT, "bridge %u became a conference\n", p_bridge->bridge_id); + } +} + +/* send data to remote Port or add to sum buffer */ +int Port::bridge_tx(unsigned char *data, int len) +{ + int write_p, space; + struct port_bridge_member *member; + signed long *sum; + unsigned char *buf; + +#ifdef WITH_VOOTP + if (p_vootp) + vootp_encrypt_stream(p_vootp, data, len); +#endif + + /* less than two ports, so drop */ + if (!p_bridge || !p_bridge->first || !p_bridge->first->next) + return -EIO; +#ifndef TEST_CONFERENCE + /* two ports, so bridge */ + if (!p_bridge->first->next->next) { + if (p_bridge->first->port == this) + return p_bridge->first->next->port->bridge_rx(data, len); + if (p_bridge->first->next->port == this) + return p_bridge->first->port->bridge_rx(data, len); + return -EINVAL; + } +#endif + /* more than two ports... */ + member = p_bridge->first; + while (member) { + if (member->port == this) + break; + member = member->next; + } + if (!member) + return -EINVAL; + write_p = member->write_p; + /* calculate space, so write pointer will not overrun (or reach) read pointer in ring buffer */ + space = (p_bridge->read_p - write_p - 1) & (BRIDGE_BUFFER - 1); + /* clip len, if it does not fit */ + if (space < len) + len = space; + /* apply audio samples to sum buffer */ + sum = p_bridge->sum_buffer; + buf = member->buffer; + while (len--) { + sum[write_p] += audio_law_to_s32[*data]; + buf[write_p] = *data++; + write_p = (write_p + 1) & (BRIDGE_BUFFER - 1); + } + /* raise write pointer */ + member->write_p = write_p; + + return 0; +} + +int bridge_timeout(struct lcr_timer *timer, void *instance, int index) +{ + struct port_bridge *bridge = (struct port_bridge *)instance; + struct port_bridge_member *member = bridge->first; + unsigned long long timer_time; + signed long *sum, sample; + unsigned char buffer[160], *buf, *d; + int i, read_p, space; + + bridge->sample_count += 160; + + /* schedule exactly 20ms from last schedule */ + timer_time = timer->timeout.tv_sec * MICRO_SECONDS + timer->timeout.tv_usec; + timer_time += 20000; /* 20 MS */ + timer->timeout.tv_sec = timer_time / MICRO_SECONDS; + timer->timeout.tv_usec = timer_time % MICRO_SECONDS; + timer->active = 1; + + while (member) { + /* calculate transmit data */ + read_p = bridge->read_p; + sum = bridge->sum_buffer; + buf = member->buffer; + d = buffer; + for (i = 0; i < 160; i++) { + sample = sum[read_p]; + sample -= audio_law_to_s32[buf[read_p]]; + buf[read_p] = silence; + if (sample < SHORT_MIN) sample = SHORT_MIN; + if (sample > SHORT_MAX) sample = SHORT_MAX; + *d++ = audio_s16_to_law[sample & 0xffff]; + read_p = (read_p + 1) & (BRIDGE_BUFFER - 1); + } + /* send data */ + member->port->bridge_rx(buffer, 160); + /* raise write pointer, if read pointer would overrun them */ + space = ((member->write_p - bridge->read_p) & (BRIDGE_BUFFER - 1)) - 160; + if (space < 0) { + space = 0; + member->write_p = read_p; +// PDEBUG(DEBUG_PORT, "bridge %u member %d has buffer underrun\n", bridge->bridge_id, member->port->p_serial); + } + /* find minimum delay */ + if (space < member->min_space) + member->min_space = space; + /* check if we should reduce buffer */ + if (bridge->sample_count >= 8000*5) { + /* reduce buffer by minimum delay */ +// PDEBUG(DEBUG_PORT, "bridge %u member %d has min space of %d samples\n", bridge->bridge_id, member->port->p_serial, member->min_space); + member->write_p = (member->write_p - member->min_space) & (BRIDGE_BUFFER - 1); + member->min_space = 1000000; /* infinite */ + } + member = member->next; + } + + /* clear sample data */ + read_p = bridge->read_p; + sum = bridge->sum_buffer; + for (i = 0; i < 160; i++) { + sum[read_p] = 0; + read_p = (read_p + 1) & (BRIDGE_BUFFER - 1); + } + + /* raise read pointer */ + bridge->read_p = read_p; + + if (bridge->sample_count >= 8000*5) + bridge->sample_count = 0; + + return 0; +} + + +/* receive data from remote Port */ +int Port::bridge_rx(unsigned char *data, int len) +{ + +#ifdef WITH_VOOTP + if (p_vootp) + vootp_decrypt_stream(p_vootp, data, len); +#endif + + return 0; +} + +#ifdef WITH_VOOTP +static void vootp_info(void *priv, const char *text) +{ + class Port *port = (class Port *)priv; + char display[strlen(text) + 1]; + + SCPY(display, text); + if (display[0]) + display[strlen(display) - 1] = '\0'; + + port->set_display(display); +} + +void Port::set_vootp(struct param_vootp *vootp) +{ + if (p_vootp) { + vootp_destroy(p_vootp); + p_vootp = NULL; + } + if (vootp->enable) { + p_vootp = vootp_create(this, (options.law=='a'), options.otp_dir, NULL, NULL, vootp->id, vootp_info); +// vootp_loglevel(VOOTP_LOGL_DEBUG); + if (!p_vootp) { + struct lcr_msg *message; + + message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_VOOTP); + message->param.vootp.failed = 1; + message_put(message); + } + } +} +#endif