backup
[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_last_tv_sec = 0;
145         p_last_tv_msec = 0;
146         p_type = type;
147         p_serial = port_serial++;
148         p_debug_nothingtosend = 0;
149         p_tone_fh = -1;
150         p_tone_fetched = NULL;
151         p_tone_name[0] = '\0';
152 //      p_knock_fh = -1;
153 //      p_knock_fetched = NULL;
154         p_state = PORT_STATE_IDLE;
155         p_epointlist = NULL;
156         memset(&p_callerinfo, 0, sizeof(p_callerinfo));
157         memset(&p_dialinginfo, 0, sizeof(p_dialinginfo));
158         memset(&p_connectinfo, 0, sizeof(p_connectinfo));
159         memset(&p_redirinfo, 0, sizeof(p_redirinfo));
160         memset(&p_capainfo, 0, sizeof(p_capainfo));
161         memset(p_mixer_buffer, 0, sizeof(p_mixer_buffer));
162         memset(p_record_buffer, 0, sizeof(p_record_buffer));
163         memset(p_stereo_buffer, 0, sizeof(p_stereo_buffer));
164         p_mixer_rel = NULL;
165         p_mixer_readp = 0;
166         p_echotest = 0;
167         next = NULL;
168         p_record = NULL;
169         p_record_type = 0;
170         p_record_length = 0;
171         p_record_filename[0] = '\0';
172
173         /* append port to chain */
174         temp = port_first;
175         tempp = &port_first;
176         while(temp)
177         {
178                 tempp = &temp->next;
179                 temp = temp->next;
180         }
181         *tempp = this;
182
183         classuse++;
184 }
185
186
187 /*
188  * port destructor
189  */
190 Port::~Port(void)
191 {
192         struct mixer_relation *relation, *rtemp;
193         class Port *temp, **tempp;
194         struct message *message;
195
196         if (p_record)
197                 close_record(0);
198
199         classuse--;
200
201         PDEBUG(DEBUG_PORT, "removing port of type %d, name '%s'\n", p_type, p_name);
202
203         /* free mixer relation chain */
204         relation = p_mixer_rel;
205         while(relation)
206         {
207                 rtemp = relation;
208                 relation = relation->next;
209                 memset(rtemp, 0, sizeof(struct mixer_relation));
210                 free(rtemp);
211                 pmemuse--;
212         }
213         p_mixer_rel = NULL; /* beeing paranoid */
214
215         /* disconnect port from endpoint */
216         while(p_epointlist)
217         {
218                 /* send disconnect */
219                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
220                 message->param.disconnectinfo.cause = 16;
221                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
222                 message_put(message);
223                 /* remove endpoint */
224                 free_epointlist(p_epointlist);
225         }
226
227         /* remove port from chain */
228         temp=port_first;
229         tempp=&port_first;
230         while(temp)
231         {
232                 if (temp == this)
233                         break;
234                 tempp = &temp->next;
235                 temp = temp->next;
236         }
237         if (temp == NULL)
238         {
239                 PERROR("PORT(%s) port not in port's list.\n", p_name);
240                 exit(-1);
241         }
242         /* detach */
243         *tempp=this->next;
244
245         /* close open tones file */
246         if (p_tone_fh >= 0)
247         {
248                 close(p_tone_fh);
249                 p_tone_fh = -1;
250                 fhuse--;
251         }
252         p_tone_fetched = NULL;
253 }
254
255 PORT_STATE_NAMES
256
257 /* set new endpoint state
258  */
259 void Port::new_state(int state)
260 {
261         PDEBUG(DEBUG_PORT, "PORT(%s) new state %s --> %s\n", p_name, state_name[p_state], state_name[state]);
262         p_state = state;
263 }
264
265
266 /*
267  * find the port with port_id
268  */ 
269 class Port *find_port_id(unsigned long port_id)
270 {
271         class Port *port = port_first;
272
273         while(port)
274         {
275 //printf("comparing: '%s' with '%s'\n", name, port->name);
276                 if (port->p_serial == port_id)
277                         return(port);
278                 port = port->next;
279         }
280
281         return(NULL);
282 }
283
284
285 /*
286  * set echotest
287  */
288 void Port::set_echotest(int echotest)
289 {
290         p_echotest = echotest;
291 }
292
293
294 /*
295  * set the file in the tone directory with the given name
296  */
297 void Port::set_tone(char *dir, char *name)
298 {
299         int fh;
300         char filename[128];
301
302         if (name == NULL)
303                 name = "";
304
305         /* no counter, no eof, normal speed */
306         p_tone_counter = 0;
307         p_tone_eof = 0;
308         p_tone_speed = 1;
309         p_tone_codec = CODEC_LAW;
310
311         if (p_tone_fh >= 0)
312         {
313                 close(p_tone_fh);
314                 p_tone_fh = -1;
315                 fhuse--;
316         }
317         p_tone_fetched = NULL;
318
319         if (name[0])
320         {
321                 if (name[0] == '/')
322                 {
323                         SPRINT(p_tone_name, "%s", name);
324                         p_tone_dir[0] = '\0';
325                 } else
326                 {
327                         SCPY(p_tone_dir, dir);
328                         SCPY(p_tone_name, name);
329                 }
330         } else
331         {
332                 p_tone_name[0]= '\0';
333                 p_tone_dir[0]= '\0';
334                 return;
335         }
336
337         if (!!strncmp(name,"cause_",6))
338                 return;
339
340         /* now we check if the cause exists, otherwhise we use error tone. */
341         if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, 0, 0)))
342         {
343                 p_tone_fetched = NULL;
344                 return;
345         }
346         SPRINT(filename, "%s_loop", p_tone_name);
347         if ((p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, 0, 0)))
348         {
349                 p_tone_fetched = NULL;
350                 return;
351         }
352         SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
353         if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0)
354         {
355                 close(fh);
356                 return;
357         }
358         SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name);
359         if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0)
360         {
361                 close(fh);
362                 return;
363         }
364
365         if (!strcmp(name,"cause_00") || !strcmp(name,"cause_10"))
366         {
367                 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using release tone\n", p_name, name+6);
368                 SPRINT(p_tone_name,"release");
369         } else
370         if (!strcmp(name,"cause_11"))
371         {
372                 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using busy tone\n", p_name, name+6);
373                 SPRINT(p_tone_name,"busy");
374         } else
375         {
376                 PDEBUG(DEBUG_PORT, "PORT(%s) Given Cause 0x%s has no tone, using error tone\n", p_name, name+6);
377                 SPRINT(p_tone_name,"error");
378         }
379 }
380
381
382 /*
383  * set the file in the tone directory for vbox playback
384  * also set the play_eof-flag
385  */
386 void Port::set_vbox_tone(char *dir, char *name)
387 {
388         char filename[256];
389
390         p_tone_speed = 1;
391         p_tone_counter = 0;
392         p_tone_codec = CODEC_LAW;
393         p_tone_eof = 1;
394
395         if (p_tone_fh >= 0)
396         {
397                 close(p_tone_fh);
398                 p_tone_fh = -1;
399                 fhuse--;
400         }
401         p_tone_fetched = NULL;
402
403         SPRINT(p_tone_dir,  dir);
404         SPRINT(p_tone_name,  name);
405
406         /* now we check if the cause exists, otherwhise we use error tone. */
407         if (p_tone_dir[0])
408         {
409                 if ((p_tone_fetched=open_tone_fetched(p_tone_dir, p_tone_name, &p_tone_codec, &p_tone_size, &p_tone_left)))
410                 {
411                         PDEBUG(DEBUG_PORT, "PORT(%s) opening fetched tone: %s\n", p_name, p_tone_name);
412                         return;
413                 }
414                 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
415                 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0)
416                 {
417                         fhuse++;
418                         PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
419                         return;
420                 }
421         } else
422         {
423                 SPRINT(filename, "%s", p_tone_name);
424                 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0)
425                 {
426                         fhuse++;
427                         PDEBUG(DEBUG_PORT, "PORT(%s) opening tone: %s\n", p_name, filename);
428                         return;
429                 }
430         }
431 }
432
433
434 /*
435  * set the file in the given directory for vbox playback
436  * also set the eof-flag
437  * also set the counter-flag
438  */
439 void Port::set_vbox_play(char *name, int offset)
440 {
441         signed long size;
442         struct message *message;
443
444         /* use ser_box_tone() */
445         set_vbox_tone("", name);
446         if (p_tone_fh < 0)
447                 return;
448
449         /* enable counter */
450         p_tone_counter = 1;
451
452         /* seek */
453         if (p_tone_name[0])
454         {
455                 /* send message with counter value */
456                 if (p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist))
457                 {
458                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
459                         message->param.counter.current = offset;
460                         message->param.counter.max = size;
461                         message_put(message);
462                 }
463         }
464 }
465
466
467 /*
468  * set the playback speed (for recording playback with different speeds)
469  */
470 void Port::set_vbox_speed(int speed)
471 {
472         /* enable vbox play mode */
473         p_tone_speed = speed;
474 }
475
476 /*
477  * read from the given file as specified in port_set_tone and return sample data
478  * silence is appended if sample ends, but only the number of samples with tones are returned
479  */
480 int Port::read_audio(unsigned char *buffer, int length)
481 {
482         int l,len;
483         int readp;
484         int nodata=0; /* to detect 0-length files and avoid endless reopen */
485         char filename[128];
486         int tone_left_before; /* temp variable to determine the change in p_tone_left */
487
488         /* nothing */
489         if (length == 0)
490                 return(0);
491
492         len = length;
493         codec_in = p_tone_codec;
494
495         /* if there is no tone set, use silence */
496         if (p_tone_name[0] == 0)
497         {
498 rest_is_silence:
499                 memset(buffer, (options.law=='a')?0x2a:0xff, len); /* silence */
500                 goto done;
501         }
502
503         /* if the file pointer is not open, we open it */
504         if (p_tone_fh<0 && p_tone_fetched==NULL)
505         {
506                 if (p_tone_dir[0])
507                 {
508                         SPRINT(filename, "%s", p_tone_name);
509                         /* if file does not exist */
510                         if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left)))
511                         {
512                                 SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
513                                 /* if file does not exist */
514                                 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
515                                 {
516                                         PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
517                                         goto try_loop;
518                                 }
519                                 fhuse++;
520                         }
521                 } else
522                 {
523                         SPRINT(filename, "%s", p_tone_name);
524                         /* if file does not exist */
525                         if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
526                         {
527                                 PDEBUG(DEBUG_PORT, "PORT(%s) no tone: %s\n", p_name, filename);
528                                 goto try_loop;
529                         }
530                         fhuse++;
531                 }
532                 PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
533         }
534
535 read_more:
536         /* file descriptor is open read data */
537         tone_left_before = p_tone_left;
538         if (p_tone_fh >= 0)
539         {
540                 l = read_tone(p_tone_fh, buffer, p_tone_codec, len, p_tone_size, &p_tone_left, p_tone_speed);
541                 if (l<0 || l>len) /* paranoia */
542                         l=0;
543                 buffer += l;
544                 len -= l;
545         }
546         if (p_tone_fetched)
547         {
548                 l = read_tone_fetched(&p_tone_fetched, buffer, len, p_tone_size, &p_tone_left, p_tone_speed);
549                 if (l<0 || l>len) /* paranoia */
550                         l=0;
551                 buffer += l;
552                 len -= l;
553         }
554
555         /* if counter is enabled, we check if we have a change */
556         if (p_tone_counter && p_tone_size>=0 && ACTIVE_EPOINT(p_epointlist))
557         {
558                 /* if we jumed to the next second */
559                 if (((p_tone_size-p_tone_left)/8000) != (p_tone_size-tone_left_before)/8000)
560                 {
561 //printf("\nsize=%d left=%d\n\n",p_tone_size,p_tone_left);
562                         struct message *message;
563                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
564                         message->param.counter.current = (p_tone_size-p_tone_left)/8000;
565                         message->param.counter.max = -1;
566                         message_put(message);
567                 }
568         }
569
570         if (len==0)
571                 goto done;
572
573         if (p_tone_fh >= 0)
574         {
575                 close(p_tone_fh);
576                 p_tone_fh = -1;
577                 fhuse--;
578         }
579         p_tone_fetched = NULL;
580
581         if (l)
582                 nodata=0;
583
584         /* if the file has 0-length */
585         if (nodata>1)
586         {
587                 PDEBUG(DEBUG_PORT, "PORT(%s) 0-length loop: %s\n", p_name, filename);
588                 p_tone_name[0]=0;
589                 p_tone_dir[0]=0;
590                 goto rest_is_silence;
591         }
592
593         /* if eof is reached, or if the normal file cannot be opened, continue with the loop file if possible */
594 try_loop:
595         if (p_tone_eof && ACTIVE_EPOINT(p_epointlist))
596         {
597                 struct message *message;
598                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_EOF);
599                 message_put(message);
600         }
601
602         if (p_tone_dir[0])
603         {
604                 /* if file does not exist */
605                 SPRINT(filename, "%s_loop", p_tone_name);
606                 if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left)))
607                 {
608                         SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name);
609                         /* if file does not exist */
610                         if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
611                         {
612                                 PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
613                                 p_tone_dir[0] = '\0';
614                                 p_tone_name[0] = '\0';
615                 //              codec_in = CODEC_LAW;
616                                 goto rest_is_silence;
617                         }
618                         fhuse++;
619                 }
620         } else
621         {
622                 SPRINT(filename, "%s_loop", p_tone_name);
623                 /* if file does not exist */
624                 if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
625                 {
626                         PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
627                         p_tone_dir[0] = '\0';
628                         p_tone_name[0] = '\0';
629         //              codec_in = CODEC_LAW;
630                         goto rest_is_silence;
631                 }
632                 fhuse++;
633         }
634         nodata++;
635         PDEBUG(DEBUG_PORT, "PORT(%s) opening %stone: %s\n", p_name, p_tone_fetched?"fetched ":"", filename);
636
637         /* now we have opened the loop */
638         goto read_more;
639
640 done:
641         return(length-len);
642 }
643
644
645 /*
646  * dummy for transmit function, since this must be inherited
647  */
648 void Port::transmit(unsigned char *buffer, int length, int tonelength)
649 {
650 }
651
652
653 /* port handler:
654  * process transmission clock */
655 int Port::handler(void)
656 {
657         return(0);
658 }
659
660 /* endpoint sends messages to the port
661  * this is called by the message_epoint inherited by child classes
662  * therefor a return=1 means: stop, no more processing
663  */
664 //extern struct message *dddebug;
665 int Port::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
666 {
667         /* check if we got audio data from one remote port */
668         switch(message_id)
669         {
670                 case MESSAGE_TONE: /* play tone */
671                 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);
672                 set_tone(param->tone.dir,param->tone.name);
673                 return(1);
674
675                 case MESSAGE_DATA: /* tx-data from upper layer */
676                 fromup(param->data.data, param->data.len);
677                 return(1);
678
679                 case MESSAGE_VBOX_TONE: /* play tone of answering machine */
680                 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine tone '%s' '%s'\n", p_name, param->tone.dir, param->tone.name);
681                 set_vbox_tone(param->tone.dir, param->tone.name);
682                 return(1);
683
684                 case MESSAGE_VBOX_PLAY: /* play recording of answering machine */
685                 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine file to play '%s' (offset %d seconds)\n", p_name, param->play.file, param->play.offset);
686                 set_vbox_play(param->play.file, param->play.offset);
687                 return(1);
688
689                 case MESSAGE_VBOX_PLAY_SPEED: /* set speed of playback (recording of answering machine) */
690                 PDEBUG(DEBUG_PORT, "PORT(%s) set answering machine playback speed %d (times)\n", p_name, param->speed);
691                 set_vbox_speed(param->speed);
692                 return(1);
693
694         }
695
696         return(0);
697 }
698
699
700 /* wave header structure */
701 struct fmt {
702         unsigned short  stereo; /* 1 = mono, 2 = stereo */
703         unsigned short  channels; /* number of channels */
704         unsigned long   sample_rate; /* sample rate */
705         unsigned long   data_rate; /* data rate */
706         unsigned short  bytes_sample; /* bytes per sample (all channels) */
707         unsigned short  bits_sample; /* bits per sample (one channel) */
708 };
709
710
711 /*
712  * open record file (actually a wave file with empty header which will be
713  * written before close, because we do not know the size yet)
714  * type=1 record annoucement,  type=0 record audio stream, type=2 record vbox
715  */
716 int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ignore, char *vbox_email, int vbox_email_file)
717 {
718         /* RIFFxxxxWAVEfmt xxxx(fmt-size)dataxxxx... */
719         char dummyheader[8+4+8+sizeof(fmt)+8];
720         char filename[256];
721
722         if (!extension)
723         {
724                 PERROR("Port(%d) not an extension\n", p_serial);
725                 return(0);
726         }
727         SCPY(p_record_extension, extension);
728         p_record_anon_ignore = anon_ignore;
729         SCPY(p_record_vbox_email, vbox_email);
730         p_record_vbox_email_file = vbox_email_file;
731         
732         if (p_record)
733         {
734                 PERROR("Port(%d) already recording\n", p_serial);
735                 return(0);
736         }
737
738         if (vbox != 0)
739                 SPRINT(filename, "%s/%s/%s/vbox", INSTALL_DATA, options.extensions_dir, p_record_extension);
740         else
741                 SPRINT(filename, "%s/%s/%s/recordings", INSTALL_DATA, options.extensions_dir, p_record_extension);
742         if (mkdir(filename, 0755) < 0)
743         {
744                 if (errno != EEXIST)
745                 {
746                         PERROR("Port(%d) cannot create directory '%s'\n", p_serial, filename);
747                         return(0);
748                 }
749         }
750
751         if (vbox == 1)
752                 UPRINT(strchr(filename,'\0'), "/announcement");
753         else
754                 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);
755         if (vbox == 2)
756         {
757                 p_record_vbox_year = now_tm->tm_year;
758                 p_record_vbox_mon = now_tm->tm_mon;
759                 p_record_vbox_mday = now_tm->tm_mday;
760                 p_record_vbox_hour = now_tm->tm_hour;
761                 p_record_vbox_min = now_tm->tm_min;
762         }
763
764         /* check, if file exists (especially when an extension calls the same extension) */
765         if (vbox != 1)
766         if ((p_record = fopen(filename, "r")))
767         {
768                 fclose(p_record);
769                 SCAT(filename, "_2nd");
770         }
771                         
772         p_record = fopen(filename, "w");
773         if (!p_record)
774         {
775                 PERROR("Port(%d) cannot record because file cannot be opened '%s'\n", p_serial, filename);
776                 return(0);
777         }
778         fduse++;
779
780         p_record_type = type;
781         p_record_vbox = vbox;
782         p_record_skip = skip;
783         p_record_length = 0;
784         switch(p_record_type)
785         {
786                 case CODEC_MONO:
787                 case CODEC_STEREO:
788                 case CODEC_8BIT:
789                 fwrite(dummyheader, sizeof(dummyheader), 1, p_record);
790                 break;
791
792                 case CODEC_LAW:
793                 break;
794         }
795         UCPY(p_record_filename, filename);
796
797         PDEBUG(DEBUG_PORT, "Port(%d) recording started with file name '%s'\n", p_serial, filename);
798         return(1);
799 }
800
801
802 /*
803  * close the recoding file, put header in front and rename
804  */
805 void Port::close_record(int beep)
806 {
807         static signed long beep_mono[] = {-10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000};
808         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};
809         unsigned long size, wsize;
810         struct fmt fmt;
811         char filename[512], indexname[512];
812         FILE *fp;
813         int i, ii;
814         char number[256], callerid[256];
815         char *p;
816         struct caller_info callerinfo;
817         char *valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_.-!$%&/()=+*;~";
818
819         if (!p_record)
820                 return;
821
822         memcpy(&callerinfo, &p_callerinfo, sizeof(struct caller_info));
823         apply_callerid_restriction(p_record_anon_ignore, -1, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.voip, callerinfo.intern, callerinfo.name);
824
825         SCPY(number, p_dialinginfo.number);
826         SCPY(callerid, numberrize_callerinfo(callerinfo.id, callerinfo.ntype));
827         if (callerid[0] == '\0')
828         {
829                 if (callerinfo.present == INFO_PRESENT_RESTRICTED)
830                         UCPY(callerid,"anonymous");
831                 else
832                         UCPY(callerid,"unknown");
833         }
834
835         /* change verboten digits */
836         p = callerid;
837         while((p=strchr(p,'*')))
838                 *(p++) = 'x';
839         p = callerid;
840         while((p=strchr(p,'/')))
841                 *(p++) = 'x';
842         p = number;
843         while((p=strchr(p,'*')))
844                 *(p++) = 'x';
845         p = number;
846         while((p=strchr(p,'/')))
847                 *(p++) = 'x';
848         i = 0;
849         ii = strlen(callerid);
850         while(i < ii)
851         {
852                 if (!strchr(valid_chars, callerid[i]))
853                         callerid[i] = '_';
854                 i++;
855         }
856         i = 0;
857         ii = strlen(number);
858         while(i < ii)
859         {
860                 if (!strchr(valid_chars, number[i]))
861                         number[i] = '_';
862                 i++;
863         }
864
865         /* add beep to the end of recording */
866         if (beep)
867         switch(p_record_type)
868         {
869                 case CODEC_MONO:
870                 i = 0;
871                 while(i < beep)
872                 {
873                         fwrite(beep_mono, sizeof(beep_mono), 1, p_record);
874                         i += sizeof(beep_mono);
875                         p_record_length += sizeof(beep_mono);
876                 }
877                 break;
878                 case CODEC_8BIT:
879                 i = 0;
880                 while(i < beep)
881                 {
882                         fwrite(beep_8bit, sizeof(beep_8bit), 1, p_record);
883                         i += sizeof(beep_8bit);
884                         p_record_length += sizeof(beep_8bit);
885                 }
886                 break;
887 #if 0
888                 case CODEC_LAW:
889                 i = 0;
890                 while(i < beep)
891                 {
892                         fwrite(beep_law, sizeof(beep_law), 1, p_record);
893                         i += sizeof(beep_law);
894                         p_record_length += sizeof(beep_law);
895                 }
896                 break;
897 #endif
898                 default:
899                 PERROR("codec %d not supported for beep adding\n", p_record_type);
900         }
901
902         /* complete header */
903         switch(p_record_type)
904         {
905                 case CODEC_MONO:
906                 case CODEC_STEREO:
907                 case CODEC_8BIT:
908                 /* cue */
909                 fprintf(p_record, "cue %c%c%c%c%c%c%c%c", 4, 0, 0, 0, 0,0,0,0);
910
911                 /* LIST */
912                 fprintf(p_record, "LIST%c%c%c%cadtl", 4, 0, 0, 0);
913
914                 /* go to header */
915                 fseek(p_record, 0, SEEK_SET);
916
917                 /* WAVEfmt xxxx(fmt-size)dataxxxx[data]cue xxxx0000LISTxxxxadtl*/
918                 size = p_record_length;
919                 wsize = 4+8+sizeof(fmt)+8+size+8+4+8+4;
920
921                 /* RIFF */
922                 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));
923
924                 /* WAVE */
925                 fprintf(p_record, "WAVE");
926
927                 /* fmt */
928                 fprintf(p_record, "fmt %c%c%c%c", sizeof(fmt), 0, 0, 0);
929                 switch(p_record_type)
930                 {
931                         case CODEC_MONO:
932                         fmt.stereo = 1;
933                         fmt.channels = 1;
934                         fmt.sample_rate = 8000; /* samples/sec */
935                         fmt.data_rate = 16000; /* full data rate */
936                         fmt.bytes_sample = 2; /* all channels */
937                         fmt.bits_sample = 16; /* one channel */
938                         break;
939
940                         case CODEC_STEREO:
941                         fmt.stereo = 1;
942                         fmt.channels = 2;
943                         fmt.sample_rate = 8000; /* samples/sec */
944                         fmt.data_rate = 32000; /* full data rate */
945                         fmt.bytes_sample = 4; /* all channels */
946                         fmt.bits_sample = 16; /* one channel */
947                         break;
948
949                         case CODEC_8BIT:
950                         fmt.stereo = 1;
951                         fmt.channels = 1;
952                         fmt.sample_rate = 8000; /* samples/sec */
953                         fmt.data_rate = 8000; /* full data rate */
954                         fmt.bytes_sample = 1; /* all channels */
955                         fmt.bits_sample = 8; /* one channel */
956                         break;
957                 }
958                 fwrite(&fmt, sizeof(fmt), 1, p_record);
959
960                 /* data */
961                 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));
962
963                 /* rename file */
964                 if (p_record_vbox == 1)
965                         SPRINT(filename, "%s.wav", p_record_filename);
966                 else
967                         SPRINT(filename, "%s_%s-%s.wav", p_record_filename, callerid, number);
968                 break;
969
970                 case CODEC_LAW:
971                 /* rename file */
972                 if (p_record_vbox == 1)
973                         SPRINT(filename, "%s.isdn", p_record_filename);
974                 else
975                         SPRINT(filename, "%s_%s-%s.isdn", p_record_filename, callerid, number);
976                 break;
977         }
978
979         fclose(p_record);
980         fduse--;
981         p_record = NULL;
982
983         if (rename(p_record_filename, filename) < 0)
984         {
985                 PERROR("Port(%d) cannot rename from '%s' to '%s'\n", p_serial, p_record_filename, filename);
986                 return;
987         }
988
989         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);
990
991         if (p_record_vbox == 2)
992         {
993                 SPRINT(indexname, "%s/%s/%s/vbox/index", INSTALL_DATA, options.extensions_dir, p_record_extension);
994                 if ((fp = fopen(indexname,"a")))
995                 {
996                         fduse++;
997
998                         /* remove path from file name */
999                         p = filename;
1000                         while(strchr(p, '/'))
1001                                 p = strchr(p, '/')+1;
1002                         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);
1003
1004                         fclose(fp);
1005                         fduse--;
1006                 } else
1007                 {
1008                         PERROR("Port(%d) cannot open index file '%s' to append.\n", p_serial, indexname);
1009                 }
1010
1011                 /* send email with sample*/
1012                 if (p_record_vbox_email[0])
1013                 {
1014                         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);
1015                 }
1016         }
1017 }
1018
1019
1020 /*
1021  * recording function
1022  * Records all data from down and from up into one single stream.
1023  * Both streams may have gaps or jitter.
1024  * A Jitter buffer for both streams is used to compensate jitter.
1025  * 
1026  * If one stream (dir) received packets, they are stored to a
1027  * buffer to wait for the other stream (dir), so both streams can 
1028  * be combined. If the buffer is full, it's read pointer is written
1029  * without mixing stream.
1030  * A flag is used to indicate what stream is currently in buffer.
1031  *
1032  * NOTE: First stereo sample (odd) is from down, second is from up.
1033  */
1034 alle buffer initialisieren
1035 record nur aufrufen, wenn recorded wird.
1036 restlicher buffer wegschreiben beim schliessen
1037 void Port::record(char *data, int length, int dir_fromup)
1038 {
1039         unsigned char write_buffer[1024], *d;
1040         signed short *s;
1041         int r, w;
1042         signed long sample;
1043
1044         /* no recording */
1045         if (!p_record || !length)
1046                 return;
1047
1048         free = ((p_record_buffer_readp - p_record_buffer_writep - 1) & RECORD_BUFFER_MASK);
1049
1050         /* the buffer stores the same data stream */
1051         if (dir_fromup == p_record_buffer_dir)
1052         {
1053                 same_again:
1054
1055                 /* first write what we can to the buffer */
1056                 while(free && length)
1057                 {
1058                         p_record_buffer[p_record_buffer_writep] = audio_law_to_s32(*data++);
1059                         p_record_buffer_writep = (p_record_buffer_writep + 1) & RECORD_BUFFER_MASK;
1060                         free--;
1061                         length--;
1062                 }
1063                 /* all written, so we return */
1064                 if (!length)
1065                         return;
1066                 /* still data left, buffer is full, so we need to write to file */
1067                 switch(p_record_type)
1068                 {
1069                         case CODEC_MONO:
1070                         s = (signed short *)write_buffer;
1071                         i = 0;
1072                         while(i < 256)
1073                         {
1074                                 *s++ = p_record_buffer[p_record_buffer_readp];
1075                                 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1076                                 i++;
1077                         }
1078                         fwrite(write_buffer, 512, 1, p_record);
1079                         break;
1080
1081                         case CODEC_STEREO:
1082                         s = (signed short *)write_buffer;
1083                         if (p_record_buffer_dir)
1084                         {
1085                                 i = 0;
1086                                 while(i < 256)
1087                                 {
1088                                         *s++ = 0; /* nothing from down */
1089                                         *s++ = p_record_buffer[p_record_buffer_readp];
1090                                         p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1091                                         i++;
1092                                 }
1093                         } else
1094                         {
1095                                 i = 0;
1096                                 while(i < 256)
1097                                 {
1098                                         *s++ = p_record_buffer[p_record_buffer_readp];
1099                                         *s++ = 0; /* nothing from up */
1100                                         p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1101                                         i++;
1102                                 }
1103                         }
1104                         fwrite(write_buffer, 1024, 1, p_record);
1105                         break;
1106
1107                         case CODEC_8BIT:
1108                         d = write_buffer;
1109                         i = 0;
1110                         while(i < 256)
1111                         {
1112                                 *d++ = (p_record_buffer[p_record_buffer_readp]+0x8000) >> 8;
1113                                 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1114                                 i++;
1115                         }
1116                         fwrite(write_buffer, 512, 1, p_record);
1117                         break;
1118
1119                         case CODEC_LAW:
1120                         d = write_buffer;
1121                         i = 0;
1122                         while(i < 256)
1123                         {
1124                                 *d++ = audio_s16_to_law[p_record_buffer[p_record_buffer_readp] & 0xffff];
1125                                 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1126                                 i++;
1127                         }
1128                         fwrite(write_buffer, 256, 1, p_record);
1129                         break;
1130                 }
1131                 /* because we still have data, we write again */
1132                 free += sizeof(write_buffer);
1133                 goto same_again;
1134         }
1135
1136         /* the buffer store the other stream */
1137         different_again:
1138         
1139         /* if buffer empty, change it */
1140         if (p_record_buffer_readp == p_record_buffer_writep)
1141         {
1142                 p_record_buffer_dir = dir_fromup;
1143                 goto same_again;
1144         }
1145         /* how much data can we mix ? */
1146         ii = (p_record_buffer_writep - p_record_buffer_readp) & RECORD_BUFFER_MASK;
1147         if (length < ii)
1148                 ii = length;
1149         /* write data mixed with the buffer */
1150         switch(p_record_type)
1151         {
1152                 case CODEC_MONO:
1153                 s = (signed short *)write_buffer;
1154                 i = 0;
1155                 while(i < ii)
1156                 {
1157                         sample = p_record_buffer[p_record_buffer_readp]
1158                                 + audio_law_to_s32(*data++);
1159                         p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1160                         if (sample < 32767)
1161                                 sample = -32767;
1162                         if (sample > 32768)
1163                                 sample = 32768;
1164                         *s++ = sample;
1165                         i++;
1166                 }
1167                 fwrite(write_buffer, ii<<1, 1, p_record);
1168                 break;
1169                 
1170                 case CODEC_STEREO:
1171                 s = (signed short *)write_buffer;
1172                 if (p_record_buffer_dir)
1173                 {
1174                         i = 0;
1175                         while(i < ii)
1176                         {
1177                                 *s++ = audio_law_to_s32(*data++);
1178                                 *s++ = p_record_buffer[p_record_buffer_readp];
1179                                 p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1180                                 i++;
1181                         }
1182                 } else
1183                 {
1184                         i = 0;
1185                         while(i < ii)
1186                         {
1187                                 *s++ = p_record_buffer[p_record_buffer_readp];
1188                                 *s++ = audio_law_to_s32(*data++);
1189                                 i++;
1190                         }
1191                 }
1192                 fwrite(write_buffer, ii<<2, 1, p_record);
1193                 break;
1194                 
1195                 case CODEC_8BIT:
1196                 d = write_buffer;
1197                 i = 0;
1198                 while(i < ii)
1199                 {
1200                         sample = p_record_buffer[p_record_buffer_readp]
1201                                 + audio_law_to_s32(*data++);
1202                         p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1203                         if (sample < 32767)
1204                                 sample = -32767;
1205                         if (sample > 32768)
1206                                 sample = 32768;
1207                         *d++ = (sample+0x8000) >> 8;
1208                         i++;
1209                 }
1210                 fwrite(write_buffer, ii, 1, p_record);
1211                 break;
1212                 
1213                 case CODEC_LAW:
1214                 d = write_buffer;
1215                 i = 0;
1216                 while(i < ii)
1217                 {
1218                         sample = p_record_buffer[p_record_buffer_readp]
1219                                 + audio_law_to_s32(*data++);
1220                         p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
1221                         if (sample < 32767)
1222                                 sample = -32767;
1223                         if (sample > 32768)
1224                                 sample = 32768;
1225                         *d++ = audio_s16_to_law[sample & 0xffff];
1226                         i++;
1227                 }
1228                 fwrite(write_buffer, ii, 1, p_record);
1229                 break;
1230         }
1231         length -= ii;
1232         /* data, but buffer empty */
1233         if (length)
1234         {
1235                 p_record_buffer_dir = dir_fromup;
1236                 goto same_again;
1237         }
1238         /* no data (maybe buffer) */
1239         return;
1240
1241 }
1242
1243
1244 /*
1245  * enque data from upper buffer
1246  */
1247 iniialisieren der werte
1248 void Port::txfromup(unsigned char *data, int length)
1249 {
1250         
1251         /* no recording */
1252         if (!length)
1253                 return;
1254
1255         /* get free samples in buffer */
1256         free = ((p_fromup_buffer_readp - p_fromup_buffer_writep - 1) & FROMUP_BUFFER_MASK);
1257         if (free < length)
1258         {
1259                 PDEBUG(DEBUG_PORT, "Port(%d): fromup_buffer overflows, this shall not happen under normal conditions\n", p_serial);
1260                 return;
1261         }
1262
1263         /* write data to buffer and return */
1264         while(length)
1265         {
1266                 p_fromup_buffer[p_fromup_buffer_writep] = *data++;
1267                 p_fromup_buffer_writep = (p_fromup_buffer_writep + 1) & FROMUP_BUFFER_MASK;
1268                 length--;
1269         }
1270         return; // must return, because length is 0
1271 }
1272