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

196 lines
6 KiB
C++
Raw Normal View History

//
// Copyright (c) 2013-2021 The SRS Authors
//
// 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>
#include <srs_rtmp_stack.hpp>
2015-03-08 07:33:08 +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;
int nb_clients;
public:
2019-04-28 01:08:05 +00:00
// The vhost total kbps.
SrsKbps* kbps;
2018-12-23 12:47:17 +00:00
SrsWallClock* clk;
public:
SrsStatisticVhost();
virtual ~SrsStatisticVhost();
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;
std::string url;
bool active;
// The publisher connection id.
std::string publisher_id;
int nb_clients;
uint64_t nb_frames;
public:
2019-04-28 01:08:05 +00:00
// The stream total kbps.
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.
SrsAvcProfile avc_profile;
2019-04-28 01:08:05 +00:00
// The level_idc, ISO_IEC_14496-10-AVC-2003.pdf, page 45.
SrsAvcLevel avc_level;
2019-04-28 01:08:05 +00:00
// The width and height in codec info.
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;
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.
SrsAacObjectType aac_object;
public:
SrsStatisticStream();
virtual ~SrsStatisticStream();
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:
// 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
{
public:
2020-11-05 03:47:24 +00:00
ISrsExpire* conn;
2015-01-05 04:40:38 +00:00
SrsStatisticStream* stream;
SrsRequest* req;
SrsRtmpConnType type;
2020-06-18 03:45:43 +00:00
std::string id;
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
};
class SrsStatistic
2015-01-05 04:40:38 +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.
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.
SrsKbps* kbps;
2018-12-23 12:47:17 +00:00
SrsWallClock* clk;
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:
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);
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.
// @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.
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.
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.
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.
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.
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.
2018-01-01 11:39:57 +00:00
virtual srs_error_t dumps_streams(SrsJsonArray* arr);
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