Skip to content

Commit 4362df7

Browse files
xengine-qytchundonglinlinxiaozhihong
authored andcommitted
Bugfix: HEVC SRT stream supports multiple PPS fields. v6.0.76 (#3722)
When the srs have multiple pps in hevc.the srs can't parse for this. problem fixed this #3604 --------- Co-authored-by: chundonglinlin <[email protected]> Co-authored-by: john <[email protected]>
1 parent add0f36 commit 4362df7

9 files changed

+127
-18
lines changed

trunk/doc/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The changelog for SRS.
77
<a name="v6-changes"></a>
88

99
## SRS 6.0 Changelog
10+
* v6.0, 2023-09-18, Merge [#3722](https://github.com/ossrs/srs/pull/3722): Bugfix: HEVC SRT stream supports multiple PPS fields. v6.0.76 (#3722)
1011
* v6.0, 2023-09-08, Merge [#3597](https://github.com/ossrs/srs/pull/3597): Fix RBSP stream parsing bug, should drop 0x03. v6.0.75 (#3597)
1112
* v6.0, 2023-09-08, Merge [#3794](https://github.com/ossrs/srs/pull/3794): Support SRS Stack token for authentication. v6.0.74 (#3794)
1213
* v6.0, 2023-09-07, Merge [#3795](https://github.com/ossrs/srs/pull/3795): Fix dash crash if format not supported. v6.0.73 (#3795)

trunk/src/app/srs_app_gb28181.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1905,7 +1905,8 @@ srs_error_t SrsGbMuxer::write_h265_vps_sps_pps(uint32_t dts, uint32_t pts)
19051905
}
19061906

19071907
std::string sh;
1908-
if ((err = hevc_->mux_sequence_header(h265_vps_, h265_sps_, h265_pps_, sh)) != srs_success) {
1908+
std::vector<string> h265_pps = { h265_pps_ };
1909+
if ((err = hevc_->mux_sequence_header(h265_vps_, h265_sps_, h265_pps, sh)) != srs_success) {
19091910
return srs_error_wrap(err, "hevc mux sequence header");
19101911
}
19111912

trunk/src/app/srs_app_srt_source.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ srs_error_t SrsSrtFrameBuilder::on_ts_video_hevc(SrsTsMessage *msg, SrsBuffer *a
538538
SrsRawHEVCStream *hevc = new SrsRawHEVCStream();
539539
SrsAutoFree(SrsRawHEVCStream, hevc);
540540

541+
std::vector<std::string> hevc_pps;
541542
// send each frame.
542543
while (!avs->empty()) {
543544
char* frame = NULL;
@@ -587,17 +588,21 @@ srs_error_t SrsSrtFrameBuilder::on_ts_video_hevc(SrsTsMessage *msg, SrsBuffer *a
587588
return srs_error_wrap(err, "demux pps");
588589
}
589590

590-
if (! pps.empty() && hevc_pps_ != pps) {
591+
if (!pps.empty()) {
591592
vps_sps_pps_change_ = true;
592593
}
593594

594-
hevc_pps_ = pps;
595+
hevc_pps.push_back(pps);
595596
continue;
596597
}
597598

598599
ipb_frames.push_back(make_pair(frame, frame_size));
599600
}
600601

602+
if (!hevc_pps.empty()) {
603+
hevc_pps_ = hevc_pps;
604+
}
605+
601606
if ((err = check_vps_sps_pps_change(msg)) != srs_success) {
602607
return srs_error_wrap(err, "check vps sps pps");
603608
}
@@ -614,7 +619,7 @@ srs_error_t SrsSrtFrameBuilder::check_vps_sps_pps_change(SrsTsMessage* msg)
614619
}
615620

616621
if (hevc_vps_.empty() || hevc_sps_.empty() || hevc_pps_.empty()) {
617-
return srs_error_new(ERROR_SRT_TO_RTMP_EMPTY_SPS_PPS, "vps or sps or pps empty");
622+
return err;
618623
}
619624

620625
// vps/sps/pps changed, generate new video sh frame and dispatch it.
@@ -662,7 +667,7 @@ srs_error_t SrsSrtFrameBuilder::on_hevc_frame(SrsTsMessage* msg, vector<pair<cha
662667
srs_error_t err = srs_success;
663668

664669
if (ipb_frames.empty()) {
665-
return srs_error_new(ERROR_SRT_CONN, "empty frame");
670+
return err;
666671
}
667672

668673
// ts tbn to flv tbn.

trunk/src/app/srs_app_srt_source.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class SrsSrtFrameBuilder : public ISrsTsHandler
131131
bool vps_sps_pps_change_;
132132
std::string hevc_vps_;
133133
std::string hevc_sps_;
134-
std::string hevc_pps_;
134+
std::vector<std::string> hevc_pps_;
135135
#endif
136136
// Record audio sepcific config had changed, if change, need to generate new audio sh frame.
137137
bool audio_sh_change_;

trunk/src/core/srs_core_version6.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
#define VERSION_MAJOR 6
1111
#define VERSION_MINOR 0
12-
#define VERSION_REVISION 75
12+
#define VERSION_REVISION 76
1313

1414
#endif

trunk/src/kernel/srs_kernel_ts.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -1952,8 +1952,13 @@ srs_error_t SrsTsPayloadPES::decode(SrsBuffer* stream, SrsTsMessage** ppmsg)
19521952
// check when fresh, the payload_unit_start_indicator
19531953
// should be 1 for the fresh msg.
19541954
if (is_fresh_msg && !packet->payload_unit_start_indicator) {
1955-
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: PES fresh packet length=%d, us=%d, cc=%d",
1955+
srs_warn("ts: PES fresh packet length=%d, us=%d, cc=%d",
19561956
msg->PES_packet_length, packet->payload_unit_start_indicator, packet->continuity_counter);
1957+
1958+
stream->skip(stream->size() - stream->pos());
1959+
srs_freep(msg);
1960+
channel->msg = NULL;
1961+
return err;
19571962
}
19581963

19591964
// check when not fresh and PES_packet_length>0,

trunk/src/protocol/srs_protocol_raw_avc.cpp

+17-9
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ srs_error_t SrsRawHEVCStream::pps_demux(char *frame, int nb_frame, std::string &
382382
return err;
383383
}
384384

385-
srs_error_t SrsRawHEVCStream::mux_sequence_header(std::string vps, std::string sps, std::string pps, std::string &hvcC)
385+
srs_error_t SrsRawHEVCStream::mux_sequence_header(std::string vps, std::string sps, std::vector<std::string>& pps, std::string &hvcC)
386386
{
387387
srs_error_t err = srs_success;
388388

@@ -400,8 +400,13 @@ srs_error_t SrsRawHEVCStream::mux_sequence_header(std::string vps, std::string s
400400
// sequenceParameterSetNALUnit
401401

402402
// use simple mode: nalu size + nalu data
403-
int nb_packet = 23 + 5 + (int)vps.length() + 5 + (int)sps.length() + 5 + (int)pps.length();
404-
char *packet = new char[nb_packet];
403+
int pps_size = 0;
404+
for (std::vector<std::string>::iterator it = pps.begin(); it != pps.end(); it++) {
405+
pps_size += 2 + it->length();
406+
}
407+
408+
int nb_packet = 23 + 5 + (int)vps.length() + 5 + (int)sps.length() + 5 + pps_size - 2;
409+
char* packet = new char[nb_packet];
405410
SrsAutoFreeA(char, packet);
406411

407412
// use stream to generate the hevc packet.
@@ -495,12 +500,15 @@ srs_error_t SrsRawHEVCStream::mux_sequence_header(std::string vps, std::string s
495500
if (true) {
496501
// nal_type
497502
stream.write_1bytes(SrsHevcNaluType_PPS & 0x3f);
498-
// numOfPictureParameterSets, always 1
499-
stream.write_2bytes(0x01);
500-
// pictureParameterSetLength
501-
stream.write_2bytes((int16_t)pps.length());
502-
// pictureParameterSetNALUnit
503-
stream.write_string(pps);
503+
// numOfPictureParameterSets
504+
stream.write_2bytes(pps.size());
505+
506+
for (std::vector<std::string>::iterator it = pps.begin(); it != pps.end(); it++) {
507+
//pictureParameterSetLength
508+
stream.write_2bytes((int16_t)it->length());
509+
//pictureParameterSetNALUnit
510+
stream.write_string(*it);
511+
}
504512
}
505513

506514
hvcC = string(packet, nb_packet);

trunk/src/protocol/srs_protocol_raw_avc.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class SrsRawHEVCStream
8282
// The hevc raw data to hevc packet, without flv payload header.
8383
// Mux the sps/pps/vps to flv sequence header packet.
8484
// @param sh output the sequence header.
85-
virtual srs_error_t mux_sequence_header(std::string vps, std::string sps, std::string pps, std::string &sh);
85+
virtual srs_error_t mux_sequence_header(std::string vps, std::string sps, std::vector<std::string>& pps, std::string& sh);
8686
// The hevc raw data to hevc packet, without flv payload header.
8787
// Mux the ibp to flv ibp packet.
8888
// @param ibp output the packet.

trunk/src/utest/srs_utest_avc.cpp

+89
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <srs_kernel_buffer.hpp>
1010
#include <srs_kernel_error.hpp>
1111
#include <srs_core_autofree.hpp>
12+
#include <srs_kernel_utility.hpp>
1213

1314
VOID TEST(SrsAVCTest, H264ParseAnnexb)
1415
{
@@ -599,3 +600,91 @@ VOID TEST(SrsAVCTest, AACMuxToFLV)
599600
}
600601
}
601602

603+
#ifdef SRS_H265
604+
605+
VOID TEST(SrsAVCTest, HevcMultiPPS)
606+
{
607+
srs_error_t err;
608+
609+
vector<uint8_t> vps = {
610+
0x40, 0x01, 0x0c, 0x06, 0x3f, 0x3f, 0x22, 0x20, 0x00, 0x00, 0x03, 0x00, 0x3f,
611+
0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x3f, 0x00, 0x00, 0x18, 0x3f, 0x24,
612+
};
613+
614+
vector<uint8_t> sps = {
615+
0x42, 0x01, 0x06, 0x22, 0x20, 0x00, 0x00, 0x03, 0x00, 0x3f, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00,
616+
0x3f, 0x00, 0x00, 0x3f, 0x01, 0x3f, 0x20, 0x02, 0x1c, 0x4d, 0x3f, 0x3f, 0x3f, 0x42, 0x3f, 0x53, 0x3f,
617+
0x3f, 0x01, 0x01, 0x01, 0x04, 0x00, 0x00, 0x0f, 0x3f, 0x00, 0x01, 0x3f, 0x3f, 0x3f, 0x68, 0x3f, 0x3f,
618+
0x00, 0x1f, 0x3a, 0x00, 0x0f, 0x3f, 0x04, 0x00, 0x3e, 0x74, 0x00, 0x1f, 0x3a, 0x08, 0x00, 0x7c, 0x3f,
619+
0x00, 0x3e, 0x74, 0x10, 0x00, 0x3f, 0x3f, 0x00, 0x7c, 0x3f, 0x5c, 0x20, 0x10, 0x40
620+
};
621+
622+
vector<uint8_t> pps_1 = {
623+
0x44, 0x01, 0x74, 0x18, 0xc2, 0xb8, 0x33, 0x3f, 0x7d, 0x75, 0x3f, 0x42, 0x28, 0x3f, 0x6b, 0x3f, 0x3f,
624+
0x11, 0x47, 0x7d, 0x72, 0x79, 0x3e, 0x4f, 0x3f, 0x3f, 0x51, 0x3f, 0x3f, 0x20, 0x3f, 0x3f, 0x3f, 0x10,
625+
0x63, 0x3f, 0x0a, 0x0e, 0x3f, 0x04, 0x42, 0x3f, 0x3f, 0x3f, 0x34, 0x22, 0x3f, 0x3f, 0x3f, 0x3f, 0x7d,
626+
0x7e, 0x4f, 0x3f, 0x3f, 0x7c, 0x57, 0x3f, 0x3f, 0x3f, 0x10, 0x41, 0x21, 0x14, 0x41, 0x3f, 0x3f, 0x3c,
627+
0x3f, 0x5f, 0x3f, 0x3f, 0x28, 0x3f, 0x73, 0x10, 0x44, 0x49, 0x47, 0x08, 0x31, 0xc4, 0x85, 0x06, 0x46,
628+
0x3f, 0x21, 0x02, 0x41, 0x54, 0x1a, 0x11, 0x45, 0x13, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x11,
629+
0x3f, 0x5e, 0x3b, 0x3f, 0x30, 0x41, 0x04, 0x3f, 0x51, 0x06, 0x3f, 0x3f, 0x3f, 0x7d, 0x7e, 0x4f, 0x3f,
630+
0x3f, 0x1d, 0x3f, 0x41, 0x11, 0x25, 0x1c, 0x20, 0x3f, 0x12, 0x14, 0x19, 0x1a, 0x08, 0x3f, 0x09, 0x05,
631+
0x50, 0x69, 0x14, 0x4f, 0x3f, 0x4f, 0x27, 0x3f, 0x3f, 0x3f, 0x28, 0x3f, 0x73, 0x10, 0xd3, 0x94, 0x71,
632+
0x3f, 0x73, 0x3f, 0x05, 0x45, 0x3f, 0x01, 0x02, 0x41, 0x54, 0x18, 0x24
633+
};
634+
635+
vector<uint8_t> pps_2 = {
636+
0x44, 0x01, 0x25, 0x06, 0x30, 0x3f, 0x0c, 0x3f, 0x4a, 0x3f, 0x3f, 0x49, 0x08, 0x3f, 0x15, 0x6b, 0x3f,
637+
0x3f, 0x11, 0x4c, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x46, 0x3b, 0x3f, 0x3f, 0x22, 0x4a,
638+
0x30, 0x51, 0x3f, 0x2c, 0x2c, 0x32, 0x34, 0x11, 0x08, 0x12, 0x0a, 0x3f, 0xd0, 0x8a, 0x29, 0x56, 0x3f,
639+
0x3c, 0x3f, 0x5f, 0x3f, 0x3f, 0x3f, 0x1f, 0x15, 0x3f, 0x3f, 0x63, 0x04, 0x10, 0x4c, 0x45, 0x35, 0x49,
640+
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x77, 0x31, 0x04, 0x44, 0x3f, 0x60, 0x3f, 0x1c, 0x58,
641+
0x58, 0x64, 0x68, 0x22, 0x10, 0x24, 0x15, 0x41, 0x3f, 0x14, 0x42, 0x55, 0x3f, 0x4f, 0x27, 0x3f, 0x3f,
642+
0x3f, 0x3f, 0x47, 0x3f, 0x78, 0x3f, 0x18, 0x3f, 0x04, 0x13, 0x11, 0x4d, 0x52, 0x64, 0x3f, 0x3f, 0x3f,
643+
0x7e, 0x4f, 0x3f, 0x3f, 0x1d, 0x3f, 0x41, 0x11, 0x25, 0x18, 0x28, 0x3f, 0x16, 0x16, 0x19, 0x1a, 0x08,
644+
0x3f, 0x09, 0x05, 0x50, 0x69, 0x10, 0x3f, 0x6b, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x11, 0x3f, 0x5e,
645+
0x3b, 0x3f, 0x30, 0x41, 0x04, 0x3f
646+
};
647+
648+
vector<uint8_t> start_code = {0x00, 0x00, 0x00, 0x01};
649+
650+
string hevc_sh;
651+
hevc_sh.append((const char*)start_code.data(), start_code.size());
652+
hevc_sh.append((const char*)vps.data(), vps.size());
653+
hevc_sh.append((const char*)start_code.data(), start_code.size());
654+
hevc_sh.append((const char*)sps.data(), sps.size());
655+
hevc_sh.append((const char*)start_code.data(), start_code.size());
656+
hevc_sh.append((const char*)pps_1.data(), pps_1.size());
657+
hevc_sh.append((const char*)start_code.data(), start_code.size());
658+
hevc_sh.append((const char*)pps_2.data(), pps_2.size());
659+
660+
SrsBuffer stream((char*)hevc_sh.data(), hevc_sh.size());
661+
662+
SrsRawHEVCStream hs;
663+
char* frame = NULL;
664+
int frame_size = 0;
665+
666+
HELPER_ASSERT_SUCCESS(hs.annexb_demux(&stream, &frame, &frame_size));
667+
EXPECT_TRUE(hs.is_vps(frame, frame_size));
668+
EXPECT_EQ(frame_size, vps.size());
669+
EXPECT_TRUE(srs_bytes_equals(frame, vps.data(), frame_size));
670+
671+
HELPER_ASSERT_SUCCESS(hs.annexb_demux(&stream, &frame, &frame_size));
672+
EXPECT_TRUE(hs.is_sps(frame, frame_size));
673+
EXPECT_EQ(frame_size, sps.size());
674+
EXPECT_TRUE(srs_bytes_equals(frame, sps.data(), frame_size));
675+
676+
HELPER_ASSERT_SUCCESS(hs.annexb_demux(&stream, &frame, &frame_size));
677+
EXPECT_TRUE(hs.is_pps(frame, frame_size));
678+
EXPECT_EQ(frame_size, pps_1.size());
679+
EXPECT_TRUE(srs_bytes_equals(frame, pps_1.data(), frame_size));
680+
681+
HELPER_ASSERT_SUCCESS(hs.annexb_demux(&stream, &frame, &frame_size));
682+
EXPECT_TRUE(hs.is_pps(frame, frame_size));
683+
EXPECT_EQ(frame_size, pps_2.size());
684+
EXPECT_TRUE(srs_bytes_equals(frame, pps_2.data(), frame_size));
685+
686+
EXPECT_TRUE(stream.empty());
687+
}
688+
689+
#endif
690+

0 commit comments

Comments
 (0)