Ignore:
Timestamp:
Jan 11, 2008 4:30:22 PM (16 years ago)
Author:
bennylp
Message:

Updated encdec sample to support PLC etc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip-apps/src/samples/encdec.c

    r1678 r1679  
    5353; 
    5454 
    55 #define HAS_G729_CODEC  0 
     55#ifndef HAS_G729_CODEC 
     56# define HAS_G729_CODEC 0 
     57#endif 
     58 
    5659#if HAS_G729_CODEC 
    57 #include "keystream_g729ab.h" 
    58 #endif 
    59  
    60 #if 0 
     60#include <keystream_g729ab.h> 
     61#endif 
     62 
     63//#undef PJ_TRACE 
     64//#define PJ_TRACE 1 
     65 
     66#ifndef PJ_TRACE 
     67#       define PJ_TRACE 0 
     68#endif 
     69 
     70#if PJ_TRACE 
    6171#   define TRACE_(expr)     PJ_LOG(4,expr) 
    6272#else 
     
    91101    pjmedia_codec_mgr *cm; 
    92102    pjmedia_codec *codec; 
    93     pjmedia_codec_info *pci; 
     103    const pjmedia_codec_info *pci; 
    94104    pjmedia_codec_param param; 
    95105    unsigned cnt, samples_per_frame; 
    96106    pj_str_t tmp; 
    97107    pjmedia_port *wavin, *wavout; 
     108    unsigned lost_pct; 
    98109    pj_status_t status; 
    99110 
     111#define T   file_msec_duration/1000, file_msec_duration%1000 
     112     
    100113    pool = pjmedia_endpt_create_pool(mept, "encdec", 1000, 1000); 
    101114 
    102115    cm = pjmedia_endpt_get_codec_mgr(mept); 
    103116 
     117#ifdef LOST_PCT 
     118    lost_pct = LOST_PCT; 
     119#else 
     120    lost_pct = 0; 
     121#endif 
     122     
    104123    cnt = 1; 
    105124    CHECK( pjmedia_codec_mgr_find_codecs_by_id(cm, pj_cstr(&tmp, codec_id),  
     
    110129 
    111130    /* Control VAD */ 
    112     param.setting.vad = 0; 
     131    param.setting.vad = 1; 
    113132 
    114133    /* Open wav for reading */ 
     
    144163            break;; 
    145164 
     165        /* Update duration */ 
     166        file_msec_duration += samples_per_frame * 1000 /  
     167                              param.info.clock_rate; 
     168 
    146169        /* Encode */ 
    147170        frm_bit.buf = bitstream; 
     
    149172        CHECK(codec->op->encode(codec, &frm_pcm, sizeof(bitstream), &frm_bit)); 
    150173 
     174        /* On DTX, write zero frame to wavout to maintain duration */ 
     175        if (frm_bit.size == 0 || frm_bit.type != PJMEDIA_FRAME_TYPE_AUDIO) { 
     176            out_frm.buf = (char*)pcmbuf; 
     177            out_frm.size = 160; 
     178            CHECK( pjmedia_port_put_frame(wavout, &out_frm) ); 
     179            TRACE_((THIS_FILE, "%d.%03d read: %u, enc: %u", 
     180                    T, frm_pcm.size, frm_bit.size)); 
     181            continue; 
     182        } 
     183         
     184        /* Simulate jitter buffer bug */ 
     185        if (pci->pt==PJMEDIA_RTP_PT_G729 && frm_bit.size == 2) { 
     186            TRACE_((THIS_FILE, "%d.%03d G729 SID frame masqueraded", T)); 
     187            frm_bit.size = 10; 
     188        } 
     189         
    151190        /* Parse the bitstream (not really necessary for this case 
    152191         * since we always decode 1 frame, but it's still good 
     
    159198        CHECK( (cnt==1 ? PJ_SUCCESS : -1) ); 
    160199 
    161         /* Decode */ 
     200        /* Decode or simulate packet loss */ 
    162201        out_frm.buf = (char*)pcmbuf; 
    163202        out_frm.size = sizeof(pcmbuf); 
    164         CHECK( codec->op->decode(codec, &frames[0], sizeof(pcmbuf),  
    165                                  &out_frm) ); 
     203         
     204        if ((pj_rand() % 100) < lost_pct) { 
     205            /* Simulate loss */ 
     206            CHECK( codec->op->recover(codec, sizeof(pcmbuf), &out_frm) ); 
     207            TRACE_((THIS_FILE, "%d.%03d Packet lost", T)); 
     208        } else { 
     209            /* Decode */ 
     210            CHECK( codec->op->decode(codec, &frames[0], sizeof(pcmbuf),  
     211                                     &out_frm) ); 
     212        } 
    166213 
    167214        /* Write to WAV */ 
    168215        CHECK( pjmedia_port_put_frame(wavout, &out_frm) ); 
    169216 
    170         file_msec_duration += samples_per_frame * 1000 /  
    171                               param.info.clock_rate; 
    172  
    173         TRACE_((THIS_FILE, "Bytes read: %u, encode: %u, decode/write: %u", 
    174                 frm_pcm.size, frm_bit.size, out_frm.size)); 
     217        TRACE_((THIS_FILE, "%d.%03d read: %u, enc: %u, dec/write: %u", 
     218                T, frm_pcm.size, frm_bit.size, out_frm.size)); 
    175219    } 
    176220 
     
    238282        printf("Duration: %ds.%03d\n", file_msec_duration/1000,  
    239283                                       file_msec_duration%1000); 
    240         printf("Time: %ds.%03d\n", t1.sec, t1.msec); 
     284        printf("Time: %lds.%03ld\n", t1.sec, t1.msec); 
    241285    } 
    242286 
Note: See TracChangeset for help on using the changeset viewer.