diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index 929dcfd9a..25effe6a7 100755 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -72,5 +72,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // compare #define srs_min(a, b) ((a < b)? a : b) +#define srs_max(a, b) ((a < b)? b : a) #endif \ No newline at end of file diff --git a/trunk/src/core/srs_core_source.cpp b/trunk/src/core/srs_core_source.cpp index f9d376a5a..638ce4c7b 100755 --- a/trunk/src/core/srs_core_source.cpp +++ b/trunk/src/core/srs_core_source.cpp @@ -30,6 +30,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include +#define CONST_MAX_JITTER_MS 500 +#define DEFAULT_FRAME_TIME_MS 10 + std::map SrsSource::pool; SrsSource* SrsSource::find(std::string stream_url) @@ -45,6 +48,7 @@ SrsSource* SrsSource::find(std::string stream_url) SrsConsumer::SrsConsumer(SrsSource* _source) { source = _source; + last_pkt_correct_time = last_pkt_time = 0; } SrsConsumer::~SrsConsumer() @@ -62,7 +66,34 @@ SrsConsumer::~SrsConsumer() int SrsConsumer::enqueue(SrsSharedPtrMessage* msg) { int ret = ERROR_SUCCESS; + + /** + * we use a very simple time jitter detect/correct algorithm, + * if the delta of time is nagative or greater than CONST_MAX_JITTER_MS, + * we enforce the delta to DEFAULT_FRAME_TIME_MS, + * and update the last_pkt_time which used to detect next jitter. + * the last_pkt_correct_time is enforce the time monotonically. + */ + int32_t time = msg->header.timestamp; + int32_t delta = time - last_pkt_time; + + // if jitter detected, reset the delta. + if (delta < 0 || delta > CONST_MAX_JITTER_MS) { + delta = DEFAULT_FRAME_TIME_MS; + + srs_info("jitter detected, delta=%d, last_pkt=%d, time=%d, correct_to=%d", + delta, last_pkt_time, time, last_pkt_correct_time + delta); + } else { + srs_verbose("timestamp no jitter. time=%d, last_pkt=%d, correct_to=%d", + time, last_pkt_time, last_pkt_correct_time + delta); + } + + last_pkt_correct_time = srs_max(0, last_pkt_correct_time + delta); + msg->header.timestamp = last_pkt_correct_time; + last_pkt_time = time; + msgs.push_back(msg); + return ret; } diff --git a/trunk/src/core/srs_core_source.hpp b/trunk/src/core/srs_core_source.hpp index 624c8ad80..49c797051 100755 --- a/trunk/src/core/srs_core_source.hpp +++ b/trunk/src/core/srs_core_source.hpp @@ -45,6 +45,8 @@ class SrsSharedPtrMessage; class SrsConsumer { private: + int32_t last_pkt_time; + int32_t last_pkt_correct_time; SrsSource* source; std::vector msgs; public: