Changeset 5734


Ignore:
Timestamp:
Feb 12, 2018 6:18:22 AM (6 years ago)
Author:
ming
Message:

Fixed #2089: Support receiving Opus packets with various frame lengths

Location:
pjproject/trunk/pjmedia
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjmedia/include/pjmedia-codec/opus.h

    r5239 r5734  
    100100    unsigned   sample_rate; /**< Sample rate in Hz.                     */ 
    101101    unsigned   channel_cnt; /**< Number of channels.                    */ 
     102    unsigned   frm_ptime;   /**< Frame time in msec.                    */ 
    102103    unsigned   bit_rate;    /**< Encoder bit rate in bps.               */ 
    103104    unsigned   packet_loss; /**< Encoder's expected packet loss pct.    */ 
  • pjproject/trunk/pjmedia/include/pjmedia/jbuf.h

    r3841 r5734  
    167167                                         unsigned max_count, 
    168168                                         pjmedia_jbuf **p_jb); 
     169 
     170/** 
     171 * Set the jitter buffer's frame duration. 
     172 * 
     173 * @param jb            The jitter buffer 
     174 * @param ptime         Frame duration. 
     175 * 
     176 * @return              PJ_SUCCESS on success. 
     177 */ 
     178PJ_DECL(pj_status_t) pjmedia_jbuf_set_ptime( pjmedia_jbuf *jb, 
     179                                             unsigned ptime); 
     180 
    169181 
    170182/** 
  • pjproject/trunk/pjmedia/src/pjmedia-codec/opus.c

    r5731 r5734  
    4343 */ 
    4444#define MAX_ENCODED_PACKET_SIZE         1280 
     45 
     46/* Default frame time (msec) */ 
     47#define PTIME                   20 
    4548 
    4649/* Tracing */ 
     
    137140    OpusRepacketizer            *dec_packer; 
    138141    pjmedia_codec_opus_config    cfg; 
    139     unsigned                     ptime; 
     142    unsigned                     enc_ptime; 
     143    unsigned                     dec_ptime; 
    140144    pjmedia_frame                dec_frame[2]; 
    141145    int                          dec_frame_index; 
     
    150154    PJMEDIA_CODEC_OPUS_DEFAULT_SAMPLE_RATE,     /* Sample rate          */ 
    151155    1,                                          /* Channel count        */ 
     156    PTIME,                                      /* Frame time           */                       
    152157    PJMEDIA_CODEC_OPUS_DEFAULT_BIT_RATE,        /* Bit rate             */ 
    153158    5,                                          /* Expected packet loss */ 
     
    387392    param->info.clock_rate = opus_cfg.sample_rate = cfg->sample_rate; 
    388393    param->info.max_bps = opus_cfg.sample_rate * 2; 
     394    param->info.frm_ptime = opus_cfg.frm_ptime = cfg->frm_ptime; 
    389395 
    390396    /* Set channel count */ 
     
    468474    attr->info.avg_bps             = opus_cfg.bit_rate; 
    469475    attr->info.max_bps             = opus_cfg.sample_rate * 2; 
    470     attr->info.frm_ptime           = 20; 
     476    attr->info.frm_ptime           = opus_cfg.frm_ptime; 
    471477    attr->setting.frm_per_pkt      = 1; 
    472478    attr->info.pcm_bits_per_sample = 16; 
     
    601607    opus_data->cfg.sample_rate = attr->info.clock_rate; 
    602608    opus_data->cfg.channel_cnt = attr->info.channel_cnt; 
    603     opus_data->ptime      = attr->info.frm_ptime; 
     609    opus_data->enc_ptime = opus_data->dec_ptime = attr->info.frm_ptime; 
    604610 
    605611    /* Allocate memory used by the codec */ 
     
    815821    } 
    816822 
    817     samples_per_frame = (opus_data->cfg.sample_rate * 
    818                          opus_data->ptime) / 1000; 
    819  
    820823    pj_memcpy(tmp_buf, pkt, pkt_size); 
    821824 
     
    837840        frames[i].buf = ((char*)pkt) + out_pos; 
    838841        frames[i].size = size; 
     842        frames[i].bit_info = opus_packet_get_nb_samples(frames[i].buf, 
     843                             frames[i].size, opus_data->cfg.sample_rate); 
     844 
     845        if (i == 0) { 
     846            unsigned ptime = frames[i].bit_info * 1000 / 
     847                             opus_data->cfg.sample_rate; 
     848            if (ptime != opus_data->dec_ptime) { 
     849                PJ_LOG(4, (THIS_FILE, "Opus ptime change detected: %d ms " 
     850                                      "--> %d ms", 
     851                                      opus_data->dec_ptime, ptime)); 
     852                opus_data->dec_ptime = ptime; 
     853                opus_data->dec_frame_index = -1; 
     854 
     855                /* Signal to the stream about ptime change. */ 
     856                frames[i].bit_info |= 0x10000; 
     857            } 
     858            samples_per_frame = frames[i].bit_info; 
     859        } 
     860 
    839861        frames[i].timestamp.u64 = ts->u64 + i * samples_per_frame; 
    840862        out_pos += size; 
     
    867889 
    868890    samples_per_frame = (opus_data->cfg.sample_rate * 
    869                          opus_data->ptime) / 1000; 
     891                         opus_data->enc_ptime) / 1000; 
    870892    frame_size = samples_per_frame * opus_data->cfg.channel_cnt * 
    871893                 sizeof(opus_int16); 
     
    9821004        frm_size = PJ_MIN(frm_size, 
    9831005                          opus_data->cfg.sample_rate * 
    984                           opus_data->ptime / 1000); 
     1006                          opus_data->dec_ptime / 1000); 
    9851007    } 
    9861008    decoded_samples = opus_decode( opus_data->dec, 
     
    10421064        /* Recover the first packet? Don't think so, fill it with zeroes. */ 
    10431065        pj_uint16_t samples_per_frame; 
    1044         samples_per_frame = (pj_uint16_t)(opus_data->cfg.sample_rate *  
    1045                                           opus_data->ptime) / 1000; 
     1066        samples_per_frame = opus_data->cfg.sample_rate * opus_data->dec_ptime/ 
     1067                            1000; 
    10461068        output->type = PJMEDIA_FRAME_TYPE_AUDIO; 
    10471069        output->size = samples_per_frame << 1; 
     
    10571079    if (inframe->type != PJMEDIA_FRAME_TYPE_AUDIO) { 
    10581080        frm_size = PJ_MIN(frm_size, opus_data->cfg.sample_rate * 
    1059                           opus_data->ptime/1000); 
     1081                          opus_data->dec_ptime/1000); 
    10601082    } 
    10611083    decoded_samples = opus_decode(opus_data->dec, 
  • pjproject/trunk/pjmedia/src/pjmedia/jbuf.c

    r4728 r5734  
    586586 
    587587 
     588PJ_DEF(pj_status_t) pjmedia_jbuf_set_ptime( pjmedia_jbuf *jb, 
     589                                            unsigned ptime) 
     590{ 
     591    PJ_ASSERT_RETURN(jb, PJ_EINVAL); 
     592 
     593    jb->jb_frame_ptime    = ptime; 
     594    jb->jb_min_shrink_gap = PJMEDIA_JBUF_DISC_MIN_GAP / ptime; 
     595    jb->jb_max_burst      = PJ_MAX(MAX_BURST_MSEC / ptime, 
     596                                   jb->jb_max_count*3/4); 
     597 
     598    return PJ_SUCCESS; 
     599} 
     600 
     601 
    588602/* 
    589603 * Set the jitter buffer to fixed delay mode. The default behavior 
  • pjproject/trunk/pjmedia/src/pjmedia/stream.c

    r5671 r5734  
    135135    unsigned                 enc_buf_count; /**< Number of samples in the 
    136136                                                 encoding buffer.           */ 
     137 
     138    pj_int16_t              *dec_buf;       /**< Decoding buffer.           */ 
     139    unsigned                 dec_buf_size;  /**< Decoding buffer size, in 
     140                                                 samples.                   */ 
     141    unsigned                 dec_buf_pos;   /**< First position in buf.     */ 
     142    unsigned                 dec_buf_count; /**< Number of samples in the 
     143                                                 decoding buffer.           */ 
     144 
     145    pj_uint16_t              dec_ptime;     /**< Decoder frame ptime in ms. */ 
     146    pj_bool_t                detect_ptime_change; 
     147                                            /**< Detect decode ptime change */ 
    137148 
    138149    unsigned                 plc_cnt;       /**< # of consecutive PLC frames*/ 
     
    500511 
    501512    samples_required = PJMEDIA_PIA_SPF(&stream->port.info); 
    502     samples_per_frame = stream->codec_param.info.frm_ptime * 
     513    samples_per_frame = stream->dec_ptime * 
    503514                        stream->codec_param.info.clock_rate * 
    504515                        stream->codec_param.info.channel_cnt / 
     
    506517    p_out_samp = (pj_int16_t*) frame->buf; 
    507518 
    508     for (samples_count=0; samples_count < samples_required; 
    509          samples_count += samples_per_frame) 
    510     { 
     519    for (samples_count=0; samples_count < samples_required;) { 
    511520        char frame_type; 
    512521        pj_size_t frame_size; 
    513522        pj_uint32_t bit_info; 
     523 
     524        if (stream->dec_buf && stream->dec_buf_pos < stream->dec_buf_count) { 
     525            unsigned nsamples_req = samples_required - samples_count; 
     526            unsigned nsamples_avail = stream->dec_buf_count - 
     527                                      stream->dec_buf_pos; 
     528            unsigned nsamples_copy = PJ_MIN(nsamples_req, nsamples_avail); 
     529             
     530            pjmedia_copy_samples(p_out_samp + samples_count, 
     531                                 stream->dec_buf + stream->dec_buf_pos, 
     532                                 nsamples_copy); 
     533            samples_count += nsamples_copy; 
     534            stream->dec_buf_pos += nsamples_copy; 
     535            continue; 
     536        } 
    514537 
    515538        /* Get frame from jitter buffer. */ 
     
    559582            } 
    560583 
     584            samples_count += samples_per_frame; 
    561585        } else if (frame_type == PJMEDIA_JB_ZERO_EMPTY_FRAME) { 
    562586 
     
    676700            /* Got "NORMAL" frame from jitter buffer */ 
    677701            pjmedia_frame frame_in, frame_out; 
     702            pj_bool_t use_dec_buf = PJ_FALSE; 
    678703 
    679704            stream->plc_cnt = 0; 
     
    687712            frame_out.buf = p_out_samp + samples_count; 
    688713            frame_out.size = frame->size - samples_count*BYTES_PER_SAMPLE; 
     714            if (stream->dec_buf && 
     715                bit_info * sizeof(pj_int16_t) > frame_out.size) 
     716            { 
     717                stream->dec_buf_pos = 0; 
     718                stream->dec_buf_count = bit_info; 
     719 
     720                use_dec_buf = PJ_TRUE; 
     721                frame_out.buf = stream->dec_buf; 
     722                frame_out.size = stream->dec_buf_size; 
     723            } 
     724 
    689725            status = pjmedia_codec_decode( stream->codec, &frame_in, 
    690726                                           (unsigned)frame_out.size, 
     
    694730                         status)); 
    695731 
    696                 pjmedia_zero_samples(p_out_samp + samples_count, 
    697                                      samples_per_frame); 
     732                if (use_dec_buf) { 
     733                    pjmedia_zero_samples(p_out_samp + samples_count, 
     734                                         samples_per_frame); 
     735                } else { 
     736                    pjmedia_zero_samples(stream->dec_buf, 
     737                                         stream->dec_buf_count); 
     738                } 
     739            } else if (use_dec_buf) { 
     740                stream->dec_buf_count = frame_out.size / sizeof(pj_int16_t); 
    698741            } 
    699742 
     
    710753                stream->jb_last_frm_cnt++; 
    711754            } 
     755            if (!use_dec_buf) 
     756                samples_count += samples_per_frame; 
    712757        } 
    713758    } 
     
    17551800                     status)); 
    17561801            count = 0; 
     1802        } else if (stream->detect_ptime_change && 
     1803                   frames[0].bit_info > 0xFFFF) 
     1804        { 
     1805            unsigned dec_ptime; 
     1806             
     1807            PJ_LOG(4, (stream->port.info.name.ptr, "codec decode " 
     1808                       "ptime change detected")); 
     1809            frames[0].bit_info &= 0xFFFF; 
     1810            dec_ptime = frames[0].bit_info * 1000 / 
     1811                        stream->codec_param.info.clock_rate; 
     1812            stream->rtp_rx_ts_len_per_frame= stream->rtp_rx_ts_len_per_frame * 
     1813                                             dec_ptime / stream->dec_ptime; 
     1814            stream->dec_ptime = dec_ptime; 
     1815            pjmedia_jbuf_set_ptime(stream->jb, stream->dec_ptime); 
    17571816        } 
    17581817 
     
    18251884 
    18261885        } else { 
    1827             ts_span = stream->codec_param.info.frm_ptime * 
     1886            ts_span = stream->dec_ptime * 
    18281887                      stream->codec_param.info.clock_rate / 
    18291888                      1000; 
    18301889        } 
    18311890#else 
    1832         ts_span = stream->codec_param.info.frm_ptime * 
     1891        ts_span = stream->dec_ptime * 
    18331892                  stream->codec_param.info.clock_rate / 
    18341893                  1000; 
     
    21242183        stream->codec_param.info.clock_rate = info->fmt.clock_rate; 
    21252184        stream->codec_param.info.channel_cnt = info->fmt.channel_cnt; 
     2185 
     2186        /* Allocate decoding buffer as Opus can send a packet duration of 
     2187         * up to 120 ms. 
     2188         */ 
     2189        stream->dec_buf_size = stream->codec_param.info.clock_rate * 120 / 
     2190                               1000; 
     2191        stream->dec_buf = (pj_int16_t*)pj_pool_alloc(pool, 
     2192                                                     stream->dec_buf_size * 
     2193                                                     sizeof(pj_int16_t)); 
    21262194    } 
    21272195 
     
    21312199 
    21322200    /* Set additional info and callbacks. */ 
     2201    stream->dec_ptime = stream->codec_param.info.frm_ptime; 
    21332202    afd->bits_per_sample = 16; 
    21342203    afd->frame_time_usec = stream->codec_param.info.frm_ptime * 
     
    22482317        stream->rtp_tx_ts_len_per_pkt *= opus_ts_modifier; 
    22492318        stream->rtp_rx_ts_len_per_frame *= opus_ts_modifier; 
     2319        stream->detect_ptime_change = PJ_TRUE; 
    22502320    } 
    22512321#endif 
Note: See TracChangeset for help on using the changeset viewer.