Blame view

trunk/research/librtmp/srs_flv_parser.c 5.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
The MIT License (MIT)

Copyright (c) 2013-2014 winlin

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

#include <stdio.h>
28
#include <string.h>
29 30 31 32 33 34 35 36 37
#include <stdlib.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "../../objs/include/srs_librtmp.h"
38
int parse_flv(srs_flv_t flv);
39 40 41 42 43 44 45
int main(int argc, char** argv)
{
    int ret = 0;
    
    // user options.
    char* in_flv_file;
    // flv handler
46
    srs_flv_t flv;
47
    
48 49 50 51
    printf("parse and show flv file detail.\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());
    
52 53 54 55 56
    if (argc <= 1) {
        printf("parse and show flv file detail\n"
            "Usage: %s in_flv_file\n"
            "   in_flv_file         flv file to parse and show.\n"
            "For example:\n"
57
            "   %s doc/source.200kbps.768x320.flv\n"
58
            "   %s ../../doc/source.200kbps.768x320.flv\n",
59 60
            argv[0], argv[0], argv[0]);
        exit(-1);
61 62 63
    }
    
    in_flv_file = argv[1];
64
    srs_human_trace("input:  %s", in_flv_file);
65
66
    if ((flv = srs_flv_open_read(in_flv_file)) == NULL) {
67
        ret = 2;
68
        srs_human_trace("open flv file failed. ret=%d", ret);
69 70 71
        return ret;
    }
    
72 73
    ret = parse_flv(flv);
    srs_flv_close(flv);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    
    return ret;
}

void digit_to_char(char* src, int ssize, char* dst, int dsize)
{
    int i, j;
    char v;
    
    for (i = 0, j = 0; i < ssize && j < dsize; i++) {
        if (j >= dsize) {
            break;
        }
        v = (src[i] >> 4) & 0x0F;
        if (v < 10) {
            dst[j++] = '0' + v;
        } else {
            dst[j++] = 'A' + (v - 10);
        }
        
        if (j >= dsize) {
            break;
        }
        v = src[i] & 0x0F;
        if (v < 10) {
            dst[j++] = '0' + v;
        } else {
            dst[j++] = 'A' + (v - 10);
        }
        
        if (j >= dsize) {
            break;
        }
        if (i < ssize - 1) {
            dst[j++] = ' ';
        }
    }
}

int parse_bytes(char* data, int size, char* hbuf, int hsize, char* tbuf, int tsize, int print_size)
{
    memset(hbuf, 0, hsize);
    memset(tbuf, 0, tsize);
117 118

    if (size > 0) {
119
        digit_to_char(data, size, hbuf, hsize - 1);
120 121 122
    }
    
    if (size > print_size * 2) {
123 124 125 126
        digit_to_char(data + size - print_size, size, tbuf, tsize - 1);
    }
}
127
int parse_flv(srs_flv_t flv)
128 129 130
{
    int ret = 0;
    
131 132
    // flv header
    char header[13];
133
    // packet data
134
    char type;
135 136
    u_int32_t timestamp = 0;
    char* data = NULL;
137
    int32_t size;
138
    int64_t offset = 0;
139
    
140 141 142 143
    if ((ret = srs_flv_read_header(flv, header)) != 0) {
        return ret;
    }
    
144
    srs_human_trace("start parse flv");
145
    for (;;) {
146
        offset = srs_flv_tellg(flv);
147
        
148 149 150
        // tag header
        if ((ret = srs_flv_read_tag_header(flv, &type, &size, &timestamp)) != 0) {
            if (srs_flv_is_eof(ret)) {
151
                srs_human_trace("parse completed.");
152 153
                return 0;
            }
154
            srs_human_trace("flv get packet failed. ret=%d", ret);
155 156 157
            return ret;
        }
        
158
        if (size <= 0) {
159
            srs_human_trace("invalid size=%d", size);
160 161 162 163 164
            break;
        }
        
        data = (char*)malloc(size);
        
165
        if ((ret = srs_flv_read_tag_data(flv, data, size)) == 0) {
166
            if ((ret = srs_human_print_rtmp_packet(type, timestamp, data, size)) == 0) {
167 168
                char hbuf[48]; char tbuf[48];
                parse_bytes(data, size, hbuf, sizeof(hbuf), tbuf, sizeof(tbuf), 16);
169
                srs_human_raw("offset=%d, first and last 16 bytes:\n"
170
                    "[+00, +15] %s\n[-15, EOF] %s\n", (int)offset, hbuf, tbuf);
171
            } else {
172
                srs_human_trace("print packet failed. ret=%d", ret);
173
            }
174
        } else {
175
            srs_human_trace("read flv failed. ret=%d", ret);
176 177 178
        }
        
        free(data);
179 180
        
        if (ret != 0) {
181
            srs_human_trace("parse failed, ret=%d", ret);
182 183
            return ret;
        }
184 185 186 187
    }
    
    return ret;
}