Blame view

trunk/src/kernel/srs_kernel_flv.hpp 5.3 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

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 type, the type of data, or other message type.
70
    *       @see SrsCodecFlvTag
71
    * @param data, the amf0 metadata which serialize from:
72 73
    *   AMF0 string: onMetaData,
    *   AMF0 object: the metadata object.
winlin authored
74
    * @remark assert data is not NULL.
75
    */
76
    virtual int write_metadata(char type, char* data, int size);
77 78
    /**
    * write audio/video packet.
winlin authored
79
    * @remark assert data is not NULL.
80 81 82
    */
    virtual int write_audio(int64_t timestamp, char* data, int size);
    virtual int write_video(int64_t timestamp, char* data, int size);
83
public:
84 85 86
    /**
    * get the tag size,
    * including the tag header, body, and 4bytes previous tag size.
winlin authored
87
    * @remark assert data_size is not negative.
88 89
    */
    static int size_tag(int data_size);
90 91 92 93
private:
    virtual int write_tag(char* header, int header_size, char* tag, int tag_size);
};
94
/**
95
* decode flv file.
96
*/
97
class SrsFlvDecoder
98 99
{
private:
100
    SrsFileReader* _fs;
101 102 103
private:
    SrsStream* tag_stream;
public:
104 105
    SrsFlvDecoder();
    virtual ~SrsFlvDecoder();
106 107
public:
    /**
108 109 110
    * 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.
111
    */
112
    virtual int initialize(SrsFileReader* fs);
113
public:
winlin authored
114 115 116 117
    /**
    * read the flv header, donot including the 4bytes previous tag size.
    * @remark assert header not NULL.
    */
118
    virtual int read_header(char header[9]);
winlin authored
119 120 121 122
    /**
    * read the tag header infos.
    * @remark assert ptype/pdata_size/ptime not NULL.
    */
123
    virtual int read_tag_header(char* ptype, int32_t* pdata_size, u_int32_t* ptime);
winlin authored
124 125 126 127
    /**
    * read the tag data.
    * @remark assert data not NULL.
    */
128
    virtual int read_tag_data(char* data, int32_t size);
winlin authored
129 130 131 132 133
    /**
    * read the 4bytes previous tag size.
    * @remark assert previous_tag_size not NULL.
    */
    virtual int read_previous_tag_size(char previous_tag_size[4]);
134 135
};
136
/**
137 138 139
* 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.
140
*/
141
class SrsFlvVodStreamDecoder
142 143
{
private:
144
    SrsFileReader* _fs;
145 146 147
private:
    SrsStream* tag_stream;
public:
148 149
    SrsFlvVodStreamDecoder();
    virtual ~SrsFlvVodStreamDecoder();
150 151
public:
    /**
152 153 154
    * 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.
155
    */
156
    virtual int initialize(SrsFileReader* fs);
157
public:
158
    /**
159 160
    * read the flv header and its size.
    * @param header, fill it 13bytes(9bytes header, 4bytes previous tag size).
winlin authored
161
    * @remark assert header not NULL.
162
    */
163
    virtual int read_header_ext(char header[13]);
164
    /**
165 166 167 168
    * 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
169
    * @remark assert pstart/psize not NULL.
170
    */
171
    virtual int read_sequence_header_summary(int64_t* pstart, int* psize);
172 173 174 175 176
public:
    /**
    * for start offset, seed to this position and response flv stream.
    */
    virtual int lseek(int64_t offset);
177 178
};
179
#endif
180