winlin

time jitter detect and correct, very simple algorithm

@@ -72,5 +72,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -72,5 +72,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
72 72
73 // compare 73 // compare
74 #define srs_min(a, b) ((a < b)? a : b) 74 #define srs_min(a, b) ((a < b)? a : b)
  75 +#define srs_max(a, b) ((a < b)? b : a)
75 76
76 #endif 77 #endif
@@ -30,6 +30,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -30,6 +30,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #include <srs_core_auto_free.hpp> 30 #include <srs_core_auto_free.hpp>
31 #include <srs_core_amf0.hpp> 31 #include <srs_core_amf0.hpp>
32 32
  33 +#define CONST_MAX_JITTER_MS 500
  34 +#define DEFAULT_FRAME_TIME_MS 10
  35 +
33 std::map<std::string, SrsSource*> SrsSource::pool; 36 std::map<std::string, SrsSource*> SrsSource::pool;
34 37
35 SrsSource* SrsSource::find(std::string stream_url) 38 SrsSource* SrsSource::find(std::string stream_url)
@@ -45,6 +48,7 @@ SrsSource* SrsSource::find(std::string stream_url) @@ -45,6 +48,7 @@ SrsSource* SrsSource::find(std::string stream_url)
45 SrsConsumer::SrsConsumer(SrsSource* _source) 48 SrsConsumer::SrsConsumer(SrsSource* _source)
46 { 49 {
47 source = _source; 50 source = _source;
  51 + last_pkt_correct_time = last_pkt_time = 0;
48 } 52 }
49 53
50 SrsConsumer::~SrsConsumer() 54 SrsConsumer::~SrsConsumer()
@@ -62,7 +66,34 @@ SrsConsumer::~SrsConsumer() @@ -62,7 +66,34 @@ SrsConsumer::~SrsConsumer()
62 int SrsConsumer::enqueue(SrsSharedPtrMessage* msg) 66 int SrsConsumer::enqueue(SrsSharedPtrMessage* msg)
63 { 67 {
64 int ret = ERROR_SUCCESS; 68 int ret = ERROR_SUCCESS;
  69 +
  70 + /**
  71 + * we use a very simple time jitter detect/correct algorithm,
  72 + * if the delta of time is nagative or greater than CONST_MAX_JITTER_MS,
  73 + * we enforce the delta to DEFAULT_FRAME_TIME_MS,
  74 + * and update the last_pkt_time which used to detect next jitter.
  75 + * the last_pkt_correct_time is enforce the time monotonically.
  76 + */
  77 + int32_t time = msg->header.timestamp;
  78 + int32_t delta = time - last_pkt_time;
  79 +
  80 + // if jitter detected, reset the delta.
  81 + if (delta < 0 || delta > CONST_MAX_JITTER_MS) {
  82 + delta = DEFAULT_FRAME_TIME_MS;
  83 +
  84 + srs_info("jitter detected, delta=%d, last_pkt=%d, time=%d, correct_to=%d",
  85 + delta, last_pkt_time, time, last_pkt_correct_time + delta);
  86 + } else {
  87 + srs_verbose("timestamp no jitter. time=%d, last_pkt=%d, correct_to=%d",
  88 + time, last_pkt_time, last_pkt_correct_time + delta);
  89 + }
  90 +
  91 + last_pkt_correct_time = srs_max(0, last_pkt_correct_time + delta);
  92 + msg->header.timestamp = last_pkt_correct_time;
  93 + last_pkt_time = time;
  94 +
65 msgs.push_back(msg); 95 msgs.push_back(msg);
  96 +
66 return ret; 97 return ret;
67 } 98 }
68 99
@@ -45,6 +45,8 @@ class SrsSharedPtrMessage; @@ -45,6 +45,8 @@ class SrsSharedPtrMessage;
45 class SrsConsumer 45 class SrsConsumer
46 { 46 {
47 private: 47 private:
  48 + int32_t last_pkt_time;
  49 + int32_t last_pkt_correct_time;
48 SrsSource* source; 50 SrsSource* source;
49 std::vector<SrsSharedPtrMessage*> msgs; 51 std::vector<SrsSharedPtrMessage*> msgs;
50 public: 52 public: