Skip to content

Commit 2cab196

Browse files
committed
change: add flv enhanced rtmp flag
1 parent cf83ebc commit 2cab196

File tree

4 files changed

+66
-49
lines changed

4 files changed

+66
-49
lines changed

libflv/include/flv-header.h

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ struct flv_video_tag_header_t
4141
uint8_t keyframe; /// video frame type: 1-key frame, 2-inter frame
4242
uint8_t avpacket; /// H.264/H.265/AV1 only:FLV_SEQUENCE_HEADER/FLV_AVPACKET/FLV_END_OF_SEQUENCE
4343
int32_t cts; /// video composition time(PTS - DTS), AVC/HEVC/AV1 only
44+
45+
int enhanced_rtmp; /// https://github.com/veovera/enhanced-rtmp/blob/main/enhanced-rtmp.pdf
4446
};
4547

4648
/// Read FLV File Header

libflv/include/flv-muxer.h

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ void flv_muxer_destroy(flv_muxer_t* muxer);
2323
/// re-create AAC/AVC sequence header
2424
int flv_muxer_reset(flv_muxer_t* muxer);
2525

26+
/// @param[in] enable muxer h264 with enhance rtmp
27+
void flv_muxer_set_enhanced_rtmp(flv_muxer_t* muxer, int enable);
28+
2629
/// @param[in] data AAC ADTS stream, 0xFFF15C40011FFC...
2730
int flv_muxer_aac(flv_muxer_t* muxer, const void* data, size_t bytes, uint32_t pts, uint32_t dts);
2831

libflv/source/flv-header.c

+42-43
Original file line numberDiff line numberDiff line change
@@ -236,58 +236,57 @@ int flv_audio_tag_header_write(const struct flv_audio_tag_header_t* audio, uint8
236236

237237
int flv_video_tag_header_write(const struct flv_video_tag_header_t* video, uint8_t* buf, size_t len)
238238
{
239-
#ifdef FLV_ENHANCE_RTMP
240239
// https://github.com/veovera/enhanced-rtmp/blob/main/enhanced-rtmp.pdf
240+
if (video->enhanced_rtmp)
241+
{
242+
if (len < 5)
243+
return -1;
241244

242-
if (len < 5)
243-
return -1;
245+
buf[0] = 0x80 | (video->keyframe << 4) /*FrameType*/;
246+
buf[0] |= (0 == video->cts && FLV_AVPACKET == video->avpacket) ? FLV_PACKET_TYPE_CODED_FRAMES_X : video->avpacket;
244247

245-
buf[0] = 0x80 | (video->keyframe << 4) /*FrameType*/;
246-
buf[0] |= (0 == video->cts && FLV_AVPACKET == video->avpacket) ? FLV_PACKET_TYPE_CODED_FRAMES_X : video->avpacket;
248+
switch (video->codecid)
249+
{
250+
case FLV_VIDEO_AV1:
251+
buf[1] = (FLV_VIDEO_FOURCC_AV1 >> 24) & 0xFF;
252+
buf[2] = (FLV_VIDEO_FOURCC_AV1 >> 16) & 0xFF;
253+
buf[3] = (FLV_VIDEO_FOURCC_AV1 >> 8) & 0xFF;
254+
buf[4] = (FLV_VIDEO_FOURCC_AV1) & 0xFF;
255+
return 5;
247256

248-
switch (video->codecid)
249-
{
250-
case FLV_VIDEO_AV1:
251-
buf[1] = (FLV_VIDEO_FOURCC_AV1 >> 24) & 0xFF;
252-
buf[2] = (FLV_VIDEO_FOURCC_AV1 >> 16) & 0xFF;
253-
buf[3] = (FLV_VIDEO_FOURCC_AV1 >> 8) & 0xFF;
254-
buf[4] = (FLV_VIDEO_FOURCC_AV1) & 0xFF;
255-
return 5;
257+
case FLV_VIDEO_H265:
258+
buf[1] = (FLV_VIDEO_FOURCC_HEVC >> 24) & 0xFF;
259+
buf[2] = (FLV_VIDEO_FOURCC_HEVC >> 16) & 0xFF;
260+
buf[3] = (FLV_VIDEO_FOURCC_HEVC >> 8) & 0xFF;
261+
buf[4] = (FLV_VIDEO_FOURCC_HEVC) & 0xFF;
262+
if (len >= 8 && FLV_AVPACKET == video->avpacket && video->cts != 0)
263+
{
264+
buf[5] = (video->cts >> 16) & 0xFF;
265+
buf[6] = (video->cts >> 8) & 0xFF;
266+
buf[7] = video->cts & 0xFF;
267+
return 8;
268+
}
269+
return 5;
256270

257-
case FLV_VIDEO_H265:
258-
buf[1] = (FLV_VIDEO_FOURCC_HEVC >> 24) & 0xFF;
259-
buf[2] = (FLV_VIDEO_FOURCC_HEVC >> 16) & 0xFF;
260-
buf[3] = (FLV_VIDEO_FOURCC_HEVC >> 8) & 0xFF;
261-
buf[4] = (FLV_VIDEO_FOURCC_HEVC) & 0xFF;
262-
if (len >= 8 && FLV_AVPACKET == video->avpacket && video->cts != 0)
263-
{
264-
buf[5] = (video->cts >> 16) & 0xFF;
265-
buf[6] = (video->cts >> 8) & 0xFF;
266-
buf[7] = video->cts & 0xFF;
267-
return 8;
268-
}
269-
return 5;
271+
case FLV_VIDEO_H266:
272+
buf[1] = (FLV_VIDEO_FOURCC_VVC >> 24) & 0xFF;
273+
buf[2] = (FLV_VIDEO_FOURCC_VVC >> 16) & 0xFF;
274+
buf[3] = (FLV_VIDEO_FOURCC_VVC >> 8) & 0xFF;
275+
buf[4] = (FLV_VIDEO_FOURCC_VVC) & 0xFF;
276+
if (len >= 8 && FLV_AVPACKET == video->avpacket && video->cts != 0)
277+
{
278+
buf[5] = (video->cts >> 16) & 0xFF;
279+
buf[6] = (video->cts >> 8) & 0xFF;
280+
buf[7] = video->cts & 0xFF;
281+
return 8;
282+
}
283+
return 5;
270284

271-
case FLV_VIDEO_H266:
272-
buf[1] = (FLV_VIDEO_FOURCC_VVC >> 24) & 0xFF;
273-
buf[2] = (FLV_VIDEO_FOURCC_VVC >> 16) & 0xFF;
274-
buf[3] = (FLV_VIDEO_FOURCC_VVC >> 8) & 0xFF;
275-
buf[4] = (FLV_VIDEO_FOURCC_VVC) & 0xFF;
276-
if (len >= 8 && FLV_AVPACKET == video->avpacket && video->cts != 0)
277-
{
278-
buf[5] = (video->cts >> 16) & 0xFF;
279-
buf[6] = (video->cts >> 8) & 0xFF;
280-
buf[7] = video->cts & 0xFF;
281-
return 8;
285+
default:
286+
break; // fallthrough
282287
}
283-
return 5;
284-
285-
default:
286-
break; // fallthrough
287288
}
288289

289-
#endif
290-
291290
if (len < 1)
292291
return -1;
293292

libflv/source/flv-muxer.c

+19-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ struct flv_muxer_t
2424
flv_muxer_handler handler;
2525
void* param;
2626

27+
int enhanced_rtmp; // support enhance rtmp extension
28+
2729
uint8_t audio_sequence_header;
2830
uint8_t video_sequence_header;
2931

@@ -59,6 +61,9 @@ struct flv_muxer_t* flv_muxer_create(flv_muxer_handler handler, void* param)
5961
flv_muxer_reset(flv);
6062
flv->handler = handler;
6163
flv->param = param;
64+
#ifdef FLV_ENHANCE_RTMP
65+
flv->enhanced_rtmp = 1;
66+
#endif
6267
return flv;
6368
}
6469

@@ -82,6 +87,11 @@ int flv_muxer_reset(struct flv_muxer_t* flv)
8287
return 0;
8388
}
8489

90+
void flv_muxer_set_enhanced_rtmp(flv_muxer_t* muxer, int enable)
91+
{
92+
muxer->enhanced_rtmp = enable;
93+
}
94+
8595
static int flv_muxer_alloc(struct flv_muxer_t* flv, size_t bytes)
8696
{
8797
void* p;
@@ -261,6 +271,7 @@ static int flv_muxer_h264(struct flv_muxer_t* flv, uint32_t pts, uint32_t dts)
261271
struct flv_video_tag_header_t video;
262272

263273
video.codecid = FLV_VIDEO_H264;
274+
video.enhanced_rtmp = flv->enhanced_rtmp;
264275
if ( /*0 == flv->video_sequence_header &&*/ flv->update && flv->v.avc.nb_sps > 0 && flv->v.avc.nb_pps > 0)
265276
{
266277
video.cts = 0;
@@ -313,6 +324,7 @@ static int flv_muxer_h265(struct flv_muxer_t* flv, uint32_t pts, uint32_t dts)
313324
struct flv_video_tag_header_t video;
314325

315326
video.codecid = FLV_VIDEO_H265;
327+
video.enhanced_rtmp = flv->enhanced_rtmp;
316328
if ( /*0 == flv->avc_sequence_header &&*/ flv->update && flv->v.hevc.numOfArrays >= 3) // vps + sps + pps
317329
{
318330
video.cts = 0;
@@ -351,9 +363,8 @@ int flv_muxer_hevc(struct flv_muxer_t* flv, const void* data, size_t bytes, uint
351363
}
352364

353365
flv->bytes = 5;
354-
#ifdef FLV_ENHANCE_RTMP
355-
flv->bytes += dts == pts ? 0 : 3;
356-
#endif
366+
if(flv->enhanced_rtmp)
367+
flv->bytes += dts == pts ? 0 : 3;
357368
flv->bytes += h265_annexbtomp4(&flv->v.hevc, data, bytes, flv->ptr + flv->bytes, flv->capacity - flv->bytes, &flv->vcl, &flv->update);
358369
if (flv->bytes <= 5)
359370
return -ENOMEM;
@@ -368,6 +379,7 @@ static int flv_muxer_h266(struct flv_muxer_t* flv, uint32_t pts, uint32_t dts)
368379
struct flv_video_tag_header_t video;
369380

370381
video.codecid = FLV_VIDEO_H266;
382+
video.enhanced_rtmp = flv->enhanced_rtmp;
371383
if ( /*0 == flv->avc_sequence_header &&*/ flv->update && flv->v.vvc.numOfArrays >= 3) // vps + sps + pps
372384
{
373385
video.cts = 0;
@@ -406,9 +418,8 @@ int flv_muxer_vvc(struct flv_muxer_t* flv, const void* data, size_t bytes, uint3
406418
}
407419

408420
flv->bytes = 5;
409-
#ifdef FLV_ENHANCE_RTMP
410-
flv->bytes += dts == pts ? 0 : 3;
411-
#endif
421+
if(flv->enhanced_rtmp)
422+
flv->bytes += dts == pts ? 0 : 3;
412423
flv->bytes += h266_annexbtomp4(&flv->v.vvc, data, bytes, flv->ptr + flv->bytes, flv->capacity - flv->bytes, &flv->vcl, &flv->update);
413424
if (flv->bytes <= 5)
414425
return -ENOMEM;
@@ -429,6 +440,7 @@ int flv_muxer_av1(flv_muxer_t* flv, const void* data, size_t bytes, uint32_t pts
429440
}
430441

431442
video.codecid = FLV_VIDEO_AV1;
443+
video.enhanced_rtmp = flv->enhanced_rtmp;
432444
if (0 == flv->video_sequence_header)
433445
{
434446
// load av1 information
@@ -476,6 +488,7 @@ int flv_muxer_avs3(flv_muxer_t* flv, const void* data, size_t bytes, uint32_t pt
476488
}
477489

478490
video.codecid = FLV_VIDEO_H266; // codec 14, same as H.266
491+
video.enhanced_rtmp = flv->enhanced_rtmp;
479492
if (0 == flv->video_sequence_header)
480493
{
481494
// load avs information

0 commit comments

Comments
 (0)