From 07e7984fdfa7d2cc672ebe3dcdcb1a1d73f61b54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Haibo=20Chen=28=E9=99=88=E6=B5=B7=E5=8D=9A=29?= <495810242@qq.com> Date: Wed, 4 Jun 2025 22:28:46 +0800 Subject: [PATCH] Player: Get codec by webrtc api: pc.getStats. v7.0.42 (#4310) 1. It cannot retrieve codec information on `Firefox` by `getSenders/getReceivers` 2. It can retrieve codec information on `Chrome` by `getReceivers`, but incorrect, like this: ![image](https://github.com/user-attachments/assets/e0bb93b1-ccd0-46c0-ae21-074934f66a1e) 3. So, we retrieve codec information from `getStats`, and it works well. 4. The timer is used because sometimes the codec cannot be retrieved when `iceGatheringState` is `complete`. 5. Testing has been completed on the browsers listed below. - [x] Chrome - [x] Edge - [x] Safari - [x] Firefox --------- Co-authored-by: winlin --- trunk/doc/CHANGELOG.md | 1 + trunk/research/players/js/srs.sdk.js | 40 +++++++++++++--------------- trunk/research/players/whep.html | 25 +++++++++++++++++ trunk/research/players/whip.html | 27 ++++++++++++++----- trunk/src/core/srs_core_version7.hpp | 2 +- 5 files changed, 65 insertions(+), 30 deletions(-) diff --git a/trunk/doc/CHANGELOG.md b/trunk/doc/CHANGELOG.md index 033f4d39f..f0485522b 100644 --- a/trunk/doc/CHANGELOG.md +++ b/trunk/doc/CHANGELOG.md @@ -7,6 +7,7 @@ The changelog for SRS. ## SRS 7.0 Changelog +* v7.0, 2025-06-04, Merge [#4310](https://github.com/ossrs/srs/pull/4310): Player: Get codec by webrtc api: pc.getStats. v7.0.42 (#4310) * v7.0, 2025-06-04, Merge [#4325](https://github.com/ossrs/srs/pull/4325): fix bug: loop transcoding #3516. v7.0.41 (#4325) * v7.0, 2025-06-04, Merge [#4341](https://github.com/ossrs/srs/pull/4341): Update the release in the README for consistent. v7.0.40 (#4341) * v7.0, 2025-06-04, Merge [#4368](https://github.com/ossrs/srs/pull/4368): Update the codename for version 7.0 to "Kai". v7.0.39 (#4368) diff --git a/trunk/research/players/js/srs.sdk.js b/trunk/research/players/js/srs.sdk.js index e86e46813..7d49ddc79 100644 --- a/trunk/research/players/js/srs.sdk.js +++ b/trunk/research/players/js/srs.sdk.js @@ -686,33 +686,29 @@ function SrsRtcWhipWhepAsync() { return self; } -// Format the codec of RTCRtpSender, kind(audio/video) is optional filter. -// https://developer.mozilla.org/en-US/docs/Web/Media/Formats/WebRTC_codecs#getting_the_supported_codecs -function SrsRtcFormatSenders(senders, kind) { +// https://developer.mozilla.org/en-US/docs/Web/API/RTCStatsReport +function SrsRtcFormatStats(stats, kind) { var codecs = []; - senders.forEach(function (sender) { - var params = sender.getParameters(); - params && params.codecs && params.codecs.forEach(function(c) { - if (kind && sender.track.kind !== kind) { - return; - } - - if (c.mimeType.indexOf('/red') > 0 || c.mimeType.indexOf('/rtx') > 0 || c.mimeType.indexOf('/fec') > 0) { - return; - } - + stats.forEach((report) => { + if (report.type === 'codec' && report.mimeType?.toLowerCase().startsWith(kind)) { var s = ''; - s += c.mimeType.replace('audio/', '').replace('video/', ''); - s += ', ' + c.clockRate + 'HZ'; - if (sender.track.kind === "audio") { - s += ', channels: ' + c.channels; + s += report.mimeType.split('/')[1] || report.mimeType; + + if (report.clockRate) { + s += ', ' + report.clockRate + 'HZ'; } - s += ', pt: ' + c.payloadType; + if (kind === 'audio' && report.channels) { + s += ', channels: ' + report.channels; + } + + if (report.payloadType) { + s += ', pt: ' + report.payloadType; + } + codecs.push(s); - }); + } }); return codecs.join(", "); -} - +} \ No newline at end of file diff --git a/trunk/research/players/whep.html b/trunk/research/players/whep.html index a1939e86a..b4a5b24da 100644 --- a/trunk/research/players/whep.html +++ b/trunk/research/players/whep.html @@ -71,6 +71,10 @@ SessionID: +

+ Audio:
+ Video: +

Simulator: Drop @@ -82,9 +86,14 @@