From 35185cf8447cd6f55c14c621cf9eed1c310ffd0e Mon Sep 17 00:00:00 2001 From: Winlin Date: Wed, 14 Dec 2022 21:05:13 +0800 Subject: [PATCH] FLV: Reset has_audio or has_video if only sequence header. (#3310) 1. Reset has_audio if got some video frames but no audio frames. 2. Reset has_video if got some audio frames but no video frames. 3. Note that audio/video frames are not sequence header. --- trunk/doc/CHANGELOG.md | 1 + trunk/src/app/srs_app_http_stream.cpp | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/trunk/doc/CHANGELOG.md b/trunk/doc/CHANGELOG.md index 5262169f7..18809e7ae 100644 --- a/trunk/doc/CHANGELOG.md +++ b/trunk/doc/CHANGELOG.md @@ -8,6 +8,7 @@ The changelog for SRS. ## SRS 5.0 Changelog +* v5.0, 2022-12-13, For [#939](https://github.com/ossrs/srs/issues/939): FLV: Reset has_audio or has_video if only sequence header. * v5.0, 2022-12-12, Merge [#3301](https://github.com/ossrs/srs/pull/3301): DASH: Fix dash crash bug when writing file. v5.0.108 * v5.0, 2022-12-09, Merge [#3296](https://github.com/ossrs/srs/pull/3296): SRT: Support SRT to RTMP to WebRTC. v5.0.107 * v5.0, 2022-12-08, Merge [#3295](https://github.com/ossrs/srs/pull/3295): API: Parse fragment of URI. v5.0.106 diff --git a/trunk/src/app/srs_app_http_stream.cpp b/trunk/src/app/srs_app_http_stream.cpp index e8c51873b..32dbf221e 100755 --- a/trunk/src/app/srs_app_http_stream.cpp +++ b/trunk/src/app/srs_app_http_stream.cpp @@ -307,18 +307,31 @@ srs_error_t SrsFlvStreamEncoder::write_tags(SrsSharedPtrMessage** msgs, int coun // For https://github.com/ossrs/srs/issues/939 if (!header_written) { - bool has_video = false; - bool has_audio = false; + bool has_video = false; bool has_audio = false; + int nn_video_frames = 0; int nn_audio_frames = 0; - for (int i = 0; i < count && (!has_video || !has_audio); i++) { + // Note that we must iterate all messages to count the audio and video frames. + for (int i = 0; i < count; i++) { SrsSharedPtrMessage* msg = msgs[i]; if (msg->is_video()) { + if (!SrsFlvVideo::sh(msg->payload, msg->size)) nn_video_frames++; has_video = true; } else if (msg->is_audio()) { + if (!SrsFlvAudio::sh(msg->payload, msg->size)) nn_audio_frames++; has_audio = true; } } + // See https://github.com/ossrs/srs/issues/939#issuecomment-1348541733 + if (nn_video_frames > 0 && nn_audio_frames == 0) { + if (has_audio) srs_trace("FLV: Reset has_audio for videos=%d and audios=%d", nn_video_frames, nn_audio_frames); + has_audio = false; + } + if (nn_audio_frames > 0 && nn_video_frames == 0) { + if (has_video) srs_trace("FLV: Reset has_video for videos=%d and audios=%d", nn_video_frames, nn_audio_frames); + has_video = false; + } + // Drop data if no A+V. if (!has_video && !has_audio) { return err;