fixes
[lcr.git] / port.cpp
index abd480d..d254d0c 100644 (file)
--- a/port.cpp
+++ b/port.cpp
@@ -19,12 +19,10 @@ Audio flow has two ways:
 
 * from the upper layer to the channel
   -> sound from remote channel
-  -> sound from asterisk
 
 Audio is required:
 
   -> if local or remote channel is not mISDN
-  -> if endpoint is linked to asterisk
   -> if call is recorded (vbox)
 
 
@@ -55,8 +53,12 @@ Functions:
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
+#include <math.h>
 #include "main.h"
 
+#define SHORT_MIN -32768
+#define SHORT_MAX 32767
+
 class Port *port_first = NULL;
 
 unsigned long port_serial = 1; /* must be 1, because 0== no port */
@@ -88,8 +90,7 @@ void Port::free_epointlist(struct epoint_list *epointlist)
 
        /* free */
        PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
-       memset(temp, 0, sizeof(struct epoint_list));
-       free(temp);
+       FREE(temp, sizeof(struct epoint_list));
        ememuse--;
 }
 
@@ -110,7 +111,7 @@ void Port::free_epointid(unsigned long epoint_id)
        }
        if (temp == 0)
        {
-               PERROR("epoint_id not in port's list, exitting.\n");
+               PERROR("epoint_id not in port's list.\n");
                return;
        }
        /* detach */
@@ -118,8 +119,7 @@ void Port::free_epointid(unsigned long epoint_id)
 
        /* free */
        PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
-       memset(temp, 0, sizeof(struct epoint_list));
-       free(temp);
+       FREE(temp, sizeof(struct epoint_list));
        ememuse--;
 }
 
@@ -131,15 +131,11 @@ struct epoint_list *Port::epointlist_new(unsigned long epoint_id)
        struct epoint_list *epointlist, **epointlistpointer;
 
        /* epointlist structure */
-       epointlist = (struct epoint_list *)calloc(1, sizeof(struct epoint_list));
+       epointlist = (struct epoint_list *)MALLOC(sizeof(struct epoint_list));
        if (!epointlist)
-       {
-               PERROR("no mem for allocating epoint_list\n");
-               return(0);
-       }
+               FATAL("No memory for epointlist\n");
        ememuse++;
        PDEBUG(DEBUG_EPOINT, "PORT(%d) allocating epoint_list.\n", p_serial);
-       memset(epointlist, 0, sizeof(struct epoint_list));
 
        /* add epoint_list to chain */
        epointlist->next = NULL;
@@ -175,11 +171,8 @@ Port::Port(int type, char *portname, struct port_settings *settings)
        }
        SCPY(p_name, portname);
        SCPY(p_tone_dir, p_settings.tones_dir); // just to be sure
-       p_last_tv_sec = 0;
-       p_last_tv_msec = 0;
        p_type = type;
        p_serial = port_serial++;
-       p_debug_nothingtosend = 0;
        p_tone_fh = -1;
        p_tone_fetched = NULL;
        p_tone_name[0] = '\0';
@@ -226,7 +219,7 @@ Port::~Port(void)
        struct message *message;
 
        if (p_record)
-               close_record(0);
+               close_record(0, 0);
 
        classuse--;
 
@@ -255,10 +248,7 @@ Port::~Port(void)
                temp = temp->next;
        }
        if (temp == NULL)
-       {
-               PERROR("PORT(%s) port not in port's list.\n", p_name);
-               exit(-1);
-       }
+               FATAL("PORT(%s) port not in port's list.\n", p_name);
        /* detach */
        *tempp=this->next;
 
@@ -495,7 +485,7 @@ void Port::set_vbox_speed(int speed)
 
 /*
  * read from the given file as specified in port_set_tone and return sample data
- * silence is appended if sample ends, but only the number of samples with tones are returned
+ * if the tone ends, the result may be less samples than requested
  */
 int Port::read_audio(unsigned char *buffer, int length)
 {
@@ -511,12 +501,8 @@ int Port::read_audio(unsigned char *buffer, int length)
        len = length;
 
        /* if there is no tone set, use silence */
-       if (p_tone_name[0] == 0)
-       {
-rest_is_silence:
-               memset(buffer, (options.law=='a')?0x2a:0xff, len); /* silence */
-               goto done;
-       }
+       if (!p_tone_name[0])
+               return(0);
 
        /* if the file pointer is not open, we open it */
        if (p_tone_fh<0 && p_tone_fetched==NULL)
@@ -586,7 +572,7 @@ read_more:
        }
 
        if (len==0)
-               goto done;
+               return(length-len);
 
        if (p_tone_fh >= 0)
        {
@@ -605,7 +591,7 @@ read_more:
                PDEBUG(DEBUG_PORT, "PORT(%s) 0-length loop: %s\n", p_name, filename);
                p_tone_name[0]=0;
                p_tone_dir[0]=0;
-               goto rest_is_silence;
+               return(length-len);
        }
 
        /* if eof is reached, or if the normal file cannot be opened, continue with the loop file if possible */
@@ -630,7 +616,7 @@ try_loop:
                                PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
                                p_tone_dir[0] = '\0';
                                p_tone_name[0] = '\0';
-                               goto rest_is_silence;
+                               return(length-len);
                        }
                        fhuse++;
                }
@@ -643,7 +629,7 @@ try_loop:
                        PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
                        p_tone_dir[0] = '\0';
                        p_tone_name[0] = '\0';
-                       goto rest_is_silence;
+                       return(length-len);
                }
                fhuse++;
        }
@@ -652,9 +638,6 @@ try_loop:
 
        /* now we have opened the loop */
        goto read_more;
-
-done:
-       return(length-len);
 }
 
 
@@ -806,10 +789,9 @@ int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ig
 /*
  * close the recoding file, put header in front and rename
  */
-void Port::close_record(int beep)
+void Port::close_record(int beep, int mute)
 {
-       static signed long beep_mono[] = {-10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000};
-       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};
+       static signed short beep_mono[256];
        unsigned long size, wsize;
        struct fmt fmt;
        char filename[512], indexname[512];
@@ -825,7 +807,7 @@ void Port::close_record(int beep)
        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);
 
        memcpy(&callerinfo, &p_callerinfo, sizeof(struct caller_info));
-       apply_callerid_restriction(p_record_anon_ignore, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.extension, callerinfo.name);
+//     apply_callerid_restriction(p_record_anon_ignore, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.extension, callerinfo.name);
 
        SCPY(number, p_dialinginfo.id);
        SCPY(callerid, numberrize_callerinfo(callerinfo.id, callerinfo.ntype));
@@ -867,41 +849,31 @@ void Port::close_record(int beep)
                i++;
        }
 
+       /* mute */
+       if (mute && p_record_type==CODEC_MONO)
+       {
+               i = p_record_length;
+               if (i > mute)
+                       i = mute;       
+               fseek(p_record, -(i<<1), SEEK_END);
+               p_record_length -= (i<<1);
+       }
        /* add beep to the end of recording */
-       if (beep)
-       switch(p_record_type)
+       if (beep && p_record_type==CODEC_MONO)
        {
-               case CODEC_MONO:
-               i = 0;
-               while(i < beep)
-               {
-                       fwrite(beep_mono, sizeof(beep_mono), 1, p_record);
-                       i += sizeof(beep_mono);
-                       p_record_length += sizeof(beep_mono);
-               }
-               break;
-               case CODEC_8BIT:
                i = 0;
-               while(i < beep)
+               while(i < 256)
                {
-                       fwrite(beep_8bit, sizeof(beep_8bit), 1, p_record);
-                       i += sizeof(beep_8bit);
-                       p_record_length += sizeof(beep_8bit);
+                       beep_mono[i] = (signed short)(sin((double)i / 5.688888888889 * 2.0 * 3.1415927) * 2000.0);
+                       i++;
                }
-               break;
-#if 0
-               case CODEC_LAW:
                i = 0;
                while(i < beep)
                {
-                       fwrite(beep_law, sizeof(beep_law), 1, p_record);
-                       i += sizeof(beep_law);
-                       p_record_length += sizeof(beep_law);
+                       fwrite(beep_mono, sizeof(beep_mono), 1, p_record);
+                       i += sizeof(beep_mono);
+                       p_record_length += sizeof(beep_mono);
                }
-               break;
-#endif
-               default:
-               PERROR("codec %d not supported for beep adding\n", p_record_type);
        }
 
        /* complete header */
@@ -1047,26 +1019,33 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
        if (!p_record || !length)
                return;
 
-       /* skip */
-       if (dir_fromup)
+       /* skip data from local caller (dtmf input) */
+       if (p_record_skip && !dir_fromup)
        {
-               /* more than we have */
+               /* more to skip than we have */
                if (p_record_skip > length)
                {
                        p_record_skip -= length;
                        return;
                }
+               /* less to skip */
                data += p_record_skip;
                length -= p_record_skip;
+               p_record_skip = 0;
        }
 
+//printf("dir=%d len=%d\n", dir_fromup, length);
+
        free = ((p_record_buffer_readp - p_record_buffer_writep - 1) & RECORD_BUFFER_MASK);
 
+//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);
+
        /* the buffer stores the same data stream */
        if (dir_fromup == p_record_buffer_dir)
        {
-               same_again:
+same_again:
 
+//printf("same free=%d length=%d\n", free, length);
                /* first write what we can to the buffer */
                while(free && length)
                {
@@ -1078,7 +1057,7 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                /* all written, so we return */
                if (!length)
                        return;
-               /* still data left, buffer is full, so we need to write to file */
+               /* still data left, buffer is full, so we need to write a chunk to file */
                switch(p_record_type)
                {
                        case CODEC_MONO:
@@ -1091,6 +1070,7 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                                i++;
                        }
                        fwrite(write_buffer, 512, 1, p_record);
+                       p_record_length += 512;
                        break;
 
                        case CODEC_STEREO:
@@ -1117,6 +1097,7 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                                }
                        }
                        fwrite(write_buffer, 1024, 1, p_record);
+                       p_record_length += 1024;
                        break;
 
                        case CODEC_8BIT:
@@ -1124,11 +1105,12 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                        i = 0;
                        while(i < 256)
                        {
-                               *d++ = (p_record_buffer[p_record_buffer_readp]+0x8000) >> 8;
+                               *d++ = ((unsigned short)(p_record_buffer[p_record_buffer_readp]+0x8000)) >> 8;
                                p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
                                i++;
                        }
                        fwrite(write_buffer, 512, 1, p_record);
+                       p_record_length += 512;
                        break;
 
                        case CODEC_LAW:
@@ -1141,14 +1123,16 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                                i++;
                        }
                        fwrite(write_buffer, 256, 1, p_record);
+                       p_record_length += 256;
                        break;
                }
                /* because we still have data, we write again */
-               free += sizeof(write_buffer);
+               free += 256;
                goto same_again;
        }
        /* the buffer stores the other stream */
        
+different_again:
        /* if buffer empty, change it */
        if (p_record_buffer_readp == p_record_buffer_writep)
        {
@@ -1159,6 +1143,12 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
        ii = (p_record_buffer_writep - p_record_buffer_readp) & RECORD_BUFFER_MASK;
        if (length < ii)
                ii = length;
+
+       if (ii > 256)
+               ii = 256;
+//printf("same ii=%d length=%d\n", ii, length);
+//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);
+
        /* write data mixed with the buffer */
        switch(p_record_type)
        {
@@ -1170,14 +1160,13 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                        sample = p_record_buffer[p_record_buffer_readp]
                                + audio_law_to_s32[*data++];
                        p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
-                       if (sample < 32767)
-                               sample = -32767;
-                       if (sample > 32768)
-                               sample = 32768;
+                       if (sample < SHORT_MIN) sample = SHORT_MIN;
+                       if (sample > SHORT_MAX) sample = SHORT_MAX;
                        *s++ = sample;
                        i++;
                }
                fwrite(write_buffer, ii<<1, 1, p_record);
+               p_record_length += (ii<<1);
                break;
                
                case CODEC_STEREO:
@@ -1199,10 +1188,12 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                        {
                                *s++ = p_record_buffer[p_record_buffer_readp];
                                *s++ = audio_law_to_s32[*data++];
+                               p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
                                i++;
                        }
                }
                fwrite(write_buffer, ii<<2, 1, p_record);
+               p_record_length += (ii<<2);
                break;
                
                case CODEC_8BIT:
@@ -1213,14 +1204,13 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                        sample = p_record_buffer[p_record_buffer_readp]
                                + audio_law_to_s32[*data++];
                        p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
-                       if (sample < 32767)
-                               sample = -32767;
-                       if (sample > 32768)
-                               sample = 32768;
+                       if (sample < SHORT_MIN) sample = SHORT_MIN;
+                       if (sample > SHORT_MAX) sample = SHORT_MAX;
                        *d++ = (sample+0x8000) >> 8;
                        i++;
                }
                fwrite(write_buffer, ii, 1, p_record);
+               p_record_length += ii;
                break;
                
                case CODEC_LAW:
@@ -1231,23 +1221,19 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                        sample = p_record_buffer[p_record_buffer_readp]
                                + audio_law_to_s32[*data++];
                        p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
-                       if (sample < 32767)
-                               sample = -32767;
-                       if (sample > 32768)
-                               sample = 32768;
+                       if (sample < SHORT_MIN) sample = SHORT_MIN;
+                       if (sample > SHORT_MAX) sample = SHORT_MAX;
                        *d++ = audio_s16_to_law[sample & 0xffff];
                        i++;
                }
                fwrite(write_buffer, ii, 1, p_record);
+               p_record_length += ii;
                break;
        }
        length -= ii;
-       /* data, but buffer empty */
+       /* still data */
        if (length)
-       {
-               p_record_buffer_dir = dir_fromup;
-               goto same_again;
-       }
+               goto different_again;
        /* no data (maybe buffer) */
        return;