diff --git a/libavformat/rtspenc.c b/libavformat/rtspenc.c index c0436c35e0..0761783b25 100644 --- a/libavformat/rtspenc.c +++ b/libavformat/rtspenc.c @@ -69,13 +69,19 @@ static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st) AVFormatContext *rtpctx = rtsp_st->transport_priv; uint8_t *buf, *ptr; int size; - uint8_t interleave_header[4]; + uint8_t *interleave_header, *interleaved_packet; size = url_close_dyn_buf(rtpctx->pb, &buf); ptr = buf; while (size > 4) { uint32_t packet_len = AV_RB32(ptr); int id; + /* The interleaving header is exactly 4 bytes, which happens to be + * the same size as the packet length header from + * url_open_dyn_packet_buf. So by writing the interleaving header + * over these bytes, we get a consecutive interleaved packet + * that can be written in one call. */ + interleaved_packet = interleave_header = ptr; ptr += 4; size -= 4; if (packet_len > size || packet_len < 2) @@ -87,8 +93,7 @@ static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st) interleave_header[0] = '$'; interleave_header[1] = id; AV_WB16(interleave_header + 2, packet_len); - url_write(rt->rtsp_hd, interleave_header, 4); - url_write(rt->rtsp_hd, ptr, packet_len); + url_write(rt->rtsp_hd, interleaved_packet, 4 + packet_len); ptr += packet_len; size -= packet_len; }