fixes
[lcr.git] / port.cpp
index a10a6f0..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,6 +53,7 @@ Functions:
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
+#include <math.h>
 #include "main.h"
 
 #define SHORT_MIN -32768
@@ -220,7 +219,7 @@ Port::~Port(void)
        struct message *message;
 
        if (p_record)
-               close_record(0);
+               close_record(0, 0);
 
        classuse--;
 
@@ -790,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];
@@ -809,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));
@@ -851,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 */
@@ -1031,19 +1019,23 @@ 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);
@@ -1051,8 +1043,9 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
        /* 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)
                {
@@ -1064,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:
@@ -1077,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:
@@ -1103,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:
@@ -1110,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:
@@ -1127,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)
        {
@@ -1145,6 +1143,10 @@ 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 */
@@ -1164,6 +1166,7 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                        i++;
                }
                fwrite(write_buffer, ii<<1, 1, p_record);
+               p_record_length += (ii<<1);
                break;
                
                case CODEC_STEREO:
@@ -1190,6 +1193,7 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                        }
                }
                fwrite(write_buffer, ii<<2, 1, p_record);
+               p_record_length += (ii<<2);
                break;
                
                case CODEC_8BIT:
@@ -1206,6 +1210,7 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                        i++;
                }
                fwrite(write_buffer, ii, 1, p_record);
+               p_record_length += ii;
                break;
                
                case CODEC_LAW:
@@ -1222,15 +1227,13 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                        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;