Repository: ossrs/srs
File: trunk/src/kernel/srs_kernel_flv.cpp (C++)

Code before:
    // Do realloc the tag headers if required.
    char* cache = tag_headers;
    if (nb_tag_headers < count) {
        srs_freepa(tag_headers);
        
        nb_tag_headers = count;
        cache = tag_headers = new char[SRS_FLV_TAG_HEADER_SIZE * count];
    }
    
    // Do realloc the pts if required.
    char* pts = ppts;
    if (nb_ppts < count) {
        srs_freepa(ppts);
        
        nb_ppts = count;
        pts = ppts = new char[SRS_FLV_PREVIOUS_TAG_SIZE * count];
    }
    
    // Now all caches are ok, start to write all messages.
    iovec* iovs = iovss; int nn_real_iovss = 0;
    for (int i = 0; i < count; i++) {
        SrsSharedPtrMessage* msg = msgs[i];
        
        // Cache FLV packet header.
        if (msg->is_audio()) {
            if (!has_audio_) continue; // Ignore audio packets if no audio stream.
            cache_audio(msg->timestamp, msg->payload, msg->size, cache);
        } else if (msg->is_video()) {
            if (!has_video_) continue; // Ignore video packets if no video stream.
            cache_video(msg->timestamp, msg->payload, msg->size, cache);
        } else {
            cache_metadata(SrsFrameTypeScript, msg->payload, msg->size, cache);
        }
        
        // Cache FLV pts.
        cache_pts(SRS_FLV_TAG_HEADER_SIZE + msg->size, pts);
        
        // Set cache to iovec.
        iovs[0].iov_base = cache;
        iovs[0].iov_len = SRS_FLV_TAG_HEADER_SIZE;
        iovs[1].iov_base = msg->payload;
        iovs[1].iov_len = msg->size;
        iovs[2].iov_base = pts;
        iovs[2].iov_len = SRS_FLV_PREVIOUS_TAG_SIZE;
        
        // Move to next cache.
        cache += SRS_FLV_TAG_HEADER_SIZE;
        pts += SRS_FLV_PREVIOUS_TAG_SIZE;
        iovs += 3; nn_real_iovss += 3;
    }


Review comment (suggestion):
If the header does not have an audio or video tag, discard the packet. It feels like this should have an additional configuration, as sometimes the packet should not be discarded.

`TRANS_BY_GPT3`

Code after:
    iovec* iovss = iovss_cache;
    do {
        int nn_might_iovss = 3 * count;
        if (nb_iovss_cache < nn_might_iovss) {
            srs_freepa(iovss_cache);

            nb_iovss_cache = nn_might_iovss;
            iovss = iovss_cache = new iovec[nn_might_iovss];
        }
    } while (false);
    
    // Do realloc the tag headers if required.
    char* cache = tag_headers;
    if (nb_tag_headers < count) {
        srs_freepa(tag_headers);
        
        nb_tag_headers = count;
        cache = tag_headers = new char[SRS_FLV_TAG_HEADER_SIZE * count];
    }
    
    // Do realloc the pts if required.
    char* pts = ppts;
    if (nb_ppts < count) {
        srs_freepa(ppts);
        
        nb_ppts = count;
        pts = ppts = new char[SRS_FLV_PREVIOUS_TAG_SIZE * count];
    }
    
    // Now all caches are ok, start to write all messages.
    iovec* iovs = iovss; int nn_real_iovss = 0;
    for (int i = 0; i < count; i++) {
        SrsSharedPtrMessage* msg = msgs[i];
        
        // Cache FLV packet header.
        if (msg->is_audio()) {
            if (drop_if_not_match_ && !has_audio_) continue; // Ignore audio packets if no audio stream.
            cache_audio(msg->timestamp, msg->payload, msg->size, cache);
        } else if (msg->is_video()) {
            if (drop_if_not_match_ && !has_video_) continue; // Ignore video packets if no video stream.
            cache_video(msg->timestamp, msg->payload, msg->size, cache);
        } else {
            cache_metadata(SrsFrameTypeScript, msg->payload, msg->size, cache);
        }
        
        // Cache FLV pts.
        cache_pts(SRS_FLV_TAG_HEADER_SIZE + msg->size, pts);
        
        // Set cache to iovec.
        iovs[0].iov_base = cache;
        iovs[0].iov_len = SRS_FLV_TAG_HEADER_SIZE;