Blame view

trunk/src/app/srs_app_pithy_print.cpp 4.6 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>
winlin authored
33 34 35

#define SRS_STAGE_DEFAULT_INTERVAL_MS 1200
36
struct SrsStageInfo : public ISrsReloadHandler
winlin authored
37
{
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
    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;
            }
74 75 76 77
            case SRS_STAGE_INGESTER: {
                pithy_print_time_ms = _srs_config->get_pithy_print_ingester();
                break;
            }
78 79 80 81 82 83 84 85 86 87
            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
88
public:
89 90 91 92 93
    virtual int on_reload_pithy_print()
    {
        update_print_time();
        return ERROR_SUCCESS;
    }
winlin authored
94 95 96 97 98
};
static std::map<int, SrsStageInfo*> _srs_stages;

SrsPithyPrint::SrsPithyPrint(int _stage_id)
{
99 100 101
    stage_id = _stage_id;
    client_id = enter_stage();
    printed_age = age = 0;
winlin authored
102 103 104 105
}

SrsPithyPrint::~SrsPithyPrint()
{
106
    leave_stage();
winlin authored
107 108 109 110
}

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

void SrsPithyPrint::leave_stage()
{
132 133 134 135
    SrsStageInfo* stage = _srs_stages[stage_id];
    srs_assert(stage != NULL);
    
    stage->nb_clients--;
winlin authored
136
137 138
    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
139 140 141 142
}

void SrsPithyPrint::elapse(int64_t time_ms)
{
143
    age += time_ms;
winlin authored
144 145 146 147
}

bool SrsPithyPrint::can_print()
{
148 149 150 151 152 153 154 155 156 157 158 159
    SrsStageInfo* stage = _srs_stages[stage_id];
    srs_assert(stage != NULL);
    
    int64_t alive_age = age - printed_age;
    int64_t can_print_age = stage->nb_clients * stage->pithy_print_time_ms;
    
    bool can_print = alive_age >= can_print_age;
    if (can_print) {
        printed_age = age;
    }
    
    return can_print;
winlin authored
160 161 162 163
}

int64_t SrsPithyPrint::get_age()
{
164
    return age;
winlin authored
165 166 167 168
}

void SrsPithyPrint::set_age(int64_t _age)
{
169
    age = _age;
winlin authored
170 171
}