1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-12 11:21:52 +00:00

time jitter detect and correct, very simple algorithm

This commit is contained in:
winlin 2013-10-23 21:31:22 +08:00
parent 2a346c2398
commit e31e3d601d
3 changed files with 34 additions and 0 deletions

View file

@ -72,5 +72,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// compare // compare
#define srs_min(a, b) ((a < b)? a : b) #define srs_min(a, b) ((a < b)? a : b)
#define srs_max(a, b) ((a < b)? b : a)
#endif #endif

View file

@ -30,6 +30,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_core_auto_free.hpp> #include <srs_core_auto_free.hpp>
#include <srs_core_amf0.hpp> #include <srs_core_amf0.hpp>
#define CONST_MAX_JITTER_MS 500
#define DEFAULT_FRAME_TIME_MS 10
std::map<std::string, SrsSource*> SrsSource::pool; std::map<std::string, SrsSource*> SrsSource::pool;
SrsSource* SrsSource::find(std::string stream_url) SrsSource* SrsSource::find(std::string stream_url)
@ -45,6 +48,7 @@ SrsSource* SrsSource::find(std::string stream_url)
SrsConsumer::SrsConsumer(SrsSource* _source) SrsConsumer::SrsConsumer(SrsSource* _source)
{ {
source = _source; source = _source;
last_pkt_correct_time = last_pkt_time = 0;
} }
SrsConsumer::~SrsConsumer() SrsConsumer::~SrsConsumer()
@ -62,7 +66,34 @@ SrsConsumer::~SrsConsumer()
int SrsConsumer::enqueue(SrsSharedPtrMessage* msg) int SrsConsumer::enqueue(SrsSharedPtrMessage* msg)
{ {
int ret = ERROR_SUCCESS; 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); msgs.push_back(msg);
return ret; return ret;
} }

View file

@ -45,6 +45,8 @@ class SrsSharedPtrMessage;
class SrsConsumer class SrsConsumer
{ {
private: private:
int32_t last_pkt_time;
int32_t last_pkt_correct_time;
SrsSource* source; SrsSource* source;
std::vector<SrsSharedPtrMessage*> msgs; std::vector<SrsSharedPtrMessage*> msgs;
public: public: