Repository: ossrs/srs
File: trunk/src/app/srs_app_http_stream.cpp (C++)

Code before:

    return enc->write_metadata(SrsFrameTypeScript, data, size);
}

bool SrsFlvStreamEncoder::has_cache()
{
    // for flv stream, use gop cache of SrsLiveSource is ok.
    return false;
}

srs_error_t SrsFlvStreamEncoder::dump_cache(SrsLiveConsumer* /*consumer*/, SrsRtmpJitterAlgorithm /*jitter*/)
{
    // for flv stream, ignore cache.
    return srs_success;
}

srs_error_t SrsFlvStreamEncoder::write_tags(SrsSharedPtrMessage** msgs, int count)
{
    srs_error_t err = srs_success;

    // For https://github.com/ossrs/srs/issues/939
    if (!header_written) {
        bool has_video = false; bool has_audio = false;
        int nn_video_frames = 0; int nn_audio_frames = 0;

        // 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;
        }

Review comment (suggestion):
Is it possible that calling this function only dumps to a single package (audio, video) and causes misjudgment?

`TRANS_BY_GPT3`

Code after:

    return enc->write_metadata(SrsFrameTypeScript, data, size);
}

void SrsFlvStreamEncoder::set_drop_if_not_match(bool v)
{
    enc->set_drop_if_not_match(v);
}

bool SrsFlvStreamEncoder::has_cache()
{
    // for flv stream, use gop cache of SrsLiveSource is ok.
    return false;
}

srs_error_t SrsFlvStreamEncoder::dump_cache(SrsLiveConsumer* /*consumer*/, SrsRtmpJitterAlgorithm /*jitter*/)
{
    // for flv stream, ignore cache.
    return srs_success;
}

srs_error_t SrsFlvStreamEncoder::write_tags(SrsSharedPtrMessage** msgs, int count)
{
    srs_error_t err = srs_success;

    // For https://github.com/ossrs/srs/issues/939
    if (!header_written) {
        bool has_video = false; bool has_audio = false;
        int nn_video_frames = 0; int nn_audio_frames = 0;

        // 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;
        }