Skip to content

Commit

Permalink
Add flv-header mux/demux flv audio/video tag header
Browse files Browse the repository at this point in the history
  • Loading branch information
ireader committed Dec 28, 2019
1 parent 389a3d2 commit f1b9533
Show file tree
Hide file tree
Showing 23 changed files with 696 additions and 474 deletions.
6 changes: 1 addition & 5 deletions libdash/test/dash-dynamic-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ static int dash_live_worker(const char* file, dash_playlist_t* dash)
uint32_t diff = 0;
uint64_t clock;

flv_parser_t* parser = flv_parser_create(dash_live_onflv, dash);

while (1)
{
void* f = flv_reader_create(file);
Expand All @@ -136,7 +134,7 @@ static int dash_live_worker(const char* file, dash_playlist_t* dash)

timestamp += diff;
s_timestamp = timestamp > s_timestamp ? timestamp : s_timestamp;
r = flv_parser_input(parser, type, dash->packet, r, timestamp);
r = flv_parser_input(type, dash->packet, r, timestamp, dash_live_onflv, dash);
if (0 != r)
{
assert(0);
Expand All @@ -148,8 +146,6 @@ static int dash_live_worker(const char* file, dash_playlist_t* dash)

diff = s_timestamp + 30;
}

flv_parser_destroy(parser);
}

static int dash_server_mpd(http_session_t* session, dash_playlist_t* dash)
Expand Down
121 changes: 121 additions & 0 deletions libflv/include/flv-header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#ifndef _flv_header_h_
#define _flv_header_h_

#include <stdint.h>

#if defined(__cplusplus)
extern "C" {
#endif

enum
{
FLV_SEQUENCE_HEADER = 0,
FLV_AVPACKET = 1,
FLV_END_OF_SEQUENCE = 2,
};

#define FLV_VIDEO_KEY_FRAME 1

struct flv_header_t
{
uint8_t FLV[3];
uint8_t version;
uint8_t audio;
uint8_t video;
uint32_t offset; // data offset
};

struct flv_tag_header_t
{
uint8_t filter; // 0-No pre-processing required
uint8_t type; // 8-audio, 9-video, 18-script data
uint32_t size; // data size
uint32_t timestamp;
uint32_t streamId;
};

struct flv_audio_tag_header_t
{
uint8_t codecid; /// audio codec id: FLV_AUDIO_AAC
uint8_t rate; /// audio sample frequence: 0-5.5 kHz, 1-11 kHz, 2-22 kHz, 3-44 kHz
uint8_t bits; /// audio sample bits: 0-8 bit samples, 1-16-bit samples
uint8_t channels; /// audio channel count: 0-Mono sound, 1-Stereo sound
uint8_t avpacket; /// AAC only:FLV_SEQUENCE_HEADER/FLV_AVPACKET
};

struct flv_video_tag_header_t
{
uint8_t codecid; /// video codec id: FLV_VIDEO_H264
uint8_t keyframe; /// video frame type: 1-key frame, 2-inter frame
uint8_t avpacket; /// H.264/H.265/AV1 only:FLV_SEQUENCE_HEADER/FLV_AVPACKET/FLV_END_OF_SEQUENCE
int32_t cts; /// video composition time(PTS - DTS), AVC/HEVC/AV1 only
};

/// Read FLV File Header
/// @return >=0-header length in byte, <0-error
int flv_header_read(struct flv_header_t* flv, const uint8_t* buf, int len);

/// Write FLV File Header
/// @param[in] audio 1-has audio, 0-don't have
/// @param[in] video 1-has video, 0-don't have
/// @param[out] buf flv header buffer
/// @param[out] len flv header length
/// @return >=0-header length in byte, <0-error
int flv_header_write(int audio, int video, uint8_t* buf, int len);


/// Read FLV Tag Header
/// @return >=0-header length in byte, <0-error
int flv_tag_header_read(struct flv_tag_header_t* tag, const uint8_t* buf, int len);

/// Write FLV Tag Header
/// @param[out] buf flv tag header buffer
/// @param[out] len flv tag header length
/// @return >=0-header length in byte, <0-error
int flv_tag_header_write(const struct flv_tag_header_t* tag, uint8_t* buf, int len);


/// Read FLV Audio Tag Header
/// @param[out] audio flv audio parameter
/// @param[in] buf flv audio tag header buffer
/// @param[in] len flv audio tag header length
/// @return >=0-header length in byte, <0-error
int flv_audio_tag_header_read(struct flv_audio_tag_header_t* audio, const uint8_t* buf, int len);

/// Write FLV Audio Tag Header
/// @param[in] audio flv audio parameter
/// @param[out] buf flv audio tag header buffer
/// @param[out] len flv audio tag header length
/// @return >=0-header length in byte, <0-error
int flv_audio_tag_header_write(const struct flv_audio_tag_header_t* audio, uint8_t* buf, int len);


/// Read FLV Video Tag Header
/// @param[out] video flv video parameter
/// @param[in] buf flv video tag header buffer
/// @param[in] len flv video tag header length
/// @return >=0-header length in byte, <0-error
int flv_video_tag_header_read(struct flv_video_tag_header_t* video, const uint8_t* buf, int len);

/// Write FLV Video Tag Header
/// @param[in] video flv video parameter
/// @param[out] buf flv video tag header buffer
/// @param[out] len flv video tag header length
/// @return >=0-header length in byte, <0-error
int flv_video_tag_header_write(const struct flv_video_tag_header_t* video, uint8_t* buf, int len);


/// Read FLV Data Tag Header
/// @return >=0-header length in byte, <0-error
int flv_data_tag_header_read(const uint8_t* buf, int len);

/// Write FLV Data Tag Header
/// @param[out] buf flv data tag header buffer
/// @param[out] len flv data tag header length
/// @return >=0-header length in byte, <0-error
int flv_data_tag_header_write(uint8_t* buf, int len);

#if defined(__cplusplus)
}
#endif
#endif /* !_flv_header_h_ */
9 changes: 2 additions & 7 deletions libflv/include/flv-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
extern "C" {
#endif

typedef struct flv_parser_t flv_parser_t;

/// Audio/Video Elementary Stream
/// @param[in] param user-defined parameter
/// @param[in] codec audio/video format (see more flv-proto.h)
Expand All @@ -19,18 +17,15 @@ typedef struct flv_parser_t flv_parser_t;
/// @param[in] dts audio/video decoding timestamp
/// @param[in] flags 1-video keyframe, other-undefined
/// @return 0-ok, other-error
typedef int(*flv_parser_handler)(void* param, int codec, const void* data, size_t bytes, uint32_t pts, uint32_t dts, int flags);

flv_parser_t* flv_parser_create(flv_parser_handler handler, void* param);
void flv_parser_destroy(flv_parser_t* parser);
typedef int (*flv_parser_handler)(void* param, int codec, const void* data, size_t bytes, uint32_t pts, uint32_t dts, int flags);

/// Input FLV Audio/Video Stream
/// @param[in] type 8-audio, 9-video, 18-script (see more flv-proto.h)
/// @param[in] data flv audio/video Stream, AudioTagHeader/VideoTagHeader + A/V Data
/// @param[in] bytes data length in byte
/// @param[in] timestamp milliseconds relative to the first tag(DTS)
/// @return 0-ok, other-error
int flv_parser_input(flv_parser_t* parser, int type, const void* data, size_t bytes, uint32_t timestamp);
int flv_parser_input(int type, const void* data, size_t bytes, uint32_t timestamp, flv_parser_handler handler, void* param);

#if defined(__cplusplus)
}
Expand Down
2 changes: 2 additions & 0 deletions libflv/include/flv-reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ extern "C" {
#endif

void* flv_reader_create(const char* file);
void* flv_reader_create2(int(*read)(void* param, void* buf, int len), void* param);
void flv_reader_destroy(void* flv);

///@param[out] tagtype 8-audio, 9-video, 18-script data
///@param[out] timestamp FLV timestamp
///@param[out] buffer FLV stream
///@return >=0-flv tag length(0 is ok but should be silently discard), <0-error
int flv_reader_read(void* flv, int* tagtype, uint32_t* timestamp, void* buffer, size_t bytes);

#if defined(__cplusplus)
Expand Down
71 changes: 0 additions & 71 deletions libflv/include/flv-tag.h

This file was deleted.

10 changes: 6 additions & 4 deletions libflv/include/flv-writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ extern "C" {
#endif

void* flv_writer_create(const char* file);
void* flv_writer_create2(int (*write)(void* param, const void* buf, int len), void* param);

void flv_writer_destroy(void* flv);

///Video: FLV VideoTagHeader + AVCVIDEOPACKET: AVCDecoderConfigurationRecord(ISO 14496-15) / One or more NALUs(four-bytes length + NALU)
///Audio: FLV AudioTagHeader + AACAUDIODATA: AudioSpecificConfig(14496-3) / Raw AAC frame data in UI8
///@param[in] data FLV Audio/Video Data(don't include FLV Tag Header)
///@param[in] type 8-audio, 9-video
/// Video: FLV VideoTagHeader + AVCVIDEOPACKET: AVCDecoderConfigurationRecord(ISO 14496-15) / One or more NALUs(four-bytes length + NALU)
/// Audio: FLV AudioTagHeader + AACAUDIODATA: AudioSpecificConfig(14496-3) / Raw AAC frame data in UI8
/// @param[in] data FLV Audio/Video Data(don't include FLV Tag Header)
/// @param[in] type 8-audio, 9-video
/// @return 0-ok, other-error
int flv_writer_input(void* flv, int type, const void* data, size_t bytes, uint32_t timestamp);

#if defined(__cplusplus)
Expand Down
1 change: 1 addition & 0 deletions libflv/libflv.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<ClCompile Include="source\aom-av1.c" />
<ClCompile Include="source\flv-demuxer-script.c" />
<ClCompile Include="source\flv-demuxer.c" />
<ClCompile Include="source\flv-header.c" />
<ClCompile Include="source\flv-muxer.c" />
<ClCompile Include="source\flv-parser.c" />
<ClCompile Include="source\flv-reader.c" />
Expand Down
3 changes: 3 additions & 0 deletions libflv/libflv.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,8 @@
<ClCompile Include="source\aom-av1.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\flv-header.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
Loading

0 comments on commit f1b9533

Please sign in to comment.