Skip to content

get codec by webrtc api: pc.getStats #4310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions trunk/research/players/js/srs.sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(", ");
}

}
25 changes: 25 additions & 0 deletions trunk/research/players/whep.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@

SessionID: <span id='sessionid'></span>

<p></p>
Audio: <span id='acodecs'></span><br/>
Video: <span id='vcodecs'></span>

<p></p>
Simulator: <a href='#' id='simulator-drop'>Drop</a>

Expand All @@ -82,9 +86,14 @@
<script type="text/javascript">
$(function(){
var sdk = null; // Global handler to do cleanup when republishing.
var statsTimer = null;
var startPlay = function() {
$('#rtc_media_player').show();

if (statsTimer) {
clearInterval(statsTimer);
}

// Close PC when user replay.
if (sdk) {
sdk.close();
Expand All @@ -97,6 +106,22 @@
// Optional callback, SDK will add track to stream.
// sdk.ontrack = function (event) { console.log('Got track', event); sdk.stream.addTrack(event.track); };

// https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/getStats
statsTimer = setInterval(() => {
sdk.pc.getStats(null).then((stats) => {
let audioStatsOutput = SrsRtcFormatStats(stats, 'audio');
let videoStatsOutput = SrsRtcFormatStats(stats, 'video');

document.querySelector("#acodecs").innerHTML = audioStatsOutput;
document.querySelector("#vcodecs").innerHTML = videoStatsOutput;

if (audioStatsOutput && videoStatsOutput) {
clearInterval(statsTimer);
console.log('Stats detected, stopping stats timer');
}
});
}, 1000);

// For example: webrtc://r.ossrs.net/live/livestream
var url = $("#txt_url").val();
sdk.play(url, {
Expand Down
27 changes: 20 additions & 7 deletions trunk/research/players/whip.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,14 @@
<script type="text/javascript">
$(function(){
var sdk = null; // Global handler to do cleanup when republishing.
var statsTimer = null;
var startPublish = function() {
$('#rtc_media_player').show();

if (statsTimer) {
clearInterval(statsTimer);
}

// Close PC when user replay.
if (sdk) {
sdk.close();
Expand All @@ -108,13 +113,21 @@
// Optional callback, SDK will add track to stream.
// sdk.ontrack = function (event) { console.log('Got track', event); sdk.stream.addTrack(event.track); };

// https://developer.mozilla.org/en-US/docs/Web/Media/Formats/WebRTC_codecs#getting_the_supported_codecs
sdk.pc.onicegatheringstatechange = function (event) {
if (sdk.pc.iceGatheringState === "complete") {
$('#acodecs').html(SrsRtcFormatSenders(sdk.pc.getSenders(), "audio"));
$('#vcodecs').html(SrsRtcFormatSenders(sdk.pc.getSenders(), "video"));
}
};
// https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/getStats
statsTimer = setInterval(() => {
sdk.pc.getStats(null).then((stats) => {
let audioStatsOutput = SrsRtcFormatStats(stats, 'audio');
let videoStatsOutput = SrsRtcFormatStats(stats, 'video');

document.querySelector("#acodecs").innerHTML = audioStatsOutput;
document.querySelector("#vcodecs").innerHTML = videoStatsOutput;

if (audioStatsOutput && videoStatsOutput) {
clearInterval(statsTimer);
console.log('Stats detected, stopping stats timer');
}
});
}, 1000);

// For example: webrtc://r.ossrs.net/live/livestream
var url = $("#txt_url").val();
Expand Down
Loading