winlin

fix #383, support mix_correct algorithm. 2.0.161.

... ... @@ -562,6 +562,7 @@ Supported operating systems and hardware:
### SRS 2.0 history
* v2.0, 2015-04-15, for [#383](https://github.com/winlinvip/simple-rtmp-server/issues/383), support mix_correct algorithm. 2.0.161.
* v2.0, 2015-04-13, for [#381](https://github.com/winlinvip/simple-rtmp-server/issues/381), support reap hls/ts by gop or not. 2.0.160.
* v2.0, 2015-04-10, enhanced on_hls_notify, support HTTP GET when reap ts.
* v2.0, 2015-04-10, refine the hls deviation for floor algorithm.
... ...
... ... @@ -1329,7 +1329,7 @@ vhost jitter.srs.com {
# about the stream monotonically increasing:
# 1. video timestamp is monotonically increasing,
# 2. audio timestamp is monotonically increasing,
# 3. video and audio timestamp is interleaved monotonically increasing.
# 3. video and audio timestamp is interleaved/mixed monotonically increasing.
# it's specified by RTMP specification, @see 3. Byte Order, Alignment, and Time Format
# however, some encoder cannot provides this feature, please set this to off to ignore time jitter.
# the time jitter algorithm:
... ... @@ -1338,8 +1338,8 @@ vhost jitter.srs.com {
# 3. off, disable the time jitter algorithm, like atc.
# default: full
time_jitter full;
# whether use the mix algorithm to correct the timestamp.
# if on, always ensure the timestamp of audio+video is monotonically increase.
# whether use the interleaved/mixed algorithm to correct the timestamp.
# if on, always ensure the timestamp of audio+video is interleaved/mixed monotonically increase.
# if off, use time_jitter to correct the timestamp if required.
# default: off
mix_correct off;
... ...
... ... @@ -807,6 +807,17 @@ int SrsConfig::reload_vhost(SrsConfDirective* old_root)
}
srs_trace("vhost %s reload time_jitter success.", vhost.c_str());
}
// mix_correct, only one per vhost
if (!srs_directive_equals(new_vhost->get("mix_correct"), old_vhost->get("mix_correct"))) {
for (it = subscribes.begin(); it != subscribes.end(); ++it) {
ISrsReloadHandler* subscribe = *it;
if ((ret = subscribe->on_reload_vhost_mix_correct(vhost)) != ERROR_SUCCESS) {
srs_error("vhost %s notify subscribes mix_correct failed. ret=%d", vhost.c_str(), ret);
return ret;
}
}
srs_trace("vhost %s reload mix_correct success.", vhost.c_str());
}
// forward, only one per vhost
if (!srs_directive_equals(new_vhost->get("forward"), old_vhost->get("forward"))) {
for (it = subscribes.begin(); it != subscribes.end(); ++it) {
... ... @@ -1419,7 +1430,7 @@ int SrsConfig::check_config()
&& n != "gop_cache" && n != "queue_length"
&& n != "refer" && n != "refer_publish" && n != "refer_play"
&& n != "forward" && n != "transcode" && n != "bandcheck"
&& n != "time_jitter"
&& n != "time_jitter" && n != "mix_correct"
&& n != "atc" && n != "atc_auto"
&& n != "debug_srs_upnode"
&& n != "mr" && n != "mw_latency" && n != "min_latency"
... ... @@ -2118,12 +2129,12 @@ bool SrsConfig::get_atc_auto(string vhost)
SrsConfDirective* conf = get_vhost(vhost);
if (!conf) {
return true;
return SRS_CONF_DEFAULT_ATC_AUTO;
}
conf = conf->get("atc_auto");
if (!conf || conf->arg0().empty()) {
return true;
return SRS_CONF_DEFAULT_ATC_AUTO;
}
return SRS_CONF_PERFER_TRUE(conf->arg0());
... ... @@ -2131,14 +2142,14 @@ bool SrsConfig::get_atc_auto(string vhost)
int SrsConfig::get_time_jitter(string vhost)
{
SrsConfDirective* dvr = get_vhost(vhost);
SrsConfDirective* conf = get_vhost(vhost);
std::string time_jitter = SRS_CONF_DEFAULT_TIME_JITTER;
if (dvr) {
SrsConfDirective* conf = dvr->get("time_jitter");
if (conf) {
conf = conf->get("time_jitter");
if (conf && !conf->arg0().empty()) {
time_jitter = conf->arg0();
}
}
... ... @@ -2146,6 +2157,22 @@ int SrsConfig::get_time_jitter(string vhost)
return _srs_time_jitter_string2int(time_jitter);
}
bool SrsConfig::get_mix_correct(string vhost)
{
SrsConfDirective* conf = get_vhost(vhost);
if (!conf) {
return SRS_CONF_DEFAULT_MIX_CORRECT;
}
conf = conf->get("mix_correct");
if (!conf || conf->arg0().empty()) {
return SRS_CONF_DEFAULT_MIX_CORRECT;
}
return SRS_CONF_PERFER_FALSE(conf->arg0());
}
double SrsConfig::get_queue_length(string vhost)
{
SrsConfDirective* conf = get_vhost(vhost);
... ...
... ... @@ -72,6 +72,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define SRS_CONF_DEFAULT_DVR_PLAN SRS_CONF_DEFAULT_DVR_PLAN_SESSION
#define SRS_CONF_DEFAULT_DVR_DURATION 30
#define SRS_CONF_DEFAULT_TIME_JITTER "full"
#define SRS_CONF_DEFAULT_ATC_AUTO true
#define SRS_CONF_DEFAULT_MIX_CORRECT false
// in seconds, the paused queue length.
#define SRS_CONF_DEFAULT_PAUSED_LENGTH 10
// the interval in seconds for bandwidth check
... ... @@ -532,6 +534,11 @@ public:
*/
virtual int get_time_jitter(std::string vhost);
/**
* whether use mix correct algorithm to ensure the timestamp
* monotonically increase.
*/
virtual bool get_mix_correct(std::string vhost);
/**
* get the cache queue length, in seconds.
* when exceed the queue length, drop packet util I frame.
* @remark, default 10.
... ...
... ... @@ -130,6 +130,11 @@ int ISrsReloadHandler::on_reload_vhost_time_jitter(string /*vhost*/)
return ERROR_SUCCESS;
}
int ISrsReloadHandler::on_reload_vhost_mix_correct(string /*vhost*/)
{
return ERROR_SUCCESS;
}
int ISrsReloadHandler::on_reload_vhost_forward(string /*vhost*/)
{
return ERROR_SUCCESS;
... ...
... ... @@ -63,6 +63,7 @@ public:
virtual int on_reload_vhost_gop_cache(std::string vhost);
virtual int on_reload_vhost_queue_length(std::string vhost);
virtual int on_reload_vhost_time_jitter(std::string vhost);
virtual int on_reload_vhost_mix_correct(std::string vhost);
virtual int on_reload_vhost_forward(std::string vhost);
virtual int on_reload_vhost_hls(std::string vhost);
virtual int on_reload_vhost_hds(std::string vhost);
... ...
... ... @@ -44,6 +44,7 @@ using namespace std;
#include <srs_rtmp_msg_array.hpp>
#include <srs_app_hds.hpp>
#include <srs_app_statistic.hpp>
#include <srs_core_autofree.hpp>
#define CONST_MAX_JITTER_MS 500
#define DEFAULT_FRAME_TIME_MS 40
... ... @@ -768,10 +769,62 @@ void SrsSource::destroy()
pool.clear();
}
SrsMixQueue::SrsMixQueue()
{
nb_videos = 0;
}
SrsMixQueue::~SrsMixQueue()
{
clear();
}
void SrsMixQueue::clear()
{
std::multimap<int64_t, SrsSharedPtrMessage*>::iterator it;
for (it = msgs.begin(); it != msgs.end(); ++it) {
SrsSharedPtrMessage* msg = it->second;
srs_freep(msg);
}
msgs.clear();
nb_videos = 0;
}
void SrsMixQueue::push(SrsSharedPtrMessage* msg)
{
msgs.insert(std::make_pair(msg->timestamp, msg));
if (msg->is_video()) {
nb_videos++;
}
}
SrsSharedPtrMessage* SrsMixQueue::pop()
{
// always keep 2+ videos
if (nb_videos < 2) {
return NULL;
}
// pop the first msg.
std::multimap<int64_t, SrsSharedPtrMessage*>::iterator it = msgs.begin();
SrsSharedPtrMessage* msg = it->second;
msgs.erase(it);
if (msg->is_video()) {
nb_videos--;
}
return msg;
}
SrsSource::SrsSource()
{
_req = NULL;
jitter_algorithm = SrsRtmpJitterAlgorithmOFF;
mix_correct = false;
mix_queue = new SrsMixQueue();
#ifdef SRS_AUTO_HLS
hls = new SrsHls();
... ... @@ -818,6 +871,7 @@ SrsSource::~SrsSource()
forwarders.clear();
}
srs_freep(mix_queue);
srs_freep(cache_metadata);
srs_freep(cache_sh_video);
srs_freep(cache_sh_audio);
... ... @@ -878,6 +932,7 @@ int SrsSource::initialize(SrsRequest* r, ISrsSourceHandler* h, ISrsHlsHandler* h
publish_edge->set_queue_size(queue_size);
jitter_algorithm = (SrsRtmpJitterAlgorithm)_srs_config->get_time_jitter(_req->vhost);
mix_correct = _srs_config->get_mix_correct(_req->vhost);
return ret;
}
... ... @@ -973,6 +1028,25 @@ int SrsSource::on_reload_vhost_time_jitter(string vhost)
return ret;
}
int SrsSource::on_reload_vhost_mix_correct(string vhost)
{
int ret = ERROR_SUCCESS;
if (_req->vhost != vhost) {
return ret;
}
bool v = _srs_config->get_mix_correct(_req->vhost);
// when changed, clear the mix queue.
if (v != mix_correct) {
mix_queue->clear();
}
mix_correct = v;
return ret;
}
int SrsSource::on_reload_vhost_forward(string vhost)
{
int ret = ERROR_SUCCESS;
... ... @@ -1330,17 +1404,21 @@ int SrsSource::on_audio(SrsCommonMessage* shared_audio)
srs_error("initialize the audio failed. ret=%d", ret);
return ret;
}
srs_verbose("initialize shared ptr audio success.");
srs_warn("Audio dts=%"PRId64", size=%d", msg.timestamp, msg.size);
srs_info("Audio dts=%"PRId64", size=%d", msg.timestamp, msg.size);
if (!mix_correct) {
return on_audio_imp(&msg);
}
return do_mix_correct(&msg);
}
int SrsSource::on_audio_imp(SrsSharedPtrMessage* msg)
{
int ret = ERROR_SUCCESS;
srs_info("Audio dts=%"PRId64", size=%d", msg->timestamp, msg->size);
#ifdef SRS_AUTO_HLS
if ((ret = hls->on_audio(msg)) != ERROR_SUCCESS) {
// apply the error strategy for hls.
... ... @@ -1490,17 +1568,21 @@ int SrsSource::on_video(SrsCommonMessage* shared_video)
srs_error("initialize the video failed. ret=%d", ret);
return ret;
}
srs_verbose("initialize shared ptr video success.");
srs_warn("Video dts=%"PRId64", size=%d", msg.timestamp, msg.size);
srs_info("Video dts=%"PRId64", size=%d", msg.timestamp, msg.size);
if (!mix_correct) {
return on_video_imp(&msg);
}
return do_mix_correct(&msg);
}
int SrsSource::on_video_imp(SrsSharedPtrMessage* msg)
{
int ret = ERROR_SUCCESS;
srs_info("Video dts=%"PRId64", size=%d", msg->timestamp, msg->size);
#ifdef SRS_AUTO_HLS
if ((ret = hls->on_video(msg)) != ERROR_SUCCESS) {
// apply the error strategy for hls.
... ... @@ -1626,6 +1708,29 @@ int SrsSource::on_video_imp(SrsSharedPtrMessage* msg)
return ret;
}
int SrsSource::do_mix_correct(SrsSharedPtrMessage* msg)
{
int ret = ERROR_SUCCESS;
// insert msg to the queue.
mix_queue->push(msg->copy());
// fetch someone from mix queue.
SrsSharedPtrMessage* m = mix_queue->pop();
if (!m) {
return ret;
}
SrsAutoFree(SrsSharedPtrMessage, m);
// consume the monotonically increase message.
if (m->is_audio()) {
return on_audio_imp(m);
}
srs_assert(m->is_video());
return on_video_imp(m);
}
int SrsSource::on_aggregate(SrsCommonMessage* msg)
{
int ret = ERROR_SUCCESS;
... ... @@ -1766,6 +1871,9 @@ int SrsSource::on_publish()
// save its id to srouce id.
on_source_id_changed(_srs_context->get_id());
// reset the mix queue.
mix_queue->clear();
// create forwarders
if ((ret = create_forwarders()) != ERROR_SUCCESS) {
srs_error("create forwarders failed. ret=%d", ret);
... ...
... ... @@ -369,6 +369,23 @@ public:
};
/**
* the mix queue to correct the timestamp for mix_correct algorithm.
*/
class SrsMixQueue
{
private:
u_int32_t nb_videos;
std::multimap<int64_t, SrsSharedPtrMessage*> msgs;
public:
SrsMixQueue();
virtual ~SrsMixQueue();
public:
virtual void clear();
virtual void push(SrsSharedPtrMessage* msg);
virtual SrsSharedPtrMessage* pop();
};
/**
* live streaming source.
*/
class SrsSource : public ISrsReloadHandler
... ... @@ -407,6 +424,9 @@ private:
std::vector<SrsConsumer*> consumers;
// the time jitter algorithm for vhost.
SrsRtmpJitterAlgorithm jitter_algorithm;
// whether use interlaced/mixed algorithm to correct timestamp.
bool mix_correct;
SrsMixQueue* mix_queue;
// hls handler.
#ifdef SRS_AUTO_HLS
SrsHls* hls;
... ... @@ -474,6 +494,7 @@ public:
virtual int on_reload_vhost_gop_cache(std::string vhost);
virtual int on_reload_vhost_queue_length(std::string vhost);
virtual int on_reload_vhost_time_jitter(std::string vhost);
virtual int on_reload_vhost_mix_correct(std::string vhost);
virtual int on_reload_vhost_forward(std::string vhost);
virtual int on_reload_vhost_hls(std::string vhost);
virtual int on_reload_vhost_hds(std::string vhost);
... ... @@ -503,6 +524,8 @@ public:
virtual int on_video(SrsCommonMessage* video);
private:
virtual int on_video_imp(SrsSharedPtrMessage* video);
private:
virtual int do_mix_correct(SrsSharedPtrMessage* msg);
public:
virtual int on_aggregate(SrsCommonMessage* msg);
/**
... ...
... ... @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// current release version
#define VERSION_MAJOR 2
#define VERSION_MINOR 0
#define VERSION_REVISION 160
#define VERSION_REVISION 161
// server info.
#define RTMP_SIG_SRS_KEY "SRS"
... ...