@@ -417,6 +417,16 @@ SrsProtocol::SrsProtocol(ISrsProtocolReaderWriter* io)
417417
418418 warned_c0c3_cache_dry = false ;
419419 auto_response_when_recv = true ;
420+
421+ cs_cache = new SrsChunkStream*[SRS_PERF_CHUNK_STREAM_CACHE];
422+ for (int cid = 0 ; cid < SRS_PERF_CHUNK_STREAM_CACHE; cid++) {
423+ SrsChunkStream* cs = new SrsChunkStream (cid);
424+ // set the perfer cid of chunk,
425+ // which will copy to the message received.
426+ cs->header .perfer_cid = cid;
427+
428+ cs_cache[cid] = cs;
429+ }
420430}
421431
422432SrsProtocol::~SrsProtocol ()
@@ -448,6 +458,13 @@ SrsProtocol::~SrsProtocol()
448458 free (out_iovs);
449459 out_iovs = NULL ;
450460 }
461+
462+ // free all chunk stream cache.
463+ for (int i = 0 ; i < SRS_PERF_CHUNK_STREAM_CACHE; i++) {
464+ SrsChunkStream* cs = cs_cache[i];
465+ srs_freep (cs);
466+ }
467+ srs_freep (cs_cache);
451468}
452469
453470void SrsProtocol::set_auto_response (bool v)
@@ -1102,17 +1119,30 @@ int SrsProtocol::recv_interlaced_message(SrsMessage** pmsg)
11021119 // get the cached chunk stream.
11031120 SrsChunkStream* chunk = NULL ;
11041121
1105- if (chunk_streams.find (cid) == chunk_streams.end ()) {
1106- chunk = chunk_streams[cid] = new SrsChunkStream (cid);
1107- // set the perfer cid of chunk,
1108- // which will copy to the message received.
1109- chunk->header .perfer_cid = cid;
1110- srs_verbose (" cache new chunk stream: fmt=%d, cid=%d" , fmt, cid);
1111- } else {
1112- chunk = chunk_streams[cid];
1122+ // use chunk stream cache to get the chunk info.
1123+ // @see https://github.com/winlinvip/simple-rtmp-server/issues/249
1124+ if (cid < SRS_PERF_CHUNK_STREAM_CACHE) {
1125+ // chunk stream cache hit.
1126+ srs_verbose (" cs-cache hit, cid=%d" , cid);
1127+ // already init, use it direclty
1128+ chunk = cs_cache[cid];
11131129 srs_verbose (" cached chunk stream: fmt=%d, cid=%d, size=%d, message(type=%d, size=%d, time=%" PRId64" , sid=%d)" ,
11141130 chunk->fmt , chunk->cid , (chunk->msg ? chunk->msg ->size : 0 ), chunk->header .message_type , chunk->header .payload_length ,
11151131 chunk->header .timestamp , chunk->header .stream_id );
1132+ } else {
1133+ // chunk stream cache miss, use map.
1134+ if (chunk_streams.find (cid) == chunk_streams.end ()) {
1135+ chunk = chunk_streams[cid] = new SrsChunkStream (cid);
1136+ // set the perfer cid of chunk,
1137+ // which will copy to the message received.
1138+ chunk->header .perfer_cid = cid;
1139+ srs_verbose (" cache new chunk stream: fmt=%d, cid=%d" , fmt, cid);
1140+ } else {
1141+ chunk = chunk_streams[cid];
1142+ srs_verbose (" cached chunk stream: fmt=%d, cid=%d, size=%d, message(type=%d, size=%d, time=%" PRId64" , sid=%d)" ,
1143+ chunk->fmt , chunk->cid , (chunk->msg ? chunk->msg ->size : 0 ), chunk->header .message_type , chunk->header .payload_length ,
1144+ chunk->header .timestamp , chunk->header .stream_id );
1145+ }
11161146 }
11171147
11181148 // chunk stream message header
0 commit comments