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