Blame view

trunk/research/librtmp/srs_rtmp_dump.c 10.7 KB
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

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.
*/
/**
gcc srs_rtmp_dump.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_rtmp_dump
*/

#include <stdio.h>
#include <stdlib.h>
29 30 31
#include <unistd.h>
#include <getopt.h>
#include <assert.h>
32 33 34

#include "../../objs/include/srs_librtmp.h"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
void parse_amf0_object(char* p, srs_amf0_t args)
{
    char opvt = 0; // object property value type.
    const char* opnp = NULL; // object property name ptr.
    const char* opvp = NULL; // object property value ptr.
    
    while (*p) {
        switch (*p++) {
            case 'O':
                while (*p && *p++ != ':') {
                }
                if (*p++ == '1') {
                    printf("amf0 object start\n");
                } else {
                    printf("amf0 object end\n");
                }
                break;
            case 'N':
                opvt = *p++;
                if (*p++ != ':') {
                    printf("object property must split by :.\n");
                    exit(-1);
                }
                opnp = p++;
                while (*p && *p++ != ':') {
                }
                p[-1] = 0;
                opvp = p;
                printf("amf0 %c property[%s]=%s\n", opvt, opnp, opvp);
                switch(opvt) {
                    case 'S':
                        srs_amf0_object_property_set(args, opnp, srs_amf0_create_string(opvp));
                        break;
                    default:
                        printf("unsupported object property.\n");
                        exit(-1);
                }
                *p=0;
                break;
            default:
                printf("only supports an object arg.\n");
                exit(-1);
        }
    }
}
81 82
int main(int argc, char** argv)
{
83 84 85
    srs_flv_t flv = NULL;
    srs_rtmp_t rtmp = NULL;
    
86 87 88
    printf("dump rtmp stream to flv file\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());
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    printf("@refer to http://rtmpdump.mplayerhq.hu/rtmpdump.1.html\n");
    
    struct option long_options[] = {
        {"rtmp", required_argument, 0, 'r'},
        {"flv", required_argument, 0, 'o'},
        {"swfUrl", required_argument, 0, 's'},
        {"tcUrl", required_argument, 0, 't'},
        {"pageUrl", required_argument, 0, 'p'},
        {"conn", required_argument, 0, 'C'},
        {"complex", no_argument, 0, 'x'},
        {"help", no_argument, 0, 'h'},
        {0, 0, 0, 0}
    };
    
    int show_help = 0;
    int complex_handshake = 0;
    const char* rtmp_url = NULL;
    const char* output_flv = NULL;
    const char* swfUrl = NULL;
    const char* tcUrl = NULL;
    const char* pageUrl = NULL;
    srs_amf0_t args = NULL;
111
    
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    int opt = 0;
    int option_index = 0;
    while((opt = getopt_long(argc, argv, "hxr:o:s:t:p:C:", long_options, &option_index)) != -1){
        switch(opt){
            case 'r':
                rtmp_url = optarg;
                break;
            case 'o':
                output_flv = optarg;
                break;
            case 's':
                swfUrl = optarg;
                break;
            case 't':
                tcUrl = optarg;
                break;
            case 'p':
                pageUrl = optarg;
                break;
            case 'C':
                if (!args) {
                    args = srs_amf0_create_object();
                }
                char* p = (char*)optarg;
                parse_amf0_object(p, args);
                break;
            case 'x':
                complex_handshake = 1;
                break;
            case 'h':
                show_help = 1;
                break;
            default:
                printf("unsupported opt.\n");
                exit(-1);
        }
    }
    
    if (!rtmp_url || show_help) {
        printf("Usage: %s -r url [-o output] [-s swfUrl] [-t tcUrl] [-p pageUrl] [-C conndata] [--complex] [-h]\n"
            "Options:\n"
            "   --rtmp -r url\n"
            "       URL of the server and media content.\n"
            "   --flv -o output\n"
            "       Specify the output file name. If the name is − or is omitted, the stream is written to stdout.\n"
            "   --complex\n"
            "       Whether use complex handshake(srs-librtmp with ssl required).\n"
            "   --swfUrl -s url\n"
            "       URL of the SWF player for the media. By default no value will be sent.\n"
            "   --tcUrl -t url\n"
            "       URL of the target stream. Defaults to rtmp[e]://host[:port]/app/playpath.\n"
            "   --pageUrl -p url\n"
            "       URL of the web page in which the media was embedded. By default no value will be sent.\n"
            "   −−conn −C type:data\n"
            "       Append arbitrary AMF data to the Connect message. The type must be B for Boolean, N for number, S for string, O for object, or Z for null. For Booleans the data must be either 0 or 1 for FALSE or TRUE, respectively. Likewise for Objects the data must be 0 or 1 to end or begin an object, respectively. Data items in subobjects may be named, by prefixing the type with 'N' and specifying the name before the value, e.g. NB:myFlag:1. This option may be used multiple times to construct arbitrary AMF sequences. E.g.\n"
            "       −C B:1 −C S:authMe −C O:1 −C NN:code:1.23 −C NS:flag:ok −C O:0\n"
            "       -C O:1 -C NS:CONN:\" -C B:4Rg9vr0\" -C O:0\n"
            "       @remark, support a object args only.\n"
            "   --help -h\n"
            "       Print a summary of command options.\n"
172
            "For example:\n"
173 174 175
            "   %s -r rtmp://127.0.0.1:1935/live/livestream -o output.flv\n"
            "   %s -h\n",
            argv[0], argv[0], argv[0]);
176 177 178
        exit(-1);
    }
    
179 180 181 182 183 184 185 186 187 188 189
    srs_human_trace("rtmp url: %s", rtmp_url);
    srs_human_trace("handshake: %s", (complex_handshake? "complex" : "simple"));
    srs_human_trace("swfUrl: %s", swfUrl);
    srs_human_trace("pageUrl: %s", pageUrl);
    srs_human_trace("tcUrl: %s", tcUrl);
    if (output_flv) {
        srs_human_trace("flv output path: %s", output_flv);
    } else {
        srs_human_trace("output to console");
    }
    
190
    rtmp = srs_rtmp_create(rtmp_url);
191
    
192
    if (srs_rtmp_dns_resolve(rtmp) != 0) {
193 194 195 196
        srs_human_trace("dns resolve failed.");
        goto rtmp_destroy;
    }
    
197
    if (srs_rtmp_connect_server(rtmp) != 0) {
198 199 200 201 202
        srs_human_trace("connect to server failed.");
        goto rtmp_destroy;
    }
    
    if (complex_handshake) {
203
        if (srs_rtmp_do_complex_handshake(rtmp) != 0) {
204 205 206 207 208
            srs_human_trace("complex handshake failed.");
            goto rtmp_destroy;
        }
        srs_human_trace("do complex handshake success");
    } else {
209
        if (srs_rtmp_do_simple_handshake(rtmp) != 0) {
210 211 212 213 214
            srs_human_trace("simple handshake failed.");
            goto rtmp_destroy;
        }
        srs_human_trace("do simple handshake success");
    }
215
    
216 217
    if (srs_rtmp_set_connect_args(rtmp, tcUrl, swfUrl, pageUrl, args) != 0) {
        srs_human_trace("set connect args failed.");
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        goto rtmp_destroy;
    }
    
    if (srs_rtmp_connect_app(rtmp) != 0) {
        srs_human_trace("connect vhost/app failed.");
        goto rtmp_destroy;
    }
    srs_human_trace("connect vhost/app success");
    
    if (srs_rtmp_play_stream(rtmp) != 0) {
        srs_human_trace("play stream failed.");
        goto rtmp_destroy;
    }
    srs_human_trace("play stream success");
    
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    if (output_flv) {
        flv = srs_flv_open_write(output_flv);
    }
    
    if (flv) {
        // flv header
        char header[9];
        // 3bytes, signature, "FLV",
        header[0] = 'F';
        header[1] = 'L';
        header[2] = 'V';
        // 1bytes, version, 0x01,
        header[3] = 0x01;
        // 1bytes, flags, UB[5] 0, UB[1] audio present, UB[1] 0, UB[1] video present.
        header[4] = 0x03; // audio + video.
        // 4bytes, dataoffset
        header[5] = 0x00;
        header[6] = 0x00;
        header[7] = 0x00;
        header[8] = 0x09;
        if (srs_flv_write_header(flv, header) != 0) {
            srs_human_trace("write flv header failed.");
            goto rtmp_destroy;
        }
257 258
    }
    
winlin authored
259
    u_int32_t pre_timestamp = 0;
260 261 262 263 264 265 266 267 268 269 270
    for (;;) {
        int size;
        char type;
        char* data;
        u_int32_t timestamp;
        
        if (srs_rtmp_read_packet(rtmp, &type, &timestamp, &data, &size) != 0) {
            srs_human_trace("read rtmp packet failed.");
            goto rtmp_destroy;
        }
        
winlin authored
271
        if (srs_human_print_rtmp_packet2(type, timestamp, data, size, pre_timestamp) != 0) {
272 273 274
            srs_human_trace("print rtmp packet failed.");
            goto rtmp_destroy;
        }
winlin authored
275
        pre_timestamp = timestamp;
276
        
277 278 279
        // we only write some types of messages to flv file.
        int is_flv_msg = type == SRS_RTMP_TYPE_AUDIO
            || type == SRS_RTMP_TYPE_VIDEO || type == SRS_RTMP_TYPE_SCRIPT;
280 281 282 283 284 285 286
            
        // for script data, ignore except onMetaData
        if (type == SRS_RTMP_TYPE_SCRIPT) {
            if (!srs_rtmp_is_onMetaData(type, data, size)) {
                is_flv_msg = 0;
            }
        }
287
288
        if (flv) {
289 290 291 292 293 294
            if (is_flv_msg) {
                if (srs_flv_write_tag(flv, type, timestamp, data, size) != 0) {
                    srs_human_trace("dump rtmp packet failed.");
                    goto rtmp_destroy;
                }
            } else {
295
                srs_human_trace("drop message type=%#x, size=%dB", type, size);
296
            }
297 298 299 300 301 302 303
        }
        
        free(data);
    }
    
rtmp_destroy:
    srs_rtmp_destroy(rtmp);
304
    srs_flv_close(flv);
305 306 307 308
    srs_human_trace("completed");
    
    return 0;
}