Changeset 1676


Ignore:
Timestamp:
Jan 11, 2008 8:22:57 AM (16 years ago)
Author:
bennylp
Message:

Ticket #444: Bug in tone generator: can't play more digits (thanks Marian Dragomir)

Location:
pjproject/trunk/pjmedia
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjmedia/include/pjmedia/tonegen.h

    r974 r1676  
    108108     * the last tone has been played. 
    109109     */ 
    110     PJMEDIA_TONEGEN_LOOP    = 1 
     110    PJMEDIA_TONEGEN_LOOP    = 1, 
     111 
     112    /** 
     113     * Disable mutex protection to the tone generator. 
     114     */ 
     115    PJMEDIA_TONEGEN_NO_LOCK = 2 
    111116}; 
    112117 
  • pjproject/trunk/pjmedia/src/pjmedia/tonegen.c

    r1266 r1676  
    1919#include <pjmedia/tonegen.h> 
    2020#include <pjmedia/errno.h> 
     21#include <pjmedia/silencedet.h> 
    2122#include <pj/assert.h> 
    2223#include <pj/ctype.h> 
     24#include <pj/lock.h> 
     25#include <pj/log.h> 
    2326#include <pj/pool.h> 
    2427 
     
    229232 
    230233#define SIGNATURE   PJMEDIA_PORT_SIGNATURE('t', 'n', 'g', 'n') 
     234#define THIS_FILE   "tonegen.c" 
     235 
     236#if 0 
     237#   define TRACE_(expr) PJ_LOG(4,expr) 
     238#else 
     239#   define TRACE_(expr) 
     240#endif 
    231241 
    232242struct tonegen 
     
    237247    unsigned            options; 
    238248    unsigned            playback_options; 
     249 
     250    /* lock */ 
     251    pj_lock_t          *lock; 
    239252 
    240253    /* Digit map */ 
     
    279292static pj_status_t tonegen_get_frame(pjmedia_port *this_port,  
    280293                                     pjmedia_frame *frame); 
     294static pj_status_t tonegen_destroy(pjmedia_port *this_port); 
    281295 
    282296/* 
     
    294308                                            pjmedia_port **p_port) 
    295309{ 
    296     const pj_str_t STR_TONE_GEN = pj_str("tone-gen"); 
     310    const pj_str_t STR_TONE_GEN = pj_str("tonegen"); 
    297311    struct tonegen  *tonegen; 
    298312    pj_status_t status; 
     
    316330    tonegen->options = options; 
    317331    tonegen->base.get_frame = &tonegen_get_frame; 
     332    tonegen->base.on_destroy = &tonegen_destroy; 
    318333    tonegen->digit_map = &digit_map; 
     334 
     335    /* Lock */ 
     336    if (options & PJMEDIA_TONEGEN_NO_LOCK) { 
     337        status = pj_lock_create_null_mutex(pool, "tonegen", &tonegen->lock); 
     338    } else { 
     339        status = pj_lock_create_simple_mutex(pool, "tonegen", &tonegen->lock); 
     340    } 
     341 
     342    if (status != PJ_SUCCESS) { 
     343        return status; 
     344    } 
     345 
     346    TRACE_((THIS_FILE, "Tonegen created: %u/%u/%u/%u", clock_rate,  
     347            channel_count, samples_per_frame, bits_per_sample)); 
    319348 
    320349    /* Done */ 
     
    356385    struct tonegen *tonegen = (struct tonegen*) port; 
    357386    PJ_ASSERT_RETURN(port->info.signature == SIGNATURE, PJ_EINVAL); 
     387 
     388    TRACE_((THIS_FILE, "tonegen_stop()")); 
     389 
     390    pj_lock_acquire(tonegen->lock); 
    358391    tonegen->count = 0; 
    359392    tonegen->cur_digit = 0; 
    360393    tonegen->dig_samples = 0; 
     394    pj_lock_release(tonegen->lock); 
     395 
    361396    return PJ_SUCCESS; 
    362397} 
    363398 
     399 
     400/* 
     401 * Callback to destroy tonegen 
     402 */ 
     403static pj_status_t tonegen_destroy(pjmedia_port *port) 
     404{ 
     405    struct tonegen *tonegen = (struct tonegen*) port; 
     406    PJ_ASSERT_RETURN(port->info.signature == SIGNATURE, PJ_EINVAL); 
     407 
     408    TRACE_((THIS_FILE, "tonegen_destroy()")); 
     409 
     410    pj_lock_acquire(tonegen->lock); 
     411    pj_lock_release(tonegen->lock); 
     412 
     413    pj_lock_destroy(tonegen->lock); 
     414 
     415    return PJ_SUCCESS; 
     416} 
    364417 
    365418/* 
     
    375428    PJ_ASSERT_RETURN(port->info.signature == SIGNATURE, PJ_EINVAL); 
    376429 
     430    pj_lock_acquire(tonegen->lock); 
     431 
    377432    if (tonegen->count == 0) { 
    378433        /* We don't have digits to play */ 
    379434        frame->type = PJMEDIA_FRAME_TYPE_NONE; 
    380         return PJ_SUCCESS; 
     435        goto on_return; 
    381436    } 
    382437 
     
    389444            tonegen->dig_samples = 0; 
    390445 
     446            TRACE_((THIS_FILE, "tonegen_get_frame(): rewind")); 
     447 
    391448        } else { 
    392449            tonegen->count = 0; 
     450            tonegen->cur_digit = 0; 
    393451            frame->type = PJMEDIA_FRAME_TYPE_NONE; 
    394             return PJ_SUCCESS; 
     452            TRACE_((THIS_FILE, "tonegen_get_frame(): no more digit")); 
     453            goto on_return; 
    395454        } 
    396455    } 
     
    403462        tonegen->cur_digit++; 
    404463        tonegen->dig_samples = 0; 
     464 
     465        TRACE_((THIS_FILE, "tonegen_get_frame(): next digit")); 
    405466    } 
    406467 
     
    415476            tonegen->dig_samples = 0; 
    416477 
     478            TRACE_((THIS_FILE, "tonegen_get_frame(): rewind")); 
     479 
    417480        } else { 
    418481            tonegen->count = 0; 
     482            tonegen->cur_digit = 0; 
    419483            frame->type = PJMEDIA_FRAME_TYPE_NONE; 
    420             return PJ_SUCCESS; 
     484            TRACE_((THIS_FILE, "tonegen_get_frame(): no more digit")); 
     485            goto on_return; 
    421486        } 
    422487    } 
     
    463528 
    464529        /* Move to next digit if we're finished with this tone */ 
    465         if (tonegen->dig_samples == on_samp + off_samp) { 
     530        if (tonegen->dig_samples >= on_samp + off_samp) { 
    466531            tonegen->cur_digit++; 
    467532            tonegen->dig_samples = 0; 
     
    486551    frame->size = port->info.bytes_per_frame; 
    487552 
     553    TRACE_((THIS_FILE, "tonegen_get_frame(): frame created, level=%u", 
     554            pjmedia_calc_avg_signal((pj_int16_t*)frame->buf, frame->size/2))); 
     555 
    488556    if (tonegen->cur_digit >= tonegen->count) { 
    489557        if ((tonegen->options|tonegen->playback_options)&PJMEDIA_TONEGEN_LOOP) 
     
    493561            tonegen->dig_samples = 0; 
    494562 
     563            TRACE_((THIS_FILE, "tonegen_get_frame(): rewind")); 
     564 
    495565        } else { 
    496566            tonegen->count = 0; 
    497         } 
    498     } 
    499  
     567            tonegen->cur_digit = 0; 
     568 
     569            TRACE_((THIS_FILE, "tonegen_get_frame(): no more digit")); 
     570        } 
     571    } 
     572 
     573on_return: 
     574    pj_lock_release(tonegen->lock); 
    500575    return PJ_SUCCESS; 
    501576} 
     
    519594    PJ_ASSERT_RETURN(count+tonegen->count <= PJMEDIA_TONEGEN_MAX_DIGITS, 
    520595                     PJ_ETOOMANY); 
     596 
     597    pj_lock_acquire(tonegen->lock); 
    521598 
    522599    /* Set playback options */ 
     
    538615    tonegen->count += count; 
    539616 
     617    pj_lock_release(tonegen->lock); 
     618 
    540619    return PJ_SUCCESS; 
    541620} 
     
    552631    struct tonegen *tonegen = (struct tonegen*) port; 
    553632    pjmedia_tone_desc tones[PJMEDIA_TONEGEN_MAX_DIGITS]; 
    554     const pjmedia_tone_digit_map *map = tonegen->digit_map; 
     633    const pjmedia_tone_digit_map *map; 
    555634    unsigned i; 
    556635 
     
    558637                     count && digits, PJ_EINVAL); 
    559638    PJ_ASSERT_RETURN(count < PJMEDIA_TONEGEN_MAX_DIGITS, PJ_ETOOMANY); 
     639 
     640    pj_lock_acquire(tonegen->lock); 
     641 
     642    map = tonegen->digit_map; 
    560643 
    561644    for (i=0; i<count; ++i) { 
     
    568651                break; 
    569652        } 
    570         if (j == map->count) 
     653        if (j == map->count) { 
     654            pj_lock_release(tonegen->lock); 
    571655            return PJMEDIA_RTP_EINDTMF; 
     656        } 
    572657 
    573658        tones[i].freq1 = map->digits[j].freq1; 
     
    578663    } 
    579664 
     665    pj_lock_release(tonegen->lock); 
     666 
    580667    return pjmedia_tonegen_play(port, count, tones, options); 
    581668} 
     
    610697    PJ_ASSERT_RETURN(m != NULL, PJ_EINVAL); 
    611698 
     699    pj_lock_acquire(tonegen->lock); 
     700 
    612701    tonegen->digit_map = m; 
    613702 
     703    pj_lock_release(tonegen->lock); 
     704 
    614705    return PJ_SUCCESS; 
    615706} 
Note: See TracChangeset for help on using the changeset viewer.