Blame view

trunk/src/kernel/srs_kernel_flv.hpp 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
/*
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.
*/
winlin authored
24 25
#ifndef SRS_KERNEL_FLV_HPP
#define SRS_KERNEL_FLV_HPP
26 27

/*
28
#include <srs_kernel_flv.hpp>
29 30 31
*/
#include <srs_core.hpp>
32 33
#include <string>
34
class SrsStream;
35 36
class SrsFileWriter;
class SrsFileReader;
37 38 39 40 41 42 43

/**
* encode data to flv file.
*/
class SrsFlvEncoder
{
private:
44
    SrsFileWriter* _fs;
45 46 47 48 49 50 51
private:
    SrsStream* tag_stream;
public:
    SrsFlvEncoder();
    virtual ~SrsFlvEncoder();
public:
    /**
52 53 54
    * initialize the underlayer file stream.
    * @remark user can initialize multiple times to encode multiple flv files.
    * @remark, user must free the fs, flv encoder never close/free it.
55
    */
56
    virtual int initialize(SrsFileWriter* fs);
57 58 59 60 61 62 63 64 65
public:
    /**
    * write flv header.
    * write following:
    *   1. E.2 The FLV header
    *   2. PreviousTagSize0 UI32 Always 0
    * that is, 9+4=13bytes.
    */
    virtual int write_header();
winlin authored
66
    virtual int write_header(char flv_header[9]);
67 68
    /**
    * write flv metadata. 
69
    * @param data, the amf0 metadata which serialize from:
70 71
    *   AMF0 string: onMetaData,
    *   AMF0 object: the metadata object.
winlin authored
72
    * @remark assert data is not NULL.
73 74 75 76
    */
    virtual int write_metadata(char* data, int size);
    /**
    * write audio/video packet.
winlin authored
77
    * @remark assert data is not NULL.
78 79 80
    */
    virtual int write_audio(int64_t timestamp, char* data, int size);
    virtual int write_video(int64_t timestamp, char* data, int size);
81
public:
82 83 84
    /**
    * get the tag size,
    * including the tag header, body, and 4bytes previous tag size.
winlin authored
85
    * @remark assert data_size is not negative.
86 87
    */
    static int size_tag(int data_size);
88 89 90 91
private:
    virtual int write_tag(char* header, int header_size, char* tag, int tag_size);
};
92
/**
93
* decode flv file.
94
*/
95
class SrsFlvDecoder
96 97
{
private:
98
    SrsFileReader* _fs;
99 100 101
private:
    SrsStream* tag_stream;
public:
102 103
    SrsFlvDecoder();
    virtual ~SrsFlvDecoder();
104 105
public:
    /**
106 107 108
    * initialize the underlayer file stream
    * @remark user can initialize multiple times to decode multiple flv files.
    * @remark, user must free the fs, flv decoder never close/free it.
109
    */
110
    virtual int initialize(SrsFileReader* fs);
111
public:
winlin authored
112 113 114 115
    /**
    * read the flv header, donot including the 4bytes previous tag size.
    * @remark assert header not NULL.
    */
116
    virtual int read_header(char header[9]);
winlin authored
117 118 119 120
    /**
    * read the tag header infos.
    * @remark assert ptype/pdata_size/ptime not NULL.
    */
121
    virtual int read_tag_header(char* ptype, int32_t* pdata_size, u_int32_t* ptime);
winlin authored
122 123 124 125
    /**
    * read the tag data.
    * @remark assert data not NULL.
    */
126
    virtual int read_tag_data(char* data, int32_t size);
winlin authored
127 128 129 130 131
    /**
    * read the 4bytes previous tag size.
    * @remark assert previous_tag_size not NULL.
    */
    virtual int read_previous_tag_size(char previous_tag_size[4]);
132 133
};
134
/**
135 136 137
* decode flv fast by only decoding the header and tag.
* used for vod flv stream to read the header and sequence header, 
* then seek to specified offset.
138
*/
139
class SrsFlvVodStreamDecoder
140 141
{
private:
142
    SrsFileReader* _fs;
143 144 145
private:
    SrsStream* tag_stream;
public:
146 147
    SrsFlvVodStreamDecoder();
    virtual ~SrsFlvVodStreamDecoder();
148 149
public:
    /**
150 151 152
    * initialize the underlayer file stream
    * @remark user can initialize multiple times to decode multiple flv files.
    * @remark, user must free the fs, flv decoder never close/free it.
153
    */
154
    virtual int initialize(SrsFileReader* fs);
155
public:
156
    /**
157 158
    * read the flv header and its size.
    * @param header, fill it 13bytes(9bytes header, 4bytes previous tag size).
winlin authored
159
    * @remark assert header not NULL.
160
    */
161
    virtual int read_header_ext(char header[13]);
162
    /**
163 164 165 166
    * read the sequence header tags offset and its size.
    * @param pstart, the start offset of sequence header.
    * @param psize, output the size, (tag header)+(tag body)+(4bytes previous tag size).
    * @remark we think the first audio/video is sequence header.
winlin authored
167
    * @remark assert pstart/psize not NULL.
168
    */
169
    virtual int read_sequence_header_summary(int64_t* pstart, int* psize);
170 171 172 173 174
public:
    /**
    * for start offset, seed to this position and response flv stream.
    */
    virtual int lseek(int64_t offset);
175 176
};
177
#endif
178