2021-05-31 04:59:21 +00:00
|
|
|
//
|
|
|
|
// Copyright (c) 2013-2021 Winlin
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
//
|
2015-01-04 14:47:12 +00:00
|
|
|
|
|
|
|
#ifndef SRS_APP_STATISTIC_HPP
|
|
|
|
#define SRS_APP_STATISTIC_HPP
|
|
|
|
|
|
|
|
#include <srs_core.hpp>
|
|
|
|
|
|
|
|
#include <map>
|
2015-01-05 04:40:38 +00:00
|
|
|
#include <string>
|
2015-09-17 05:36:02 +00:00
|
|
|
#include <vector>
|
2015-01-04 14:47:12 +00:00
|
|
|
|
2015-03-08 07:33:08 +00:00
|
|
|
#include <srs_kernel_codec.hpp>
|
2015-08-22 05:57:34 +00:00
|
|
|
#include <srs_rtmp_stack.hpp>
|
2015-03-08 07:33:08 +00:00
|
|
|
|
2015-03-08 11:59:10 +00:00
|
|
|
class SrsKbps;
|
2018-12-23 12:47:17 +00:00
|
|
|
class SrsWallClock;
|
2015-01-04 14:47:12 +00:00
|
|
|
class SrsRequest;
|
2020-11-05 03:47:24 +00:00
|
|
|
class ISrsExpire;
|
2015-09-19 04:27:31 +00:00
|
|
|
class SrsJsonObject;
|
|
|
|
class SrsJsonArray;
|
2020-11-04 10:15:43 +00:00
|
|
|
class ISrsKbpsDelta;
|
2015-01-04 14:47:12 +00:00
|
|
|
|
2015-01-05 04:40:38 +00:00
|
|
|
struct SrsStatisticVhost
|
2015-01-04 14:47:12 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-06-18 03:45:43 +00:00
|
|
|
std::string id;
|
2015-01-05 04:40:38 +00:00
|
|
|
std::string vhost;
|
2015-08-22 14:51:59 +00:00
|
|
|
int nb_streams;
|
2015-08-21 07:51:20 +00:00
|
|
|
int nb_clients;
|
2015-03-08 11:59:10 +00:00
|
|
|
public:
|
2019-04-28 01:08:05 +00:00
|
|
|
// The vhost total kbps.
|
2015-03-08 11:59:10 +00:00
|
|
|
SrsKbps* kbps;
|
2018-12-23 12:47:17 +00:00
|
|
|
SrsWallClock* clk;
|
2015-01-05 05:08:11 +00:00
|
|
|
public:
|
|
|
|
SrsStatisticVhost();
|
|
|
|
virtual ~SrsStatisticVhost();
|
2015-08-21 07:51:20 +00:00
|
|
|
public:
|
2018-01-01 11:39:57 +00:00
|
|
|
virtual srs_error_t dumps(SrsJsonObject* obj);
|
2015-01-04 14:47:12 +00:00
|
|
|
};
|
2015-01-05 04:12:21 +00:00
|
|
|
|
2015-01-05 04:40:38 +00:00
|
|
|
struct SrsStatisticStream
|
2015-01-04 14:47:12 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-06-18 03:45:43 +00:00
|
|
|
std::string id;
|
2015-01-05 04:40:38 +00:00
|
|
|
SrsStatisticVhost* vhost;
|
|
|
|
std::string app;
|
|
|
|
std::string stream;
|
2015-01-05 05:08:11 +00:00
|
|
|
std::string url;
|
2015-08-22 05:03:10 +00:00
|
|
|
bool active;
|
2021-05-14 00:21:16 +00:00
|
|
|
// The publisher connection id.
|
|
|
|
std::string publisher_id;
|
2015-08-21 07:51:20 +00:00
|
|
|
int nb_clients;
|
2017-04-23 12:55:51 +00:00
|
|
|
uint64_t nb_frames;
|
2015-03-08 11:59:10 +00:00
|
|
|
public:
|
2019-04-28 01:08:05 +00:00
|
|
|
// The stream total kbps.
|
2015-03-08 11:59:10 +00:00
|
|
|
SrsKbps* kbps;
|
2018-12-23 12:47:17 +00:00
|
|
|
SrsWallClock* clk;
|
2015-03-08 07:33:08 +00:00
|
|
|
public:
|
|
|
|
bool has_video;
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsVideoCodecId vcodec;
|
2019-04-28 01:08:05 +00:00
|
|
|
// The profile_idc, ISO_IEC_14496-10-AVC-2003.pdf, page 45.
|
2015-03-08 10:33:35 +00:00
|
|
|
SrsAvcProfile avc_profile;
|
2019-04-28 01:08:05 +00:00
|
|
|
// The level_idc, ISO_IEC_14496-10-AVC-2003.pdf, page 45.
|
2015-03-08 10:33:35 +00:00
|
|
|
SrsAvcLevel avc_level;
|
2019-04-28 01:08:05 +00:00
|
|
|
// The width and height in codec info.
|
2015-08-28 10:00:24 +00:00
|
|
|
int width;
|
|
|
|
int height;
|
2015-03-08 07:33:08 +00:00
|
|
|
public:
|
|
|
|
bool has_audio;
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsAudioCodecId acodec;
|
|
|
|
SrsAudioSampleRate asample_rate;
|
2017-02-12 12:46:24 +00:00
|
|
|
SrsAudioChannels asound_type;
|
2019-04-28 01:08:05 +00:00
|
|
|
// The audio specified
|
|
|
|
// audioObjectType, in 1.6.2.1 AudioSpecificConfig, page 33,
|
|
|
|
// 1.5.1.1 Audio object type definition, page 23,
|
|
|
|
// in ISO_IEC_14496-3-AAC-2001.pdf.
|
2015-03-08 09:56:49 +00:00
|
|
|
SrsAacObjectType aac_object;
|
2015-01-05 05:08:11 +00:00
|
|
|
public:
|
|
|
|
SrsStatisticStream();
|
|
|
|
virtual ~SrsStatisticStream();
|
2015-08-21 07:51:20 +00:00
|
|
|
public:
|
2018-01-01 11:39:57 +00:00
|
|
|
virtual srs_error_t dumps(SrsJsonObject* obj);
|
2015-03-08 07:33:08 +00:00
|
|
|
public:
|
2021-05-14 00:21:16 +00:00
|
|
|
// Publish the stream, id is the publisher.
|
|
|
|
virtual void publish(std::string id);
|
2019-04-28 01:08:05 +00:00
|
|
|
// Close the stream.
|
2015-03-08 07:33:08 +00:00
|
|
|
virtual void close();
|
2015-01-05 04:40:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SrsStatisticClient
|
|
|
|
{
|
2015-01-05 04:20:46 +00:00
|
|
|
public:
|
2020-11-05 03:47:24 +00:00
|
|
|
ISrsExpire* conn;
|
2015-01-05 04:40:38 +00:00
|
|
|
SrsStatisticStream* stream;
|
2015-08-22 05:57:34 +00:00
|
|
|
SrsRequest* req;
|
|
|
|
SrsRtmpConnType type;
|
2020-06-18 03:45:43 +00:00
|
|
|
std::string id;
|
2019-04-10 01:07:03 +00:00
|
|
|
srs_utime_t create;
|
2015-08-21 09:00:52 +00:00
|
|
|
public:
|
|
|
|
SrsStatisticClient();
|
|
|
|
virtual ~SrsStatisticClient();
|
|
|
|
public:
|
2018-01-01 11:39:57 +00:00
|
|
|
virtual srs_error_t dumps(SrsJsonObject* obj);
|
2015-01-05 04:40:38 +00:00
|
|
|
};
|
|
|
|
|
2021-05-14 00:56:41 +00:00
|
|
|
class SrsStatistic
|
2015-01-05 04:40:38 +00:00
|
|
|
{
|
2015-01-05 04:20:46 +00:00
|
|
|
private:
|
2015-01-04 14:47:12 +00:00
|
|
|
static SrsStatistic *_instance;
|
2019-04-28 01:08:05 +00:00
|
|
|
// The id to identify the sever.
|
2021-01-07 08:47:49 +00:00
|
|
|
std::string _server_id;
|
2015-08-21 09:00:52 +00:00
|
|
|
private:
|
2019-04-28 01:08:05 +00:00
|
|
|
// The key: vhost id, value: vhost object.
|
2020-06-18 03:45:43 +00:00
|
|
|
std::map<std::string, SrsStatisticVhost*> vhosts;
|
2019-04-28 01:08:05 +00:00
|
|
|
// The key: vhost url, value: vhost Object.
|
2015-08-21 09:00:52 +00:00
|
|
|
// @remark a fast index for vhosts.
|
|
|
|
std::map<std::string, SrsStatisticVhost*> rvhosts;
|
|
|
|
private:
|
2019-04-28 01:08:05 +00:00
|
|
|
// The key: stream id, value: stream Object.
|
2020-06-18 03:45:43 +00:00
|
|
|
std::map<std::string, SrsStatisticStream*> streams;
|
2019-04-28 01:08:05 +00:00
|
|
|
// The key: stream url, value: stream Object.
|
2015-08-21 09:00:52 +00:00
|
|
|
// @remark a fast index for streams.
|
|
|
|
std::map<std::string, SrsStatisticStream*> rstreams;
|
|
|
|
private:
|
2019-04-28 01:08:05 +00:00
|
|
|
// The key: client id, value: stream object.
|
2020-06-18 03:45:43 +00:00
|
|
|
std::map<std::string, SrsStatisticClient*> clients;
|
2019-04-28 01:08:05 +00:00
|
|
|
// The server total kbps.
|
2015-03-08 11:59:10 +00:00
|
|
|
SrsKbps* kbps;
|
2018-12-23 12:47:17 +00:00
|
|
|
SrsWallClock* clk;
|
2015-01-05 04:20:46 +00:00
|
|
|
private:
|
2015-01-05 04:40:38 +00:00
|
|
|
SrsStatistic();
|
|
|
|
virtual ~SrsStatistic();
|
|
|
|
public:
|
|
|
|
static SrsStatistic* instance();
|
2015-03-08 07:33:08 +00:00
|
|
|
public:
|
2021-01-07 08:47:49 +00:00
|
|
|
virtual SrsStatisticVhost* find_vhost_by_id(std::string vid);
|
|
|
|
virtual SrsStatisticVhost* find_vhost_by_name(std::string name);
|
2020-06-18 03:45:43 +00:00
|
|
|
virtual SrsStatisticStream* find_stream(std::string sid);
|
2020-07-05 15:26:55 +00:00
|
|
|
virtual SrsStatisticClient* find_client(std::string client_id);
|
2015-08-21 09:00:52 +00:00
|
|
|
public:
|
2019-04-28 01:08:05 +00:00
|
|
|
// When got video info for stream.
|
2018-01-01 11:39:57 +00:00
|
|
|
virtual srs_error_t on_video_info(SrsRequest* req, SrsVideoCodecId vcodec, SrsAvcProfile avc_profile,
|
|
|
|
SrsAvcLevel avc_level, int width, int height);
|
2019-04-28 01:08:05 +00:00
|
|
|
// When got audio info for stream.
|
2018-01-01 11:39:57 +00:00
|
|
|
virtual srs_error_t on_audio_info(SrsRequest* req, SrsAudioCodecId acodec, SrsAudioSampleRate asample_rate,
|
|
|
|
SrsAudioChannels asound_type, SrsAacObjectType aac_object);
|
2019-04-28 01:08:05 +00:00
|
|
|
// When got videos, update the frames.
|
|
|
|
// We only stat the total number of video frames.
|
2018-01-01 11:39:57 +00:00
|
|
|
virtual srs_error_t on_video_frames(SrsRequest* req, int nb_frames);
|
2019-04-28 01:08:05 +00:00
|
|
|
// When publish stream.
|
|
|
|
// @param req the request object of publish connection.
|
2021-05-14 00:21:16 +00:00
|
|
|
// @param publisher_id The id of publish connection.
|
|
|
|
virtual void on_stream_publish(SrsRequest* req, std::string publisher_id);
|
2019-04-28 01:08:05 +00:00
|
|
|
// When close stream.
|
2015-03-08 07:33:08 +00:00
|
|
|
virtual void on_stream_close(SrsRequest* req);
|
2015-01-05 04:40:38 +00:00
|
|
|
public:
|
2019-04-28 01:08:05 +00:00
|
|
|
// When got a client to publish/play stream,
|
|
|
|
// @param id, the client srs id.
|
|
|
|
// @param req, the client request object.
|
|
|
|
// @param conn, the physical absract connection object.
|
|
|
|
// @param type, the type of connection.
|
2021-05-14 00:21:16 +00:00
|
|
|
virtual srs_error_t on_client(std::string id, SrsRequest* req, ISrsExpire* conn, SrsRtmpConnType type);
|
2019-04-28 01:08:05 +00:00
|
|
|
// Client disconnect
|
|
|
|
// @remark the on_disconnect always call, while the on_client is call when
|
|
|
|
// only got the request object, so the client specified by id maybe not
|
|
|
|
// exists in stat.
|
2021-05-14 00:21:16 +00:00
|
|
|
virtual void on_disconnect(std::string id);
|
2019-04-28 01:08:05 +00:00
|
|
|
// Sample the kbps, add delta bytes of conn.
|
|
|
|
// Use kbps_sample() to get all result of kbps stat.
|
2021-05-14 00:21:16 +00:00
|
|
|
virtual void kbps_add_delta(std::string id, ISrsKbpsDelta* delta);
|
2019-04-28 01:08:05 +00:00
|
|
|
// Calc the result for all kbps.
|
|
|
|
// @return the server kbps.
|
2015-03-08 11:59:10 +00:00
|
|
|
virtual SrsKbps* kbps_sample();
|
2015-01-05 04:40:38 +00:00
|
|
|
public:
|
2019-04-28 01:08:05 +00:00
|
|
|
// Get the server id, used to identify the server.
|
|
|
|
// For example, when restart, the server id must changed.
|
2021-01-07 08:47:49 +00:00
|
|
|
virtual std::string server_id();
|
2019-04-28 01:08:05 +00:00
|
|
|
// Dumps the vhosts to amf0 array.
|
2018-01-01 11:39:57 +00:00
|
|
|
virtual srs_error_t dumps_vhosts(SrsJsonArray* arr);
|
2019-04-28 01:08:05 +00:00
|
|
|
// Dumps the streams to amf0 array.
|
2021-09-06 02:10:53 +00:00
|
|
|
// @param start the start index, from 0.
|
|
|
|
// @param count the max count of streams to dump.
|
|
|
|
virtual srs_error_t dumps_streams(SrsJsonArray* arr, int start, int count);
|
2019-04-28 01:08:05 +00:00
|
|
|
// Dumps the clients to amf0 array
|
|
|
|
// @param start the start index, from 0.
|
|
|
|
// @param count the max count of clients to dump.
|
2018-01-01 11:39:57 +00:00
|
|
|
virtual srs_error_t dumps_clients(SrsJsonArray* arr, int start, int count);
|
2015-03-08 07:33:08 +00:00
|
|
|
private:
|
|
|
|
virtual SrsStatisticVhost* create_vhost(SrsRequest* req);
|
|
|
|
virtual SrsStatisticStream* create_stream(SrsStatisticVhost* vhost, SrsRequest* req);
|
2015-01-04 14:47:12 +00:00
|
|
|
};
|
|
|
|
|
2015-01-05 04:14:02 +00:00
|
|
|
#endif
|