Blame view

trunk/research/librtmp/srs_bandwidth_check.c 4.9 KB
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2015 winlin
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

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.
*/
/**
24
gcc srs_bandwidth_check.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_bandwidth_check
25 26 27 28 29 30 31 32 33 34 35 36 37
*/

#include <stdio.h>
#include <stdlib.h>

#include "../../objs/include/srs_librtmp.h"

int main(int argc, char** argv)
{
    int ret = 0;
    srs_rtmp_t rtmp;
    
    // packet data
38 39
    int size;
    char type;
40
    char* data;
41
    u_int32_t timestamp;
42 43
    
    // srs debug info.
44
    char srs_server_ip[128];
45
    char srs_server[128];
46 47
    char srs_primary[128];
    char srs_authors[128];
48 49 50
    char srs_version[32];
    int srs_id = 0;
    int srs_pid = 0;
51
    // bandwidth test data.
52 53 54 55 56 57 58 59 60 61 62 63
    int64_t start_time = 0;
    int64_t end_time = 0;
    int play_kbps = 0;
    int publish_kbps = 0;
    int play_bytes = 0;
    int publish_bytes = 0;
    int play_duration = 0;
    int publish_duration = 0;
    
    // set to zero.
    srs_server_ip[0] = 0;
    srs_server[0] = 0;
64 65
    srs_primary[0] = 0;
    srs_authors[0] = 0;
66
    srs_version[0] = 0;
67
    
68 69 70 71
    printf("RTMP bandwidth check/test with server.\n");
    printf("srs(simple-rtmp-server) client librtmp library.\n");
    printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision());
    
72 73 74
    if (argc <= 1) {
        printf("RTMP bandwidth check/test with server.\n"
            "Usage: %s <rtmp_url>\n"
winlin authored
75
            "   rtmp_url     RTMP bandwidth url to check. format: rtmp://server:port/app?key=xxx,vhost=xxx\n"
76 77
            "For example:\n"
            "   %s rtmp://127.0.0.1:1935/app?key=35c9b402c12a7246868752e2878f7e0e,vhost=bandcheck.srs.com\n"
78
            "   %s rtmp://127.0.0.1:1935/app?key=35c9b402c12a7246868752e2878f7e0e,vhost=bandcheck.srs.com>/dev/null\n"
79
            "@remark, output text to stdout, while json to stderr.\n",
80
            argv[0], argv[0], argv[0]);
81
        exit(-1);
82 83 84 85
    }
    
    rtmp = srs_rtmp_create2(argv[1]);
    
86
    srs_human_trace("bandwidth check/test url: %s", argv[1]);
87
    
88
    if ((ret = srs_rtmp_handshake(rtmp)) != 0) {
89
        srs_human_trace("simple handshake failed.");
90 91
        goto rtmp_destroy;
    }
92
    srs_human_trace("simple handshake success");
93
    
94
    if ((ret = srs_rtmp_connect_app2(rtmp, 
95 96
        srs_server_ip, srs_server, srs_primary, srs_authors, srs_version, 
        &srs_id, &srs_pid)) != 0) {
97
        srs_human_trace("connect vhost/app failed.");
98 99
        goto rtmp_destroy;
    }
100
    srs_human_trace("connect vhost/app success");
101
    
102
    if ((ret = srs_rtmp_bandwidth_check(rtmp, 
103 104 105
        &start_time, &end_time, &play_kbps, &publish_kbps,
        &play_bytes, &publish_bytes, &play_duration, &publish_duration)) != 0
    ) {
106
        srs_human_trace("bandwidth check/test failed.");
107 108
        goto rtmp_destroy;
    }
109
    srs_human_trace("bandwidth check/test success");
110
    
111
    srs_human_trace("\n%s, %s, %s\n"
112
        "%s, %s, srs_pid=%d, srs_id=%d\n"
113 114
        "duration: %dms(%d+%d)\n"
        "play: %dkbps\n"
115
        "publish: %dkbps", 
116
        (char*)srs_server, (char*)srs_primary, (char*)srs_authors,
117
        (char*)srs_server_ip, (char*)srs_version, srs_pid, srs_id,
118 119 120 121 122 123 124
        (int)(end_time - start_time), play_duration, publish_duration,
        play_kbps, 
        publish_kbps);
    
rtmp_destroy:
    srs_rtmp_destroy(rtmp);
    
125 126
    fprintf(stderr, "{\"code\":%d,"
        "\"srs_server\":\"%s\", "
127 128
        "\"srs_primary\":\"%s\", "
        "\"srs_authors\":\"%s\", "
129 130 131 132 133 134
        "\"srs_server_ip\":\"%s\", "
        "\"srs_version\":\"%s\", "
        "\"srs_pid\":%d, "
        "\"srs_id\":%d, "
        "\"duration\":%d, "
        "\"play_duration\":%d, "
135
        "\"publish_duration\":%d,"
136 137 138 139
        "\"play_kbps\":%d, "
        "\"publish_kbps\":%d"
        "}",
        ret,
140
        (char*)srs_server, (char*)srs_primary, (char*)srs_authors,
141 142 143
        (char*)srs_server_ip, (char*)srs_version, srs_pid, srs_id,
        (int)(end_time - start_time), play_duration, publish_duration,
        play_kbps, publish_kbps);
144
    
145
    srs_human_trace(" ");
146
    srs_human_trace("completed");
147
    
148 149
    return ret;
}