a68ca59768a7e2ebb2dce9a4364a85c29b767350
[lcr.git] / port.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** port                                                                      **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include "main.h"
21
22 #define BETTERDELAY
23
24 //#define MIXER_DEBUG /* debug mixer buffer overflow and underrun */
25
26 class Port *port_first = NULL;
27
28 unsigned long port_serial = 1; /* must be 1, because 0== no port */
29
30
31 /* free epointlist relation
32  */
33 void Port::free_epointlist(struct epoint_list *epointlist)
34 {
35         struct epoint_list *temp, **tempp;
36
37         temp = p_epointlist;
38         tempp = &p_epointlist;
39         while(temp)
40         {
41                 if (temp == epointlist)
42                         break;
43
44                 tempp = &temp->next;
45                 temp = temp->next;
46         }
47         if (temp == 0)
48         {
49                 PERROR("SOFTWARE ERROR: epointlist not in port's list.\n");
50                 return;
51         }
52         /* detach */
53         *tempp=temp->next;
54
55         /* free */
56         PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
57         memset(temp, 0, sizeof(struct epoint_list));
58         free(temp);
59         ememuse--;
60 }
61
62
63 void Port::free_epointid(unsigned long epoint_id)
64 {
65         struct epoint_list *temp, **tempp;
66
67         temp = p_epointlist;
68         tempp = &p_epointlist;
69         while(temp)
70         {
71                 if (temp->epoint_id == epoint_id)
72                         break;
73
74                 tempp = &temp->next;
75                 temp = temp->next;
76         }
77         if (temp == 0)
78         {
79                 PERROR("epoint_id not in port's list, exitting.\n");
80                 return;
81         }
82         /* detach */
83         *tempp=temp->next;
84
85         /* free */
86         PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
87         memset(temp, 0, sizeof(struct epoint_list));
88         free(temp);
89         ememuse--;
90 }
91
92
93 /* create new epointlist relation
94  */
95 struct epoint_list *Port::epointlist_new(unsigned long epoint_id)
96 {
97         struct epoint_list *epointlist, **epointlistpointer;
98
99         /* epointlist structure */
100         epointlist = (struct epoint_list *)calloc(1, sizeof(struct epoint_list));
101         if (!epointlist)
102         {
103                 PERROR("no mem for allocating epoint_list\n");
104                 return(0);
105         }
106         ememuse++;
107         PDEBUG(DEBUG_EPOINT, "PORT(%d) allocating epoint_list.\n", p_serial);
108         memset(epointlist, 0, sizeof(struct epoint_list));
109
110         /* add epoint_list to chain */
111         epointlist->next = NULL;
112         epointlistpointer = &p_epointlist;
113         while(*epointlistpointer)
114                 epointlistpointer = &((*epointlistpointer)->next);
115         *epointlistpointer = epointlist;
116
117         /* link to epoint */
118         epointlist->epoint_id = epoint_id;
119         epointlist->active = 1;
120
121         return(epointlist);
122 }
123
124
125 /*
126  * port constructor
127  */
128 Port::Port(int type, char *portname, struct port_settings *settings)
129 {
130         class Port *temp, **tempp;
131
132         PDEBUG(DEBUG_PORT, "new port of type %d, name '%s'\n", type, portname);
133
134         /* initialize object */
135         if (settings)
136                 memcpy(&p_settings, settings, sizeof(struct port_settings));
137         else
138         {
139                 memset(&p_settings, 0, sizeof(p_settings));
140                 SCPY(p_settings.tones_dir, options.tones_dir);
141         }
142         SCPY(p_name, portname);
143         SCPY(p_tone_dir, p_settings.tones_dir); // just to be sure
144         p_type = type;
145         p_serial = port_serial++;
146         p_debug_nothingtosend = 0;
147         p_tone_fh = -1;
148         p_tone_fetched = NULL;
149         p_tone_name[0] = '\0';
150 //      p_knock_fh = -1;
151 //      p_knock_fetched = NULL;
152         p_state = PORT_STATE_IDLE;
153         p_epointlist = NULL;
154         memset(&p_callerinfo, 0, sizeof(p_callerinfo));
155         memset(&p_dialinginfo, 0, sizeof(p_dialinginfo));
156         memset(&p_connectinfo, 0, sizeof(p_connectinfo));
157         memset(&p_redirinfo, 0, sizeof(p_redirinfo));
158         memset(&p_capainfo, 0, sizeof(p_capainfo));
159         memset(p_mixer_buffer, 0, sizeof(p_mixer_buffer));
160         memset(p_record_buffer, 0, sizeof(p_record_buffer));
161         memset(p_stereo_buffer, 0, sizeof(p_stereo_buffer));
162         p_mixer_rel = NULL;
163         p_mixer_readp = 0;
164         p_echotest = 0;
165         next = NULL;
166         p_record = NULL;
167         p_record_type = 0;
168         p_record_length = 0;
169         p_record_filename[0] = '\0';
170
171         /* append port to chain */
172         temp = port_first;
173         tempp = &port_first;
174         while(temp)
175         {
176                 tempp = &temp->next;
177                 temp = temp->next;
178         }
179         *tempp = this;
180
181         classuse++;
182 }
183
184
185 /*
186  * port destructor
187  */
188 Port::~Port(void)
189 {
190         struct mixer_relation *relation, *rtemp;
191         class Port *temp, **tempp;
192         struct message *message;
193
194         if (p_record)
195                 close_record(0);
196
197         classuse--;
198
199         PDEBUG(DEBUG_PORT, "removing port of type %d, name '%s'\n", p_type, p_name);
200
201         /* free mixer relation chain */
202         relation = p_mixer_rel;
203         while(relation)
204         {
205                 rtemp = relation;
206                 relation = relation->next;
207                 memset(rtemp, 0, sizeof(struct mixer_relation));
208                 free(rtemp);
209                 pmemuse--;
210         }
211         p_mixer_rel = NULL; /* beeing paranoid */
212
213         /* disconnect port from endpoint */
214         while(p_epointlist)
215         {
216                 /* send disconnect */
217                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
218                 message->param.disconnectinfo.cause = 16;
219                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
220                 message_put(message);
221                 /* remove endpoint */
222                 free_epointlist(p_epointlist);
223         }
224
225         /* remove port from chain */
226         temp=port_first;
227         tempp=&port_first;
228         while(temp)
229         {
230                 if (temp == this)
231                         break;
232                 tempp = &temp->next;
233                 temp = temp->next;
234         }
235         if (temp == NULL)
236         {
237                 PERROR("PORT(%s) port not in port's list.\n", p_name);
238                 exit(-1);
239         }
240         /* detach */
241         *tempp=this->next;
242
243         /* close open tones file */
244         if (p_tone_fh >= 0)
245         {
246                 close(p_tone_fh);
247                 p_tone_fh = -1;
248                 fhuse--;
249         }
250         p_tone_fetched = NULL;
251 }
252
253 PORT_STATE_NAMES
254
255 /* set new endpoint state
256  */
257 void Port::new_state(int state)
258 {
259         PDEBUG(DEBUG_PORT, "PORT(%s) new state %s --> %s\n", p_name, state_name[p_state], state_name[state]);
260         p_state = state;
261 }
262
263
264 /*
265  * find the port using h323 token
266  */ 
267 class Port *find_port_with_token(char *name)
268 {
269         class Port *port = port_first;
270
271         while(port)
272         {
273 //printf("comparing: '%s' with '%s'\n", name, p_name);
274                 if ((port->p_type==PORT_TYPE_H323_IN || port->p_type==PORT_TYPE_H323_OUT) && !strcmp(port->p_name, name))
275                         return(port);
276                 port = port->next;
277         }
278
279         return(NULL);
280 }
281
282
283 /*
284  * find the port with port_id
285  */ 
286 class Port *find_port_id(unsigned long port_id)
287 {
288         class Port *port = port_first;
289
290         while(port)
291         {
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 #if 0
302 /*
303  * open the knock sound
304  */
305 void Port::set_knock(char *tones_dir_epoint)
306 {
307         char filename[128], *tones_dir;
308
309         /* check if we have the epoint override the tones_dir of options.conf */
310         tones_dir = options.tones_dir;
311         if (tones_dir_epoint[0])
312         {
313                 tones_dir = tones_dir_epoint;
314         }
315
316         if (p_knock_fh >= 0)
317         {
318                 close(p_knock_fh);
319                 p_knock_fh = -1;
320                 fhuse--;
321         }
322         p_knock_fetched = NULL;
323
324         if ((p_knock_fetched=open_tone_fetched(tones_dir, "knock", &p_knock_codec, &p_knock_size, &p_knock_left)))
325         {
326                 PDEBUG(DEBUG_PORT, "PORT(%s) opening fetched tone: %s\n", p_name, "knock");
327                 return;
328         }
329
330         SPRINT(filename, "%s/%s/knock", INSTALL_DATA, tones_dir);
331         if ((p_knock_fh=open_tone(filename, &p_knock_codec, &p_knock_size, &p_knock_left)) >= 0)
332         {
333                 PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
334                 fhuse++;
335         } else
336         {
337                 p_knock_fh = -1;
338         }
339 }
340 #endif
341
342
343 /*
344  * set echotest
345  */
346 void Port::set_echotest(int echotest)
347 {
348         p_echotest = echotest;
349 }
350
351 /*
352  * set the file in the tone directory with the given name
353  */
354 void Port::set_tone(char *dir, char *name)
355 {
356         int fh;
357         char filename[128];
358
359         if (name == NULL)
360                 name = "";
361
362 #if 0
363         /* knocking ? */
364         if (!strcmp(name, "knock"))
365         {
366                 set_knock(tones_dir_epoint);
367                 return;
368         }
369 #endif
370
371         /* no counter, no eof, normal speed */
372         p_tone_counter = 0;
373         p_tone_eof = 0;
374         p_tone_speed = 1;
375         p_tone_codec = CODEC_LAW;
376
377         if (p_tone_fh >= 0)
378         {
379                 close(p_tone_fh);
380                 p_tone_fh = -1;
381                 fhuse--;
382         }
383         p_tone_fetched = NULL;
384
385         if (name[0])
386         {
387                 if (name[0] == '/')
388                 {
389                         SPRINT(p_tone_name, "%s", name);
390                         p_tone_dir[0] = '\0';
391                 } else
392                 {
393                         SCPY(p_tone_dir, dir);
394                         SCPY(p_tone_name, name);
395                 }
396         } else
397         {
398                 p_tone_name[0]= '\0';
399                 p_tone_dir[0]= '\0';
400                 return;
401         }
402
403         if (!!strncmp(name,"cause_",6))
404                 return;
405
406         /* now we check if the cause exists, otherwhise we use error tone. */
407         if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, 0, 0)))
408         {
409                 p_tone_fetched = NULL;
410                 return;
411         }
412         SPRINT(filename, "%s_loop", p_tone_name);
413         if ((p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, 0, 0)))
414         {
415                 p_tone_fetched = NULL;
416                 return;
417         }
418         SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
419         if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0)
420         {
421                 close(fh);
422                 return;
423         }
424         SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name);
425         if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0)
426         {
427                 close(fh);
428                 return;
429         }
430
431         if (!strcmp(name,"cause_00") || !strcmp(name,"cause_10"))
432         {
433                 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using release tone\n", p_name, name+6);
434                 SPRINT(p_tone_name,"release");
435         } else
436         if (!strcmp(name,"cause_11"))
437         {
438                 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using busy tone\n", p_name, name+6);
439                 SPRINT(p_tone_name,"busy");
440         } else
441         {
442                 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using error tone\n", p_name, name+6);
443                 SPRINT(p_tone_name,"error");
444         }
445 }
446
447
448 /*
449  * set the file in the tone directory for vbox playback
450  * also set the play_eof-flag
451  */
452 void Port::set_vbox_tone(char *dir, char *name)
453 {
454         char filename[256];
455
456         p_tone_speed = 1;
457         p_tone_counter = 0;
458         p_tone_codec = CODEC_LAW;
459         p_tone_eof = 1;
460
461         if (p_tone_fh >= 0)
462         {
463                 close(p_tone_fh);
464                 p_tone_fh = -1;
465                 fhuse--;
466         }
467         p_tone_fetched = NULL;
468
469         SPRINT(p_tone_dir,  dir);
470         SPRINT(p_tone_name,  name);
471
472         /* now we check if the cause exists, otherwhise we use error tone. */
473         if (p_tone_dir[0])
474         {
475                 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, &p_tone_size, &p_tone_left)))
476                 {
477                         PDEBUG(DEBUG_PORT, "PORT(%s) opening fetched tone: %s\n", p_name, p_tone_name);
478                         return;
479                 }
480                 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
481                 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0)
482                 {
483                         fhuse++;
484                         PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
485                         return;
486                 }
487         } else
488         {
489                 SPRINT(filename, "%s", p_tone_name);
490                 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0)
491                 {
492                         fhuse++;
493                         PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
494                         return;
495                 }
496         }
497 }
498
499
500 /*
501  * set the file in the given directory for vbox playback
502  * also set the eof-flag
503  * also set the counter-flag
504  */
505 void Port::set_vbox_play(char *name, int offset)
506 {
507         signed long size;
508         struct message *message;
509
510         /* use ser_box_tone() */
511         set_vbox_tone("", name);
512         if (p_tone_fh < 0)
513                 return;
514
515         /* enable counter */
516         p_tone_counter = 1;
517
518         /* seek */
519         if (p_tone_name[0])
520         {
521 //printf("\n\n\n tone_codec = %d\n\n\n\n",p_tone_codec);
522 #if 0
523                 switch(p_tone_codec)
524                 {
525                         case CODEC_LAW:
526                         case CODEC_8BIT:
527                         lseek(p_tone_fh, offset*8000L, SEEK_SET);
528                         size = p_tone_size / 8000L;
529                         if (offset*8000L <= p_tone_left)
530                                 p_tone_left -= offset*8000L;
531                         break;
532
533                         case CODEC_MONO:
534                         lseek(p_tone_fh, offset*16000L, SEEK_SET);
535                         size = p_tone_size / 16000L;
536                         if (offset*16000L <= p_tone_left)
537                                 p_tone_left -= offset*16000L;
538 //printf("\n\n\n size = %d\n\n\n\n",size);
539                         break;
540
541                         case CODEC_STEREO:
542                         lseek(p_tone_fh, offset*32000L, SEEK_SET);
543                         size = p_tone_size / 32000L;
544                         if (offset*32000L <= p_tone_left)
545                                 p_tone_left -= offset*32000L;
546                         break;
547         
548                         default:
549                         PERROR("no codec specified! exitting...\n");
550                         exit(-1);
551                 }
552 #else
553                 lseek(p_tone_fh, offset*8000L, SEEK_SET);
554                 size = p_tone_size / 8000L;
555                 if (offset*8000L <= p_tone_left)
556                         p_tone_left -= offset*8000L;
557 #endif
558
559
560                 /* send message with counter value */
561                 if (p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist))
562                 {
563                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
564                         message->param.counter.current = offset;
565                         message->param.counter.max = size;
566                         message_put(message);
567                 }
568         }
569 }
570
571
572 /*
573  * set the playback speed (for recording playback with different speeds)
574  */
575 void Port::set_vbox_speed(int speed)
576 {
577         /* enable vbox play mode */
578         p_tone_speed = speed;
579 }
580
581 /* write data to port's mixer buffer
582  * it will be read by the port using read_audio
583  * synchronisation is also done when writing is too fast and buffer is full
584  * the mixer buffer will only mix what cannot be mixed by kernel mixer
585  *
586  * also write data to the record buffer. the record buffer will mix everything.
587  */
588 void Port::mixer(union parameter *param)
589 {
590         struct mixer_relation *relation, **relationpointer;
591         int len;
592         unsigned char *data;
593         signed short *data_16;
594         int writep;
595         signed long *buffer, *record;
596         int must_mix = 1; /* set, if we need to mix (not done by kernel) */
597
598         /* we do not mix if we have audio from ourself but we need to record
599          * unless we have a local echo enabled
600          */
601         if (param->data.port_id==p_serial && !p_echotest)
602                 must_mix = 0;
603
604         if ((param->data.port_type&PORT_CLASS_MASK)==PORT_CLASS_mISDN
605          && (p_type&PORT_CLASS_MASK)==PORT_CLASS_mISDN)
606         {
607                 must_mix = 0;
608         }
609         if (!p_record && !must_mix) /* if we do not record AND no need to mix */
610         {
611                 return;
612         }
613
614         /* get the relation to the write pointer. if no relation exists
615          * for the given source port, we create one.
616          */
617         relation = p_mixer_rel;
618         relationpointer = &(p_mixer_rel);
619         if (!relation) /* there is no relation at all */
620         {
621                 /* clear buffer to 0-volume and reset writep */
622                 memset(p_mixer_buffer, 0, sizeof(p_mixer_buffer));
623                 memset(p_record_buffer, 0, sizeof(p_record_buffer));
624                 memset(p_stereo_buffer, 0, sizeof(p_stereo_buffer));
625         } else /* else because we do not need to run a 0-loop */
626         while(relation)
627         {
628                 if (relation->port_id == param->data.port_id)
629                         break;
630                 relationpointer = &(relation->next);
631                 relation = relation->next;
632         }
633         if (!relation)
634         {
635                 relation = *relationpointer = (struct mixer_relation *)calloc(1, sizeof(struct mixer_relation));
636                 if (!relation)
637                 {
638                         PERROR("fatal error: cannot alloc memory for port mixer relation\n");   
639                         return; /* no mem */
640                 }
641                 pmemuse++;
642                 memset(relation, 0, sizeof(struct mixer_relation));
643                 relation->port_id = param->data.port_id;
644                 /* put write buffer in front of read buffer */
645 #ifndef BETTERDELAY
646                 relation->mixer_writep = (p_mixer_readp+(PORT_BUFFER/2))%PORT_BUFFER;
647 #else
648                 relation->mixer_writep = p_mixer_readp;
649 #endif
650 #ifdef MIXER_DEBUG
651                 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);
652 #endif
653         }
654
655         /* adding remote's audio data to our mixer_buffer / record_buffer */
656         len = param->data.len;
657         if (param->data.compressed == 0) /* in case of 16 bit data */
658                 len >>= 1;
659         if (len>PORT_BUFFER)
660                 PERROR("fatal error: audio data from remote port #%d is larger than mixer buffer %d>%d\n", param->data.port_id, len, PORT_BUFFER);
661         writep = relation->mixer_writep;
662         buffer = p_mixer_buffer;
663         record = p_record_buffer;
664         /* if stereo should be recorded */
665         if (param->data.port_id == p_serial)
666         if (p_record_type == CODEC_STEREO)
667                 record = p_stereo_buffer;
668         /* NOTE: if read and write pointer are equal, the buffer is full */
669         if ((p_mixer_readp-writep+PORT_BUFFER)%PORT_BUFFER < len) /* we would overrun the read pointer */
670         {
671 #ifdef MIXER_DEBUG
672                 PERROR("PORT(%s) buffer overrun, source port #%d is sending too fast. (dropping %d samples)\n", p_name, param->data.port_id, len);
673 #endif
674                 // we do not resync since dropping causes slowing writepointer down anyway...
675                 //relation->mixer_writep = (p_mixer_readp+(PORT_BUFFER/2))%PORT_BUFFER;
676                 return;
677         }
678
679         if (!p_record && must_mix)
680         {
681                 /* WE MUST MIX BUT DO NOT RECORD */
682                 if (param->data.compressed)
683                 {
684                         /* compressed data */
685                         data = param->data.data;
686                         if (len+writep >= PORT_BUFFER) /* data hits the buffer end */
687                         {
688                                 len = len+writep-PORT_BUFFER; /* rest to write in front of buffer */
689                                 while(writep < PORT_BUFFER) /* write till buffer end */
690                                         buffer[writep++] += audio_law_to_s32[*(data++)];
691                                 writep = 0;
692                         }
693                         while(len--) /* write rest */
694                                 buffer[writep++] += audio_law_to_s32[*(data++)];
695                 } else
696                 {
697                         /* uncompressed data */
698                         data_16 = (signed short *)param->data.data;
699                         if (len+writep >= PORT_BUFFER) /* data hits the buffer end */
700                         {
701                                 len = len+writep-PORT_BUFFER; /* rest to write in front of buffer */
702                                 while(writep < PORT_BUFFER) /* write till buffer end */
703                                         buffer[writep++] += *(data_16++);
704                                 writep = 0;
705                         }
706                         while(len--) /* write rest */
707                                 buffer[writep++] += *(data_16++);
708                 }
709         } else /* else */
710         if (p_record && !must_mix)
711         {
712                 /* WE MUST RECORD BUT DO NOT MIX */
713                 if (param->data.compressed)
714                 {
715                         /* compressed data */
716                         data = param->data.data;
717                         if (len+writep >= PORT_BUFFER) /* data hits the buffer end */
718                         {
719                                 len = len+writep-PORT_BUFFER; /* rest to write in front of buffer */
720                                 while(writep < PORT_BUFFER) /* write till buffer end */
721                                         record[writep++] += audio_law_to_s32[*(data++)];
722                                 writep = 0;
723                         }
724                         while(len--) /* write rest */
725                                 record[writep++] += audio_law_to_s32[*(data++)];
726                 } else
727                 {
728                         /* uncompressed data */
729                         data_16 = (signed short *)param->data.data;
730                         if (len+writep >= PORT_BUFFER) /* data hits the buffer end */
731                         {
732                                 len = len+writep-PORT_BUFFER; /* rest to write in front of buffer */
733                                 while(writep < PORT_BUFFER) /* write till buffer end */
734                                         record[writep++] += *(data_16++);
735                                 writep = 0;
736                         }
737                         while(len--) /* write rest */
738                                 record[writep++] += *(data_16++);
739                 }
740         } else
741         {
742                 /* WE MUST MIX AND MUST RECORD */
743                 if (param->data.compressed)
744                 {
745                         /* compressed data */
746                         data = param->data.data;
747                         if (len+writep >= PORT_BUFFER) /* data hits the buffer end */
748                         {
749                                 len = len+writep-PORT_BUFFER; /* rest to write in front of buffer */
750                                 while(writep < PORT_BUFFER) /* write till buffer end */
751                                 {
752                                         buffer[writep] += audio_law_to_s32[*data];
753                                         record[writep++] += audio_law_to_s32[*(data++)];
754                                 }
755                                 writep = 0;
756                         }
757                         while(len--) /* write rest */
758                         {
759                                 buffer[writep] += audio_law_to_s32[*data];
760                                 record[writep++] += audio_law_to_s32[*(data++)];
761                         }
762                 } else
763                 {
764                         /* uncompressed data */
765                         data_16 = (signed short *)param->data.data;
766                         if (len+writep >= PORT_BUFFER) /* data hits the buffer end */
767                         {
768                                 len = len+writep-PORT_BUFFER; /* rest to write in front of buffer */
769                                 while(writep < PORT_BUFFER) /* write till buffer end */
770                                 {
771                                         buffer[writep] += *data_16;
772                                         record[writep++] += *(data_16++);
773                                 }
774                                 writep = 0;
775                         }
776                         while(len--) /* write rest */
777                         {
778                                 buffer[writep] += *data_16;
779                                 record[writep++] += *(data_16++);
780                         }
781                 }
782         }
783         relation->mixer_writep = writep; /* new write pointer */
784 //      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);
785 }
786
787
788 /*
789  * read from the given file as specified in port_set_tone and return sample data
790  * this data is mixed with the user space mixer (if anything mixed) and
791  * filled into the buffer as many as given by length.
792  * if compressed is true, the result in buffer is alaw/ulaw-compressed
793  * otherwhise it is 16 bit audio. in this case the buffer size must be twice the lenght.
794  * the length is the number of samples, not bytes in buffer!
795  */
796 int Port::read_audio(unsigned char *buffer, int length, int compressed)
797 {
798         int l,len;
799         unsigned short temp_buffer[PORT_BUFFER]; /* buffer for up to 32 bit of data */
800         int codec_in; /* current codec to use */
801         unsigned char *buf_in8; /* buffer pointer for alaw/ulaw/8bit data */
802         signed short *buf_in16; /* buffer pointer for alaw/ulaw/8bit data */
803         signed short *buf_out16; /* buffer pointer for outgoing audio data */
804         signed long *mix_buffer, *mix_buffer2, sample; /* pointer to mixer buffer */
805         struct mixer_relation *relation, **relationpointer;
806         int readp;
807         int nodata=0; /* to detect 0-length files */
808         char filename[128];
809         signed short record_buffer[PORT_BUFFER<<1], *rec_buffer; /* buffer of recorded part which will be written (*2 for stereo) */
810         unsigned char *rec8_buffer; /* used to send 8-bit audio (wave-8bit, law) */
811         int tone_left_before; /* temp variable to determine the change in p_tone_left */
812
813         /* nothing */
814         if (length == 0)
815                 return(0);
816
817         /* just in case, we get too much to do */
818         if (length > PORT_BUFFER-1)
819                 length = PORT_BUFFER-1;
820
821         len = length;
822         buf_in8 = (unsigned char *)temp_buffer;
823         buf_in16 = (signed short *)temp_buffer;
824
825         codec_in = p_tone_codec;
826
827         /* if there is no tone set, use silence */
828         if (p_tone_name[0] == 0)
829         {
830                 codec_in = CODEC_LAW;
831 rest_is_silence:
832                 switch(codec_in)
833                 {
834                         case CODEC_LAW:
835                         memset(buf_in8, (options.law=='a')?0x2a:0xff, len); /* silence */
836                         break;
837
838                         case CODEC_MONO:
839                         case CODEC_8BIT:
840                         case CODEC_STEREO:
841                         memset(buf_in16, 0, len<<1); /* silence */
842                         break;
843
844                         default:
845                         PERROR("Software error: no codec during silence\n");
846                         exit(-1);
847                 }
848                 goto mix_to_buffer;
849         }
850
851         /* if the file pointer is not open, we open it */
852         if (p_tone_fh<0 && p_tone_fetched==NULL)
853         {
854                 if (p_tone_dir[0])
855                 {
856                         SPRINT(filename, "%s", p_tone_name);
857                         /* if file does not exist */
858                         if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left)))
859                         {
860                                 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
861                                 /* if file does not exist */
862                                 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
863                                 {
864                                         PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
865                                         goto try_loop;
866                                 }
867                                 fhuse++;
868                         }
869                 } else
870                 {
871                         SPRINT(filename, "%s", p_tone_name);
872                         /* if file does not exist */
873                         if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
874                         {
875                                 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
876                                 goto try_loop;
877                         }
878                         fhuse++;
879                 }
880                 codec_in = p_tone_codec;
881 //printf("\n\ncodec=%d\n\n\n", p_tone_codec);
882                 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
883         }
884
885 read_more:
886         /* file descriptor is open read data */
887         tone_left_before = p_tone_left;
888         if (p_tone_fh >= 0)
889         {
890                 switch(codec_in)
891                 {
892                         case CODEC_LAW:
893                         l = read_tone(p_tone_fh, buf_in8, codec_in, len, p_tone_size, &p_tone_left, p_tone_speed);
894                         if (l<0 || l>len) /* paranoia */
895                                 l=0;
896                         buf_in8 += l;
897                         len -= l;
898                         break;
899
900                         case CODEC_8BIT:
901                         l = read_tone(p_tone_fh, buf_in16, codec_in, len, p_tone_size, &p_tone_left, p_tone_speed);
902                         if (l<0 || l>len) /* paranoia */
903                                 l=0;
904                         buf_in16 += l;
905                         len -= l;
906                         break;
907
908                         case CODEC_MONO:
909                         l = read_tone(p_tone_fh, buf_in16, codec_in, len, p_tone_size, &p_tone_left, p_tone_speed);
910                         if (l<0 || l>len) /* paranoia */
911                                 l=0;
912                         buf_in16 += l;
913                         len -= l;
914                         break;
915
916                         case CODEC_STEREO:
917                         l = read_tone(p_tone_fh, buf_in16, codec_in, len, p_tone_size, &p_tone_left, p_tone_speed);
918                         if (l<0 || l>len) /* paranoia */
919                                 l=0;
920                         buf_in16 += l;
921                         len -= l;
922                         break;
923
924                         default:
925                         PERROR("Software error: current tone reading has no codec\n");
926                         exit(-1);
927                 }
928         }
929         if (p_tone_fetched)
930         {
931                 switch(codec_in)
932                 {
933                         case CODEC_LAW:
934                         l = read_tone_fetched(&p_tone_fetched, buf_in8, codec_in, len, p_tone_size, &p_tone_left, p_tone_speed);
935                         if (l<0 || l>len) /* paranoia */
936                                 l=0;
937                         buf_in8 += l;
938                         len -= l;
939                         break;
940
941                         case CODEC_MONO:
942                         l = read_tone_fetched(&p_tone_fetched, buf_in16, codec_in, len, p_tone_size, &p_tone_left, p_tone_speed);
943                         if (l<0 || l>len) /* paranoia */
944                                 l=0;
945                         buf_in16 += l;
946                         len -= l;
947                         break;
948
949                         default:
950                         PERROR("Software error: current tone reading has no codec\n");
951                         exit(-1);
952                 }
953         }
954
955         /* if counter is enabled, we check if we have a change */
956         if (p_tone_counter && p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist))
957         {
958                 /* if we jumed to the next second */
959                 if (((p_tone_size-p_tone_left)/8000) != (p_tone_size-tone_left_before)/8000)
960                 {
961 //printf("\nsize=%d left=%d\n\n",p_tone_size,p_tone_left);
962                         struct message *message;
963                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
964                         message->param.counter.current = (p_tone_size-p_tone_left)/8000;
965                         message->param.counter.max = -1;
966                         message_put(message);
967                 }
968         }
969
970         if (len==0)
971                 goto mix_to_buffer;
972
973         if (p_tone_fh >= 0)
974         {
975                 close(p_tone_fh);
976                 p_tone_fh = -1;
977                 fhuse--;
978         }
979         p_tone_fetched = NULL;
980
981         if (l)
982                 nodata=0;
983
984         /* if the file has 0-length */
985         if (nodata>1)
986         {
987                 PDEBUG(DEBUG_PORT, "PORT(%s) 0-length loop: %s\n", p_name, filename);
988                 p_tone_name[0]=0;
989                 p_tone_dir[0]=0;
990                 goto rest_is_silence;
991         }
992
993         /* if eof is reached, or if the normal file cannot be opened, continue with the loop file if possible */
994 try_loop:
995         if (p_tone_eof && ACTIVE_EPOINT(p_epointlist))
996         {
997                 struct message *message;
998                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_EOF);
999                 message_put(message);
1000         }
1001
1002         if (p_tone_dir[0])
1003         {
1004                 /* if file does not exist */
1005                 SPRINT(filename, "%s_loop", p_tone_name);
1006                 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left)))
1007                 {
1008                         SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name);
1009                         /* if file does not exist */
1010                         if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
1011                         {
1012                                 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
1013                                 p_tone_dir[0] = '\0';
1014                                 p_tone_name[0] = '\0';
1015                 //              codec_in = CODEC_LAW;
1016                                 goto rest_is_silence;
1017                         }
1018                         fhuse++;
1019                 }
1020         } else
1021         {
1022                 SPRINT(filename, "%s_loop", p_tone_name);
1023                 /* if file does not exist */
1024                 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
1025                 {
1026                         PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
1027                         p_tone_dir[0] = '\0';
1028                         p_tone_name[0] = '\0';
1029         //              codec_in = CODEC_LAW;
1030                         goto rest_is_silence;
1031                 }
1032                 fhuse++;
1033         }
1034         codec_in = p_tone_codec;
1035         nodata++;
1036         PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
1037
1038         /* now we have opened the loop */
1039         goto read_more;
1040
1041         /* **********
1042          * now we mix
1043          * we take the buffer content and mix the mixer_buffer to what we got
1044          */ 
1045 mix_to_buffer:
1046         /* release all writepointer which will underrun, since writing of
1047          * remote port data is too slow
1048          */
1049         relation = p_mixer_rel;
1050         readp = p_mixer_readp;
1051         relationpointer = &(p_mixer_rel);
1052         while(relation) /* if no relation, this is skipped */
1053         {
1054                 /* NOTE: if writep and readp are equal, the distance ist max
1055                  * == PORT_BUFFER
1056                  */
1057                 if (((relation->mixer_writep-readp-1+PORT_BUFFER)%PORT_BUFFER)+1 <= length) /* underrun */
1058                 {
1059                         /* remove port relation in order to resync.
1060                          * this is also caused by ports which do not transmit
1061                          * anymore.
1062                          */
1063 #ifdef MIXER_DEBUG
1064                         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);
1065 #endif
1066                         
1067                         *relationpointer = relation->next;
1068                         memset(relation, 0, sizeof(struct mixer_relation));
1069                         free(relation);
1070                         pmemuse--;
1071                         relation = *relationpointer;
1072                         continue;
1073                 }
1074                 relationpointer = &(relation->next);
1075                 relation = relation->next;
1076         }
1077
1078         /* if we do recording, we write the record data and the buffer data to the record fp and increment record_count */
1079         if (p_record)
1080         switch (p_record_type)
1081         {
1082                 case CODEC_MONO:
1083                 /* convert from mixer to uncompressed 16 bit mono audio */
1084                 switch(codec_in)
1085                 {
1086                         case CODEC_MONO:
1087                         case CODEC_STEREO:
1088                         case CODEC_8BIT:
1089                         len = length;
1090                         mix_buffer = p_record_buffer;
1091                         rec_buffer = record_buffer;
1092                         buf_in16 = (signed short *)temp_buffer;
1093                         if (len+readp >= PORT_BUFFER) /* data hits the buffer end */
1094                         {
1095                                 len = len+readp-PORT_BUFFER; /* rest to read from buffer start */
1096                                 while(readp < PORT_BUFFER)
1097                                 {       /* mix till buffer end */
1098                                         sample = mix_buffer[readp]+*(buf_in16++);
1099                                         mix_buffer[readp++] = 0;
1100                                         if (sample < -32767)
1101                                                 sample = -32767;
1102                                         if (sample > 32767)
1103                                                 sample = 32767;
1104                                         *(rec_buffer++) = sample;
1105                                 }
1106                                 readp = 0;
1107                         }
1108                         while(len--) /* write rest */
1109                         {       /* mix till buffer end */
1110                                 sample = mix_buffer[readp]+*(buf_in16++);
1111                                 mix_buffer[readp++] = 0;
1112                                 if (sample < -32767)
1113                                         sample = -32767;
1114                                 if (sample > 32767)
1115                                         sample = 32767;
1116                                 *(rec_buffer++) = sample;
1117                         }
1118
1119                         /* restore (changed) read pointer for further use */
1120                         readp = p_mixer_readp;
1121
1122                         /* now write the rec_buffer to the file */
1123                         if (p_record_skip)
1124                         {
1125                                 p_record_skip -= length;
1126                                 if (p_record_skip < 0)
1127                                         p_record_skip = 0;
1128                         } else
1129                         {
1130                                 fwrite(record_buffer, (length<<1), 1, p_record);
1131                                 p_record_length = (length<<1) + p_record_length;
1132                         }
1133                         break;
1134
1135                         case CODEC_LAW:
1136                         len = length;
1137                         mix_buffer = p_record_buffer;
1138                         rec_buffer = record_buffer;
1139                         buf_in8 = (unsigned char *)temp_buffer;
1140                         if (len+readp >= PORT_BUFFER) /* data hits the buffer end */
1141                         {
1142                                 len = len+readp-PORT_BUFFER; /* rest to read from buffer start */
1143                                 while(readp < PORT_BUFFER)
1144                                 {       /* mix till buffer end */
1145                                         sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)];
1146                                         mix_buffer[readp++] = 0;
1147                                         if (sample < -32767)
1148                                                 sample = -32767;
1149                                         if (sample > 32767)
1150                                                 sample = 32767;
1151                                         *(rec_buffer++) = sample;
1152                                 }
1153                                 readp = 0;
1154                         }
1155                         while(len--) /* write rest */
1156                         {       /* mix till buffer end */
1157                                 sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)];
1158                                 mix_buffer[readp++] = 0;
1159                                 if (sample < -32767)
1160                                         sample = -32767;
1161                                 if (sample > 32767)
1162                                         sample = 32767;
1163                                 *(rec_buffer++) = sample;
1164                         }
1165
1166                         /* restore (changed) read pointer for further use */
1167                         readp = p_mixer_readp;
1168
1169                         /* now write the rec_buffer to the file */
1170                         if (p_record_skip)
1171                         {
1172                                 p_record_skip -= length;
1173                                 if (p_record_skip < 0)
1174                                         p_record_skip = 0;
1175                         } else
1176                         {
1177                                 fwrite(record_buffer, (length<<1), 1, p_record);
1178                                 p_record_length = (length<<1) + p_record_length;
1179                         }
1180                         break;
1181
1182                 }
1183                 break;
1184
1185                 case CODEC_STEREO:
1186                 /* convert from mixer to uncompressed 16 bit stereo audio */
1187                 switch(codec_in)
1188                 {
1189                         case CODEC_MONO:
1190                         case CODEC_STEREO:
1191                         case CODEC_8BIT:
1192                         len = length;
1193                         mix_buffer = p_record_buffer;
1194                         mix_buffer2 = p_stereo_buffer;
1195                         rec_buffer = record_buffer;
1196                         buf_in16 = (signed short *)temp_buffer;
1197                         if (len+readp >= PORT_BUFFER) /* data hits the buffer end */
1198                         {
1199                                 len = len+readp-PORT_BUFFER; /* rest to read from buffer start */
1200                                 while(readp < PORT_BUFFER)
1201                                 {       /* mix till buffer end */
1202                                         sample = mix_buffer[readp]+*(buf_in16++);
1203                                         mix_buffer[readp] = 0;
1204                                         if (sample < -32767)
1205                                                 sample = -32767;
1206                                         if (sample > 32767)
1207                                                 sample = 32767;
1208                                         *(rec_buffer++) = sample;
1209
1210                                         *(rec_buffer++) = mix_buffer2[readp];
1211                                         mix_buffer2[readp++] = 0;
1212                                 }
1213                                 readp = 0;
1214                         }
1215                         while(len--) /* write rest */
1216                         {       /* mix till buffer end */
1217                                 sample = mix_buffer[readp]+*(buf_in16++);
1218                                 mix_buffer[readp] = 0;
1219                                 if (sample < -32767)
1220                                         sample = -32767;
1221                                 if (sample > 32767)
1222                                         sample = 32767;
1223                                 *(rec_buffer++) = sample;
1224                                 *(rec_buffer++) = mix_buffer2[readp];
1225                                 mix_buffer2[readp++] = 0;
1226                         }
1227
1228                         /* restore (changed) read pointer for further use */
1229                         readp = p_mixer_readp;
1230
1231                         /* now write the rec_buffer to the file */
1232                         if (p_record_skip)
1233                         {
1234                                 p_record_skip -= length;
1235                                 if (p_record_skip < 0)
1236                                         p_record_skip = 0;
1237                         } else
1238                         {
1239                                 fwrite(record_buffer, (length<<2), 1, p_record);
1240                                 p_record_length = (length<<2) + p_record_length;
1241                         }
1242                         break;
1243
1244                         case CODEC_LAW:
1245                         len = length;
1246                         mix_buffer = p_record_buffer;
1247                         mix_buffer2 = p_stereo_buffer;
1248                         rec_buffer = record_buffer;
1249                         buf_in8 = (unsigned char *)temp_buffer;
1250                         if (len+readp >= PORT_BUFFER) /* data hits the buffer end */
1251                         {
1252                                 len = len+readp-PORT_BUFFER; /* rest to read from buffer start */
1253                                 while(readp < PORT_BUFFER)
1254                                 {       /* mix till buffer end */
1255                                         sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)];
1256                                         mix_buffer[readp] = 0;
1257                                         if (sample < -32767)
1258                                                 sample = -32767;
1259                                         if (sample > 32767)
1260                                                 sample = 32767;
1261                                         *(rec_buffer++) = sample;
1262
1263                                         *(rec_buffer++) = mix_buffer2[readp];
1264                                         mix_buffer2[readp++] = 0;
1265                                 }
1266                                 readp = 0;
1267                         }
1268                         while(len--) /* write rest */
1269                         {       /* mix till buffer end */
1270                                 sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)];
1271                                 mix_buffer[readp] = 0;
1272                                 if (sample < -32767)
1273                                         sample = -32767;
1274                                 if (sample > 32767)
1275                                         sample = 32767;
1276                                 *(rec_buffer++) = sample;
1277                                 *(rec_buffer++) = mix_buffer2[readp];
1278                                 mix_buffer2[readp++] = 0;
1279                         }
1280
1281                         /* restore (changed) read pointer for further use */
1282                         readp = p_mixer_readp;
1283
1284                         /* now write the rec_buffer to the file */
1285                         if (p_record_skip)
1286                         {
1287                                 p_record_skip -= length;
1288                                 if (p_record_skip < 0)
1289                                         p_record_skip = 0;
1290                         } else
1291                         {
1292                                 fwrite(record_buffer, (length<<2), 1, p_record);
1293                                 p_record_length = (length<<2) + p_record_length;
1294                         }
1295                         break;
1296                 }
1297                 break;
1298
1299                 case CODEC_8BIT:
1300                 /* convert from mixer to uncompressed 8 bit mono audio */
1301                 switch(codec_in)
1302                 {
1303                         case CODEC_MONO:
1304                         case CODEC_STEREO:
1305                         case CODEC_8BIT:
1306                         len = length;
1307                         mix_buffer = p_record_buffer;
1308                         rec8_buffer = (unsigned char *)record_buffer;
1309                         buf_in16 = (signed short *)temp_buffer;
1310                         if (len+readp >= PORT_BUFFER) /* data hits the buffer end */
1311                         {
1312                                 len = len+readp-PORT_BUFFER; /* rest to read from buffer start */
1313                                 while(readp < PORT_BUFFER)
1314                                 {       /* mix till buffer end */
1315                                         sample = mix_buffer[readp]+*(buf_in16++);
1316                                         mix_buffer[readp++] = 0;
1317                                         if (sample < -32767)
1318                                                 sample = -32767;
1319                                         if (sample > 32767)
1320                                                 sample = 32767;
1321                                         *(rec8_buffer++) = (sample>>8)+0x80;
1322                                 }
1323                                 readp = 0;
1324                         }
1325                         while(len--) /* write rest */
1326                         {       /* mix till buffer end */
1327                                 sample = mix_buffer[readp]+*(buf_in16++);
1328                                 mix_buffer[readp++] = 0;
1329                                 if (sample < -32767)
1330                                         sample = -32767;
1331                                 if (sample > 32767)
1332                                         sample = 32767;
1333                                 *(rec8_buffer++) = (sample>>8)+0x80;
1334                         }
1335
1336                         /* restore (changed) read pointer for further use */
1337                         readp = p_mixer_readp;
1338
1339                         /* now write the rec_buffer to the file */
1340                         if (p_record_skip)
1341                         {
1342                                 p_record_skip -= length;
1343                                 if (p_record_skip < 0)
1344                                         p_record_skip = 0;
1345                         } else
1346                         {
1347                                 fwrite(record_buffer, (length), 1, p_record);
1348                                 p_record_length = (length) + p_record_length;
1349                         }
1350                         break;
1351
1352                         case CODEC_LAW:
1353                         len = length;
1354                         mix_buffer = p_record_buffer;
1355                         rec8_buffer = (unsigned char *)record_buffer;
1356                         buf_in8 = (unsigned char *)temp_buffer;
1357                         if (len+readp >= PORT_BUFFER) /* data hits the buffer end */
1358                         {
1359                                 len = len+readp-PORT_BUFFER; /* rest to read from buffer start */
1360                                 while(readp < PORT_BUFFER)
1361                                 {       /* mix till buffer end */
1362                                         sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)];
1363                                         mix_buffer[readp++] = 0;
1364                                         if (sample < -32767)
1365                                                 sample = -32767;
1366                                         if (sample > 32767)
1367                                                 sample = 32767;
1368                                         *(rec8_buffer++) = (sample>>8)+0x80;
1369                                 }
1370                                 readp = 0;
1371                         }
1372                         while(len--) /* write rest */
1373                         {       /* mix till buffer end */
1374                                 sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)];
1375                                 mix_buffer[readp++] = 0;
1376                                 if (sample < -32767)
1377                                         sample = -32767;
1378                                 if (sample > 32767)
1379                                         sample = 32767;
1380                                 *(rec8_buffer++) = (sample>>8)+0x80;
1381                         }
1382
1383                         /* restore (changed) read pointer for further use */
1384                         readp = p_mixer_readp;
1385
1386                         /* now write the rec_buffer to the file */
1387                         if (p_record_skip)
1388                         {
1389                                 p_record_skip -= length;
1390                                 if (p_record_skip < 0)
1391                                         p_record_skip = 0;
1392                         } else
1393                         {
1394                                 fwrite(record_buffer, (length), 1, p_record);
1395                                 p_record_length = (length) + p_record_length;
1396                         }
1397                         break;
1398
1399                 }
1400                 break;
1401
1402                 case CODEC_LAW:
1403                 case CODEC_OFF: /* if no codec is specified, the recorded data will be stored as LAW */
1404                 /* convert from mixer to law */
1405                 switch(codec_in)
1406                 {
1407                         case CODEC_MONO:
1408                         case CODEC_STEREO:
1409                         case CODEC_8BIT:
1410                         len = length;
1411                         mix_buffer = p_record_buffer;
1412                         rec8_buffer = (unsigned char *)record_buffer;
1413                         buf_in16 = (signed short *)temp_buffer;
1414                         if (len+readp >= PORT_BUFFER) /* data hits the buffer end */
1415                         {
1416                                 len = len+readp-PORT_BUFFER; /* rest to read from buffer start */
1417                                 while(readp < PORT_BUFFER)
1418                                 {       /* mix till buffer end */
1419                                         sample = mix_buffer[readp]+*(buf_in16++);
1420                                         mix_buffer[readp++] = 0;
1421                                         if (sample < -32767)
1422                                                 sample = -32767;
1423                                         if (sample > 32767)
1424                                                 sample = 32767;
1425                                         *(rec8_buffer++) = audio_s16_to_law[sample & 0xffff];
1426                                 }
1427                                 readp = 0;
1428                         }
1429                         while(len--) /* write rest */
1430                         {       /* mix till buffer end */
1431                                 sample = mix_buffer[readp]+*(buf_in16++);
1432                                 mix_buffer[readp++] = 0;
1433                                 if (sample < -32767)
1434                                         sample = -32767;
1435                                 if (sample > 32767)
1436                                         sample = 32767;
1437                                 *(rec8_buffer++) = audio_s16_to_law[sample & 0xffff];
1438                         }
1439
1440                         /* restore (changed) read pointer for further use */
1441                         readp = p_mixer_readp;
1442
1443                         /* now write the rec_buffer to the file */
1444                         if (p_record_skip)
1445                         {
1446                                 p_record_skip -= length;
1447                                 if (p_record_skip < 0)
1448                                         p_record_skip = 0;
1449                         } else
1450                         {
1451                                 fwrite(record_buffer, (length), 1, p_record);
1452                                 p_record_length = (length) + p_record_length;
1453                         }
1454                         break;
1455
1456                         case CODEC_LAW:
1457                         len = length;
1458                         mix_buffer = p_record_buffer;
1459                         rec8_buffer = (unsigned char *)record_buffer;
1460                         buf_in8 = (unsigned char *)temp_buffer;
1461                         if (len+readp >= PORT_BUFFER) /* data hits the buffer end */
1462                         {
1463                                 len = len+readp-PORT_BUFFER; /* rest to read from buffer start */
1464                                 while(readp < PORT_BUFFER)
1465                                 {       /* mix till buffer end */
1466                                         sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)];
1467                                         mix_buffer[readp++] = 0;
1468                                         if (sample < -32767)
1469                                                 sample = -32767;
1470                                         if (sample > 32767)
1471                                                 sample = 32767;
1472                                         *(rec8_buffer++) = audio_s16_to_law[sample & 0xffff];
1473                                 }
1474                                 readp = 0;
1475                         }
1476                         while(len--) /* write rest */
1477                         {       /* mix till buffer end */
1478                                 sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)];
1479                                 mix_buffer[readp++] = 0;
1480                                 if (sample < -32767)
1481                                         sample = -32767;
1482                                 if (sample > 32767)
1483                                         sample = 32767;
1484                                 *(rec8_buffer++) = audio_s16_to_law[sample & 0xffff];
1485                         }
1486
1487                         /* restore (changed) read pointer for further use */
1488                         readp = p_mixer_readp;
1489
1490                         /* now write the rec_buffer to the file */
1491                         if (p_record_skip)
1492                         {
1493                                 p_record_skip -= length;
1494                                 if (p_record_skip < 0)
1495                                         p_record_skip = 0;
1496                         } else
1497                         {
1498                                 fwrite(record_buffer, (length), 1, p_record);
1499                                 p_record_length = (length) + p_record_length;
1500                         }
1501                         break;
1502                 }
1503                 break;
1504         }
1505
1506         /* if we have no transmitting relation, we do not need read mixer */
1507         if (!p_mixer_rel)
1508         {       /* nothing mixed to(no rel), so we are just done */
1509                 if (compressed)
1510                 {
1511                         /* compress to law */
1512                         len = length;
1513                         buf_in8 = (unsigned char *)temp_buffer;
1514                         buf_in16 = (signed short *)temp_buffer;
1515                         buf_out16 = (signed short *)buffer;
1516                         switch(codec_in)
1517                         {
1518                                 case CODEC_MONO:
1519                                 case CODEC_STEREO:
1520                                 case CODEC_8BIT:
1521                                 while(len--)
1522                                         *(buffer++) = audio_s16_to_law[*(buf_in16++) & 0xffff];
1523                                 break;
1524
1525                                 case CODEC_LAW:
1526                                 memcpy(buffer, temp_buffer, length);
1527                                 break;
1528
1529                                 default:
1530                                 PERROR("Software error: current tone for unmixed & uncompressed, has no codec\n");
1531                                 exit(-1);
1532                         }
1533                 } else
1534                 {
1535                         /* uncompress law files */
1536                         len = length;
1537                         buf_in8 = (unsigned char *)temp_buffer;
1538                         buf_in16 = (signed short *)temp_buffer;
1539                         buf_out16 = (signed short *)buffer;
1540                         switch(codec_in)
1541                         {
1542                                 case CODEC_MONO:
1543                                 case CODEC_STEREO:
1544                                 case CODEC_8BIT:
1545                                 while(len--)
1546                                         (*(buf_out16++)) = *(buf_in16++);
1547                                 break;
1548
1549                                 case CODEC_LAW:
1550                                 while(len--)
1551                                         (*(buf_out16++)) = audio_law_to_s32[*(buf_in8++)];
1552                                 break;
1553
1554                                 default:
1555                                 PERROR("Software error: current tone for unmixed & uncompressed, has no codec\n");
1556                                 exit(-1);
1557                         }
1558                 }
1559                 return(length);
1560         }
1561
1562 //      PDEBUG(DEBUG_PORT, "PORT(%s) mixing %d bytes. (readp=%d, PORT_BUFFER=%d)\n", p_name, length, readp, PORT_BUFFER);
1563         /* now we got our stuff and we'll mix it baby */
1564
1565         len = length;
1566         mix_buffer = p_mixer_buffer;
1567         if (compressed)
1568         {
1569                 /* convert from mixer to compressed law data */
1570                 switch(codec_in)
1571                 {
1572                         case CODEC_MONO:
1573                         case CODEC_STEREO:
1574                         case CODEC_8BIT:
1575                         buf_in16 = (signed short *)temp_buffer;
1576                         if (len+readp >= PORT_BUFFER) /* data hits the buffer end */
1577                         {
1578                                 len = len+readp-PORT_BUFFER; /* rest to read from buffer start */
1579                                 while(readp < PORT_BUFFER)
1580                                 {       /* mix till buffer end */
1581                                         sample = mix_buffer[readp]+*(buf_in16++);
1582                                         mix_buffer[readp++] = 0;
1583                                         if (sample < -32767)
1584                                                 sample = -32767;
1585                                         if (sample > 32767)
1586                                                 sample = 32767;
1587                                         *(buffer++) = audio_s16_to_law[sample & 0xffff];
1588                                 }
1589                                 readp = 0;
1590                         }
1591                         while(len--) /* write rest */
1592                         {       /* mix till buffer end */
1593                                 sample = mix_buffer[readp]+*(buf_in16++);
1594                                 mix_buffer[readp++] = 0;
1595                                 if (sample < -32767)
1596                                         sample = -32767;
1597                                 if (sample > 32767)
1598                                         sample = 32767;
1599                                 *(buffer++) = audio_s16_to_law[sample & 0xffff];
1600                         }
1601                         break;
1602
1603                         case CODEC_LAW:
1604                         buf_in8 = (unsigned char *)temp_buffer;
1605                         if (len+readp >= PORT_BUFFER) /* data hits the buffer end */
1606                         {
1607                                 len = len+readp-PORT_BUFFER; /* rest to read from buffer start */
1608                                 while(readp < PORT_BUFFER)
1609                                 {       /* mix till buffer end */
1610                                         sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)];
1611                                         mix_buffer[readp++] = 0;
1612                                         if (sample < -32767)
1613                                                 sample = -32767;
1614                                         if (sample > 32767)
1615                                                 sample = 32767;
1616                                         *(buffer++) = audio_s16_to_law[sample & 0xffff];
1617                                 }
1618                                 readp = 0;
1619                         }
1620                         while(len--) /* write rest */
1621                         {       /* mix till buffer end */
1622                                 sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)];
1623                                 mix_buffer[readp++] = 0;
1624                                 if (sample < -32767)
1625                                         sample = -32767;
1626                                 if (sample > 32767)
1627                                         sample = 32767;
1628                                 *(buffer++) = audio_s16_to_law[sample & 0xffff];
1629                         }
1630                         break;
1631
1632                         default:
1633                         PERROR("Software error: current tone for compressed data has no codec\n");
1634                         exit(-1);
1635                 }
1636         } else
1637         {
1638                 /* convert from mixer to uncompressed 16 bit audio */
1639                 switch(codec_in)
1640                 {
1641                         case CODEC_MONO:
1642                         case CODEC_STEREO:
1643                         case CODEC_8BIT:
1644                         buf_in16 = (signed short *)temp_buffer;
1645                         buf_out16 = (signed short *)buffer;
1646                         if (len+readp >= PORT_BUFFER) /* data hits the buffer end */
1647                         {
1648                                 len = len+readp-PORT_BUFFER; /* rest to read from buffer start */
1649                                 while(readp < PORT_BUFFER)
1650                                 {       /* mix till buffer end */
1651                                         sample = mix_buffer[readp]+*(buf_in16++);
1652                                         mix_buffer[readp++] = 0;
1653                                         if (sample < -32767)
1654                                                 sample = -32767;
1655                                         if (sample > 32767)
1656                                                 sample = 32767;
1657                                         *(buf_out16++) = sample;
1658                                 }
1659                                 readp = 0;
1660                         }
1661                         while(len--) /* write rest */
1662                         {       /* mix till buffer end */
1663                                 sample = mix_buffer[readp]+*(buf_in16++);
1664                                 mix_buffer[readp++] = 0;
1665                                 if (sample < -32767)
1666                                         sample = -32767;
1667                                 if (sample > 32767)
1668                                         sample = 32767;
1669                                 *(buf_out16++) = sample;
1670                         }
1671                         break;
1672
1673                         case CODEC_LAW:
1674                         buf_in8 = (unsigned char *)temp_buffer;
1675                         buf_out16 = (signed short *)buffer;
1676                         if (len+readp >= PORT_BUFFER) /* data hits the buffer end */
1677                         {
1678                                 len = len+readp-PORT_BUFFER; /* rest to read from buffer start */
1679                                 while(readp < PORT_BUFFER)
1680                                 {       /* mix till buffer end */
1681                                         sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)];
1682                                         mix_buffer[readp++] = 0;
1683                                         if (sample < -32767)
1684                                                 sample = -32767;
1685                                         if (sample > 32767)
1686                                                 sample = 32767;
1687                                         *(buf_out16++) = sample;
1688                                 }
1689                                 readp = 0;
1690                         }
1691                         while(len--) /* write rest */
1692                         {       /* mix till buffer end */
1693                                 sample = mix_buffer[readp]+audio_law_to_s32[*(buf_in8++)];
1694                                 mix_buffer[readp++] = 0;
1695                                 if (sample < -32767)
1696                                         sample = -32767;
1697                                 if (sample > 32767)
1698                                         sample = 32767;
1699                                 *(buf_out16++) = sample;
1700                         }
1701                         break;
1702
1703                         default:
1704                         PERROR("Software error: current tone for uncompressed data has no codec\n");
1705                         exit(-1);
1706                 }
1707         }
1708
1709         p_mixer_readp = readp; /* new read pointer */
1710
1711         return(length);
1712 }
1713
1714
1715 /* dummy handler */
1716 int Port::handler(void)
1717 {
1718         return(0);
1719 }
1720
1721 /* endpoint sends messages to the port
1722  * this is called by the message_epoint inherited by child classes
1723  * therefor a return=1 means: stop, no more processing
1724  */
1725 //extern struct message *dddebug;
1726 int Port::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
1727 {
1728         /* check if we got audio data from one remote port */
1729         switch(message_id)
1730         {
1731                 case MESSAGE_TONE: /* play tone */
1732                 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);
1733                 set_tone(param->tone.dir,param->tone.name);
1734                 return(1);
1735
1736                 case MESSAGE_DATA:
1737 //printf("port=%s, epoint=%d\n",p_cardname, epoint->e_serial);
1738                 mixer(param);
1739                 return(1);
1740
1741                 case MESSAGE_VBOX_TONE: /* play tone of answering machine */
1742                 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine tone '%s' '%s'\n", p_name, param->tone.dir, param->tone.name);
1743                 set_vbox_tone(param->tone.dir, param->tone.name);
1744                 return(1);
1745
1746                 case MESSAGE_VBOX_PLAY: /* play recording of answering machine */
1747                 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine file to play '%s' (offset %d seconds)\n", p_name, param->play.file, param->play.offset);
1748                 set_vbox_play(param->play.file, param->play.offset);
1749                 return(1);
1750
1751                 case MESSAGE_VBOX_PLAY_SPEED: /* set speed of playback (recording of answering machine) */
1752                 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine playback speed %d (times)\n", p_name, param->speed);
1753                 set_vbox_speed(param->speed);
1754                 return(1);
1755
1756         }
1757
1758         return(0);
1759 }
1760
1761
1762 /*
1763  * special function generate individual isdn debug logs
1764  */
1765 void Port::printisdn(char *fmt, ...)
1766 {
1767         char buffer[4096];
1768         char name[128];
1769         va_list args;
1770         FILE *fp;
1771
1772         va_start(args,fmt);
1773         VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
1774         buffer[sizeof(buffer)-1]=0;
1775         va_end(args);
1776
1777         PDEBUG_RUNTIME(NULL, 0, DEBUG_PORT, "PORT(%s serial=%ld): %s\n", p_name, p_serial, buffer);
1778         if (options.deb & DEBUG_LOG)
1779         {
1780                 SPRINT(name, "%s/debug_%s.log", INSTALL_DATA, p_name);
1781                 if (!(fp = fopen(name, "a")))
1782                         return;
1783         
1784                 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);
1785                 fclose(fp);
1786         }
1787 }
1788
1789
1790 /* wave header structure */
1791 struct fmt {
1792         unsigned short  stereo; /* 1 = mono, 2 = stereo */
1793         unsigned short  channels; /* number of channels */
1794         unsigned long   sample_rate; /* sample rate */
1795         unsigned long   data_rate; /* data rate */
1796         unsigned short  bytes_sample; /* bytes per sample (all channels) */
1797         unsigned short  bits_sample; /* bits per sample (one channel) */
1798 };
1799
1800
1801 /*
1802  * open record file (actually a wave file with empty header which will be
1803  * written before close, because we do not know the size yet)
1804  * type=1 record annoucement,  type=0 record audio stream, type=2 record vbox
1805  */
1806 int Port::open_record(int type, int vbox, int skip, char *terminal, int anon_ignore, char *vbox_email, int vbox_email_file)
1807 {
1808         /* RIFFxxxxWAVEfmt xxxx(fmt-size)dataxxxx... */
1809         char dummyheader[8+4+8+sizeof(fmt)+8];
1810         char filename[256];
1811
1812         if (!terminal)
1813         {
1814                 PERROR("Port(%d) not a terminal\n", p_serial);
1815                 return(0);
1816         }
1817         SCPY(p_record_extension, terminal);
1818         p_record_anon_ignore = anon_ignore;
1819         SCPY(p_record_vbox_email, vbox_email);
1820         p_record_vbox_email_file = vbox_email_file;
1821         
1822         if (p_record)
1823         {
1824                 PERROR("Port(%d) already recording\n", p_serial);
1825                 return(0);
1826         }
1827
1828         if (vbox != 0)
1829                 SPRINT(filename, "%s/%s/%s/vbox", INSTALL_DATA, options.extensions_dir, p_record_extension);
1830         else
1831                 SPRINT(filename, "%s/%s/%s/recordings", INSTALL_DATA, options.extensions_dir, p_record_extension);
1832         if (mkdir(filename, 0755) < 0)
1833         {
1834                 if (errno != EEXIST)
1835                 {
1836                         PERROR("Port(%d) cannot create directory '%s'\n", p_serial, filename);
1837                         return(0);
1838                 }
1839         }
1840
1841         if (vbox == 1)
1842                 UPRINT(strchr(filename,'\0'), "/announcement");
1843         else
1844                 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);
1845         if (vbox == 2)
1846         {
1847                 p_record_vbox_year = now_tm->tm_year;
1848                 p_record_vbox_mon = now_tm->tm_mon;
1849                 p_record_vbox_mday = now_tm->tm_mday;
1850                 p_record_vbox_hour = now_tm->tm_hour;
1851                 p_record_vbox_min = now_tm->tm_min;
1852         }
1853
1854         /* check, if file exists (especially when an extension calls the same extension) */
1855         if (vbox != 1)
1856         if ((p_record = fopen(filename, "r")))
1857         {
1858                 fclose(p_record);
1859                 SCAT(filename, "_2nd");
1860         }
1861                         
1862         p_record = fopen(filename, "w");
1863         if (!p_record)
1864         {
1865                 PERROR("Port(%d) cannot record because file cannot be opened '%s'\n", p_serial, filename);
1866                 return(0);
1867         }
1868         fduse++;
1869
1870         p_record_type = type;
1871         p_record_vbox = vbox;
1872         p_record_skip = skip;
1873         p_record_length = 0;
1874         switch(p_record_type)
1875         {
1876                 case CODEC_MONO:
1877                 case CODEC_STEREO:
1878                 case CODEC_8BIT:
1879                 fwrite(dummyheader, sizeof(dummyheader), 1, p_record);
1880                 break;
1881
1882                 case CODEC_LAW:
1883                 break;
1884         }
1885         UCPY(p_record_filename, filename);
1886
1887         PDEBUG(DEBUG_PORT, "Port(%d) recording started with file name '%s'\n", p_serial, filename);
1888         return(1);
1889 }
1890
1891
1892 /*
1893  * close the recoding file, put header in front and rename
1894  */
1895 void Port::close_record(int beep)
1896 {
1897         static signed long beep_mono[] = {-10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000};
1898         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};
1899         unsigned long size, wsize;
1900         struct fmt fmt;
1901         char filename[512], indexname[512];
1902         FILE *fp;
1903         int i, ii;
1904         char number[256], callerid[256];
1905         char *p;
1906         struct caller_info callerinfo;
1907         char *valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_.-!$%&/()=+*;~";
1908
1909         if (!p_record)
1910                 return;
1911
1912         memcpy(&callerinfo, &p_callerinfo, sizeof(struct caller_info));
1913         apply_callerid_restriction(p_record_anon_ignore, -1, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.voip, callerinfo.intern, callerinfo.name);
1914
1915         SCPY(number, p_dialinginfo.number);
1916         SCPY(callerid, numberrize_callerinfo(callerinfo.id, callerinfo.ntype));
1917         if (callerid[0] == '\0')
1918         {
1919                 if (callerinfo.present == INFO_PRESENT_RESTRICTED)
1920                         UCPY(callerid,"anonymous");
1921                 else
1922                         UCPY(callerid,"unknown");
1923         }
1924
1925         /* change verboten digits */
1926         p = callerid;
1927         while((p=strchr(p,'*')))
1928                 *(p++) = 'x';
1929         p = callerid;
1930         while((p=strchr(p,'/')))
1931                 *(p++) = 'x';
1932         p = number;
1933         while((p=strchr(p,'*')))
1934                 *(p++) = 'x';
1935         p = number;
1936         while((p=strchr(p,'/')))
1937                 *(p++) = 'x';
1938         i = 0;
1939         ii = strlen(callerid);
1940         while(i < ii)
1941         {
1942                 if (!strchr(valid_chars, callerid[i]))
1943                         callerid[i] = '_';
1944                 i++;
1945         }
1946         i = 0;
1947         ii = strlen(number);
1948         while(i < ii)
1949         {
1950                 if (!strchr(valid_chars, number[i]))
1951                         number[i] = '_';
1952                 i++;
1953         }
1954
1955         /* add beep to the end of recording */
1956         if (beep)
1957         switch(p_record_type)
1958         {
1959                 case CODEC_MONO:
1960                 i = 0;
1961                 while(i < beep)
1962                 {
1963                         fwrite(beep_mono, sizeof(beep_mono), 1, p_record);
1964                         i += sizeof(beep_mono);
1965                         p_record_length += sizeof(beep_mono);
1966                 }
1967                 break;
1968                 case CODEC_8BIT:
1969                 i = 0;
1970                 while(i < beep)
1971                 {
1972                         fwrite(beep_8bit, sizeof(beep_8bit), 1, p_record);
1973                         i += sizeof(beep_8bit);
1974                         p_record_length += sizeof(beep_8bit);
1975                 }
1976                 break;
1977 #if 0
1978                 case CODEC_LAW:
1979                 i = 0;
1980                 while(i < beep)
1981                 {
1982                         fwrite(beep_law, sizeof(beep_law), 1, p_record);
1983                         i += sizeof(beep_law);
1984                         p_record_length += sizeof(beep_law);
1985                 }
1986                 break;
1987 #endif
1988                 default:
1989                 PERROR("codec %d not supported for beep adding\n", p_record_type);
1990         }
1991
1992         /* complete header */
1993         switch(p_record_type)
1994         {
1995                 case CODEC_MONO:
1996                 case CODEC_STEREO:
1997                 case CODEC_8BIT:
1998                 /* cue */
1999                 fprintf(p_record, "cue %c%c%c%c%c%c%c%c", 4, 0, 0, 0, 0,0,0,0);
2000
2001                 /* LIST */
2002                 fprintf(p_record, "LIST%c%c%c%cadtl", 4, 0, 0, 0);
2003
2004                 /* go to header */
2005                 fseek(p_record, 0, SEEK_SET);
2006
2007                 /* WAVEfmt xxxx(fmt-size)dataxxxx[data]cue xxxx0000LISTxxxxadtl*/
2008                 size = p_record_length;
2009                 wsize = 4+8+sizeof(fmt)+8+size+8+4+8+4;
2010
2011                 /* RIFF */
2012                 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));
2013
2014                 /* WAVE */
2015                 fprintf(p_record, "WAVE");
2016
2017                 /* fmt */
2018                 fprintf(p_record, "fmt %c%c%c%c", sizeof(fmt), 0, 0, 0);
2019                 switch(p_record_type)
2020                 {
2021                         case CODEC_MONO:
2022                         fmt.stereo = 1;
2023                         fmt.channels = 1;
2024                         fmt.sample_rate = 8000; /* samples/sec */
2025                         fmt.data_rate = 16000; /* full data rate */
2026                         fmt.bytes_sample = 2; /* all channels */
2027                         fmt.bits_sample = 16; /* one channel */
2028                         break;
2029
2030                         case CODEC_STEREO:
2031                         fmt.stereo = 1;
2032                         fmt.channels = 2;
2033                         fmt.sample_rate = 8000; /* samples/sec */
2034                         fmt.data_rate = 32000; /* full data rate */
2035                         fmt.bytes_sample = 4; /* all channels */
2036                         fmt.bits_sample = 16; /* one channel */
2037                         break;
2038
2039                         case CODEC_8BIT:
2040                         fmt.stereo = 1;
2041                         fmt.channels = 1;
2042                         fmt.sample_rate = 8000; /* samples/sec */
2043                         fmt.data_rate = 8000; /* full data rate */
2044                         fmt.bytes_sample = 1; /* all channels */
2045                         fmt.bits_sample = 8; /* one channel */
2046                         break;
2047                 }
2048                 fwrite(&fmt, sizeof(fmt), 1, p_record);
2049
2050                 /* data */
2051                 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));
2052
2053                 /* rename file */
2054                 if (p_record_vbox == 1)
2055                         SPRINT(filename, "%s.wav", p_record_filename);
2056                 else
2057                         SPRINT(filename, "%s_%s-%s.wav", p_record_filename, callerid, number);
2058                 break;
2059
2060                 case CODEC_LAW:
2061                 /* rename file */
2062                 if (p_record_vbox == 1)
2063                         SPRINT(filename, "%s.isdn", p_record_filename);
2064                 else
2065                         SPRINT(filename, "%s_%s-%s.isdn", p_record_filename, callerid, number);
2066                 break;
2067
2068                 default:
2069                 if (p_record_vbox == 1)
2070                         SPRINT(filename, "%s.unknown", p_record_filename);
2071                 else
2072                         SPRINT(filename, "%s_%s-%s.unknown", p_record_filename, callerid, number);
2073         }
2074
2075         fclose(p_record);
2076         fduse--;
2077         p_record = NULL;
2078
2079         if (rename(p_record_filename, filename) < 0)
2080         {
2081                 PERROR("Port(%d) cannot rename from '%s' to '%s'\n", p_serial, p_record_filename, filename);
2082                 return;
2083         }
2084
2085         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);
2086
2087         if (p_record_vbox == 2)
2088         {
2089                 SPRINT(indexname, "%s/%s/%s/vbox/index", INSTALL_DATA, options.extensions_dir, p_record_extension);
2090                 if ((fp = fopen(indexname,"a")))
2091                 {
2092                         fduse++;
2093
2094                         /* remove path from file name */
2095                         p = filename;
2096                         while(strchr(p, '/'))
2097                                 p = strchr(p, '/')+1;
2098                         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);
2099
2100                         fclose(fp);
2101                         fduse--;
2102                 } else
2103                 {
2104                         PERROR("Port(%d) cannot open index file '%s' to append.\n", p_serial, indexname);
2105                 }
2106
2107                 /* send email with sample*/
2108                 if (p_record_vbox_email[0])
2109                 {
2110                         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);
2111                 }
2112         }
2113 }
2114
2115
2116