2017-03-25 09:21:39 +00:00
|
|
|
/**
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2017-03-25 13:29:29 +00:00
|
|
|
* Copyright (c) 2013-2017 OSSRS(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.
|
|
|
|
*/
|
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;
|
2015-01-04 14:47:12 +00:00
|
|
|
class SrsRequest;
|
2015-03-08 11:59:10 +00:00
|
|
|
class SrsConnection;
|
2015-09-19 04:27:31 +00:00
|
|
|
class SrsJsonObject;
|
|
|
|
class SrsJsonArray;
|
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:
|
2015-01-05 05:08:11 +00:00
|
|
|
int64_t 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:
|
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* vhost total kbps.
|
|
|
|
*/
|
2015-03-08 11:59:10 +00:00
|
|
|
SrsKbps* kbps;
|
2015-01-05 05:08:11 +00:00
|
|
|
public:
|
|
|
|
SrsStatisticVhost();
|
|
|
|
virtual ~SrsStatisticVhost();
|
2015-08-21 07:51:20 +00:00
|
|
|
public:
|
2015-09-19 04:27:31 +00:00
|
|
|
virtual int 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:
|
2015-01-05 05:08:11 +00:00
|
|
|
int64_t 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;
|
|
|
|
int connection_cid;
|
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:
|
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* stream total kbps.
|
|
|
|
*/
|
2015-03-08 11:59:10 +00:00
|
|
|
SrsKbps* kbps;
|
2015-03-08 07:33:08 +00:00
|
|
|
public:
|
|
|
|
bool has_video;
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsVideoCodecId vcodec;
|
2017-01-26 09:28:49 +00:00
|
|
|
// profile_idc, ISO_IEC_14496-10-AVC-2003.pdf, page 45.
|
2015-03-08 10:33:35 +00:00
|
|
|
SrsAvcProfile avc_profile;
|
2017-01-26 09:28:49 +00:00
|
|
|
// level_idc, ISO_IEC_14496-10-AVC-2003.pdf, page 45.
|
2015-03-08 10:33:35 +00:00
|
|
|
SrsAvcLevel avc_level;
|
2015-08-28 10:00:24 +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;
|
2017-02-12 12:46:24 +00:00
|
|
|
SrsAudioChannels asound_type;
|
2015-03-08 07:33:08 +00:00
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* 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:
|
2015-09-19 04:27:31 +00:00
|
|
|
virtual int dumps(SrsJsonObject* obj);
|
2015-03-08 07:33:08 +00:00
|
|
|
public:
|
2015-05-08 08:45:25 +00:00
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* publish the stream.
|
|
|
|
*/
|
2015-08-22 05:03:10 +00:00
|
|
|
virtual void publish(int cid);
|
2015-03-08 07:33:08 +00:00
|
|
|
/**
|
2017-03-25 09:21:39 +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:
|
2015-01-05 04:40:38 +00:00
|
|
|
SrsStatisticStream* stream;
|
2015-08-22 05:36:15 +00:00
|
|
|
SrsConnection* conn;
|
2015-08-22 05:57:34 +00:00
|
|
|
SrsRequest* req;
|
|
|
|
SrsRtmpConnType type;
|
2015-01-05 04:40:38 +00:00
|
|
|
int id;
|
2015-08-22 05:57:34 +00:00
|
|
|
int64_t create;
|
2015-08-21 09:00:52 +00:00
|
|
|
public:
|
|
|
|
SrsStatisticClient();
|
|
|
|
virtual ~SrsStatisticClient();
|
|
|
|
public:
|
2015-09-19 04:27:31 +00:00
|
|
|
virtual int dumps(SrsJsonObject* obj);
|
2015-01-05 04:40:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SrsStatistic
|
|
|
|
{
|
2015-01-05 04:20:46 +00:00
|
|
|
private:
|
2015-01-04 14:47:12 +00:00
|
|
|
static SrsStatistic *_instance;
|
2015-01-05 05:08:11 +00:00
|
|
|
// the id to identify the sever.
|
|
|
|
int64_t _server_id;
|
2015-08-21 09:00:52 +00:00
|
|
|
private:
|
|
|
|
// key: vhost id, value: vhost object.
|
|
|
|
std::map<int64_t, SrsStatisticVhost*> vhosts;
|
|
|
|
// key: vhost url, value: vhost Object.
|
|
|
|
// @remark a fast index for vhosts.
|
|
|
|
std::map<std::string, SrsStatisticVhost*> rvhosts;
|
|
|
|
private:
|
|
|
|
// key: stream id, value: stream Object.
|
|
|
|
std::map<int64_t, SrsStatisticStream*> streams;
|
|
|
|
// key: stream url, value: stream Object.
|
|
|
|
// @remark a fast index for streams.
|
|
|
|
std::map<std::string, SrsStatisticStream*> rstreams;
|
|
|
|
private:
|
2015-01-05 04:40:38 +00:00
|
|
|
// key: client id, value: stream object.
|
|
|
|
std::map<int, SrsStatisticClient*> clients;
|
2015-03-08 11:59:10 +00:00
|
|
|
// server total kbps.
|
|
|
|
SrsKbps* kbps;
|
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:
|
2015-08-21 09:00:52 +00:00
|
|
|
virtual SrsStatisticVhost* find_vhost(int vid);
|
2015-08-27 15:55:59 +00:00
|
|
|
virtual SrsStatisticVhost* find_vhost(std::string name);
|
2015-08-21 09:00:52 +00:00
|
|
|
virtual SrsStatisticStream* find_stream(int sid);
|
|
|
|
virtual SrsStatisticClient* find_client(int cid);
|
|
|
|
public:
|
2015-03-08 07:33:08 +00:00
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* when got video info for stream.
|
|
|
|
*/
|
|
|
|
virtual int on_video_info(SrsRequest* req, SrsVideoCodecId vcodec, SrsAvcProfile avc_profile, SrsAvcLevel avc_level, int width, int height);
|
2015-03-08 07:33:08 +00:00
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* when got audio info for stream.
|
|
|
|
*/
|
|
|
|
virtual int on_audio_info(SrsRequest* req, SrsAudioCodecId acodec, SrsAudioSampleRate asample_rate, SrsAudioChannels asound_type, SrsAacObjectType aac_object);
|
2017-04-23 12:55:51 +00:00
|
|
|
/**
|
|
|
|
* When got videos, update the frames.
|
|
|
|
* We only stat the total number of video frames.
|
|
|
|
*/
|
|
|
|
virtual int on_video_frames(SrsRequest* req, int nb_frames);
|
2015-03-08 07:33:08 +00:00
|
|
|
/**
|
2015-08-22 05:03:10 +00:00
|
|
|
* when publish stream.
|
|
|
|
* @param req the request object of publish connection.
|
|
|
|
* @param cid the cid of publish connection.
|
|
|
|
*/
|
|
|
|
virtual void on_stream_publish(SrsRequest* req, int cid);
|
2015-05-08 08:45:25 +00:00
|
|
|
/**
|
2017-03-25 09:21:39 +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:
|
|
|
|
/**
|
2015-07-08 09:42:35 +00:00
|
|
|
* when got a client to publish/play stream,
|
|
|
|
* @param id, the client srs id.
|
|
|
|
* @param req, the client request object.
|
2015-08-22 05:36:15 +00:00
|
|
|
* @param conn, the physical absract connection object.
|
2015-08-22 05:57:34 +00:00
|
|
|
* @param type, the type of connection.
|
2015-07-08 09:42:35 +00:00
|
|
|
*/
|
2015-08-22 05:57:34 +00:00
|
|
|
virtual int on_client(int id, SrsRequest* req, SrsConnection* conn, SrsRtmpConnType type);
|
2015-01-06 14:20:31 +00:00
|
|
|
/**
|
2015-07-08 09:42:35 +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.
|
|
|
|
*/
|
2015-03-08 07:33:08 +00:00
|
|
|
virtual void on_disconnect(int id);
|
2015-03-08 11:59:10 +00:00
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* sample the kbps, add delta bytes of conn.
|
|
|
|
* use kbps_sample() to get all result of kbps stat.
|
|
|
|
*/
|
2015-05-22 12:31:07 +00:00
|
|
|
// TODO: FIXME: the add delta must use IKbpsDelta interface instead.
|
2015-03-08 11:59:10 +00:00
|
|
|
virtual void kbps_add_delta(SrsConnection* conn);
|
|
|
|
/**
|
2017-03-25 09:21:39 +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:
|
2015-01-05 05:08:11 +00:00
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* get the server id, used to identify the server.
|
|
|
|
* for example, when restart, the server id must changed.
|
|
|
|
*/
|
2015-01-05 05:08:11 +00:00
|
|
|
virtual int64_t server_id();
|
2015-01-05 04:40:38 +00:00
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* dumps the vhosts to amf0 array.
|
|
|
|
*/
|
2015-09-19 04:27:31 +00:00
|
|
|
virtual int dumps_vhosts(SrsJsonArray* arr);
|
2015-01-05 04:40:38 +00:00
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* dumps the streams to amf0 array.
|
|
|
|
*/
|
2015-09-19 04:27:31 +00:00
|
|
|
virtual int dumps_streams(SrsJsonArray* arr);
|
2015-08-21 09:00:52 +00:00
|
|
|
/**
|
2015-08-28 06:39:29 +00:00
|
|
|
* dumps the clients to amf0 array
|
2015-08-21 09:00:52 +00:00
|
|
|
* @param start the start index, from 0.
|
|
|
|
* @param count the max count of clients to dump.
|
|
|
|
*/
|
2015-09-19 04:27:31 +00:00
|
|
|
virtual int 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
|