From 55afc6700c7b7fc6aeabaab8c22593badfaa92de Mon Sep 17 00:00:00 2001
From: cnderrauber <zengjie9004@gmail.com>
Date: Mon, 13 May 2024 18:17:31 +0800
Subject: [PATCH] Ensure DD ext for svc codecs (#1132)

---
 .changeset/spicy-coats-approve.md        |  5 +++++
 src/room/PCTransport.ts                  | 17 ++++++++++-------
 src/room/participant/LocalParticipant.ts |  3 +--
 src/room/participant/publishUtils.ts     |  5 +++++
 4 files changed, 21 insertions(+), 9 deletions(-)
 create mode 100644 .changeset/spicy-coats-approve.md

diff --git a/.changeset/spicy-coats-approve.md b/.changeset/spicy-coats-approve.md
new file mode 100644
index 0000000000..13cbbf9be0
--- /dev/null
+++ b/.changeset/spicy-coats-approve.md
@@ -0,0 +1,5 @@
+---
+'livekit-client': patch
+---
+
+Ensure DD ext for svc codecs
diff --git a/src/room/PCTransport.ts b/src/room/PCTransport.ts
index 5459b2972f..9eb6f63a28 100644
--- a/src/room/PCTransport.ts
+++ b/src/room/PCTransport.ts
@@ -266,8 +266,6 @@ export default class PCTransport extends EventEmitter {
       if (media.type === 'audio') {
         ensureAudioNackAndStereo(media, [], []);
       } else if (media.type === 'video') {
-        ensureVideoDDExtensionForSVC(media);
-        // mung sdp for codec bitrate setting that can't apply by sendEncoding
         this.trackBitrates.some((trackbr): boolean => {
           if (!media.msid || !trackbr.cid || !media.msid.includes(trackbr.cid)) {
             return false;
@@ -286,6 +284,16 @@ export default class PCTransport extends EventEmitter {
             return true;
           }
 
+          if (isSVCCodec(trackbr.codec)) {
+            ensureVideoDDExtensionForSVC(media);
+          }
+
+          // TODO: av1 slow starting issue already fixed in chrome 124, clean this after some versions
+          // mung sdp for av1 bitrate setting that can't apply by sendEncoding
+          if (trackbr.codec !== 'av1') {
+            return true;
+          }
+
           const startBitrate = Math.round(trackbr.maxbr * startBitrateForSVC);
 
           for (const fmtp of media.fmtp) {
@@ -552,11 +560,6 @@ function ensureVideoDDExtensionForSVC(
     payloads?: string | undefined;
   } & MediaDescription,
 ) {
-  const codec = media.rtp[0]?.codec?.toLowerCase();
-  if (!isSVCCodec(codec)) {
-    return;
-  }
-
   let maxID = 0;
   const ddFound = media.ext?.some((ext): boolean => {
     if (ext.uri === ddExtensionURI) {
diff --git a/src/room/participant/LocalParticipant.ts b/src/room/participant/LocalParticipant.ts
index 292d1403f7..2d4e730603 100644
--- a/src/room/participant/LocalParticipant.ts
+++ b/src/room/participant/LocalParticipant.ts
@@ -880,8 +880,7 @@ export default class LocalParticipant extends Participant {
             maxbr: encodings[0]?.maxBitrate ? encodings[0].maxBitrate / 1000 : 0,
           });
         }
-      } else if (track.codec && track.codec == 'av1' && encodings[0]?.maxBitrate) {
-        // AV1 requires setting x-start-bitrate in SDP
+      } else if (track.codec && isSVCCodec(track.codec) && encodings[0]?.maxBitrate) {
         this.engine.pcManager.publisher.setTrackCodecBitrate({
           cid: req.cid,
           codec: track.codec,
diff --git a/src/room/participant/publishUtils.ts b/src/room/participant/publishUtils.ts
index 0026b93d48..d8fa4417a1 100644
--- a/src/room/participant/publishUtils.ts
+++ b/src/room/participant/publishUtils.ts
@@ -171,6 +171,11 @@ export function computeVideoEncodings(
       });
     }
 
+    if (original.encoding.priority) {
+      encodings[0].priority = original.encoding.priority;
+      encodings[0].networkPriority = original.encoding.priority;
+    }
+
     log.debug(`using svc encoding`, { encodings });
     return encodings;
   }