srs中时间戳抖动处理

来源:互联网 发布:工业设计网站 知乎 编辑:程序博客网 时间:2024/05/22 16:55

http://blog.csdn.net/lcalqf/article/details/59543410



[cpp] view plain copy
  1. /** 
  2. * the time jitter algorithm: 
  3. * 1. full, to ensure stream start at zero, and ensure stream monotonically increasing. 
  4. * 2. zero, only ensure sttream start at zero, ignore timestamp jitter. 
  5. * 3. off, disable the time jitter algorithm, like atc. 
  6. */  
  7. enum SrsRtmpJitterAlgorithm  
  8. {  
  9.     SrsRtmpJitterAlgorithmFULL = 0x01,  
  10.     SrsRtmpJitterAlgorithmZERO,  
  11.     SrsRtmpJitterAlgorithmOFF  
  12. };  
  13. int _srs_time_jitter_string2int(std::string time_jitter);  
  14.   
  15. /** 
  16. * time jitter detect and correct, 
  17. * to ensure the rtmp stream is monotonically. 
  18. */  
  19. class SrsRtmpJitter  
  20. {  
  21. private:  
  22.     int64_t last_pkt_time;  
  23.     int64_t last_pkt_correct_time;  
  24. public:  
  25.     SrsRtmpJitter();  
  26.     virtual ~SrsRtmpJitter();  
  27. public:  
  28.     /** 
  29.     * detect the time jitter and correct it. 
  30.     * @param tba, the audio timebase, used to calc the "right" delta if jitter detected. 
  31.     * @param tbv, the video timebase, used to calc the "right" delta if jitter detected. 
  32.     * @param start_at_zero whether ensure stream start at zero. 
  33.     * @param mono_increasing whether ensure stream is monotonically inscreasing. 
  34.     */  
  35.     virtual int correct(SrsSharedPtrMessage* msg, int tba, int tbv, SrsRtmpJitterAlgorithm ag);  
  36.     /** 
  37.     * get current client time, the last packet time. 
  38.     */  
  39.     virtual int get_time();  
  40. };  
超过抖动区域,置为默认值,都是相对时间
[cpp] view plain copy
  1. #define CONST_MAX_JITTER_MS         250  
  2. #define CONST_MAX_JITTER_MS_NEG         -250  
  3. #define DEFAULT_FRAME_TIME_MS         10  
  4.   
  5. // for 26ms per audio packet,  
  6. // 115 packets is 3s.  
  7. #define SRS_PURE_AUDIO_GUESS_COUNT 115  
  8.   
  9. // when got these videos or audios, pure audio or video, mix ok.  
  10. #define SRS_MIX_CORRECT_PURE_AV 10  
  11.   
  12. // the time to cleanup source in ms.  
  13. #define SRS_SOURCE_CLEANUP 30000  
  14.   
  15. int _srs_time_jitter_string2int(std::string time_jitter)  
  16. {  
  17.     if (time_jitter == "full") {  
  18.         return SrsRtmpJitterAlgorithmFULL;  
  19.     } else if (time_jitter == "zero") {  
  20.         return SrsRtmpJitterAlgorithmZERO;  
  21.     } else {  
  22.         return SrsRtmpJitterAlgorithmOFF;  
  23.     }  
  24. }  
  25.   
  26. SrsRtmpJitter::SrsRtmpJitter()  
  27. {  
  28.     last_pkt_correct_time = -1;  
  29.     last_pkt_time = 0;  
  30. }  
  31.   
  32. SrsRtmpJitter::~SrsRtmpJitter()  
  33. {  
  34. }  
  35.   
  36. int SrsRtmpJitter::correct(SrsSharedPtrMessage* msg, SrsRtmpJitterAlgorithm ag)  
  37. {  
  38.     int ret = ERROR_SUCCESS;  
  39.       
  40.     // for performance issue  
  41.     if (ag != SrsRtmpJitterAlgorithmFULL) {  
  42.         // all jitter correct features is disabled, ignore.  
  43.         if (ag == SrsRtmpJitterAlgorithmOFF) {  
  44.             return ret;  
  45.         }  
  46.       
  47.         // start at zero, but donot ensure monotonically increasing.  
  48.         if (ag == SrsRtmpJitterAlgorithmZERO) {  
  49.             // for the first time, last_pkt_correct_time is -1.  
  50.             if (last_pkt_correct_time == -1) {  
  51.                 last_pkt_correct_time = msg->timestamp;  
  52.             }  
  53.             msg->timestamp -= last_pkt_correct_time;  
  54.             return ret;  
  55.         }  
  56.           
  57.         // other algorithm, ignore.  
  58.         return ret;  
  59.     }  
  60.       
  61.     // full jitter algorithm, do jitter correct.  
  62.     // set to 0 for metadata.  
  63.     if (!msg->is_av()) {  
  64.         msg->timestamp = 0;  
  65.         return ret;  
  66.     }  
  67.       
  68.     /** 
  69.     * we use a very simple time jitter detect/correct algorithm: 
  70.     * 1. delta: ensure the delta is positive and valid, 
  71.     *     we set the delta to DEFAULT_FRAME_TIME_MS, 
  72.     *     if the delta of time is nagative or greater than CONST_MAX_JITTER_MS. 
  73.     * 2. last_pkt_time: specifies the original packet time, 
  74.     *     is used to detect next jitter. 
  75.     * 3. last_pkt_correct_time: simply add the positive delta,  
  76.     *     and enforce the time monotonically. 
  77.     */  
  78.     int64_t time = msg->timestamp;  
  79.     int64_t delta = time - last_pkt_time;  
  80.   
  81.     // if jitter detected, reset the delta.  
  82.     if (delta < CONST_MAX_JITTER_MS_NEG || delta > CONST_MAX_JITTER_MS) {  
  83.         // use default 10ms to notice the problem of stream.  
  84.         // @see https://github.com/ossrs/srs/issues/425  
  85.         delta = DEFAULT_FRAME_TIME_MS;  
  86.           
  87.         srs_info("jitter detected, last_pts=%"PRId64", pts=%"PRId64", diff=%"PRId64", last_time=%"PRId64", time=%"PRId64", diff=%"PRId64"",  
  88.             last_pkt_time, time, time - last_pkt_time, last_pkt_correct_time, last_pkt_correct_time + delta, delta);  
  89.     } else {  
  90.         srs_verbose("timestamp no jitter. time=%"PRId64", last_pkt=%"PRId64", correct_to=%"PRId64"",   
  91.             time, last_pkt_time, last_pkt_correct_time + delta);  
  92.     }  
  93.       
  94.     last_pkt_correct_time = srs_max(0, last_pkt_correct_time + delta);  
  95.       
  96.     msg->timestamp = last_pkt_correct_time;  
  97.     last_pkt_time = time;  
  98.       
  99.     return ret;  
  100. }  
  101.   
  102. int SrsRtmpJitter::get_time()  
  103. {  
  104.     return (int)last_pkt_correct_time;  
  105. }