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