From 4f0abf7eeca25de444638007c47da82a3faf0fd8 Mon Sep 17 00:00:00 2001 From: Michael Lynch Date: Thu, 3 Nov 2022 12:03:16 -0400 Subject: [PATCH] Assign stream index on outgoing RTP packets (#182) * Assign stream index on outgoing RTP packets (#5) * Correctly assign mindex on outgoing rtp packets Previously mindex was not set and defaulted to zero. This lead to most packets getting dropped because of sequence number reuse when streaming both audio and video. This reorders the SDP entries for video and audio so that video is first in both a video-only and a audio+video configuration. This means that the mindex for video packets should always be zero, and for audio (if present) should always be one. This assumes there will never be an audio-only configuration. * Adjust comments Co-authored-by: Michael Lynch * Add preprocessor conditional to guard packet.mindex setting The mindex field wasn't added to the janus_plugin_rtp_packet until Janus 1.0, so this change adds a precompiler check to ensure JANUS_PLUGIN_API_VERSION is >= 100 before assigning a value to the mindex field. * Preserve audio-then-video ordering for Janus 0.x Co-authored-by: Louis Goessling --- janus/src/client.c | 5 +++++ janus/src/plugin.c | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/janus/src/client.c b/janus/src/client.c index 6f32368..6436a91 100644 --- a/janus/src/client.c +++ b/janus/src/client.c @@ -102,6 +102,11 @@ static void *_common_thread(void *v_client, bool video) { packet.video = rtp->video; packet.buffer = (char *)rtp->datagram; packet.length = rtp->used; +#if JANUS_PLUGIN_API_VERSION >= 100 + // The uStreamer Janus plugin places video in stream index 0 and audio + // (if available) in stream index 1. + packet.mindex = (rtp->video ? 0 : 1); +#endif janus_plugin_rtp_extensions_reset(&packet.extensions); // FIXME: Это очень эффективный способ уменьшить задержку, но WebRTC стек в хроме и фоксе // слишком корявый, чтобы обработать это, из-за чего на кейфреймах начинаются заикания. diff --git a/janus/src/plugin.c b/janus/src/plugin.c index d4e467e..91b0392 100644 --- a/janus/src/plugin.c +++ b/janus/src/plugin.c @@ -440,7 +440,14 @@ static struct janus_plugin_result *_plugin_handle_message( "s=PiKVM uStreamer" RN "t=0 0" RN "%s%s", +#if JANUS_PLUGIN_API_VERSION >= 100 + // Place video SDP before audio SDP so that the video and audio streams + // have predictable indices, even if audio is not available. + us_get_now_id() >> 1, video_sdp, audio_sdp +#else + // For versions of Janus prior to 1.x, place the audio SDP first. us_get_now_id() >> 1, audio_sdp, video_sdp +#endif ); free(audio_sdp); free(video_sdp);