Blame view

trunk/src/app/srs_app_statistic.cpp 11.6 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

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.
*/

#include <srs_app_statistic.hpp>
26
#include <unistd.h>
27 28 29
#include <sstream>
using namespace std;
30
#include <srs_rtmp_sdk.hpp>
31
#include <srs_app_json.hpp>
32
#include <srs_protocol_kbps.hpp>
33
#include <srs_app_conn.hpp>
winlin authored
34 35
#include <srs_app_config.hpp>
#include <srs_kernel_utility.hpp>
qiang.li authored
36
37
int64_t srs_gvid = getpid();
38
39
int64_t srs_generate_id()
40
{
41
    return srs_gvid++;
42 43 44 45
}

SrsStatisticVhost::SrsStatisticVhost()
{
46
    id = srs_generate_id();
47 48 49
    
    kbps = new SrsKbps();
    kbps->set_io(NULL, NULL);
50 51 52 53
}

SrsStatisticVhost::~SrsStatisticVhost()
{
54
    srs_freep(kbps);
55 56 57 58
}

SrsStatisticStream::SrsStatisticStream()
{
59
    id = srs_generate_id();
60
    vhost = NULL;
winlin authored
61
    status = STATISTIC_STREAM_STATUS_IDLING;
62 63 64
    
    has_video = false;
    vcodec = SrsCodecVideoReserved;
65 66
    avc_profile = SrsAvcProfileReserved;
    avc_level = SrsAvcLevelReserved;
67 68 69 70 71
    
    has_audio = false;
    acodec = SrsCodecAudioReserved1;
    asample_rate = SrsCodecAudioSampleRateReserved;
    asound_type = SrsCodecAudioSoundTypeReserved;
72
    aac_object = SrsAacObjectTypeReserved;
73 74 75
    
    kbps = new SrsKbps();
    kbps->set_io(NULL, NULL);
76 77 78 79
}

SrsStatisticStream::~SrsStatisticStream()
{
80
    srs_freep(kbps);
81 82
}
winlin authored
83 84 85 86 87
void SrsStatisticStream::publish()
{
    status = STATISTIC_STREAM_STATUS_PUBLISHING;
}
88 89 90 91
void SrsStatisticStream::close()
{
    has_video = false;
    has_audio = false;
winlin authored
92
    status = STATISTIC_STREAM_STATUS_IDLING;
93 94
}
winlin authored
95
SrsStatistic* SrsStatistic::_instance = new SrsStatistic();
qiang.li authored
96 97 98

SrsStatistic::SrsStatistic()
{
99
    _server_id = srs_generate_id();
100 101 102
    
    kbps = new SrsKbps();
    kbps->set_io(NULL, NULL);
qiang.li authored
103 104 105 106
}

SrsStatistic::~SrsStatistic()
{
107 108
    srs_freep(kbps);
    
winlin authored
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    if (true) {
        std::map<std::string, SrsStatisticVhost*>::iterator it;
        for (it = vhosts.begin(); it != vhosts.end(); it++) {
            SrsStatisticVhost* vhost = it->second;
            srs_freep(vhost);
        }
    }
    if (true) {
        std::map<std::string, SrsStatisticStream*>::iterator it;
        for (it = streams.begin(); it != streams.end(); it++) {
            SrsStatisticStream* stream = it->second;
            srs_freep(stream);
        }
    }
    if (true) {
        std::map<int, SrsStatisticClient*>::iterator it;
        for (it = clients.begin(); it != clients.end(); it++) {
            SrsStatisticClient* client = it->second;
            srs_freep(client);
        }
129 130 131 132 133 134
    }
}

SrsStatistic* SrsStatistic::instance()
{
    return _instance;
qiang.li authored
135 136
}
137
int SrsStatistic::on_video_info(SrsRequest* req, 
138
    SrsCodecVideo vcodec, SrsAvcProfile avc_profile, SrsAvcLevel avc_level
139
) {
winlin authored
140
    int ret = ERROR_SUCCESS;
141
    
142 143 144 145 146 147 148
    SrsStatisticVhost* vhost = create_vhost(req);
    SrsStatisticStream* stream = create_stream(vhost, req);

    stream->has_video = true;
    stream->vcodec = vcodec;
    stream->avc_profile = avc_profile;
    stream->avc_level = avc_level;
149
    
150 151 152 153 154
    return ret;
}

int SrsStatistic::on_audio_info(SrsRequest* req,
    SrsCodecAudio acodec, SrsCodecAudioSampleRate asample_rate, SrsCodecAudioSoundType asound_type,
155
    SrsAacObjectType aac_object
156 157
) {
    int ret = ERROR_SUCCESS;
158
    
159 160 161 162 163 164 165
    SrsStatisticVhost* vhost = create_vhost(req);
    SrsStatisticStream* stream = create_stream(vhost, req);

    stream->has_audio = true;
    stream->acodec = acodec;
    stream->asample_rate = asample_rate;
    stream->asound_type = asound_type;
166
    stream->aac_object = aac_object;
167 168 169 170
    
    return ret;
}
winlin authored
171 172 173 174 175 176 177 178
void SrsStatistic::on_stream_publish(SrsRequest* req)
{
    SrsStatisticVhost* vhost = create_vhost(req);
    SrsStatisticStream* stream = create_stream(vhost, req);

    stream->publish();
}
179 180 181 182 183 184 185 186 187 188 189 190 191 192
void SrsStatistic::on_stream_close(SrsRequest* req)
{
    SrsStatisticVhost* vhost = create_vhost(req);
    SrsStatisticStream* stream = create_stream(vhost, req);

    stream->close();
}

int SrsStatistic::on_client(int id, SrsRequest* req)
{
    int ret = ERROR_SUCCESS;
    
    SrsStatisticVhost* vhost = create_vhost(req);
    SrsStatisticStream* stream = create_stream(vhost, req);
193 194 195 196 197

    // create client if not exists
    SrsStatisticClient* client = NULL;
    if (clients.find(id) == clients.end()) {
        client = new SrsStatisticClient();
198
        client->id = id;
199 200 201 202 203 204 205 206 207
        client->stream = stream;
        clients[id] = client;
    } else {
        client = clients[id];
    }

    return ret;
}
208
void SrsStatistic::on_disconnect(int id)
209 210 211 212
{
    std::map<int, SrsStatisticClient*>::iterator it;
    it = clients.find(id);
    if (it != clients.end()) {
213 214 215
        SrsStatisticClient* client = it->second;
        srs_freep(client);
        clients.erase(it);
216 217 218
    }
}
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
void SrsStatistic::kbps_add_delta(SrsConnection* conn)
{
    int id = conn->srs_id();
    if (clients.find(id) == clients.end()) {
        return;
    }
    
    SrsStatisticClient* client = clients[id];
    
    // resample the kbps to collect the delta.
    conn->resample();
    
    // add delta of connection to kbps.
    // for next sample() of server kbps can get the stat.
    kbps->add_delta(conn);
    client->stream->kbps->add_delta(conn);
    client->stream->vhost->kbps->add_delta(conn);
    
    // cleanup the delta.
    conn->cleanup();
}

SrsKbps* SrsStatistic::kbps_sample()
{
    kbps->sample();
    if (true) {
        std::map<std::string, SrsStatisticVhost*>::iterator it;
        for (it = vhosts.begin(); it != vhosts.end(); it++) {
            SrsStatisticVhost* vhost = it->second;
            vhost->kbps->sample();
        }
    }
    if (true) {
        std::map<std::string, SrsStatisticStream*>::iterator it;
        for (it = streams.begin(); it != streams.end(); it++) {
            SrsStatisticStream* stream = it->second;
            stream->kbps->sample();
        }
    }
    
    return kbps;
}
262 263 264 265 266
int64_t SrsStatistic::server_id()
{
    return _server_id;
}
267
int SrsStatistic::dumps_vhosts(stringstream& ss)
qiang.li authored
268
{
winlin authored
269
    int ret = ERROR_SUCCESS;
270
271
    ss << SRS_JARRAY_START;
272 273 274
    std::map<std::string, SrsStatisticVhost*>::iterator it;
    for (it = vhosts.begin(); it != vhosts.end(); it++) {
        SrsStatisticVhost* vhost = it->second;
275
        if (it != vhosts.begin()) {
276
            ss << SRS_JFIELD_CONT;
277
        }
winlin authored
278 279 280
        
        // dumps the config of vhost.
        bool hls_enabled = _srs_config->get_hls_enabled(vhost->vhost);
281
282 283 284 285
        ss << SRS_JOBJECT_START
                << SRS_JFIELD_ORG("id", vhost->id) << SRS_JFIELD_CONT
                << SRS_JFIELD_STR("name", vhost->vhost) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("send_bytes", vhost->kbps->get_send_bytes()) << SRS_JFIELD_CONT
winlin authored
286 287 288 289 290
                << SRS_JFIELD_ORG("recv_bytes", vhost->kbps->get_recv_bytes()) << SRS_JFIELD_CONT
                << SRS_JFIELD_NAME("hls") << SRS_JOBJECT_START
                    << SRS_JFIELD_BOOL("enabled", hls_enabled) << SRS_JFIELD_CONT
                    << SRS_JFIELD_ORG("fragment", _srs_config->get_hls_fragment(vhost->vhost))
                << SRS_JOBJECT_END
291
            << SRS_JOBJECT_END;
292
    }
293
    ss << SRS_JARRAY_END;
294
winlin authored
295
    return ret;
qiang.li authored
296 297
}
298
int SrsStatistic::dumps_streams(stringstream& ss)
qiang.li authored
299
{
winlin authored
300
    int ret = ERROR_SUCCESS;
301
    
302
    ss << SRS_JARRAY_START;
303 304 305
    std::map<std::string, SrsStatisticStream*>::iterator it;
    for (it = streams.begin(); it != streams.end(); it++) {
        SrsStatisticStream* stream = it->second;
306
        if (it != streams.begin()) {
307
            ss << SRS_JFIELD_CONT;
308 309
        }
310 311 312 313 314 315 316 317 318
        int client_num = 0;
        std::map<int, SrsStatisticClient*>::iterator it_client;
        for (it_client = clients.begin(); it_client != clients.end(); it_client++) {
            SrsStatisticClient* client = it_client->second;
            if (client->stream == stream) {
                client_num++;
            }
        }
319 320 321 322
        ss << SRS_JOBJECT_START
                << SRS_JFIELD_ORG("id", stream->id) << SRS_JFIELD_CONT
                << SRS_JFIELD_STR("name", stream->stream) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("vhost", stream->vhost->id) << SRS_JFIELD_CONT
winlin authored
323
                << SRS_JFIELD_STR("app", stream->app) << SRS_JFIELD_CONT
324 325
                << SRS_JFIELD_ORG("clients", client_num) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("send_bytes", stream->kbps->get_send_bytes()) << SRS_JFIELD_CONT
winlin authored
326
                << SRS_JFIELD_ORG("recv_bytes", stream->kbps->get_recv_bytes()) << SRS_JFIELD_CONT
winlin authored
327 328
                << SRS_JFIELD_ORG("live_ms", srs_get_system_time_ms()) << SRS_JFIELD_CONT
                << SRS_JFIELD_STR("status", stream->status) << SRS_JFIELD_CONT;
winlin authored
329
        
330
        if (!stream->has_video) {
331
            ss  << SRS_JFIELD_NULL("video") << SRS_JFIELD_CONT;
332
        } else {
333 334 335 336 337 338 339
            ss  << SRS_JFIELD_NAME("video")
                    << SRS_JOBJECT_START
                        << SRS_JFIELD_STR("codec", srs_codec_video2str(stream->vcodec)) << SRS_JFIELD_CONT
                        << SRS_JFIELD_STR("profile", srs_codec_avc_profile2str(stream->avc_profile)) << SRS_JFIELD_CONT
                        << SRS_JFIELD_ORG("level", srs_codec_avc_level2str(stream->avc_level))
                    << SRS_JOBJECT_END
                << SRS_JFIELD_CONT;
340 341 342
        }
                
        if (!stream->has_audio) {
343
            ss  << SRS_JFIELD_NULL("audio");
344
        } else {
345 346 347 348 349 350 351
            ss  << SRS_JFIELD_NAME("audio")
                    << SRS_JOBJECT_START
                        << SRS_JFIELD_STR("codec", srs_codec_audio2str(stream->acodec)) << SRS_JFIELD_CONT
                        << SRS_JFIELD_ORG("sample_rate", (int)flv_sample_rates[stream->asample_rate]) << SRS_JFIELD_CONT
                        << SRS_JFIELD_ORG("channel", (int)stream->asound_type + 1) << SRS_JFIELD_CONT
                        << SRS_JFIELD_STR("profile", srs_codec_aac_object2str(stream->aac_object))
                    << SRS_JOBJECT_END;
352 353
        }
        
354
        ss << SRS_JOBJECT_END;
355
    }
356
    ss << SRS_JARRAY_END;
357
    
winlin authored
358 359
    return ret;
}
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

SrsStatisticVhost* SrsStatistic::create_vhost(SrsRequest* req)
{
    SrsStatisticVhost* vhost = NULL;
    
    // create vhost if not exists.
    if (vhosts.find(req->vhost) == vhosts.end()) {
        vhost = new SrsStatisticVhost();
        vhost->vhost = req->vhost;
        vhosts[req->vhost] = vhost;
        return vhost;
    }

    vhost = vhosts[req->vhost];
    
    return vhost;
}

SrsStatisticStream* SrsStatistic::create_stream(SrsStatisticVhost* vhost, SrsRequest* req)
{
    std::string url = req->get_stream_url();
    
    SrsStatisticStream* stream = NULL;
    
    // create stream if not exists.
    if (streams.find(url) == streams.end()) {
        stream = new SrsStatisticStream();
        stream->vhost = vhost;
        stream->stream = req->stream;
winlin authored
389
        stream->app = req->app;
390 391 392 393 394 395 396 397 398 399
        stream->url = url;
        streams[url] = stream;
        return stream;
    }
    
    stream = streams[url];
    
    return stream;
}