Blame view

trunk/research/librtmp/srs_flv_parser.c 7.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 28 29 30 31 32 33 34 35 36 37
/*
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>
#include <stdlib.h>
#include <unistd.h>

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

#include "../../objs/include/srs_librtmp.h"
#include "srs_research_public.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 52 53
    
    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"
            "   %s ../../doc/source.200kbps.768x320.flv\n",
winlin authored
54
            argv[0], argv[0]);
55 56 57 58 59 60 61 62 63 64 65 66
        ret = 1;
        exit(ret);
        return ret;
    }
    
    in_flv_file = argv[1];
    
    trace("parse and show flv file detail.");
    trace("srs(simple-rtmp-server) client librtmp library.");
    trace("version: %d.%d.%d", srs_version_major(), srs_version_minor(), srs_version_revision());
    trace("input:  %s", in_flv_file);
67
    if ((flv = srs_flv_open_read(in_flv_file)) == NULL) {
68 69 70 71 72
        ret = 2;
        trace("open flv file failed. ret=%d", ret);
        return ret;
    }
    
73 74
    ret = parse_flv(flv);
    srs_flv_close(flv);
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 117
    
    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);
118 119

    if (size > 0) {
120
        digit_to_char(data, size, hbuf, hsize - 1);
121 122 123
    }
    
    if (size > print_size * 2) {
124 125 126 127
        digit_to_char(data + size - print_size, size, tbuf, tsize - 1);
    }
}
128 129
#define FLV_HEADER_SIZE 11
int parse_script_data(u_int32_t timestamp, char* data, int size, int64_t offset)
130 131 132 133 134
{
    int ret = 0;
    
    char hbuf[48];
    char tbuf[48];
135 136 137 138 139 140 141 142 143 144 145
    
    int amf0_size = 0;
    int nparsed = 0;
    
    srs_amf0_t amf0_name;
    char* amf0_name_str = NULL;
    
    srs_amf0_t amf0_data;
    char* amf0_data_str = NULL;
    
    // bytes
146 147
    parse_bytes(data, size, hbuf, sizeof(hbuf), tbuf, sizeof(tbuf), 16);
    
148 149 150 151
    // amf0
    amf0_name = srs_amf0_parse(data, size, &nparsed);
    if (amf0_name == NULL || nparsed >= size) {
        trace("invalid amf0 name data.");
152 153
        return -1;
    }
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    amf0_data = srs_amf0_parse(data + nparsed, size - nparsed, &nparsed);
    
    trace("packet type=%s, time=%d, size=%d, data-size=%d, \n"
        "offset=%d\n[+00, +15] %s\n[-15, EOF] %s\n%s%s", 
        srs_type2string(SRS_RTMP_TYPE_SCRIPT), timestamp, size + FLV_HEADER_SIZE, size, 
        (int)offset, hbuf, tbuf, 
        srs_amf0_human_print(amf0_name, &amf0_name_str, &amf0_size), 
        srs_amf0_human_print(amf0_data, &amf0_data_str, &amf0_size));
    
    srs_amf0_free(amf0_name);
    srs_amf0_free_bytes(amf0_name_str);
    
    srs_amf0_free(amf0_data);
    srs_amf0_free_bytes(amf0_data_str);

    return ret;
}

int parse_audio_data(u_int32_t timestamp, char* data, int size, int64_t offset)
{
    int ret = 0;
    
    char hbuf[48];
    char tbuf[48];
    
    // bytes
    parse_bytes(data, size, hbuf, sizeof(hbuf), tbuf, sizeof(tbuf), 16);
    
    trace("packet type=%s, time=%d, size=%d, data-size=%d, \n"
        "offset=%d\n[+00, +15] %s\n[-15, EOF] %s\n", 
        srs_type2string(SRS_RTMP_TYPE_AUDIO), timestamp, size + FLV_HEADER_SIZE, size, 
        (int)offset, hbuf, tbuf);
186
    
187 188
    return ret;
}
189
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
int parse_video_data(u_int32_t timestamp, char* data, int size, int64_t offset)
{
    int ret = 0;
    
    char hbuf[48];
    char tbuf[48];
    
    // bytes
    parse_bytes(data, size, hbuf, sizeof(hbuf), tbuf, sizeof(tbuf), 16);
    
    trace("packet type=%s, time=%d, size=%d, data-size=%d, \n"
        "offset=%d\n[+00, +15] %s\n[-15, EOF] %s\n", 
        srs_type2string(SRS_RTMP_TYPE_VIDEO), timestamp, size + FLV_HEADER_SIZE, size, 
        (int)offset, hbuf, tbuf);
        
205 206 207
    return ret;
}
208
int parse_flv(srs_flv_t flv)
209 210 211
{
    int ret = 0;
    
212 213
    // flv header
    char header[13];
214
    // packet data
215
    char type;
216 217
    u_int32_t timestamp = 0;
    char* data = NULL;
218
    int32_t size;
219
    int64_t offset = 0;
220
    
221 222 223 224
    if ((ret = srs_flv_read_header(flv, header)) != 0) {
        return ret;
    }
    
225 226
    trace("start parse flv");
    for (;;) {
227
        offset = srs_flv_tellg(flv);
228
        
229 230 231
        // tag header
        if ((ret = srs_flv_read_tag_header(flv, &type, &size, &timestamp)) != 0) {
            if (srs_flv_is_eof(ret)) {
232 233 234
                trace("parse completed.");
                return 0;
            }
winlin authored
235
            trace("flv get packet failed. ret=%d", ret);
236 237 238
            return ret;
        }
        
239 240 241 242 243 244 245 246 247 248
        if (size <= 0) {
            trace("invalid size=%d", size);
            break;
        }
        
        data = (char*)malloc(size);
        if ((ret = srs_flv_read_tag_data(flv, data, size)) != 0) {
            return ret;
        }
        
249 250
        // data tag
        if (type == SRS_RTMP_TYPE_AUDIO) {
251
            if ((ret = parse_audio_data(timestamp, data, size, offset)) != 0) {
252 253 254
                return ret;
            }
        } else if (type == SRS_RTMP_TYPE_VIDEO) {
255
            if ((ret = parse_video_data(timestamp, data, size, offset)) != 0) {
256 257 258
                return ret;
            }
        } else {
259
            if ((ret = parse_script_data(timestamp, data, size, offset)) != 0) {
260 261 262 263 264 265 266 267 268
                return ret;
            }
        }
        
        free(data);
    }
    
    return ret;
}