1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/app/srs_app_hourglass.hpp

107 lines
3.7 KiB
C++
Raw Normal View History

2017-03-25 09:21:39 +00:00
/**
* The MIT License (MIT)
*
2019-12-30 02:10:35 +00:00
* Copyright (c) 2013-2020 Winlin
2017-03-25 09:21:39 +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.
*/
#ifndef SRS_APP_HOURGLASS_HPP
#define SRS_APP_HOURGLASS_HPP
#include <srs_core.hpp>
#include <srs_app_st.hpp>
#include <map>
#include <string>
class SrsCoroutine;
2019-04-30 00:24:52 +00:00
// The handler for the tick.
class ISrsHourGlass
{
public:
ISrsHourGlass();
virtual ~ISrsHourGlass();
public:
2019-04-30 00:24:52 +00:00
// When time is ticked, this function is called.
virtual srs_error_t notify(int event, srs_utime_t interval, srs_utime_t tick) = 0;
};
// The hourglass(timer or SrsTimer) for special tasks,
// while these tasks are attached to some intervals, for example,
// there are N=3 tasks bellow:
// 1. A heartbeat every 3s.
// 2. A print message every 5s.
// 3. A notify backend every 7s.
2019-04-30 00:24:52 +00:00
// The hourglass will call back when ticks:
// 1. Got notify(event=1, time=3)
// 2. Got notify(event=2, time=5)
// 3. Got notify(event=1, time=6)
// 4. Got notify(event=3, time=7)
// 5. Got notify(event=1, time=9)
// 6. Got notify(event=2, time=10)
// It's a complex and high-performance timer.
2019-04-30 00:24:52 +00:00
//
// Usage:
// SrsHourGlass* hg = new SrsHourGlass("nack", handler, 100 * SRS_UTIME_MILLISECONDS);
//
// hg->tick(1, 300 * SRS_UTIME_MILLISECONDS);
// hg->tick(2, 500 * SRS_UTIME_MILLISECONDS);
// hg->tick(3, 700 * SRS_UTIME_MILLISECONDS);
//
// // The hg will create a thread for timer.
// hg->start();
class SrsHourGlass : virtual public ISrsCoroutineHandler
{
private:
std::string label_;
SrsCoroutine* trd;
ISrsHourGlass* handler;
2019-04-18 00:11:16 +00:00
srs_utime_t _resolution;
2019-04-30 00:24:52 +00:00
// The ticks:
// key: the event of tick.
2019-04-30 00:24:52 +00:00
// value: the interval of tick.
2019-04-18 00:11:16 +00:00
std::map<int, srs_utime_t> ticks;
2019-04-30 00:24:52 +00:00
// The total elapsed time,
// for each cycle, we increase it with a resolution.
2019-04-18 00:11:16 +00:00
srs_utime_t total_elapse;
public:
// TODO: FIMXE: Refine to SrsHourGlass(std::string label);
SrsHourGlass(std::string label, ISrsHourGlass* h, srs_utime_t resolution);
virtual ~SrsHourGlass();
public:
// Start or stop the hourglass.
virtual srs_error_t start();
virtual void stop();
public:
// TODO: FIXME: Refine to tick with handler. Remove the tick(interval).
// Add a pair of tick(event, interval).
// @param event the event of tick, default is 0.
2019-04-18 00:11:16 +00:00
// @param interval the interval in srs_utime_t of tick.
virtual srs_error_t tick(srs_utime_t interval);
virtual srs_error_t tick(int event, srs_utime_t interval);
public:
2019-04-30 00:24:52 +00:00
// Cycle the hourglass, which will sleep resolution every time.
// and call handler when ticked.
2018-01-01 11:39:57 +00:00
virtual srs_error_t cycle();
};
#endif