AirenSoft / OvenPlayer

OvenPlayer is JavaScript-based LLHLS and WebRTC Player for OvenMediaEngine.
https://OvenMediaEngine.com/ovenplayer
MIT License
508 stars 124 forks source link

Is there a way to get the RTCPeerConnection instance from the player when playing webrtc video #358

Closed mgara closed 1 year ago

mgara commented 1 year ago

I want to display the fps on the video, is there a way to do that knowing that the functionality exists if the RTCPeerConnection is exposed as follows :

// pc is the RTCPeerConnection instance and
// sender is the RTCRtpSender for the video track.

pc.getStats(sender.track).then(function(stats) {
    stats.forEach(report => {
        if(report.type === 'outbound-rtp' && report.kind === 'video') {
            console.log(`Frames per second: ${report.framesEncoded}`);
        }
    });
});
SangwonOh commented 1 year ago

@mgara Hi. You can use this event.


const player = OvenPlayer.create('player', {
        sources: [
            {
                type: 'webrtc',
                file: 'xxx'
            },
        ]
    });

player.on('peerConnectionPrepared', function (myPeerConnection) {
        setInterval(() => {
            myPeerConnection.getStats(null).then((stats) => {
                let statsOutput = "";

                stats.forEach((report) => {
                    statsOutput +=
                        `<h2>Report: ${report.type}</h2>\n<strong>ID:</strong> ${report.id}<br>\n` +
                        `<strong>Timestamp:</strong> ${report.timestamp}<br>\n`;

                    Object.keys(report).forEach((statName) => {
                        if (
                            statName !== "id" &&
                            statName !== "timestamp" &&
                            statName !== "type"
                        ) {
                            statsOutput += `<strong>${statName}:</strong> ${report[statName]}<br>\n`;
                        }
                    });
                });

                document.querySelector(".stats-box").innerHTML = statsOutput;
            });
        }, 1000);
    });

    player.on('peerConnectionDestroyed', function (pc) {
        console.log('PeerConnection has been destroyed >A<')
    });
mgara commented 1 year ago

Thanks @SangwonOh appreciate your answer, tested and worked !