Skip to content

RTMP: Use extended timestamp as delta when chunk fmt=1/2. v6.0.167 v7.0.37 #4356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 29, 2025

Conversation

pzx601917159
Copy link
Contributor

@pzx601917159 pzx601917159 commented May 20, 2025

  1. When the chunk message header employs type 1 and type 2, the extended timestamp denotes the time delta.
  2. When the DTS (Decoding Time Stamp) experiences a jump and exceeds 16777215, there can be errors in DTS calculation, and if the audio and video delta differs, it may result in audio-video synchronization issues.

TRANS_BY_GPT4

@winlinvip winlinvip added the TransByAI Translated by AI/GPT. label May 20, 2025
@winlinvip
Copy link
Member

  1. Please explain more about background and context.
  2. Please show how to reproduce this issue.
  3. Please add utests to cover it.

@pzx601917159
Copy link
Contributor Author

pzx601917159 commented May 22, 2025

  1. During the process of switching between primary and backup streams for live streaming, the stream pushed to SRS experienced a discontinuity in DTS (Decoding Time Stamp), leading to errors in DTS calculation. If the format (fmt) is 1, the DTS delta values for audio and video are inconsistent. These values are also being treated as absolute timestamps, which can result in audio-video synchronization issues.

  2. As demonstrated in the unit test file, specifically the ProtocolRecvAVExtTimedelta test case, the DTS discontinuity can be replicated by using the CDN's primary and backup stream functionality to push the stream to SRS. This can be achieved by setting the output_ts_offset parameter in ffmpeg, or by using the provided file dts_large_gap.zip. The command to push the stream to a local SRS using ffmpeg version 4.3.2 is: ffmpeg.exe -i .\dts_skip_large.flv -dts_error_threshold 100000000 -c copy -f flv "rtmp:/xxxx:xxxx/live/livestream".

  3. The unit test has been added to the srs_protocol_rtmp_stack under the name ProtocolRecvAVExtTimedelta.

TRANS_BY_GPT4

@winlinvip
Copy link
Member

winlinvip commented May 22, 2025

From your utest, I understand that the problem is from the vague specification: If chunk fmt=1 and extended timestamp present, is it a delta or value?

For example, if we got too packets here:

    uint8_t video_packet0[] = {
            // 12bytes header, 1byts chunk header, 11bytes msg heder
            0x04, // fmt 0
            0x00, 0x2a, 0xf8, // timestamp 11000
            0x00, 0x00, 0x80, // length, 128
            0x09, // message_type
            0x00, 0x00, 0x00, 0x01, // stream_id
            // msg payload start
            ..
    };

    uint8_t video_packet1[] = {
            // 12bytes header, 1byts chunk header, 11bytes msg heder
            0x44, // fmt 1
            0xff, 0xff, 0xff, // indicates extended timestamp 0xffffff(16777215)
            0x00, 0x00, 0x80, // length, 128
            0x09, // message_type
            0x00, 0xff, 0xfc, 0x18, // extended timestamp delta:16776216
            // msg payload start
            ...
    };

In the video_packet1, the extended timestamp 0xfffc18(16776216) is a delta or absolute value?

  • If delta, the timestamp of video_packet1 should be 16776216+11000
  • If absolute value, the timestamp of video_packet1 should be 16776216

Unfortunately, SRS followed the 2019 version rtmp.part1.Chunk-Stream.pdf. In fact, RTMP v1 2012 version is quiet clear than 2009:

RTMP v1 2012 version

5.3.1.2.1. Type 0
timestamp (3 bytes): For a type-0 chunk, the absolute timestamp of
 the message is sent here. If the timestamp is greater than or
 equal to 16777215 (hexadecimal 0xFFFFFF), this field MUST be
 16777215, indicating the presence of the Extended Timestamp field
 to encode the full 32 bit timestamp. Otherwise, this field SHOULD
 be the entire timestamp.

5.3.1.2.5. Common Header Fields
timestamp delta (3 bytes): For a type-1 or type-2 chunk, the
 difference between the previous chunk’s timestamp and the current
 chunk’s timestamp is sent here. If the delta is greater than or
 equal to 16777215 (hexadecimal 0xFFFFFF), this field MUST be
 16777215, indicating the presence of the Extended Timestamp field
 to encode the full 32 bit delta. Otherwise, this field SHOULD be
 the actual delta.

I think it makes sense to consider extended timestamp as delta when fmt=1 (type-1) in chunk header.

I have also filed an issue at veovera/enhanced-rtmp#42

TRANS_BY_GPT4

@winlinvip winlinvip changed the title [fix][rtmp]fix dts calc error when dts jump RTMP: Use extended timestamp as delta when chunk fmt=1/2 May 22, 2025
@pzx601917159 pzx601917159 force-pushed the develop branch 3 times, most recently from 25dd340 to 2c16421 Compare May 28, 2025 02:09
duiniuluantanqin and others added 2 commits May 29, 2025 11:21
In the scenario of converting WebRTC to RTMP, this conversion will not
proceed until an SenderReport is received; for reference, see:
ossrs#2470.
Thus, if HTTP-FLV streaming is attempted before the SR is received, the
FLV Header will contain only audio, devoid of video content.
This error can be resolved by disabling `guess_has_av` in the
configuration file, since we can guarantee that both audio and video are
present in the test cases.

However, in the original regression tests, the
`TestRtcPublish_HttpFlvPlay` test case contains a bug:

https://github.com/ossrs/srs/blob/5a404c089baa93b906d2452ef47e2ba8a9e6211c/trunk/3rdparty/srs-bench/srs/rtc_test.go#L2421-L2424

The test would pass when `hasAudio` is true and `hasVideo` is false,
which is actually incorrect. Therefore, it has been revised so that the
test now only passes if both values are true.

---------

Co-authored-by: Copilot <[email protected]>
Co-authored-by: winlin <[email protected]>
@winlinvip winlinvip changed the title RTMP: Use extended timestamp as delta when chunk fmt=1/2 RTMP: Use extended timestamp as delta when chunk fmt=1/2. v6.0.167 v7.0.37 May 29, 2025
@winlinvip winlinvip added the RefinedByAI Refined by AI/GPT. label May 29, 2025
@winlinvip winlinvip merged commit e1263bf into ossrs:develop May 29, 2025
17 checks passed
winlinvip added a commit that referenced this pull request May 29, 2025
…4356)

1. When the chunk message header employs type 1 and type 2, the extended
timestamp denotes the time delta.
2. When the DTS (Decoding Time Stamp) experiences a jump and exceeds
16777215, there can be errors in DTS calculation, and if the audio and
video delta differs, it may result in audio-video synchronization
issues.

---------

`TRANS_BY_GPT4`

---------

Co-authored-by: 彭治湘 <[email protected]>
Co-authored-by: Haibo Chen(陈海博) <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: winlin <[email protected]>
winlinvip added a commit that referenced this pull request May 29, 2025
….0.37 (#4356)

1. When the chunk message header employs type 1 and type 2, the extended
timestamp denotes the time delta.
2. When the DTS (Decoding Time Stamp) experiences a jump and exceeds
16777215, there can be errors in DTS calculation, and if the audio and
video delta differs, it may result in audio-video synchronization
issues.

---------

`TRANS_BY_GPT4`

---------

Co-authored-by: 彭治湘 <[email protected]>
Co-authored-by: Haibo Chen(陈海博) <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: winlin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RefinedByAI Refined by AI/GPT. TransByAI Translated by AI/GPT.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants