Blame view

trunk/src/app/srs_app_statistic.hpp 4.8 KB
qiang.li authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
qiang.li authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

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_STATISTIC_HPP
#define SRS_APP_STATISTIC_HPP

/*
#include <srs_app_statistic.hpp>
*/

#include <srs_core.hpp>

#include <map>
winlin authored
34
#include <string>
qiang.li authored
35
36 37
#include <srs_kernel_codec.hpp>
38
class SrsKbps;
qiang.li authored
39
class SrsRequest;
40
class SrsConnection;
qiang.li authored
41
winlin authored
42
struct SrsStatisticVhost
qiang.li authored
43 44
{
public:
45
    int64_t id;
winlin authored
46
    std::string vhost;
47
public:
48 49 50 51 52
    /**
    * vhost total kbps.
    */
    SrsKbps* kbps;
public:
53 54
    SrsStatisticVhost();
    virtual ~SrsStatisticVhost();
qiang.li authored
55
};
56
winlin authored
57
struct SrsStatisticStream
qiang.li authored
58 59
{
public:
60
    int64_t id;
winlin authored
61 62 63
    SrsStatisticVhost* vhost;
    std::string app;
    std::string stream;
64 65
    std::string url;
public:
66 67 68 69 70
    /**
    * stream total kbps.
    */
    SrsKbps* kbps;
public:
71 72 73
    bool has_video;
    SrsCodecVideo vcodec;
    // profile_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.
74
    SrsAvcProfile avc_profile;
75
    // level_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.
76
    SrsAvcLevel avc_level;
77 78 79 80 81 82 83 84 85 86 87
public:
    bool has_audio;
    SrsCodecAudio acodec;
    SrsCodecAudioSampleRate asample_rate;
    SrsCodecAudioSoundType asound_type;
    /**
    * audio specified
    * audioObjectType, in 1.6.2.1 AudioSpecificConfig, page 33,
    * 1.5.1.1 Audio object type definition, page 23,
    *           in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf.
    */
88
    SrsAacObjectType aac_object;
89
public:
90 91
    SrsStatisticStream();
    virtual ~SrsStatisticStream();
92 93 94 95 96
public:
    /**
    * close the stream.
    */
    virtual void close();
winlin authored
97 98 99 100
};

struct SrsStatisticClient
{
101
public:
winlin authored
102 103 104 105 106 107
    SrsStatisticStream* stream;
    int id;
};

class SrsStatistic
{
108
private:
qiang.li authored
109
    static SrsStatistic *_instance;
110 111
    // the id to identify the sever.
    int64_t _server_id;
winlin authored
112 113
    // key: vhost name, value: vhost object.
    std::map<std::string, SrsStatisticVhost*> vhosts;
114
    // key: stream url, value: stream object.
winlin authored
115 116 117
    std::map<std::string, SrsStatisticStream*> streams;
    // key: client id, value: stream object.
    std::map<int, SrsStatisticClient*> clients;
118 119
    // server total kbps.
    SrsKbps* kbps;
120
private:
winlin authored
121 122 123 124 125 126
    SrsStatistic();
    virtual ~SrsStatistic();
public:
    static SrsStatistic* instance();
public:
    /**
127 128 129
    * when got video info for stream.
    */
    virtual int on_video_info(SrsRequest* req, 
130
        SrsCodecVideo vcodec, SrsAvcProfile avc_profile, SrsAvcLevel avc_level
131 132 133 134 135 136
    );
    /**
    * when got audio info for stream.
    */
    virtual int on_audio_info(SrsRequest* req,
        SrsCodecAudio acodec, SrsCodecAudioSampleRate asample_rate, SrsCodecAudioSoundType asound_type,
137
        SrsAacObjectType aac_object
138 139 140 141 142 143 144
    );
    /**
    * when close stream.
    */
    virtual void on_stream_close(SrsRequest* req);
public:
    /**
winlin authored
145 146 147 148
    * when got a client to publish/play stream,
    * @param id, the client srs id.
    * @param req, the client request object.
    */
149
    virtual int on_client(int id, SrsRequest* req);
150
    /**
151
    * client disconnect
152
    */
153
    virtual void on_disconnect(int id);
154 155 156 157 158 159 160 161 162 163
    /**
    * sample the kbps, add delta bytes of conn.
    * use kbps_sample() to get all result of kbps stat.
    */
    virtual void kbps_add_delta(SrsConnection* conn);
    /**
    * calc the result for all kbps.
    * @return the server kbps.
    */
    virtual SrsKbps* kbps_sample();
winlin authored
164 165
public:
    /**
166 167 168 169 170
    * get the server id, used to identify the server.
    * for example, when restart, the server id must changed.
    */
    virtual int64_t server_id();
    /**
winlin authored
171 172 173 174 175 176 177
    * dumps the vhosts to sstream in json.
    */
    virtual int dumps_vhosts(std::stringstream& ss);
    /**
    * dumps the streams to sstream in json.
    */
    virtual int dumps_streams(std::stringstream& ss);
178 179 180
private:
    virtual SrsStatisticVhost* create_vhost(SrsRequest* req);
    virtual SrsStatisticStream* create_stream(SrsStatisticVhost* vhost, SrsRequest* req);
qiang.li authored
181 182
};
winlin authored
183
#endif