For DOV: Cast input of some string macros
[lcr.git] / port.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** port                                                                      **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 /* HOW TO audio?
13
14 Audio flow has two ways:
15
16 * from channel to the upper layer
17   -> sound from mISDN channel
18   -> announcement from vbox channel
19
20 * from the upper layer to the channel
21   -> sound from remote channel
22
23 Audio is required:
24
25   -> if local or remote channel is not mISDN
26   -> if call is recorded (vbox)
27
28
29 Functions:
30
31 * PmISDN::txfromup
32   -> audio from upper layer is buffered for later transmission to channel
33 * PmISDN::handler
34   -> buffered audio from upper layer or tones are transmitted via system clock
35 * mISDN_handler
36   -> rx-data from port to record() and upper layer
37   -> tx-data from port (dsp) to record()
38 * VboxPort::handler
39   -> streaming announcement to upper layer
40   -> recording announcement
41 * VboxPort::message_epoint
42   -> recording audio message from upper layer
43   
44
45    
46 */
47
48 #include "main.h"
49
50 /* enable to test conference mixing, even if only two members are bridged */
51 //#define TEST_CONFERENCE 1
52
53 #define SHORT_MIN -32768
54 #define SHORT_MAX 32767
55
56 class Port *port_first = NULL;
57
58 unsigned int port_serial = 1; /* must be 1, because 0== no port */
59
60 struct port_bridge *p_bridge_first;
61
62 static void remove_bridge(struct port_bridge *bridge, class Port *port);
63
64 /* free epointlist relation
65  */
66 void Port::free_epointlist(struct epoint_list *epointlist)
67 {
68         struct epoint_list *temp, **tempp;
69
70         temp = p_epointlist;
71         tempp = &p_epointlist;
72         while(temp) {
73                 if (temp == epointlist)
74                         break;
75
76                 tempp = &temp->next;
77                 temp = temp->next;
78         }
79         if (temp == 0) {
80                 PERROR("SOFTWARE ERROR: epointlist not in port's list.\n");
81                 return;
82         }
83         /* detach */
84         *tempp=temp->next;
85
86         /* free */
87         PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
88         FREE(temp, sizeof(struct epoint_list));
89         ememuse--;
90 }
91
92
93 void Port::free_epointid(unsigned int epoint_id)
94 {
95         struct epoint_list *temp, **tempp;
96
97         temp = p_epointlist;
98         tempp = &p_epointlist;
99         while(temp) {
100                 if (temp->epoint_id == epoint_id)
101                         break;
102
103                 tempp = &temp->next;
104                 temp = temp->next;
105         }
106         if (temp == 0) {
107                 PERROR("epoint_id not in port's list.\n");
108                 return;
109         }
110         /* detach */
111         *tempp=temp->next;
112
113         /* free */
114         PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
115         FREE(temp, sizeof(struct epoint_list));
116         ememuse--;
117 }
118
119
120 /* create new epointlist relation
121  */
122 struct epoint_list *Port::epointlist_new(unsigned int epoint_id)
123 {
124         struct epoint_list *epointlist, **epointlistpointer;
125
126         /* epointlist structure */
127         epointlist = (struct epoint_list *)MALLOC(sizeof(struct epoint_list));
128         if (!epointlist)
129                 FATAL("No memory for epointlist\n");
130         ememuse++;
131         PDEBUG(DEBUG_EPOINT, "PORT(%d) allocating epoint_list.\n", p_serial);
132
133         /* add epoint_list to chain */
134         epointlist->next = NULL;
135         epointlistpointer = &p_epointlist;
136         while(*epointlistpointer)
137                 epointlistpointer = &((*epointlistpointer)->next);
138         *epointlistpointer = epointlist;
139
140         /* link to epoint */
141         epointlist->epoint_id = epoint_id;
142         epointlist->active = 1;
143
144         return(epointlist);
145 }
146
147
148 /*
149  * port constructor
150  */
151 Port::Port(int type, const char *portname, struct port_settings *settings, struct interface *interface)
152 {
153         class Port *temp, **tempp;
154
155         /* initialize object */
156         if (settings)
157                 memcpy(&p_settings, settings, sizeof(struct port_settings));
158         else {
159                 memset(&p_settings, 0, sizeof(p_settings));
160         }
161         SCPY(p_name, portname);
162         if (interface)
163                 SCPY(p_interface_name, interface->name);
164         p_tone_dir[0] = '\0';
165         p_type = type;
166         p_serial = port_serial++;
167         p_tone_fh = -1;
168         p_tone_fetched = NULL;
169         p_tone_name[0] = '\0';
170         p_state = PORT_STATE_IDLE;
171         p_epointlist = NULL;
172         memset(&p_callerinfo, 0, sizeof(p_callerinfo));
173         memset(&p_dialinginfo, 0, sizeof(p_dialinginfo));
174         memset(&p_connectinfo, 0, sizeof(p_connectinfo));
175         memset(&p_redirinfo, 0, sizeof(p_redirinfo));
176         memset(&p_capainfo, 0, sizeof(p_capainfo));
177         p_echotest = 0;
178         p_bridge = 0;
179
180         /* call recording */
181         p_record = NULL;
182         p_tap = 0;
183         p_record_type = 0;
184         p_record_length = 0;
185         p_record_skip = 0;
186         p_record_filename[0] = '\0';
187         p_record_buffer_readp = 0;
188         p_record_buffer_writep = 0;
189         p_record_buffer_dir = 0;
190
191         /* VoOTP */
192 #ifdef WITH_VOOTP
193         p_vootp = NULL;
194 #endif
195
196         /* append port to chain */
197         next = NULL;
198         temp = port_first;
199         tempp = &port_first;
200         while(temp) {
201                 tempp = &temp->next;
202                 temp = temp->next;
203         }
204         *tempp = this;
205
206         classuse++;
207
208         PDEBUG(DEBUG_PORT, "new port (%d) of type 0x%x, name '%s' interface '%s'\n", p_serial, type, portname, p_interface_name);
209 }
210
211
212 /*
213  * port destructor
214  */
215 Port::~Port(void)
216 {
217         class Port *temp, **tempp;
218         struct lcr_msg *message;
219
220         PDEBUG(DEBUG_PORT, "removing port (%d) of type 0x%x, name '%s' interface '%s'\n", p_serial, p_type, p_name, p_interface_name);
221
222 #ifdef WITH_VOOTP
223         if (p_vootp) {
224                 vootp_destroy(p_vootp);
225                 p_vootp = NULL;
226         }
227 #endif
228
229         if (p_bridge) {
230                 PDEBUG(DEBUG_PORT, "Removing us from bridge %u\n", p_bridge->bridge_id);
231                 remove_bridge(p_bridge, this);
232         }
233
234         if (p_record)
235                 close_record(0, 0);
236
237         classuse--;
238
239         /* disconnect port from endpoint */
240         while(p_epointlist) {
241                 /* send disconnect */
242                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
243                 message->param.disconnectinfo.cause = 16;
244                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
245                 message_put(message);
246                 /* remove endpoint */
247                 free_epointlist(p_epointlist);
248         }
249
250         /* remove port from chain */
251         temp=port_first;
252         tempp=&port_first;
253         while(temp) {
254                 if (temp == this)
255                         break;
256                 tempp = &temp->next;
257                 temp = temp->next;
258         }
259         if (temp == NULL)
260                 FATAL("PORT(%s) port not in port's list.\n", p_name);
261         /* detach */
262         *tempp=this->next;
263
264         /* close open tones file */
265         if (p_tone_fh >= 0) {
266                 close(p_tone_fh);
267                 p_tone_fh = -1;
268                 fhuse--;
269         }
270         p_tone_fetched = NULL;
271 }
272
273 PORT_STATE_NAMES
274
275 /* set new endpoint state
276  */
277 void Port::new_state(int state)
278 {
279         PDEBUG(DEBUG_PORT, "PORT(%s) new state %s --> %s\n", p_name, state_name[p_state], state_name[state]);
280         p_state = state;
281 }
282
283
284 /*
285  * find the port with port_id
286  */ 
287 class Port *find_port_id(unsigned int port_id)
288 {
289         class Port *port = port_first;
290
291         while(port) {
292 //printf("comparing: '%s' with '%s'\n", name, port->name);
293                 if (port->p_serial == port_id)
294                         return(port);
295                 port = port->next;
296         }
297
298         return(NULL);
299 }
300
301
302 /*
303  * set echotest
304  */
305 void Port::set_echotest(int echotest)
306 {
307         p_echotest = echotest;
308 }
309
310
311 /*
312  * set the file in the tone directory with the given name
313  */
314 void Port::set_tone(const char *dir, const char *name)
315 {
316         int fh;
317         char filename[128];
318
319         if (name == NULL)
320                 name = "";
321
322         if (!dir || !dir[0])
323                 dir = options.tones_dir; /* just in case we have no PmISDN instance */
324
325         /* no counter, no eof, normal speed */
326         p_tone_counter = 0;
327         p_tone_eof = 0;
328         p_tone_speed = 1;
329         p_tone_codec = CODEC_LAW;
330
331         if (p_tone_fh >= 0) {
332                 close(p_tone_fh);
333                 p_tone_fh = -1;
334                 fhuse--;
335         }
336         p_tone_fetched = NULL;
337
338         if (name[0]) {
339                 if (name[0] == '/') {
340                         SPRINT(p_tone_name, "%s", name);
341                         p_tone_dir[0] = '\0';
342                 } else {
343                         SCPY(p_tone_dir, dir);
344                         SCPY(p_tone_name, name);
345                 }
346                 /* trigger playback */
347                 update_load();
348         } else {
349                 p_tone_name[0]= '\0';
350                 p_tone_dir[0]= '\0';
351                 return;
352         }
353
354         if (!!strncmp(name,"cause_",6))
355                 return;
356
357         /* now we check if the cause exists, otherwhise we use error tone. */
358         if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, 0, 0))) {
359                 p_tone_fetched = NULL;
360                 return;
361         }
362         SPRINT(filename, "%s_loop", p_tone_name);
363         if ((p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, 0, 0))) {
364                 p_tone_fetched = NULL;
365                 return;
366         }
367         SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
368         if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0) {
369                 close(fh);
370                 return;
371         }
372         SPRINT(filename, "%s/%s/%s_loop", SHARE_DATA, p_tone_dir, p_tone_name);
373         if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0) {
374                 close(fh);
375                 return;
376         }
377
378         if (!strcmp(name,"cause_00") || !strcmp(name,"cause_10")) {
379                 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using release tone\n", p_name, name+6);
380                 SPRINT(p_tone_name,"release");
381         } else
382         if (!strcmp(name,"cause_11")) {
383                 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using busy tone\n", p_name, name+6);
384                 SPRINT(p_tone_name,"busy");
385         } else {
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");
388         }
389 }
390
391
392 void Port::set_display(const char *text)
393 {
394 }
395
396 /*
397  * set the file in the tone directory for vbox playback
398  * also set the play_eof-flag
399  */
400 void Port::set_vbox_tone(const char *dir, const char *name)
401 {
402         char filename[256];
403
404         p_tone_speed = 1;
405         p_tone_counter = 0;
406         p_tone_codec = CODEC_LAW;
407         p_tone_eof = 1;
408
409         if (p_tone_fh >= 0) {
410                 close(p_tone_fh);
411                 p_tone_fh = -1;
412                 fhuse--;
413         }
414         p_tone_fetched = NULL;
415
416         SPRINT(p_tone_dir,  dir);
417         SPRINT(p_tone_name,  name);
418         /* trigger playback */
419         update_load();
420
421         /* now we check if the cause exists, otherwhise we use error tone. */
422         if (p_tone_dir[0]) {
423                 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, &p_tone_size, &p_tone_left))) {
424                         PDEBUG(DEBUG_PORT, "PORT(%s) opening fetched tone: %s\n", p_name, p_tone_name);
425                         return;
426                 }
427                 SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
428                 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0) {
429                         fhuse++;
430                         PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
431                         return;
432                 }
433         } else {
434                 SPRINT(filename, "%s", p_tone_name);
435                 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0) {
436                         fhuse++;
437                         PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
438                         return;
439                 }
440         }
441 }
442
443
444 /*
445  * set the file in the given directory for vbox playback
446  * also set the eof-flag
447  * also set the counter-flag
448  */
449 void Port::set_vbox_play(const char *name, int offset)
450 {
451         struct lcr_msg *message;
452
453         /* use ser_box_tone() */
454         set_vbox_tone("", name);
455         if (p_tone_fh < 0)
456                 return;
457
458         /* enable counter */
459         p_tone_counter = 1;
460
461         /* seek */
462         if (p_tone_name[0]) {
463                 /* send message with counter value */
464                 if (p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist)) {
465                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
466                         message->param.counter.current = offset;
467                         message->param.counter.max = p_tone_size;
468                         message_put(message);
469                 }
470         }
471 }
472
473
474 /*
475  * set the playback speed (for recording playback with different speeds)
476  */
477 void Port::set_vbox_speed(int speed)
478 {
479         /* enable vbox play mode */
480         p_tone_speed = speed;
481 }
482
483 /*
484  * read from the given file as specified in port_set_tone and return sample data
485  * if the tone ends, the result may be less samples than requested
486  */
487 int Port::read_audio(unsigned char *buffer, int length)
488 {
489         int l = 0,len;
490         int nodata=0; /* to detect 0-length files and avoid endless reopen */
491         char filename[128];
492         int tone_left_before; /* temp variable to determine the change in p_tone_left */
493
494         /* nothing */
495         if (length == 0)
496                 return(0);
497
498         len = length;
499
500         /* if there is no tone set, use silence */
501         if (!p_tone_name[0])
502                 return(0);
503
504         /* if the file pointer is not open, we open it */
505         if (p_tone_fh<0 && p_tone_fetched==NULL) {
506                 if (p_tone_dir[0]) {
507                         SPRINT(filename, "%s", p_tone_name);
508                         /* if file does not exist */
509                         if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left))) {
510                                 SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
511                                 /* if file does not exist */
512                                 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
513                                         PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
514                                         goto try_loop;
515                                 }
516                                 fhuse++;
517                         }
518                 } else {
519                         SPRINT(filename, "%s", p_tone_name);
520                         /* if file does not exist */
521                         if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
522                                 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
523                                 goto try_loop;
524                         }
525                         fhuse++;
526                 }
527                 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
528         }
529
530 read_more:
531         /* file descriptor is open read data */
532         tone_left_before = p_tone_left;
533         if (p_tone_fh >= 0) {
534                 l = read_tone(p_tone_fh, buffer, p_tone_codec, len, p_tone_size, &p_tone_left, p_tone_speed);
535                 if (l<0 || l>len) /* paranoia */
536                         l=0;
537                 buffer += l;
538                 len -= l;
539         }
540         if (p_tone_fetched) {
541                 l = read_tone_fetched(&p_tone_fetched, buffer, len, p_tone_size, &p_tone_left, p_tone_speed);
542                 if (l<0 || l>len) /* paranoia */
543                         l=0;
544                 buffer += l;
545                 len -= l;
546         }
547
548         /* if counter is enabled, we check if we have a change */
549         if (p_tone_counter && p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist)) {
550                 /* if we jumed to the next second */
551                 if (((p_tone_size-p_tone_left)/8000) != (p_tone_size-tone_left_before)/8000) {
552 //printf("\nsize=%d left=%d\n\n",p_tone_size,p_tone_left);
553                         struct lcr_msg *message;
554                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
555                         message->param.counter.current = (p_tone_size-p_tone_left)/8000;
556                         message->param.counter.max = -1;
557                         message_put(message);
558                 }
559         }
560
561         if (len==0)
562                 return(length-len);
563
564         if (p_tone_fh >= 0) {
565                 close(p_tone_fh);
566                 p_tone_fh = -1;
567                 fhuse--;
568         }
569         p_tone_fetched = NULL;
570
571         if (l)
572                 nodata=0;
573
574         /* if the file has 0-length */
575         if (nodata>1) {
576                 PDEBUG(DEBUG_PORT, "PORT(%s) 0-length loop: %s\n", p_name, filename);
577                 p_tone_name[0]=0;
578                 p_tone_dir[0]=0;
579                 return(length-len);
580         }
581
582         /* if eof is reached, or if the normal file cannot be opened, continue with the loop file if possible */
583 try_loop:
584         if (p_tone_eof && ACTIVE_EPOINT(p_epointlist)) {
585                 struct lcr_msg *message;
586                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_EOF);
587                 message_put(message);
588         }
589
590         if (p_tone_dir[0]) {
591                 /* if file does not exist */
592                 SPRINT(filename, "%s_loop", p_tone_name);
593                 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left))) {
594                         SPRINT(filename, "%s/%s/%s_loop", SHARE_DATA, p_tone_dir, p_tone_name);
595                         /* if file does not exist */
596                         if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
597                                 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
598                                 p_tone_dir[0] = '\0';
599                                 p_tone_name[0] = '\0';
600                                 return(length-len);
601                         }
602                         fhuse++;
603                 }
604         } else {
605                 SPRINT(filename, "%s_loop", p_tone_name);
606                 /* if file does not exist */
607                 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0) {
608                         PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
609                         p_tone_dir[0] = '\0';
610                         p_tone_name[0] = '\0';
611                         return(length-len);
612                 }
613                 fhuse++;
614         }
615         nodata++;
616         PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
617
618         /* now we have opened the loop */
619         goto read_more;
620 }
621
622
623 /* Endpoint sends messages to the port
624  * This is called by the message_epoint, inherited by child classes.
625  * Therefor a return 1 means: "already handled here"
626  */
627 //extern struct lcr_msg *dddebug;
628 int Port::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
629 {
630         /* check if we got audio data from one remote port */
631         switch(message_id) {
632         case MESSAGE_TONE: /* play tone */
633                 PDEBUG(DEBUG_PORT, "PORT(%s) setting tone '%s' dir '%s'\n", p_name, param->tone.name, param->tone.dir);
634                 set_tone(param->tone.dir,param->tone.name);
635                 return 1;
636
637         case MESSAGE_VBOX_TONE: /* play tone of answering machine */
638                 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine tone '%s' '%s'\n", p_name, param->tone.dir, param->tone.name);
639                 set_vbox_tone(param->tone.dir, param->tone.name);
640                 return 1;
641
642         case MESSAGE_VBOX_PLAY: /* play recording of answering machine */
643                 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine file to play '%s' (offset %d seconds)\n", p_name, param->play.file, param->play.offset);
644                 set_vbox_play(param->play.file, param->play.offset);
645                 return 1;
646
647         case MESSAGE_VBOX_PLAY_SPEED: /* set speed of playback (recording of answering machine) */
648                 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine playback speed %d (times)\n", p_name, param->speed);
649                 set_vbox_speed(param->speed);
650                 return 1;
651
652         case MESSAGE_BRIDGE: /* create / join / leave / destroy bridge */
653                 PDEBUG(DEBUG_PORT, "PORT(%s) bridging to id %d\n", p_name, param->bridge_id);
654                 bridge(param->bridge_id);
655                 return 1;
656
657 #ifdef WITH_VOOTP
658         case MESSAGE_VOOTP: /* enable / disable VoOTP */
659                 PDEBUG(DEBUG_PORT, "PORT(%s) VoOTP enabled: %d\n", p_name, param->vootp.enable);
660                 set_vootp(&param->vootp);
661                 return 1;
662 #endif
663         }
664
665         return 0;
666 }
667
668
669 /* wave header structure */
670 struct fmt {
671         unsigned short  stereo; /* 1 = mono, 2 = stereo */
672         unsigned short  channels; /* number of channels */
673         unsigned int    sample_rate; /* sample rate */
674         unsigned int    data_rate; /* data rate */
675         unsigned short  bytes_sample; /* bytes per sample (all channels) */
676         unsigned short  bits_sample; /* bits per sample (one channel) */
677 };
678
679
680 /*
681  * open record file (actually a wave file with empty header which will be
682  * written before close, because we do not know the size yet)
683  * type=1 record annoucement,  type=0 record audio stream, type=2 record vbox
684  */
685 int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ignore, const char *vbox_email, int vbox_email_file)
686 {
687         /* RIFFxxxxWAVEfmt xxxx(fmt-size)dataxxxx... */
688         char dummyheader[8+4+8+sizeof(fmt)+8];
689         char filename[256];
690         time_t now;
691         struct tm *now_tm;
692         int __attribute__((__unused__)) ret;
693
694         if (!extension) {
695                 PERROR("Port(%d) not an extension\n", p_serial);
696                 return(0);
697         }
698         SCPY(p_record_extension, extension);
699         p_record_anon_ignore = anon_ignore;
700         SCPY(p_record_vbox_email, vbox_email);
701         p_record_vbox_email_file = vbox_email_file;
702         
703         if (p_record) {
704                 PERROR("Port(%d) already recording\n", p_serial);
705                 return(0);
706         }
707
708         if (vbox != 0)
709                 SPRINT(filename, "%s/%s/vbox", EXTENSION_DATA, p_record_extension);
710         else
711                 SPRINT(filename, "%s/%s/recordings", EXTENSION_DATA, p_record_extension);
712         if (mkdir(filename, 0755) < 0) {
713                 if (errno != EEXIST) {
714                         PERROR("Port(%d) cannot create directory '%s'\n", p_serial, filename);
715                         return(0);
716                 }
717         }
718
719         if (vbox == 1)
720                 UPRINT(strchr(filename,'\0'), "/announcement");
721         else {
722                 time(&now);
723                 now_tm = localtime(&now);
724                 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);
725         }
726         if (vbox == 2) {
727                 p_record_vbox_year = now_tm->tm_year;
728                 p_record_vbox_mon = now_tm->tm_mon;
729                 p_record_vbox_mday = now_tm->tm_mday;
730                 p_record_vbox_hour = now_tm->tm_hour;
731                 p_record_vbox_min = now_tm->tm_min;
732         }
733
734         /* check, if file exists (especially when an extension calls the same extension) */
735         if (vbox != 1)
736         if ((p_record = fopen(filename, "r"))) {
737                 fclose(p_record);
738                 SCAT(filename, "_2nd");
739         }
740                         
741         p_record = fopen(filename, "w");
742         if (!p_record) {
743                 PERROR("Port(%d) cannot record because file cannot be opened '%s'\n", p_serial, filename);
744                 return(0);
745         }
746         update_rxoff();
747         fduse++;
748
749         p_record_type = type;
750         p_record_vbox = vbox;
751         p_record_skip = skip;
752         p_record_length = 0;
753         switch(p_record_type) {
754                 case CODEC_MONO:
755                 case CODEC_STEREO:
756                 case CODEC_8BIT:
757                 ret = fwrite(dummyheader, sizeof(dummyheader), 1, p_record);
758                 break;
759
760                 case CODEC_LAW:
761                 break;
762         }
763         UCPY(p_record_filename, filename);
764
765         PDEBUG(DEBUG_PORT, "Port(%d) recording started with file name '%s'\n", p_serial, filename);
766         return(1);
767 }
768
769
770 /*
771  * close the recoding file, put header in front and rename
772  */
773 void Port::close_record(int beep, int mute)
774 {
775         static signed short beep_mono[256];
776         unsigned int size = 0, wsize = 0;
777         struct fmt fmt;
778         char filename[512], indexname[512];
779         FILE *fp;
780         int i, ii;
781         char number[256], callerid[256];
782         char *p;
783         struct caller_info callerinfo;
784         const char *valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_.-!$%&/()=+*;~";
785         int __attribute__((__unused__)) ret;
786
787         if (!p_record)
788                 return;
789         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);
790
791         memcpy(&callerinfo, &p_callerinfo, sizeof(struct caller_info));
792 //      apply_callerid_restriction(p_record_anon_ignore, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.extension, callerinfo.name);
793
794         SCPY(number, p_dialinginfo.id);
795         SCPY(callerid, numberrize_callerinfo(callerinfo.id, callerinfo.ntype, options.national, options.international));
796         if (callerid[0] == '\0') {
797                 if (callerinfo.present == INFO_PRESENT_RESTRICTED)
798                         UCPY(callerid,"anonymous");
799                 else
800                         UCPY(callerid,"unknown");
801         }
802
803         /* change verboten digits */
804         p = callerid;
805         while((p=strchr(p,'*')))
806                 *(p++) = 'x';
807         p = callerid;
808         while((p=strchr(p,'/')))
809                 *(p++) = 'x';
810         p = number;
811         while((p=strchr(p,'*')))
812                 *(p++) = 'x';
813         p = number;
814         while((p=strchr(p,'/')))
815                 *(p++) = 'x';
816         i = 0;
817         ii = strlen(callerid);
818         while(i < ii) {
819                 if (!strchr(valid_chars, callerid[i]))
820                         callerid[i] = '_';
821                 i++;
822         }
823         i = 0;
824         ii = strlen(number);
825         while(i < ii) {
826                 if (!strchr(valid_chars, number[i]))
827                         number[i] = '_';
828                 i++;
829         }
830
831         /* mute */
832         if (mute && p_record_type==CODEC_MONO) {
833                 i = p_record_length;
834                 if (i > mute)
835                         i = mute;       
836                 fseek(p_record, -(i<<1), SEEK_END);
837                 p_record_length -= (i<<1);
838         }
839         /* add beep to the end of recording */
840         if (beep && p_record_type==CODEC_MONO) {
841                 i = 0;
842                 while(i < 256) {
843                         beep_mono[i] = (signed short)(sin((double)i / 5.688888888889 * 2.0 * 3.1415927) * 2000.0);
844                         i++;
845                 }
846                 i = 0;
847                 while(i < beep) {
848                         ret = fwrite(beep_mono, sizeof(beep_mono), 1, p_record);
849                         i += sizeof(beep_mono);
850                         p_record_length += sizeof(beep_mono);
851                 }
852         }
853
854         /* complete header */
855         switch(p_record_type) {
856                 case CODEC_MONO:
857                 case CODEC_STEREO:
858                 case CODEC_8BIT:
859                 /* cue */
860                 fprintf(p_record, "cue %c%c%c%c%c%c%c%c", 4, 0, 0, 0, 0,0,0,0);
861
862                 /* LIST */
863                 fprintf(p_record, "LIST%c%c%c%cadtl", 4, 0, 0, 0);
864
865                 /* go to header */
866                 fseek(p_record, 0, SEEK_SET);
867
868                 /* WAVEfmt xxxx(fmt-size)dataxxxx[data]cue xxxx0000LISTxxxxadtl*/
869                 size = p_record_length;
870                 wsize = 4+8+sizeof(fmt)+8+size+8+4+8+4;
871
872                 /* RIFF */
873                 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));
874
875                 /* WAVE */
876                 fprintf(p_record, "WAVE");
877
878                 /* fmt */
879                 fprintf(p_record, "fmt %c%c%c%c", (unsigned int)sizeof(fmt), 0, 0, 0);
880                 switch(p_record_type) {
881                         case CODEC_MONO:
882                         fmt.stereo = 1;
883                         fmt.channels = 1;
884                         fmt.sample_rate = 8000; /* samples/sec */
885                         fmt.data_rate = 16000; /* full data rate */
886                         fmt.bytes_sample = 2; /* all channels */
887                         fmt.bits_sample = 16; /* one channel */
888                         break;
889
890                         case CODEC_STEREO:
891                         fmt.stereo = 1;
892                         fmt.channels = 2;
893                         fmt.sample_rate = 8000; /* samples/sec */
894                         fmt.data_rate = 32000; /* full data rate */
895                         fmt.bytes_sample = 4; /* all channels */
896                         fmt.bits_sample = 16; /* one channel */
897                         break;
898
899                         case CODEC_8BIT:
900                         fmt.stereo = 1;
901                         fmt.channels = 1;
902                         fmt.sample_rate = 8000; /* samples/sec */
903                         fmt.data_rate = 8000; /* full data rate */
904                         fmt.bytes_sample = 1; /* all channels */
905                         fmt.bits_sample = 8; /* one channel */
906                         break;
907                 }
908                 ret = fwrite(&fmt, sizeof(fmt), 1, p_record);
909
910                 /* data */
911                 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));
912
913                 /* rename file */
914                 if (p_record_vbox == 1)
915                         SPRINT(filename, "%s.wav", p_record_filename);
916                 else
917                         SPRINT(filename, "%s_%s-%s.wav", p_record_filename, callerid, number);
918                 break;
919
920                 case CODEC_LAW:
921                 /* rename file */
922                 if (p_record_vbox == 1)
923                         SPRINT(filename, "%s.isdn", p_record_filename);
924                 else
925                         SPRINT(filename, "%s_%s-%s.isdn", p_record_filename, callerid, number);
926                 break;
927         }
928
929         fclose(p_record);
930         fduse--;
931         p_record = NULL;
932         update_rxoff();
933
934         if (rename(p_record_filename, filename) < 0) {
935                 PERROR("Port(%d) cannot rename from '%s' to '%s'\n", p_serial, p_record_filename, filename);
936                 return;
937         }
938
939         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);
940
941         if (p_record_vbox == 2) {
942                 SPRINT(indexname, "%s/%s/vbox/index", EXTENSION_DATA, p_record_extension);
943                 if ((fp = fopen(indexname,"a"))) {
944                         fduse++;
945
946                         /* remove path from file name */
947                         p = filename;
948                         while(strchr(p, '/'))
949                                 p = strchr(p, '/')+1;
950                         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);
951
952                         fclose(fp);
953                         fduse--;
954                 } else {
955                         PERROR("Port(%d) cannot open index file '%s' to append.\n", p_serial, indexname);
956                 }
957
958                 /* send email with sample*/
959                 if (p_record_vbox_email[0]) {
960                         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);
961                 }
962         }
963 }
964
965
966 /*
967  * recording function
968  * Records all data from down and from up into one single stream.
969  * Both streams may have gaps or jitter.
970  * A Jitter buffer for both streams is used to compensate jitter.
971  * 
972  * If one stream (dir) received packets, they are stored to a
973  * buffer to wait for the other stream (dir), so both streams can 
974  * be combined. If the buffer is full, it's content is written
975  * without mixing stream. (assuming only one stram (dir) exists.)
976  * A flag is used to indicate what stream is currently in buffer.
977  *
978  * NOTE: First stereo sample (odd) is from down, second is from up.
979  */
980 void Port::record(unsigned char *data, int length, int dir_fromup)
981 {
982         unsigned char write_buffer[1024], *d;
983         signed short *s;
984         int free, i, ii;
985         signed int sample;
986         int __attribute__((__unused__)) ret;
987
988         /* no recording */
989         if (!p_record || !length)
990                 return;
991
992         /* skip data from local caller (dtmf input) */
993         if (p_record_skip && !dir_fromup) {
994                 /* more to skip than we have */
995                 if (p_record_skip > length) {
996                         p_record_skip -= length;
997                         return;
998                 }
999                 /* less to skip */
1000                 data += p_record_skip;
1001                 length -= p_record_skip;
1002                 p_record_skip = 0;
1003         }
1004
1005 //printf("dir=%d len=%d\n", dir_fromup, length);
1006
1007         free = ((p_record_buffer_readp - p_record_buffer_writep - 1) & RECORD_BUFFER_MASK);
1008
1009 //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);
1010
1011         /* the buffer stores the same data stream */
1012         if (dir_fromup == p_record_buffer_dir) {
1013 same_again:
1014
1015 //printf("same free=%d length=%d\n", free, length);
1016                 /* first write what we can to the buffer */
1017                 while(free && length) {
1018                         p_record_buffer[p_record_buffer_writep] = audio_law_to_s32[*data++];
1019                         p_record_buffer_writep = (p_record_buffer_writep + 1) & RECORD_BUFFER_MASK;
1020                         free--;
1021                         length--;
1022                 }
1023                 /* all written, so we return */
1024                 if (!length)
1025                         return;
1026                 /* still data left, buffer is full, so we need to write a chunk to file */
1027                 switch(p_record_type) {
1028                         case CODEC_MONO:
1029                         s = (signed short *)write_buffer;
1030                         i = 0;
1031                         while(i < 256) {
1032                                 *s++ = p_record_buffer[p_record_buffer_readp];
1033                                 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1034                                 i++;
1035                         }
1036                         ret = fwrite(write_buffer, 512, 1, p_record);
1037                         p_record_length += 512;
1038                         break;
1039
1040                         case CODEC_STEREO:
1041                         s = (signed short *)write_buffer;
1042                         if (p_record_buffer_dir) {
1043                                 i = 0;
1044                                 while(i < 256) {
1045                                         *s++ = 0; /* nothing from down */
1046                                         *s++ = p_record_buffer[p_record_buffer_readp];
1047                                         p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1048                                         i++;
1049                                 }
1050                         } else {
1051                                 i = 0;
1052                                 while(i < 256) {
1053                                         *s++ = p_record_buffer[p_record_buffer_readp];
1054                                         *s++ = 0; /* nothing from up */
1055                                         p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1056                                         i++;
1057                                 }
1058                         }
1059                         ret = fwrite(write_buffer, 1024, 1, p_record);
1060                         p_record_length += 1024;
1061                         break;
1062
1063                         case CODEC_8BIT:
1064                         d = write_buffer;
1065                         i = 0;
1066                         while(i < 256) {
1067                                 *d++ = ((unsigned short)(p_record_buffer[p_record_buffer_readp]+0x8000)) >> 8;
1068                                 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1069                                 i++;
1070                         }
1071                         ret = fwrite(write_buffer, 512, 1, p_record);
1072                         p_record_length += 512;
1073                         break;
1074
1075                         case CODEC_LAW:
1076                         d = write_buffer;
1077                         i = 0;
1078                         while(i < 256) {
1079                                 *d++ = audio_s16_to_law[p_record_buffer[p_record_buffer_readp] & 0xffff];
1080                                 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1081                                 i++;
1082                         }
1083                         ret = fwrite(write_buffer, 256, 1, p_record);
1084                         p_record_length += 256;
1085                         break;
1086                 }
1087                 /* because we still have data, we write again */
1088                 free += 256;
1089                 goto same_again;
1090         }
1091         /* the buffer stores the other stream */
1092         
1093 different_again:
1094         /* if buffer empty, change it */
1095         if (p_record_buffer_readp == p_record_buffer_writep) {
1096                 p_record_buffer_dir = dir_fromup;
1097                 goto same_again;
1098         }
1099         /* how much data can we mix ? */
1100         ii = (p_record_buffer_writep - p_record_buffer_readp) & RECORD_BUFFER_MASK;
1101         if (length < ii)
1102                 ii = length;
1103
1104         if (ii > 256)
1105                 ii = 256;
1106 //printf("same ii=%d length=%d\n", ii, length);
1107 //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);
1108
1109         /* write data mixed with the buffer */
1110         switch(p_record_type) {
1111                 case CODEC_MONO:
1112                 s = (signed short *)write_buffer;
1113                 i = 0;
1114                 while(i < ii) {
1115                         sample = p_record_buffer[p_record_buffer_readp]
1116                                 + audio_law_to_s32[*data++];
1117                         p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1118                         if (sample < SHORT_MIN) sample = SHORT_MIN;
1119                         if (sample > SHORT_MAX) sample = SHORT_MAX;
1120                         *s++ = sample;
1121                         i++;
1122                 }
1123                 ret = fwrite(write_buffer, ii<<1, 1, p_record);
1124                 p_record_length += (ii<<1);
1125                 break;
1126                 
1127                 case CODEC_STEREO:
1128                 s = (signed short *)write_buffer;
1129                 if (p_record_buffer_dir) {
1130                         i = 0;
1131                         while(i < ii) {
1132                                 *s++ = audio_law_to_s32[*data++];
1133                                 *s++ = p_record_buffer[p_record_buffer_readp];
1134                                 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1135                                 i++;
1136                         }
1137                 } else {
1138                         i = 0;
1139                         while(i < ii) {
1140                                 *s++ = p_record_buffer[p_record_buffer_readp];
1141                                 *s++ = audio_law_to_s32[*data++];
1142                                 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1143                                 i++;
1144                         }
1145                 }
1146                 ret = fwrite(write_buffer, ii<<2, 1, p_record);
1147                 p_record_length += (ii<<2);
1148                 break;
1149                 
1150                 case CODEC_8BIT:
1151                 d = write_buffer;
1152                 i = 0;
1153                 while(i < ii) {
1154                         sample = p_record_buffer[p_record_buffer_readp]
1155                                 + audio_law_to_s32[*data++];
1156                         p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1157                         if (sample < SHORT_MIN) sample = SHORT_MIN;
1158                         if (sample > SHORT_MAX) sample = SHORT_MAX;
1159                         *d++ = (sample+0x8000) >> 8;
1160                         i++;
1161                 }
1162                 ret = fwrite(write_buffer, ii, 1, p_record);
1163                 p_record_length += ii;
1164                 break;
1165                 
1166                 case CODEC_LAW:
1167                 d = write_buffer;
1168                 i = 0;
1169                 while(i < ii) {
1170                         sample = p_record_buffer[p_record_buffer_readp]
1171                                 + audio_law_to_s32[*data++];
1172                         p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1173                         if (sample < SHORT_MIN) sample = SHORT_MIN;
1174                         if (sample > SHORT_MAX) sample = SHORT_MAX;
1175                         *d++ = audio_s16_to_law[sample & 0xffff];
1176                         i++;
1177                 }
1178                 ret = fwrite(write_buffer, ii, 1, p_record);
1179                 p_record_length += ii;
1180                 break;
1181         }
1182         length -= ii;
1183         /* still data */
1184         if (length)
1185                 goto different_again;
1186         /* no data (maybe buffer) */
1187         return;
1188
1189 }
1190
1191 void Port::tap(unsigned char *data, int length, int dir_fromup)
1192 {
1193 }
1194
1195 void Port::update_rxoff(void)
1196 {
1197 }
1198
1199 void Port::update_load(void)
1200 {
1201 }
1202
1203
1204 /*
1205  * bridge handling
1206  */
1207
1208 int bridge_timeout(struct lcr_timer *timer, void *instance, int index);
1209
1210 static void remove_bridge(struct port_bridge *bridge, class Port *port)
1211 {
1212         struct port_bridge **temp = &p_bridge_first;
1213         while (*temp) {
1214                 if (*temp == bridge) {
1215                         struct port_bridge_member **memberp = &bridge->first, *member;
1216
1217                         /* loop until we are found */
1218                         while(*memberp) {
1219                                 if ((*memberp)->port == port) {
1220                                         member = *memberp;
1221                                         *memberp = member->next;
1222                                         FREE(member, sizeof(struct port_bridge_member));
1223                                         memuse--;
1224 #ifndef TEST_CONFERENCE
1225                                         if (bridge->first && bridge->first->next && !bridge->first->next->next) {
1226 #else
1227                                         if (bridge->first && !bridge->first->next) {
1228 #endif
1229                                                 PDEBUG(DEBUG_PORT, "bridge %u is no conference anymore\n", bridge->bridge_id);
1230                                                 del_timer(&bridge->timer);
1231                                         }
1232                                         break;
1233                                 }
1234                                 memberp = &((*memberp)->next);
1235                         }
1236                         /* if bridge is empty, remove it */
1237                         if (bridge->first == NULL) {
1238                                 PDEBUG(DEBUG_PORT, "Remove bridge %u\n", bridge->bridge_id);
1239                                 *temp = bridge->next;
1240                                 FREE(bridge, sizeof(struct port_bridge));
1241                                 memuse--;
1242                         }
1243                         return;
1244                 }
1245                 temp = &((*temp)->next);
1246         }
1247         PERROR("Bridge %p not found in list\n", bridge);
1248 }
1249
1250 void Port::bridge(unsigned int bridge_id)
1251 {
1252         struct port_bridge_member **memberp;
1253
1254         /* Remove bridge, if we leave bridge or if we join a different bridge. */
1255         if (p_bridge && bridge_id != p_bridge->bridge_id) {
1256                 PDEBUG(DEBUG_PORT, "Remove port %u from bridge %u, because out new bridge is %u\n", p_serial, p_bridge->bridge_id, bridge_id);
1257                 remove_bridge(p_bridge, this);
1258                 p_bridge = NULL;
1259         }
1260
1261         /* if we leave bridge */
1262         if (!bridge_id)
1263                 return;
1264
1265         /* find bridge */
1266         if (!p_bridge) {
1267                 struct port_bridge *temp = p_bridge_first;
1268
1269                 while (temp) {
1270                         if (temp->bridge_id == bridge_id)
1271                                 break;
1272                         temp = temp->next;
1273                 }
1274                 p_bridge = temp;
1275                 if (p_bridge)
1276                         PDEBUG(DEBUG_PORT, "Port %d found existing bridge %u.\n", p_serial, p_bridge->bridge_id);
1277         }
1278
1279         /* create bridge */
1280         if (!p_bridge) {
1281                 struct port_bridge **temp = &p_bridge_first;
1282
1283                 p_bridge = (struct port_bridge *) MALLOC(sizeof(struct port_bridge));
1284                 memuse++;
1285                 p_bridge->bridge_id = bridge_id;
1286
1287                 /* attach bridge instance to list */
1288                 while (*temp)
1289                         temp = &((*temp)->next);
1290                 *temp = p_bridge;
1291                 PDEBUG(DEBUG_PORT, "Port %d creating not existing bridge %u.\n", p_serial, p_bridge->bridge_id);
1292         }
1293
1294         /* attach to bridge */
1295         memberp = &p_bridge->first;
1296         while(*memberp) {
1297                 if ((*memberp)->port == this) {
1298                         /* already joined */
1299                         return;
1300                 }
1301                 memberp = &((*memberp)->next);
1302         }
1303         *memberp = (struct port_bridge_member *) MALLOC(sizeof(struct port_bridge_member));
1304         memuse++;
1305         (*memberp)->port = this;
1306         /* check if bridge becomes a conference */
1307 #ifndef TEST_CONFERENCE
1308         if (p_bridge->first->next && p_bridge->first->next->next && !p_bridge->first->next->next->next) {
1309                 p_bridge->first->next->next->write_p = 0;
1310                 p_bridge->first->next->next->min_space = 0;
1311                 memset(p_bridge->first->next->next->buffer, silence, sizeof((*memberp)->buffer));
1312 #else
1313         if (p_bridge->first->next && !p_bridge->first->next->next) {
1314 #endif
1315                 p_bridge->first->next->write_p = 0;
1316                 p_bridge->first->next->min_space = 0;
1317                 memset(p_bridge->first->next->buffer, silence, sizeof((*memberp)->buffer));
1318                 p_bridge->first->write_p = 0;
1319                 p_bridge->first->min_space = 0;
1320                 memset(p_bridge->first->buffer, silence, sizeof((*memberp)->buffer));
1321                 memset(p_bridge->sum_buffer, 0, sizeof(p_bridge->sum_buffer));
1322                 p_bridge->read_p = 0;
1323                 add_timer(&p_bridge->timer, bridge_timeout, p_bridge, 0);
1324                 schedule_timer(&p_bridge->timer, 0, 20000); /* 20 MS */
1325                 p_bridge->sample_count = 0;
1326                 PDEBUG(DEBUG_PORT, "bridge %u became a conference\n", p_bridge->bridge_id);
1327         }
1328 }
1329
1330 /* send data to remote Port or add to sum buffer */
1331 int Port::bridge_tx(unsigned char *data, int len)
1332 {
1333         int write_p, space;
1334         struct port_bridge_member *member;
1335         signed long *sum;
1336         unsigned char *buf;
1337
1338 #ifdef WITH_VOOTP
1339         if (p_vootp)
1340                 vootp_encrypt_stream(p_vootp, data, len);
1341 #endif
1342
1343         /* less than two ports, so drop */
1344         if (!p_bridge || !p_bridge->first || !p_bridge->first->next)
1345                 return -EIO;
1346 #ifndef TEST_CONFERENCE
1347         /* two ports, so bridge */
1348         if (!p_bridge->first->next->next) {
1349                 if (p_bridge->first->port == this)
1350                         return p_bridge->first->next->port->bridge_rx(data, len);
1351                 if (p_bridge->first->next->port == this)
1352                         return p_bridge->first->port->bridge_rx(data, len);
1353                 return -EINVAL;
1354         }
1355 #endif
1356         /* more than two ports... */
1357         member = p_bridge->first;
1358         while (member) {
1359                 if (member->port == this)
1360                         break;
1361                 member = member->next;
1362         }
1363         if (!member)
1364                 return -EINVAL;
1365         write_p = member->write_p;
1366         /* calculate space, so write pointer will not overrun (or reach) read pointer in ring buffer */
1367         space = (p_bridge->read_p - write_p - 1) & (BRIDGE_BUFFER - 1);
1368         /* clip len, if it does not fit */
1369         if (space < len)
1370                 len = space;
1371         /* apply audio samples to sum buffer */
1372         sum = p_bridge->sum_buffer;
1373         buf = member->buffer;
1374         while (len--) {
1375                 sum[write_p] += audio_law_to_s32[*data];
1376                 buf[write_p] = *data++;
1377                 write_p = (write_p + 1) & (BRIDGE_BUFFER - 1);
1378         }
1379         /* raise write pointer */
1380         member->write_p = write_p;
1381
1382         return 0;
1383 }
1384
1385 int bridge_timeout(struct lcr_timer *timer, void *instance, int index)
1386 {
1387         struct port_bridge *bridge = (struct port_bridge *)instance;
1388         struct port_bridge_member *member = bridge->first;
1389         unsigned long long timer_time;
1390         signed long *sum, sample;
1391         unsigned char buffer[160], *buf, *d;
1392         int i, read_p, space;
1393         
1394         bridge->sample_count += 160;
1395
1396         /* schedule exactly 20ms from last schedule */
1397         timer_time = timer->timeout.tv_sec * MICRO_SECONDS + timer->timeout.tv_usec;
1398         timer_time += 20000; /* 20 MS */
1399         timer->timeout.tv_sec = timer_time / MICRO_SECONDS;
1400         timer->timeout.tv_usec = timer_time % MICRO_SECONDS;
1401         timer->active = 1;
1402
1403         while (member) {
1404                 /* calculate transmit data */
1405                 read_p = bridge->read_p;
1406                 sum = bridge->sum_buffer;
1407                 buf = member->buffer;
1408                 d = buffer;
1409                 for (i = 0; i < 160; i++) {
1410                         sample = sum[read_p];
1411                         sample -= audio_law_to_s32[buf[read_p]];
1412                         buf[read_p] = silence;
1413                         if (sample < SHORT_MIN) sample = SHORT_MIN;
1414                         if (sample > SHORT_MAX) sample = SHORT_MAX;
1415                         *d++ = audio_s16_to_law[sample & 0xffff];
1416                         read_p = (read_p + 1) & (BRIDGE_BUFFER - 1);
1417                 }
1418                 /* send data */
1419                 member->port->bridge_rx(buffer, 160);
1420                 /* raise write pointer, if read pointer would overrun them */
1421                 space = ((member->write_p - bridge->read_p) & (BRIDGE_BUFFER - 1)) - 160;
1422                 if (space < 0) {
1423                         space = 0;
1424                         member->write_p = read_p;
1425 //                      PDEBUG(DEBUG_PORT, "bridge %u member %d has buffer underrun\n", bridge->bridge_id, member->port->p_serial);
1426                 }
1427                 /* find minimum delay */
1428                 if (space < member->min_space)
1429                         member->min_space = space;
1430                 /* check if we should reduce buffer */
1431                 if (bridge->sample_count >= 8000*5) {
1432                         /* reduce buffer by minimum delay */
1433 //                      PDEBUG(DEBUG_PORT, "bridge %u member %d has min space of %d samples\n", bridge->bridge_id, member->port->p_serial, member->min_space);
1434                         member->write_p = (member->write_p - member->min_space) & (BRIDGE_BUFFER - 1);
1435                         member->min_space = 1000000; /* infinite */
1436                 }
1437                 member = member->next;
1438         }
1439
1440         /* clear sample data */
1441         read_p = bridge->read_p;
1442         sum = bridge->sum_buffer;
1443         for (i = 0; i < 160; i++) {
1444                 sum[read_p] = 0;
1445                 read_p = (read_p + 1) & (BRIDGE_BUFFER - 1);
1446         }
1447
1448         /* raise read pointer */
1449         bridge->read_p = read_p;
1450
1451         if (bridge->sample_count >= 8000*5)
1452                 bridge->sample_count = 0;
1453
1454         return 0;
1455 }
1456
1457
1458 /* receive data from remote Port */
1459 int Port::bridge_rx(unsigned char *data, int len)
1460 {
1461
1462 #ifdef WITH_VOOTP
1463         if (p_vootp)
1464                 vootp_decrypt_stream(p_vootp, data, len);
1465 #endif
1466
1467         return 0;
1468 }
1469
1470 #ifdef WITH_VOOTP
1471 static void vootp_info(void *priv, const char *text)
1472 {
1473         class Port *port = (class Port *)priv;
1474         char display[strlen(text) + 1];
1475
1476         SCPY(display, text);
1477         if (display[0])
1478                 display[strlen(display) - 1] = '\0';
1479
1480         port->set_display(display);
1481 }
1482
1483 void Port::set_vootp(struct param_vootp *vootp)
1484 {
1485         if (p_vootp) {
1486                 vootp_destroy(p_vootp);
1487                 p_vootp = NULL;
1488         }
1489         if (vootp->enable) {
1490                 p_vootp = vootp_create(this, (options.law=='a'), options.otp_dir, NULL, NULL, vootp->id, vootp_info);
1491 //              vootp_loglevel(VOOTP_LOGL_DEBUG);
1492                 if (!p_vootp) {
1493                         struct lcr_msg *message;
1494
1495                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_VOOTP);
1496                         message->param.vootp.failed = 1;
1497                         message_put(message);
1498                 }
1499         }
1500 }
1501 #endif