Blame view

trunk/src/app/srs_app_pithy_print.cpp 5.6 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
winlin authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

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.
*/
24
#include <srs_app_pithy_print.hpp>
winlin authored
25 26 27 28

#include <stdlib.h>
#include <map>
29
#include <srs_kernel_log.hpp>
30
#include <srs_app_config.hpp>
31
#include <srs_kernel_error.hpp>
32
#include <srs_kernel_utility.hpp>
winlin authored
33
34
SrsStageInfo::SrsStageInfo(int _stage_id)
winlin authored
35
{
36 37
    stage_id = _stage_id;
    nb_clients = 0;
38
    age = 0;
39
    
40 41 42 43 44 45 46 47 48 49 50 51
    update_print_time();
    
    _srs_config->subscribe(this);
}

SrsStageInfo::~SrsStageInfo()
{
    _srs_config->unsubscribe(this);
}

void SrsStageInfo::update_print_time()
{
52
    pithy_print_time_ms = _srs_config->get_pithy_print_ms();
53 54 55 56
}

void SrsStageInfo::elapse(int64_t diff)
{
57
    age += diff;
58 59 60 61 62 63
}

bool SrsStageInfo::can_print()
{
    int64_t can_print_age = nb_clients * pithy_print_time_ms;
    
64
    bool can_print = age >= can_print_age;
65
    if (can_print) {
66
        age = 0;
67
    }
68 69 70 71 72 73 74 75 76 77
    
    return can_print;
}

int SrsStageInfo::on_reload_pithy_print()
{
    update_print_time();
    return ERROR_SUCCESS;
}
    
winlin authored
78 79 80 81
static std::map<int, SrsStageInfo*> _srs_stages;

SrsPithyPrint::SrsPithyPrint(int _stage_id)
{
82 83
    stage_id = _stage_id;
    client_id = enter_stage();
84
    previous_tick = srs_get_system_time_ms();
85
    _age = 0;
winlin authored
86 87
}
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
///////////////////////////////////////////////////////////
// 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);
}
winlin authored
162 163
SrsPithyPrint::~SrsPithyPrint()
{
164
    leave_stage();
winlin authored
165 166 167 168
}

int SrsPithyPrint::enter_stage()
{
169 170 171 172
    SrsStageInfo* stage = NULL;
    
    std::map<int, SrsStageInfo*>::iterator it = _srs_stages.find(stage_id);
    if (it == _srs_stages.end()) {
173 174
        stage = new SrsStageInfo(stage_id);
        _srs_stages[stage_id] = stage;
175 176 177 178 179 180 181 182 183 184 185
    } 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;
winlin authored
186 187 188 189
}

void SrsPithyPrint::leave_stage()
{
190 191 192 193
    SrsStageInfo* stage = _srs_stages[stage_id];
    srs_assert(stage != NULL);
    
    stage->nb_clients--;
winlin authored
194
195 196
    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);
winlin authored
197 198
}
199
void SrsPithyPrint::elapse()
winlin authored
200
{
201 202 203
    SrsStageInfo* stage = _srs_stages[stage_id];
    srs_assert(stage != NULL);
    
204
    int64_t diff = srs_get_system_time_ms() - previous_tick;
205
    diff = srs_max(0, diff);
206
    
207 208
    stage->elapse(diff);
    _age += diff;
209
    previous_tick = srs_get_system_time_ms();
winlin authored
210 211 212 213
}

bool SrsPithyPrint::can_print()
{
214 215 216
    SrsStageInfo* stage = _srs_stages[stage_id];
    srs_assert(stage != NULL);
    
217
    return stage->can_print();
winlin authored
218 219
}
220
int64_t SrsPithyPrint::age()
winlin authored
221
{
222
    return _age;
winlin authored
223 224
}
225