winlin

fix bug #67: fix pithy print bug, stage must has a age

@@ -34,69 +34,83 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -34,69 +34,83 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 34
35 #define SRS_STAGE_DEFAULT_INTERVAL_MS 1200 35 #define SRS_STAGE_DEFAULT_INTERVAL_MS 1200
36 36
37 -struct SrsStageInfo : public ISrsReloadHandler 37 +SrsStageInfo::SrsStageInfo(int _stage_id)
38 { 38 {
39 - int stage_id;  
40 - int pithy_print_time_ms;  
41 - int nb_clients; 39 + stage_id = _stage_id;
  40 + nb_clients = 0;
  41 + _age = printed_age = 0;
42 42
43 - SrsStageInfo(int _stage_id)  
44 - {  
45 - stage_id = _stage_id;  
46 - nb_clients = 0;  
47 -  
48 - update_print_time();  
49 -  
50 - _srs_config->subscribe(this);  
51 - }  
52 - virtual ~SrsStageInfo()  
53 - {  
54 - _srs_config->unsubscribe(this);  
55 - }  
56 - void update_print_time()  
57 - {  
58 - switch (stage_id) {  
59 - case SRS_STAGE_PLAY_USER: {  
60 - pithy_print_time_ms = _srs_config->get_pithy_print_play();  
61 - break;  
62 - }  
63 - case SRS_STAGE_PUBLISH_USER: {  
64 - pithy_print_time_ms = _srs_config->get_pithy_print_publish();  
65 - break;  
66 - }  
67 - case SRS_STAGE_FORWARDER: {  
68 - pithy_print_time_ms = _srs_config->get_pithy_print_forwarder();  
69 - break;  
70 - }  
71 - case SRS_STAGE_ENCODER: {  
72 - pithy_print_time_ms = _srs_config->get_pithy_print_encoder();  
73 - break;  
74 - }  
75 - case SRS_STAGE_INGESTER: {  
76 - pithy_print_time_ms = _srs_config->get_pithy_print_ingester();  
77 - break;  
78 - }  
79 - case SRS_STAGE_EDGE: {  
80 - pithy_print_time_ms = _srs_config->get_pithy_print_edge();  
81 - break;  
82 - }  
83 - case SRS_STAGE_HLS: {  
84 - pithy_print_time_ms = _srs_config->get_pithy_print_hls();  
85 - break;  
86 - }  
87 - default: {  
88 - pithy_print_time_ms = SRS_STAGE_DEFAULT_INTERVAL_MS;  
89 - break;  
90 - } 43 + update_print_time();
  44 +
  45 + _srs_config->subscribe(this);
  46 +}
  47 +
  48 +SrsStageInfo::~SrsStageInfo()
  49 +{
  50 + _srs_config->unsubscribe(this);
  51 +}
  52 +
  53 +void SrsStageInfo::update_print_time()
  54 +{
  55 + switch (stage_id) {
  56 + case SRS_STAGE_PLAY_USER: {
  57 + pithy_print_time_ms = _srs_config->get_pithy_print_play();
  58 + break;
  59 + }
  60 + case SRS_STAGE_PUBLISH_USER: {
  61 + pithy_print_time_ms = _srs_config->get_pithy_print_publish();
  62 + break;
  63 + }
  64 + case SRS_STAGE_FORWARDER: {
  65 + pithy_print_time_ms = _srs_config->get_pithy_print_forwarder();
  66 + break;
  67 + }
  68 + case SRS_STAGE_ENCODER: {
  69 + pithy_print_time_ms = _srs_config->get_pithy_print_encoder();
  70 + break;
  71 + }
  72 + case SRS_STAGE_INGESTER: {
  73 + pithy_print_time_ms = _srs_config->get_pithy_print_ingester();
  74 + break;
  75 + }
  76 + case SRS_STAGE_EDGE: {
  77 + pithy_print_time_ms = _srs_config->get_pithy_print_edge();
  78 + break;
  79 + }
  80 + case SRS_STAGE_HLS: {
  81 + pithy_print_time_ms = _srs_config->get_pithy_print_hls();
  82 + break;
  83 + }
  84 + default: {
  85 + pithy_print_time_ms = SRS_STAGE_DEFAULT_INTERVAL_MS;
  86 + break;
91 } 87 }
92 } 88 }
93 -public:  
94 - virtual int on_reload_pithy_print()  
95 - {  
96 - update_print_time();  
97 - return ERROR_SUCCESS; 89 +}
  90 +
  91 +void SrsStageInfo::elapse(int64_t diff)
  92 +{
  93 + _age += diff;
  94 +}
  95 +
  96 +bool SrsStageInfo::can_print()
  97 +{
  98 + int64_t can_print_age = nb_clients * pithy_print_time_ms;
  99 +
  100 + bool can_print = _age >= can_print_age;
  101 + if (can_print) {
  102 + _age = 0;
98 } 103 }
99 -}; 104 +
  105 + return can_print;
  106 +}
  107 +
  108 +int SrsStageInfo::on_reload_pithy_print()
  109 +{
  110 + update_print_time();
  111 + return ERROR_SUCCESS;
  112 +}
  113 +
100 static std::map<int, SrsStageInfo*> _srs_stages; 114 static std::map<int, SrsStageInfo*> _srs_stages;
101 115
102 SrsPithyPrint::SrsPithyPrint(int _stage_id) 116 SrsPithyPrint::SrsPithyPrint(int _stage_id)
@@ -104,7 +118,7 @@ SrsPithyPrint::SrsPithyPrint(int _stage_id) @@ -104,7 +118,7 @@ SrsPithyPrint::SrsPithyPrint(int _stage_id)
104 stage_id = _stage_id; 118 stage_id = _stage_id;
105 client_id = enter_stage(); 119 client_id = enter_stage();
106 previous_tick = srs_get_system_time_ms(); 120 previous_tick = srs_get_system_time_ms();
107 - printed_age = _age = 0; 121 + _age = 0;
108 } 122 }
109 123
110 SrsPithyPrint::~SrsPithyPrint() 124 SrsPithyPrint::~SrsPithyPrint()
@@ -146,9 +160,14 @@ void SrsPithyPrint::leave_stage() @@ -146,9 +160,14 @@ void SrsPithyPrint::leave_stage()
146 160
147 void SrsPithyPrint::elapse() 161 void SrsPithyPrint::elapse()
148 { 162 {
  163 + SrsStageInfo* stage = _srs_stages[stage_id];
  164 + srs_assert(stage != NULL);
  165 +
149 int64_t diff = srs_get_system_time_ms() - previous_tick; 166 int64_t diff = srs_get_system_time_ms() - previous_tick;
  167 + diff = srs_max(0, diff);
150 168
151 - _age += srs_max(0, diff); 169 + stage->elapse(diff);
  170 + _age += diff;
152 previous_tick = srs_get_system_time_ms(); 171 previous_tick = srs_get_system_time_ms();
153 } 172 }
154 173
@@ -157,15 +176,7 @@ bool SrsPithyPrint::can_print() @@ -157,15 +176,7 @@ bool SrsPithyPrint::can_print()
157 SrsStageInfo* stage = _srs_stages[stage_id]; 176 SrsStageInfo* stage = _srs_stages[stage_id];
158 srs_assert(stage != NULL); 177 srs_assert(stage != NULL);
159 178
160 - int64_t alive_age = _age - printed_age;  
161 - int64_t can_print_age = stage->nb_clients * stage->pithy_print_time_ms;  
162 -  
163 - bool can_print = alive_age >= can_print_age;  
164 - if (can_print) {  
165 - printed_age = _age;  
166 - }  
167 -  
168 - return can_print; 179 + return stage->can_print();
169 } 180 }
170 181
171 int64_t SrsPithyPrint::age() 182 int64_t SrsPithyPrint::age()
@@ -30,6 +30,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -30,6 +30,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 30
31 #include <srs_core.hpp> 31 #include <srs_core.hpp>
32 32
  33 +#include <srs_app_reload.hpp>
  34 +
33 // the pithy stage for all play clients. 35 // the pithy stage for all play clients.
34 #define SRS_STAGE_PLAY_USER 1 36 #define SRS_STAGE_PLAY_USER 1
35 // the pithy stage for all publish clients. 37 // the pithy stage for all publish clients.
@@ -45,6 +47,26 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -45,6 +47,26 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 // the pithy stage for all edge. 47 // the pithy stage for all edge.
46 #define SRS_STAGE_EDGE 7 48 #define SRS_STAGE_EDGE 7
47 49
  50 +class SrsStageInfo : public ISrsReloadHandler
  51 +{
  52 +public:
  53 + int stage_id;
  54 + int pithy_print_time_ms;
  55 + int nb_clients;
  56 +public:
  57 + int64_t _age;
  58 + int64_t printed_age;
  59 +public:
  60 + SrsStageInfo(int _stage_id);
  61 + virtual ~SrsStageInfo();
  62 + virtual void update_print_time();
  63 +public:
  64 + virtual void elapse(int64_t diff);
  65 + virtual bool can_print();
  66 +public:
  67 + virtual int on_reload_pithy_print();
  68 +};
  69 +
48 /** 70 /**
49 * the stage is used for a collection of object to do print, 71 * the stage is used for a collection of object to do print,
50 * the print time in a stage is constant and not changed. 72 * the print time in a stage is constant and not changed.
@@ -58,7 +80,6 @@ private: @@ -58,7 +80,6 @@ private:
58 int stage_id; 80 int stage_id;
59 // in ms. 81 // in ms.
60 int64_t _age; 82 int64_t _age;
61 - int64_t printed_age;  
62 int64_t previous_tick; 83 int64_t previous_tick;
63 public: 84 public:
64 /** 85 /**
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // current release version 31 // current release version
32 #define VERSION_MAJOR "0" 32 #define VERSION_MAJOR "0"
33 #define VERSION_MINOR "9" 33 #define VERSION_MINOR "9"
34 -#define VERSION_REVISION "97" 34 +#define VERSION_REVISION "98"
35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION 35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
36 // server info. 36 // server info.
37 #define RTMP_SIG_SRS_KEY "srs" 37 #define RTMP_SIG_SRS_KEY "srs"