Changeset 5734
- Timestamp:
- Feb 12, 2018 6:18:22 AM (7 years ago)
- Location:
- pjproject/trunk/pjmedia
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjmedia/include/pjmedia-codec/opus.h
r5239 r5734 100 100 unsigned sample_rate; /**< Sample rate in Hz. */ 101 101 unsigned channel_cnt; /**< Number of channels. */ 102 unsigned frm_ptime; /**< Frame time in msec. */ 102 103 unsigned bit_rate; /**< Encoder bit rate in bps. */ 103 104 unsigned packet_loss; /**< Encoder's expected packet loss pct. */ -
pjproject/trunk/pjmedia/include/pjmedia/jbuf.h
r3841 r5734 167 167 unsigned max_count, 168 168 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 */ 178 PJ_DECL(pj_status_t) pjmedia_jbuf_set_ptime( pjmedia_jbuf *jb, 179 unsigned ptime); 180 169 181 170 182 /** -
pjproject/trunk/pjmedia/src/pjmedia-codec/opus.c
r5731 r5734 43 43 */ 44 44 #define MAX_ENCODED_PACKET_SIZE 1280 45 46 /* Default frame time (msec) */ 47 #define PTIME 20 45 48 46 49 /* Tracing */ … … 137 140 OpusRepacketizer *dec_packer; 138 141 pjmedia_codec_opus_config cfg; 139 unsigned ptime; 142 unsigned enc_ptime; 143 unsigned dec_ptime; 140 144 pjmedia_frame dec_frame[2]; 141 145 int dec_frame_index; … … 150 154 PJMEDIA_CODEC_OPUS_DEFAULT_SAMPLE_RATE, /* Sample rate */ 151 155 1, /* Channel count */ 156 PTIME, /* Frame time */ 152 157 PJMEDIA_CODEC_OPUS_DEFAULT_BIT_RATE, /* Bit rate */ 153 158 5, /* Expected packet loss */ … … 387 392 param->info.clock_rate = opus_cfg.sample_rate = cfg->sample_rate; 388 393 param->info.max_bps = opus_cfg.sample_rate * 2; 394 param->info.frm_ptime = opus_cfg.frm_ptime = cfg->frm_ptime; 389 395 390 396 /* Set channel count */ … … 468 474 attr->info.avg_bps = opus_cfg.bit_rate; 469 475 attr->info.max_bps = opus_cfg.sample_rate * 2; 470 attr->info.frm_ptime = 20;476 attr->info.frm_ptime = opus_cfg.frm_ptime; 471 477 attr->setting.frm_per_pkt = 1; 472 478 attr->info.pcm_bits_per_sample = 16; … … 601 607 opus_data->cfg.sample_rate = attr->info.clock_rate; 602 608 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; 604 610 605 611 /* Allocate memory used by the codec */ … … 815 821 } 816 822 817 samples_per_frame = (opus_data->cfg.sample_rate *818 opus_data->ptime) / 1000;819 820 823 pj_memcpy(tmp_buf, pkt, pkt_size); 821 824 … … 837 840 frames[i].buf = ((char*)pkt) + out_pos; 838 841 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 839 861 frames[i].timestamp.u64 = ts->u64 + i * samples_per_frame; 840 862 out_pos += size; … … 867 889 868 890 samples_per_frame = (opus_data->cfg.sample_rate * 869 opus_data-> ptime) / 1000;891 opus_data->enc_ptime) / 1000; 870 892 frame_size = samples_per_frame * opus_data->cfg.channel_cnt * 871 893 sizeof(opus_int16); … … 982 1004 frm_size = PJ_MIN(frm_size, 983 1005 opus_data->cfg.sample_rate * 984 opus_data-> ptime / 1000);1006 opus_data->dec_ptime / 1000); 985 1007 } 986 1008 decoded_samples = opus_decode( opus_data->dec, … … 1042 1064 /* Recover the first packet? Don't think so, fill it with zeroes. */ 1043 1065 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; 1046 1068 output->type = PJMEDIA_FRAME_TYPE_AUDIO; 1047 1069 output->size = samples_per_frame << 1; … … 1057 1079 if (inframe->type != PJMEDIA_FRAME_TYPE_AUDIO) { 1058 1080 frm_size = PJ_MIN(frm_size, opus_data->cfg.sample_rate * 1059 opus_data-> ptime/1000);1081 opus_data->dec_ptime/1000); 1060 1082 } 1061 1083 decoded_samples = opus_decode(opus_data->dec, -
pjproject/trunk/pjmedia/src/pjmedia/jbuf.c
r4728 r5734 586 586 587 587 588 PJ_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 588 602 /* 589 603 * Set the jitter buffer to fixed delay mode. The default behavior -
pjproject/trunk/pjmedia/src/pjmedia/stream.c
r5671 r5734 135 135 unsigned enc_buf_count; /**< Number of samples in the 136 136 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 */ 137 148 138 149 unsigned plc_cnt; /**< # of consecutive PLC frames*/ … … 500 511 501 512 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 * 503 514 stream->codec_param.info.clock_rate * 504 515 stream->codec_param.info.channel_cnt / … … 506 517 p_out_samp = (pj_int16_t*) frame->buf; 507 518 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;) { 511 520 char frame_type; 512 521 pj_size_t frame_size; 513 522 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 } 514 537 515 538 /* Get frame from jitter buffer. */ … … 559 582 } 560 583 584 samples_count += samples_per_frame; 561 585 } else if (frame_type == PJMEDIA_JB_ZERO_EMPTY_FRAME) { 562 586 … … 676 700 /* Got "NORMAL" frame from jitter buffer */ 677 701 pjmedia_frame frame_in, frame_out; 702 pj_bool_t use_dec_buf = PJ_FALSE; 678 703 679 704 stream->plc_cnt = 0; … … 687 712 frame_out.buf = p_out_samp + samples_count; 688 713 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 689 725 status = pjmedia_codec_decode( stream->codec, &frame_in, 690 726 (unsigned)frame_out.size, … … 694 730 status)); 695 731 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); 698 741 } 699 742 … … 710 753 stream->jb_last_frm_cnt++; 711 754 } 755 if (!use_dec_buf) 756 samples_count += samples_per_frame; 712 757 } 713 758 } … … 1755 1800 status)); 1756 1801 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); 1757 1816 } 1758 1817 … … 1825 1884 1826 1885 } else { 1827 ts_span = stream-> codec_param.info.frm_ptime *1886 ts_span = stream->dec_ptime * 1828 1887 stream->codec_param.info.clock_rate / 1829 1888 1000; 1830 1889 } 1831 1890 #else 1832 ts_span = stream-> codec_param.info.frm_ptime *1891 ts_span = stream->dec_ptime * 1833 1892 stream->codec_param.info.clock_rate / 1834 1893 1000; … … 2124 2183 stream->codec_param.info.clock_rate = info->fmt.clock_rate; 2125 2184 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)); 2126 2194 } 2127 2195 … … 2131 2199 2132 2200 /* Set additional info and callbacks. */ 2201 stream->dec_ptime = stream->codec_param.info.frm_ptime; 2133 2202 afd->bits_per_sample = 16; 2134 2203 afd->frame_time_usec = stream->codec_param.info.frm_ptime * … … 2248 2317 stream->rtp_tx_ts_len_per_pkt *= opus_ts_modifier; 2249 2318 stream->rtp_rx_ts_len_per_frame *= opus_ts_modifier; 2319 stream->detect_ptime_change = PJ_TRUE; 2250 2320 } 2251 2321 #endif
Note: See TracChangeset
for help on using the changeset viewer.