]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
fix audio issue in portaudio
authorAnthony Minessale <anthony.minessale@gmail.com>
Thu, 19 Mar 2009 18:30:56 +0000 (18:30 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Thu, 19 Mar 2009 18:30:56 +0000 (18:30 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@12669 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/mod/endpoints/mod_portaudio/mod_portaudio.c
src/mod/endpoints/mod_portaudio/pablio.c
src/switch_rtp.c

index 7e3d2f719486076bd531bb4c2999bcdf9faf55a6..3c4c603e990aae3d74997e33f8ab39b9371d1d1b 100644 (file)
@@ -124,7 +124,8 @@ static struct {
        private_t *call_list;
        int ring_interval;
        GFLAGS flags;
-       switch_timer_t timer;
+       switch_timer_t read_timer;
+       switch_timer_t write_timer;
        switch_timer_t hold_timer;
        int dual_streams;
        time_t deactivate_timer;
@@ -249,7 +250,7 @@ static switch_status_t channel_on_init(switch_core_session_t *session)
                }
 
                while (switch_channel_get_state(channel) == CS_INIT && !switch_test_flag(tech_pvt, TFLAG_ANSWER)) {
-                       switch_size_t olen = globals.timer.samples;
+                       switch_size_t olen = globals.read_timer.samples;
 
                        if (switch_micro_time_now() - last >= waitsec) {
                                char buf[512];
@@ -268,7 +269,7 @@ static switch_status_t channel_on_init(switch_core_session_t *session)
                        }
 
                        if (ring_file) {
-                               if (switch_core_timer_next(&globals.timer) != SWITCH_STATUS_SUCCESS) {
+                               if (switch_core_timer_next(&globals.read_timer) != SWITCH_STATUS_SUCCESS) {
                                        switch_core_file_close(&fh);
                                        break;
                                }
@@ -279,7 +280,7 @@ static switch_status_t channel_on_init(switch_core_session_t *session)
                                }
 
                                if (globals.ring_stream) {
-                                       WriteAudioStream(globals.ring_stream, abuf, (long) olen, &globals.timer);
+                                       WriteAudioStream(globals.ring_stream, abuf, (long) olen, &globals.write_timer);
                                }
                        } else {
                                switch_yield(10000);
@@ -355,8 +356,12 @@ static void destroy_codecs(void)
                switch_core_codec_destroy(&globals.write_codec);
        }
 
-       if (globals.timer.timer_interface) {
-               switch_core_timer_destroy(&globals.timer);
+       if (globals.read_timer.timer_interface) {
+               switch_core_timer_destroy(&globals.read_timer);
+       }
+
+       if (globals.write_timer.timer_interface) {
+               switch_core_timer_destroy(&globals.write_timer);
        }
 
        if (globals.hold_timer.timer_interface) {
@@ -597,14 +602,13 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch
        
        switch_mutex_lock(globals.device_lock);
        samples = ReadAudioStream(globals.audio_stream, globals.read_frame.data,
-                                                         globals.read_codec.implementation->samples_per_packet, &globals.timer);
+                                                         globals.read_codec.implementation->samples_per_packet, &globals.read_timer);
        switch_mutex_unlock(globals.device_lock);
        
        if (samples) {
                globals.read_frame.datalen = samples * 2;
                globals.read_frame.samples = samples;
 
-               //switch_core_timer_check(&globals.timer, SWITCH_TRUE);
                *frame = &globals.read_frame;
 
                if (!switch_test_flag((&globals), GFLAG_MOUTH)) {
@@ -649,7 +653,7 @@ static switch_status_t channel_write_frame(switch_core_session_t *session, switc
 
        if (globals.audio_stream) {
                if (switch_test_flag((&globals), GFLAG_EAR)) {
-                       WriteAudioStream(globals.audio_stream, (short *) frame->data, (int) (frame->datalen / sizeof(SAMPLE)), &globals.timer);
+                       WriteAudioStream(globals.audio_stream, (short *) frame->data, (int) (frame->datalen / sizeof(SAMPLE)), &globals.write_timer);
                }
                status = SWITCH_STATUS_SUCCESS;
        }
@@ -1278,8 +1282,12 @@ static switch_status_t engage_device(int restart)
                destroy_codecs();
        }
 
-       if (globals.timer.timer_interface) {
-               switch_core_timer_sync(&globals.timer);
+       if (globals.read_timer.timer_interface) {
+               switch_core_timer_sync(&globals.read_timer);
+       }
+
+       if (globals.write_timer.timer_interface) {
+               switch_core_timer_sync(&globals.write_timer);
        }
 
        if (globals.audio_stream) {
@@ -1310,13 +1318,26 @@ static switch_status_t engage_device(int restart)
                }
        }
 
-       if (!globals.timer.timer_interface) {
-               if (switch_core_timer_init(&globals.timer,
+       if (!globals.read_timer.timer_interface) {
+               if (switch_core_timer_init(&globals.read_timer,
+                                                                  globals.timer_name, codec_ms, globals.read_codec.implementation->samples_per_packet,
+                                                                  module_pool) != SWITCH_STATUS_SUCCESS) {
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "setup timer failed!\n");
+                       switch_core_codec_destroy(&globals.read_codec);
+                       switch_core_codec_destroy(&globals.write_codec);
+                       return SWITCH_STATUS_FALSE;
+               }
+       }
+
+
+       if (!globals.write_timer.timer_interface) {
+               if (switch_core_timer_init(&globals.write_timer,
                                                                   globals.timer_name, codec_ms, globals.read_codec.implementation->samples_per_packet,
                                                                   module_pool) != SWITCH_STATUS_SUCCESS) {
                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "setup timer failed!\n");
                        switch_core_codec_destroy(&globals.read_codec);
                        switch_core_codec_destroy(&globals.write_codec);
+                       switch_core_timer_destroy(&globals.read_timer);
                        return SWITCH_STATUS_FALSE;
                }
        }
@@ -1328,7 +1349,8 @@ static switch_status_t engage_device(int restart)
                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "setup hold timer failed!\n");
                        switch_core_codec_destroy(&globals.read_codec);
                        switch_core_codec_destroy(&globals.write_codec);
-                       switch_core_timer_destroy(&globals.timer);
+                       switch_core_timer_destroy(&globals.read_timer);
+                       switch_core_timer_destroy(&globals.write_timer);
                        return SWITCH_STATUS_FALSE;
                }
        }
@@ -1366,7 +1388,8 @@ static switch_status_t engage_device(int restart)
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't open audio device\n");
                switch_core_codec_destroy(&globals.read_codec);
                switch_core_codec_destroy(&globals.write_codec);
-               switch_core_timer_destroy(&globals.timer);
+               switch_core_timer_destroy(&globals.read_timer);
+               switch_core_timer_destroy(&globals.write_timer);
                switch_core_timer_destroy(&globals.hold_timer);
                return SWITCH_STATUS_FALSE;
        }
@@ -1963,8 +1986,8 @@ SWITCH_STANDARD_API(pa_cmd)
                                                                           playfile, seconds, samples, globals.read_codec.implementation->actual_samples_per_second);
                                
                                while (switch_core_file_read(&fh, abuf, &olen) == SWITCH_STATUS_SUCCESS) {
-                                       WriteAudioStream(globals.audio_stream, abuf, (long) olen, &globals.timer);
-                                       switch_core_timer_next(&globals.timer);
+                                       WriteAudioStream(globals.audio_stream, abuf, (long) olen, &globals.read_timer);
+                                       switch_core_timer_next(&globals.read_timer);
                                        samples -= (int) olen;
                                        if (samples <= 0) {
                                                break;
@@ -1993,8 +2016,8 @@ SWITCH_STANDARD_API(pa_cmd)
                        int i;
                        for(i = 0; i < 400; i++) {
                                if ((samples = ReadAudioStream(globals.audio_stream, globals.read_frame.data,
-                                               globals.read_codec.implementation->samples_per_packet, &globals.timer))) {
-                                       WriteAudioStream(globals.audio_stream, globals.read_frame.data, (long) samples, &globals.timer);
+                                               globals.read_codec.implementation->samples_per_packet, &globals.read_timer))) {
+                                       WriteAudioStream(globals.audio_stream, globals.read_frame.data, (long) samples, &globals.write_timer);
                                        success = 1;
                                }
                                switch_yield(10000);
index af4b0016c49446aab7d683bf70bf0c329b4de8a9..12eab8ff0d5cc9f792a3f72f9998757bf66aba43 100644 (file)
@@ -146,17 +146,15 @@ long WriteAudioStream(PABLIO_Stream * aStream, void *data, long numFrames, switc
        char *p = (char *) data;
        long numBytes = aStream->bytesPerFrame * numFrames;
 
-       while (numBytes > 0) {
-               bytesWritten = PaUtil_WriteRingBuffer(&aStream->outFIFO, p, numBytes);
-               numBytes -= bytesWritten;
-               p += bytesWritten;
-               if (numBytes > 0) {
-                       if (switch_core_timer_check(timer, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) {
-                               PaUtil_FlushRingBuffer(&aStream->outFIFO);
-                               return 0;
-                       }
-                       switch_cond_next();
-               }
+       switch_core_timer_next(timer);
+
+       bytesWritten = PaUtil_WriteRingBuffer(&aStream->outFIFO, p, numBytes);
+       numBytes -= bytesWritten;
+       p += bytesWritten;
+       
+       if (numBytes > 0) {
+               PaUtil_FlushRingBuffer(&aStream->outFIFO);
+               return 0;
        }
        return numFrames;
 }
@@ -170,30 +168,31 @@ long ReadAudioStream(PABLIO_Stream * aStream, void *data, long numFrames, switch
        long bytesRead = 0;
        char *p = (char *) data;
        long avail, totalBytes = 0, neededBytes = aStream->bytesPerFrame * numFrames;
+       int max = 5000;
 
-       for (;;) {
-               avail = PaUtil_GetRingBufferReadAvailable(&aStream->inFIFO);
-               
-               if (switch_core_timer_check(timer, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) {
-                       break;
-               }
+       switch_core_timer_next(timer);
+
+       while(totalBytes < neededBytes && --max > 0) {
 
+               avail = PaUtil_GetRingBufferReadAvailable(&aStream->inFIFO);
+               //printf("AVAILABLE BYTES %ld pass %d\n", avail, 5000 - max);
                if (avail >= neededBytes * 6) {
                        PaUtil_FlushRingBuffer(&aStream->inFIFO);
                        avail = 0;
-               }
-
-               bytesRead = 0;
+               } else {
 
-               if (totalBytes < neededBytes && avail >= neededBytes) {
-                       bytesRead = PaUtil_ReadRingBuffer(&aStream->inFIFO, p, neededBytes);
-                       totalBytes += bytesRead;
-               }
+                       bytesRead = 0;
+                       
+                       if (totalBytes < neededBytes && avail >= neededBytes) {
+                               bytesRead = PaUtil_ReadRingBuffer(&aStream->inFIFO, p, neededBytes);
+                               totalBytes += bytesRead;
+                       }
 
-               if (bytesRead) {
-                       p += bytesRead;
-               } else {
-                       switch_cond_next();
+                       if (bytesRead) {
+                               p += bytesRead;
+                       } else {
+                               switch_cond_next();
+                       }
                }
        }
 
index 69aae18a4bdc3bc97fd54ae1896bc349e32cad6d..026f1ecea227960b4afac13aea83dea7fc2449ff 100644 (file)
@@ -1990,12 +1990,12 @@ static int rtp_common_write(switch_rtp_t *rtp_session,
                }
 
                if (rtp_session->timer.interval && 
-                       (rtp_session->timer.samplecount - rtp_session->last_write_samplecount) > rtp_session->samples_per_interval * 2) {
+                       (rtp_session->timer.samplecount - rtp_session->last_write_samplecount) > rtp_session->samples_per_interval * 10) {
                        m++;
                }
 
                if (!rtp_session->timer.interval && 
-                       ((unsigned)((switch_micro_time_now() - rtp_session->last_write_timestamp))) > (rtp_session->ms_per_packet *2)) {
+                       ((unsigned)((switch_micro_time_now() - rtp_session->last_write_timestamp))) > (rtp_session->ms_per_packet * 10)) {
                        m++;
                }