Blame view

trunk/src/app/srs_app_pithy_print.cpp 4.8 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2014 winlin
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 31
#include <srs_app_config.hpp>
#include <srs_app_reload.hpp>
32
#include <srs_kernel_error.hpp>
33
#include <srs_kernel_utility.hpp>
winlin authored
34 35 36

#define SRS_STAGE_DEFAULT_INTERVAL_MS 1200
37
struct SrsStageInfo : public ISrsReloadHandler
winlin authored
38
{
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    int stage_id;
    int pithy_print_time_ms;
    int nb_clients;
    
    SrsStageInfo(int _stage_id)
    {
        stage_id = _stage_id;
        nb_clients = 0;
        
        update_print_time();
        
        _srs_config->subscribe(this);
    }
    virtual ~SrsStageInfo()
    {
        _srs_config->unsubscribe(this);
    }
    void update_print_time()
    {
        switch (stage_id) {
            case SRS_STAGE_PLAY_USER: {
                pithy_print_time_ms = _srs_config->get_pithy_print_play();
                break;
            }
            case SRS_STAGE_PUBLISH_USER: {
                pithy_print_time_ms = _srs_config->get_pithy_print_publish();
                break;
            }
            case SRS_STAGE_FORWARDER: {
                pithy_print_time_ms = _srs_config->get_pithy_print_forwarder();
                break;
            }
            case SRS_STAGE_ENCODER: {
                pithy_print_time_ms = _srs_config->get_pithy_print_encoder();
                break;
            }
75 76 77 78
            case SRS_STAGE_INGESTER: {
                pithy_print_time_ms = _srs_config->get_pithy_print_ingester();
                break;
            }
79 80 81 82
            case SRS_STAGE_EDGE: {
                pithy_print_time_ms = _srs_config->get_pithy_print_edge();
                break;
            }
83 84 85 86 87 88 89 90 91 92
            case SRS_STAGE_HLS: {
                pithy_print_time_ms = _srs_config->get_pithy_print_hls();
                break;
            }
            default: {
                pithy_print_time_ms = SRS_STAGE_DEFAULT_INTERVAL_MS;
                break;
            }
        }
    }
winlin authored
93
public:
94 95 96 97 98
    virtual int on_reload_pithy_print()
    {
        update_print_time();
        return ERROR_SUCCESS;
    }
winlin authored
99 100 101 102 103
};
static std::map<int, SrsStageInfo*> _srs_stages;

SrsPithyPrint::SrsPithyPrint(int _stage_id)
{
104 105
    stage_id = _stage_id;
    client_id = enter_stage();
106 107
    previous_tick = srs_get_system_time_ms();
    printed_age = _age = 0;
winlin authored
108 109 110 111
}

SrsPithyPrint::~SrsPithyPrint()
{
112
    leave_stage();
winlin authored
113 114 115 116
}

int SrsPithyPrint::enter_stage()
{
117 118 119 120
    SrsStageInfo* stage = NULL;
    
    std::map<int, SrsStageInfo*>::iterator it = _srs_stages.find(stage_id);
    if (it == _srs_stages.end()) {
121 122
        stage = new SrsStageInfo(stage_id);
        _srs_stages[stage_id] = stage;
123 124 125 126 127 128 129 130 131 132 133
    } 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
134 135 136 137
}

void SrsPithyPrint::leave_stage()
{
138 139 140 141
    SrsStageInfo* stage = _srs_stages[stage_id];
    srs_assert(stage != NULL);
    
    stage->nb_clients--;
winlin authored
142
143 144
    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
145 146
}
147
void SrsPithyPrint::elapse()
winlin authored
148
{
149 150 151 152
    int64_t diff = srs_get_system_time_ms() - previous_tick;
    
    _age += srs_max(0, diff);
    previous_tick = srs_get_system_time_ms();
winlin authored
153 154 155 156
}

bool SrsPithyPrint::can_print()
{
157 158 159
    SrsStageInfo* stage = _srs_stages[stage_id];
    srs_assert(stage != NULL);
    
160
    int64_t alive_age = _age - printed_age;
161 162 163 164
    int64_t can_print_age = stage->nb_clients * stage->pithy_print_time_ms;
    
    bool can_print = alive_age >= can_print_age;
    if (can_print) {
165
        printed_age = _age;
166 167 168
    }
    
    return can_print;
winlin authored
169 170
}
171
int64_t SrsPithyPrint::age()
winlin authored
172
{
173
    return _age;
winlin authored
174 175
}