2013-11-29 14:21:31 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2014-12-31 12:32:09 +00:00
|
|
|
Copyright (c) 2013-2015 winlin
|
2013-11-29 14:21:31 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-02 13:49:09 +00:00
|
|
|
#include <srs_app_pithy_print.hpp>
|
2013-11-29 14:21:31 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <map>
|
|
|
|
|
2014-03-01 02:42:55 +00:00
|
|
|
#include <srs_kernel_log.hpp>
|
2014-03-02 13:49:09 +00:00
|
|
|
#include <srs_app_config.hpp>
|
2014-03-01 02:30:16 +00:00
|
|
|
#include <srs_kernel_error.hpp>
|
2014-04-26 13:41:18 +00:00
|
|
|
#include <srs_kernel_utility.hpp>
|
2013-11-29 14:21:31 +00:00
|
|
|
|
2014-05-14 06:34:28 +00:00
|
|
|
SrsStageInfo::SrsStageInfo(int _stage_id)
|
2013-11-29 14:21:31 +00:00
|
|
|
{
|
2014-05-14 06:34:28 +00:00
|
|
|
stage_id = _stage_id;
|
|
|
|
nb_clients = 0;
|
2014-09-26 09:47:43 +00:00
|
|
|
age = 0;
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2014-05-14 06:34:28 +00:00
|
|
|
update_print_time();
|
|
|
|
|
|
|
|
_srs_config->subscribe(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsStageInfo::~SrsStageInfo()
|
|
|
|
{
|
|
|
|
_srs_config->unsubscribe(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SrsStageInfo::update_print_time()
|
|
|
|
{
|
2015-02-19 10:56:21 +00:00
|
|
|
pithy_print_time_ms = _srs_config->get_pithy_print_ms();
|
2014-05-14 06:34:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SrsStageInfo::elapse(int64_t diff)
|
|
|
|
{
|
2014-05-14 06:35:29 +00:00
|
|
|
age += diff;
|
2014-05-14 06:34:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SrsStageInfo::can_print()
|
|
|
|
{
|
|
|
|
int64_t can_print_age = nb_clients * pithy_print_time_ms;
|
|
|
|
|
2014-05-14 06:35:29 +00:00
|
|
|
bool can_print = age >= can_print_age;
|
2014-05-14 06:34:28 +00:00
|
|
|
if (can_print) {
|
2014-05-14 06:35:29 +00:00
|
|
|
age = 0;
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
2014-05-14 06:34:28 +00:00
|
|
|
|
|
|
|
return can_print;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsStageInfo::on_reload_pithy_print()
|
|
|
|
{
|
|
|
|
update_print_time();
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2013-11-29 14:21:31 +00:00
|
|
|
static std::map<int, SrsStageInfo*> _srs_stages;
|
|
|
|
|
|
|
|
SrsPithyPrint::SrsPithyPrint(int _stage_id)
|
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
stage_id = _stage_id;
|
|
|
|
client_id = enter_stage();
|
2014-04-26 13:41:18 +00:00
|
|
|
previous_tick = srs_get_system_time_ms();
|
2014-05-14 06:34:28 +00:00
|
|
|
_age = 0;
|
2013-11-29 14:21:31 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 10:56:21 +00:00
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
// pithy-print consts values
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
// the pithy stage for all play clients.
|
|
|
|
#define SRS_CONSTS_STAGE_PLAY_USER 1
|
|
|
|
// the pithy stage for all publish clients.
|
|
|
|
#define SRS_CONSTS_STAGE_PUBLISH_USER 2
|
|
|
|
// the pithy stage for all forward clients.
|
|
|
|
#define SRS_CONSTS_STAGE_FORWARDER 3
|
|
|
|
// the pithy stage for all encoders.
|
|
|
|
#define SRS_CONSTS_STAGE_ENCODER 4
|
|
|
|
// the pithy stage for all hls.
|
|
|
|
#define SRS_CONSTS_STAGE_HLS 5
|
|
|
|
// the pithy stage for all ingesters.
|
|
|
|
#define SRS_CONSTS_STAGE_INGESTER 6
|
|
|
|
// the pithy stage for all edge.
|
|
|
|
#define SRS_CONSTS_STAGE_EDGE 7
|
|
|
|
// the pithy stage for all stream caster.
|
|
|
|
#define SRS_CONSTS_STAGE_CASTER 8
|
|
|
|
// the pithy stage for all http stream.
|
|
|
|
#define SRS_CONSTS_STAGE_HTTP_STREAM 9
|
|
|
|
// the pithy stage for all http stream cache.
|
|
|
|
#define SRS_CONSTS_STAGE_HTTP_STREAM_CACHE 10
|
|
|
|
|
|
|
|
SrsPithyPrint* SrsPithyPrint::create_rtmp_play()
|
|
|
|
{
|
|
|
|
return new SrsPithyPrint(SRS_CONSTS_STAGE_PLAY_USER);
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsPithyPrint* SrsPithyPrint::create_rtmp_publish()
|
|
|
|
{
|
|
|
|
return new SrsPithyPrint(SRS_CONSTS_STAGE_PUBLISH_USER);
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsPithyPrint* SrsPithyPrint::create_hls()
|
|
|
|
{
|
|
|
|
return new SrsPithyPrint(SRS_CONSTS_STAGE_HLS);
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsPithyPrint* SrsPithyPrint::create_forwarder()
|
|
|
|
{
|
|
|
|
return new SrsPithyPrint(SRS_CONSTS_STAGE_FORWARDER);
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsPithyPrint* SrsPithyPrint::create_encoder()
|
|
|
|
{
|
|
|
|
return new SrsPithyPrint(SRS_CONSTS_STAGE_ENCODER);
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsPithyPrint* SrsPithyPrint::create_ingester()
|
|
|
|
{
|
|
|
|
return new SrsPithyPrint(SRS_CONSTS_STAGE_INGESTER);
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsPithyPrint* SrsPithyPrint::create_edge()
|
|
|
|
{
|
|
|
|
return new SrsPithyPrint(SRS_CONSTS_STAGE_EDGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsPithyPrint* SrsPithyPrint::create_caster()
|
|
|
|
{
|
|
|
|
return new SrsPithyPrint(SRS_CONSTS_STAGE_CASTER);
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsPithyPrint* SrsPithyPrint::create_http_stream()
|
|
|
|
{
|
|
|
|
return new SrsPithyPrint(SRS_CONSTS_STAGE_HTTP_STREAM);
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsPithyPrint* SrsPithyPrint::create_http_stream_cache()
|
|
|
|
{
|
|
|
|
return new SrsPithyPrint(SRS_CONSTS_STAGE_HTTP_STREAM_CACHE);
|
|
|
|
}
|
|
|
|
|
2013-11-29 14:21:31 +00:00
|
|
|
SrsPithyPrint::~SrsPithyPrint()
|
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
leave_stage();
|
2013-11-29 14:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int SrsPithyPrint::enter_stage()
|
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
SrsStageInfo* stage = NULL;
|
|
|
|
|
|
|
|
std::map<int, SrsStageInfo*>::iterator it = _srs_stages.find(stage_id);
|
|
|
|
if (it == _srs_stages.end()) {
|
2014-04-07 06:20:03 +00:00
|
|
|
stage = new SrsStageInfo(stage_id);
|
|
|
|
_srs_stages[stage_id] = stage;
|
2014-03-18 03:32:58 +00:00
|
|
|
} else {
|
|
|
|
stage = it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_assert(stage != NULL);
|
|
|
|
client_id = stage->nb_clients++;
|
|
|
|
|
|
|
|
srs_verbose("enter stage, stage_id=%d, client_id=%d, nb_clients=%d, time_ms=%d",
|
|
|
|
stage->stage_id, client_id, stage->nb_clients, stage->pithy_print_time_ms);
|
|
|
|
|
|
|
|
return client_id;
|
2013-11-29 14:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SrsPithyPrint::leave_stage()
|
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
SrsStageInfo* stage = _srs_stages[stage_id];
|
|
|
|
srs_assert(stage != NULL);
|
|
|
|
|
|
|
|
stage->nb_clients--;
|
2013-11-29 14:21:31 +00:00
|
|
|
|
2014-03-18 03:32:58 +00:00
|
|
|
srs_verbose("leave stage, stage_id=%d, client_id=%d, nb_clients=%d, time_ms=%d",
|
|
|
|
stage->stage_id, client_id, stage->nb_clients, stage->pithy_print_time_ms);
|
2013-11-29 14:21:31 +00:00
|
|
|
}
|
|
|
|
|
2014-04-26 13:41:18 +00:00
|
|
|
void SrsPithyPrint::elapse()
|
2013-11-29 14:21:31 +00:00
|
|
|
{
|
2014-05-14 06:34:28 +00:00
|
|
|
SrsStageInfo* stage = _srs_stages[stage_id];
|
|
|
|
srs_assert(stage != NULL);
|
|
|
|
|
2014-04-26 13:41:18 +00:00
|
|
|
int64_t diff = srs_get_system_time_ms() - previous_tick;
|
2014-05-14 06:34:28 +00:00
|
|
|
diff = srs_max(0, diff);
|
2014-04-26 13:41:18 +00:00
|
|
|
|
2014-05-14 06:34:28 +00:00
|
|
|
stage->elapse(diff);
|
|
|
|
_age += diff;
|
2014-04-26 13:41:18 +00:00
|
|
|
previous_tick = srs_get_system_time_ms();
|
2013-11-29 14:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SrsPithyPrint::can_print()
|
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
SrsStageInfo* stage = _srs_stages[stage_id];
|
|
|
|
srs_assert(stage != NULL);
|
|
|
|
|
2014-05-14 06:34:28 +00:00
|
|
|
return stage->can_print();
|
2013-11-29 14:21:31 +00:00
|
|
|
}
|
|
|
|
|
2014-04-26 13:41:18 +00:00
|
|
|
int64_t SrsPithyPrint::age()
|
2013-11-29 14:21:31 +00:00
|
|
|
{
|
2014-04-26 13:41:18 +00:00
|
|
|
return _age;
|
2013-11-29 14:21:31 +00:00
|
|
|
}
|
|
|
|
|
2014-08-02 14:18:39 +00:00
|
|
|
|