Blame view

trunk/research/librtmp/srs_flv_injecter.c 11.9 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
/*
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_flv_injecter.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_flv_injecter
*/

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

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

#include "../../objs/include/srs_librtmp.h"
winlin authored
37 38

#define ERROR_INJECTED 10000
39 40 41 42 43 44 45 46 47 48 49 50 51 52

int process(const char* in_flv_file, const char* out_flv_file, srs_flv_t* pic, srs_flv_t* poc);
int main(int argc, char** argv)
{
    int ret = 0;
    
    // user options.
    char* in_flv_file;
    char* out_flv_file;
    // flv handler
    srs_flv_t ic = NULL;
    srs_flv_t oc = NULL;
    
    // temp variables.
winlin authored
53
    int tmp_file_size = 0;
54
    char* tmp_file;
55 56 57 58

    printf("inject flv file keyframes to metadata.\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());
59 60 61 62 63 64 65
    
    if (argc <= 2) {
        printf("inject flv file keyframes to metadata\n"
            "Usage: %s in_flv_file out_flv_file\n"
            "   in_flv_file         input flv file to inject.\n"
            "   out_flv_file        the inject output file, can be in_flv_file.\n"
            "For example:\n"
66
            "   %s doc/source.200kbps.768x320.flv injected.flv\n"
67
            "   %s ../../doc/source.200kbps.768x320.flv injected.flv\n",
68 69
            argv[0], argv[0], argv[0]);
        exit(-1);
70 71 72 73 74
    }
    
    in_flv_file = argv[1];
    out_flv_file = argv[2];
    
winlin authored
75 76 77
    tmp_file_size = strlen(out_flv_file) + strlen(".tmp") + 1;
    tmp_file = (char*)malloc(tmp_file_size);
    snprintf(tmp_file, tmp_file_size, "%s.tmp", out_flv_file);
78
    
79 80 81
    srs_human_trace("input:  %s", in_flv_file);
    srs_human_trace("output:  %s", out_flv_file);
    srs_human_trace("tmp_file:  %s", tmp_file);
82
winlin authored
83
    ret = process(in_flv_file, tmp_file, &ic, &oc);
84 85 86
    
    srs_flv_close(ic);
    srs_flv_close(oc);
winlin authored
87 88 89
    
    if (ret != 0) {
        unlink(tmp_file);
winlin authored
90 91
        if (ret == ERROR_INJECTED) {
            ret = 0;
92
            srs_human_trace("file already injected.");
winlin authored
93
        } else {
94
            srs_human_trace("error, remove tmp file.");
winlin authored
95
        }
winlin authored
96 97
    } else {
        rename(tmp_file, out_flv_file);
98
        srs_human_trace("completed, rename to %s", out_flv_file);
winlin authored
99 100
    }
    
101 102 103 104 105 106 107 108 109
    free(tmp_file);
    
    return ret;
}

int process(const char* in_flv_file, const char* out_flv_file, srs_flv_t* pic, srs_flv_t* poc)
{
    int ret = 0;
    
110 111 112 113 114 115 116 117 118 119 120 121 122 123
    srs_flv_t ic;
    srs_flv_t oc;
    
    // to adjust metadata.
    // the ic metadata end offset, the next tag start offset.
    // all oc metadata must adjust according to:
    //      adjust = new_metadata_end_offset - metadata_end_offset
    int64_t metadata_end_offset = 0;
    
    // metadata
    srs_amf0_t amf0_name = NULL;
    srs_amf0_t amf0_data = NULL;
    srs_amf0_t filepositions = NULL;
    
124
    if ((ic = srs_flv_open_read(in_flv_file)) == NULL) {
125
        ret = 2;
126
        srs_human_trace("open input flv file failed. ret=%d", ret);
127 128
        return ret;
    }
129
    *pic = ic;
130
    
131
    if ((oc = srs_flv_open_write(out_flv_file)) == NULL) {
132
        ret = 2;
133
        srs_human_trace("open output flv file failed. ret=%d", ret);
134 135
        return ret;
    }
136 137 138 139 140 141 142 143 144 145
    *poc = oc;
    
    /**
    * we use two roundtrip to avoid the paddings of metadata,
    * to support large keyframes videos without padding fields.
    */
    // build keyframes offset to metadata.
    if ((ret = build_keyframes(ic, &amf0_name, &amf0_data, &filepositions, &metadata_end_offset)) != 0) {
        return ret;
    }
146
    
147 148
    // inject the metadata to oc.
    if ((ret = do_inject_flv(ic, oc, amf0_name, amf0_data, filepositions, metadata_end_offset)) != 0) {
149 150 151
        return ret;
    }
    
152 153 154 155
    // TODO: FIXME: mem leak when error.
    srs_amf0_free(amf0_name);
    srs_amf0_free(amf0_data);
    
156 157 158
    return ret;
}
winlin authored
159 160 161 162 163 164 165 166
int parse_metadata(char* data, int size, srs_amf0_t* pname, srs_amf0_t* pdata)
{
    int ret = 0;
    
    int nparsed = 0;
    *pname = srs_amf0_parse(data, size, &nparsed);
    
    if (*pname == NULL || nparsed >= size) {
167
        srs_human_trace("invalid amf0 name data.");
winlin authored
168 169 170 171 172
        return -1;
    }
    
    *pdata = srs_amf0_parse(data + nparsed, size - nparsed, &nparsed);
    if (*pdata == NULL || nparsed > size) {
173
        srs_human_trace("invalid amf0 value data");
winlin authored
174 175 176 177 178 179
        return -1;
    }
    
    return ret;
}
180
int build_keyframes(srs_flv_t ic, srs_amf0_t *pname, srs_amf0_t* pdata, srs_amf0_t* pfilepositions, int64_t* pmetadata_end_offset)
181 182 183
{
    int ret = 0;
    
winlin authored
184 185
    // flv header
    char header[13];
186
    
winlin authored
187 188 189 190 191
    // packet data
    char type;
    u_int32_t timestamp = 0;
    char* data = NULL;
    int32_t size;
winlin authored
192
    int64_t offset = 0;
winlin authored
193 194 195 196
    
    // metadata
    srs_amf0_t amf0_name = NULL;
    srs_amf0_t amf0_data = NULL;
197
    
winlin authored
198 199 200
    srs_amf0_t keyframes = NULL;
    srs_amf0_t filepositions = NULL;
    srs_amf0_t times = NULL;
winlin authored
201 202 203 204
    
    // reset to generate metadata
    srs_flv_lseek(ic, 0);
    
winlin authored
205
    if ((ret = srs_flv_read_header(ic, header)) != 0) {
winlin authored
206 207 208
        return ret;
    }
    
209
    srs_human_trace("build keyframe infos from flv");
winlin authored
210
    for (;;) {
winlin authored
211 212
        offset = srs_flv_tellg(ic);
        
winlin authored
213
        // tag header
winlin authored
214
        if ((ret = srs_flv_read_tag_header(ic, &type, &size, &timestamp)) != 0) {
winlin authored
215
            if (srs_flv_is_eof(ret)) {
216
                srs_human_trace("parse completed.");
217
                return 0;
winlin authored
218
            }
219
            srs_human_trace("flv get packet failed. ret=%d", ret);
winlin authored
220 221 222 223
            return ret;
        }
        
        if (size <= 0) {
224
            srs_human_trace("invalid size=%d", size);
225
            return ret;
winlin authored
226 227 228 229
        }
        
        // TODO: FIXME: mem leak when error.
        data = (char*)malloc(size);
winlin authored
230
        if ((ret = srs_flv_read_tag_data(ic, data, size)) != 0) {
winlin authored
231 232 233 234 235
            return ret;
        }
        
        // data tag
        if (type == SRS_RTMP_TYPE_VIDEO) {
winlin authored
236 237 238 239
            if (!srs_flv_is_sequence_header(data, size) && srs_flv_is_keyframe(data, size)) {
                srs_amf0_strict_array_append(filepositions, srs_amf0_create_number(offset));
                srs_amf0_strict_array_append(times, srs_amf0_create_number(((double)timestamp)/ 1000));
            }
winlin authored
240
        } else if (type == SRS_RTMP_TYPE_SCRIPT) {
241
            *pmetadata_end_offset = srs_flv_tellg(ic);
winlin authored
242 243 244
            if ((ret = parse_metadata(data, size, &amf0_name, &amf0_data)) != 0) {
                return ret;
            }
winlin authored
245
            
246 247 248
            *pname = amf0_name;
            *pdata = amf0_data;
            
winlin authored
249 250
            if (srs_amf0_is_object(amf0_data)) {
                keyframes = srs_amf0_object_property(amf0_data, "keyframes");
251
                if (keyframes == NULL) {
winlin authored
252 253
                    keyframes = srs_amf0_create_object();
                    srs_amf0_object_property_set(amf0_data, "keyframes", keyframes);
winlin authored
254
                }
255
                // always clear the old keyframes.
winlin authored
256
                srs_amf0_object_clear(keyframes);
257 258
                
                *pfilepositions = filepositions = srs_amf0_create_strict_array();
winlin authored
259
                srs_amf0_object_property_set(keyframes, "filepositions", filepositions);
260
                
winlin authored
261 262 263 264
                times = srs_amf0_create_strict_array();
                srs_amf0_object_property_set(keyframes, "times", times);
            } else if (srs_amf0_is_ecma_array(amf0_data)) {
                keyframes = srs_amf0_ecma_array_property(amf0_data, "keyframes");
265
                if (keyframes == NULL) {
winlin authored
266 267
                    keyframes = srs_amf0_create_object();
                    srs_amf0_ecma_array_property_set(amf0_data, "keyframes", keyframes);
winlin authored
268
                }
269
                // always clear the old keyframes.
winlin authored
270
                srs_amf0_object_clear(keyframes);
271 272
                
                *pfilepositions = filepositions = srs_amf0_create_strict_array();
winlin authored
273
                srs_amf0_object_property_set(keyframes, "filepositions", filepositions);
274
                
winlin authored
275
                times = srs_amf0_create_strict_array();
winlin authored
276
                srs_amf0_object_property_set(keyframes, "times", times);
winlin authored
277 278 279 280 281 282
            }
        }
        
        free(data);
    }
    
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    return ret;
}

int do_inject_flv(srs_flv_t ic, srs_flv_t oc, srs_amf0_t amf0_name, srs_amf0_t amf0_data, srs_amf0_t filepositions, int64_t metadata_end_offset)
{
    int ret = 0;
    
    // flv header
    char header[13];
    // packet data
    char type;
    u_int32_t timestamp = 0;
    char* data = NULL;
    int32_t size;
    
    // metadata
    srs_amf0_t fileposition = NULL;
    int amf0_name_size = 0;
    int i;
    
    // the metadata end offset, the next tag start offset.
    int64_t new_metadata_end_offset = 0;
    int offset_adjust = 0;
    
winlin authored
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    // reset to write injected file
    srs_flv_lseek(ic, 0);
    
    if ((ret = srs_flv_read_header(ic, header)) != 0) {
        return ret;
    }
    
    if ((ret = srs_flv_write_header(oc, header)) != 0) {
        return ret;
    }
    
    // write metadata
    if (amf0_name != NULL && amf0_data != NULL) {
        amf0_name_size = srs_amf0_size(amf0_name);
        size = amf0_name_size + srs_amf0_size(amf0_data);
322 323 324 325 326 327 328 329 330 331

        // adjust all offset of keyframes.
        new_metadata_end_offset = srs_flv_tellg(oc) + srs_flv_size_tag(size);
        // the adjust is new offset sub the old offset of metadata end.
        offset_adjust = new_metadata_end_offset - metadata_end_offset;
        for (i = 0; i < srs_amf0_strict_array_property_count(filepositions); i++) {
            fileposition = srs_amf0_strict_array_property_at(filepositions, i);
            srs_amf0_set_number(fileposition, srs_amf0_to_number(fileposition) + offset_adjust);
        }
        
winlin authored
332
        data = (char*)malloc(size);
333
        memset(data, 0, size);
winlin authored
334 335 336 337 338 339 340 341 342 343 344
        if ((ret = srs_amf0_serialize(amf0_name, data, amf0_name_size)) != 0) {
            return ret;
        }
        if ((ret = srs_amf0_serialize(amf0_data, data + amf0_name_size, size - amf0_name_size)) != 0) {
            return ret;
        }
        if ((ret = srs_flv_write_tag(oc, SRS_RTMP_TYPE_SCRIPT, 0, data, size)) != 0) {
            return ret;
        }
        free(data);
    }
345
    
346
    srs_human_trace("build keyframe infos from flv");
winlin authored
347 348 349 350
    for (;;) {
        // tag header
        if ((ret = srs_flv_read_tag_header(ic, &type, &size, &timestamp)) != 0) {
            if (srs_flv_is_eof(ret)) {
351
                srs_human_trace("parse completed.");
winlin authored
352 353
                return 0;
            }
354
            srs_human_trace("flv get packet failed. ret=%d", ret);
winlin authored
355 356 357 358
            return ret;
        }
        
        if (size <= 0) {
359
            srs_human_trace("invalid size=%d", size);
winlin authored
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
            break;
        }
        
        // TODO: FIXME: mem leak when error.
        data = (char*)malloc(size);
        if ((ret = srs_flv_read_tag_data(ic, data, size)) != 0) {
            return ret;
        }
        
        // data tag
        if (type == SRS_RTMP_TYPE_SCRIPT) {
            continue;
        }
        
        // copy
        if ((ret = srs_flv_write_tag(oc, type, timestamp, data, size)) != 0) {
            return ret;
winlin authored
377 378 379 380 381
        }
        
        free(data);
    }
    
382 383
    return ret;
}