add stats and state changes in echo example
This commit is contained in:
1 parent
ea698850a6
commit
253a9b95e1
2 files changed
+69
-10
No files matched your search
+2
-2
@@ -117,7 +117,7 @@
|
||||
?>
|
||||
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
|
||||
<script type="application/javascript" src="signaling.global.js"></script>
|
||||
<script type="application/javascript" src="index.js"></script>
|
||||
<script type="application/javascript" src="/echo/signaling.global.js"></script>
|
||||
<script type="application/javascript" src="/echo/index.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
+67
-8
@@ -1,17 +1,22 @@
|
||||
<?php
|
||||
|
||||
use React\EventLoop\Loop;
|
||||
use Webrtc\DataChannel\RTCDataChannel;
|
||||
use Webrtc\RTP\MediaStreamTrack\MediaStreamTrack;
|
||||
use Webrtc\SDP\RTCSessionDescription;
|
||||
use Webrtc\Signaling\Adapters\WebSocketSignalingAdapter;
|
||||
use Webrtc\Signaling\Request;
|
||||
use Webrtc\Signaling\RTCSignaling;
|
||||
//use Webrtc\Stats\RTCInboundRtpStreamStats;
|
||||
//use Webrtc\Stats\RTCOutboundRtpStreamStats;
|
||||
use Webrtc\Stats\RTCTransportStats;
|
||||
use Webrtc\Webrtc\Enum\ConnectionState;
|
||||
use Webrtc\Webrtc\RTCPeerConnection;
|
||||
use function React\Async\async;
|
||||
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
|
||||
function createOffer(Request $request): void
|
||||
{
|
||||
// TODO: If you want to authorize user before accessing to the WebRTC
|
||||
@@ -21,25 +26,79 @@ function createOffer(Request $request): void
|
||||
// }
|
||||
|
||||
async(function () use ($request) {
|
||||
$loop = Loop::get();
|
||||
$pc = new RTCPeerConnection();
|
||||
|
||||
//Stats
|
||||
$lastBytesSent = 0;
|
||||
$lastBytesReceived = 0;
|
||||
$statsPeriodicCheck = $loop->addPeriodicTimer(1, function () use ($pc, &$lastBytesSent, &$lastBytesReceived) {
|
||||
$stats = $pc->getStats();
|
||||
|
||||
$bytesSent = 0;
|
||||
$bytesReceived = 0;
|
||||
|
||||
foreach ($stats->getStats() as $stat) {
|
||||
// if ($stat instanceof RTCOutboundRTPStreamStats) {
|
||||
// $bytesSent += $stat->bytesSent;
|
||||
// }
|
||||
//
|
||||
// if ($stat instanceof RTCInboundRTPStreamStats) {
|
||||
// $bytesReceived += $stat->packetsReceived; // TODO: it is a bug in it
|
||||
// }
|
||||
|
||||
if ($stat instanceof RTCTransportStats) {
|
||||
$bytesSent += $stat->bytesSent;
|
||||
$bytesReceived += $stat->bytesReceived;
|
||||
}
|
||||
}
|
||||
|
||||
echo sprintf(
|
||||
"\rUpload Bandwidth: %d Kbps | Download Bandwidth: %d Kbps",
|
||||
intval(($bytesSent - $lastBytesSent) / 1024),
|
||||
intval(($bytesReceived - $lastBytesReceived) / 1024)
|
||||
);
|
||||
|
||||
$lastBytesSent = $bytesSent;
|
||||
$lastBytesReceived = $bytesReceived;
|
||||
});
|
||||
|
||||
// state changes trigger
|
||||
$pc->on("connectionstatechange", function () use ($pc, $loop, $statsPeriodicCheck) {
|
||||
if ($pc->getConnectionState() === ConnectionState::failed) {
|
||||
$pc->close();
|
||||
}
|
||||
|
||||
if (in_array($pc->getConnectionState(), [ConnectionState::closed, ConnectionState::failed])) {
|
||||
$loop->cancelTimer($statsPeriodicCheck);
|
||||
}
|
||||
echo sprintf("connectionstatechange: %s\n", $pc->getConnectionState()->name);
|
||||
});
|
||||
|
||||
$pc->on("iceconnectionstatechange", function () use ($pc) {
|
||||
echo sprintf("iceconnectionstatechange: %s\n", $pc->getIceConnectionState()->name);
|
||||
});
|
||||
|
||||
$pc->on("icegatheringstatechange", function () use ($pc) {
|
||||
echo sprintf("icegatheringstatechange: %s\n", $pc->getIceGatheringState()->name);
|
||||
});
|
||||
|
||||
$pc->on("signalingstatechange", function () use ($pc) {
|
||||
echo sprintf("signalingstatechange: %s\n", $pc->getSignalingState()->name);
|
||||
});
|
||||
|
||||
// Echo Datachannel
|
||||
$pc->on("datachannel", function (RTCDataChannel $channel) {
|
||||
$channel->on("message", function (string $message) use ($channel) {
|
||||
$channel->send($message);
|
||||
});
|
||||
});
|
||||
|
||||
$pc->on("connectionstatechange", function () use ($pc) {
|
||||
echo sprintf("connectionstatechange: %s\n", $pc->getConnectionState()->name);
|
||||
if ($pc->getConnectionState() === ConnectionState::failed) {
|
||||
$pc->close();
|
||||
}
|
||||
});
|
||||
|
||||
// Echo Track without a filter
|
||||
$pc->on("track", function (MediaStreamTrack $track) use ($pc) {
|
||||
// var_dump($track->getKind()->name . " => " . spl_object_id($track));
|
||||
$pc->addTrack($track);
|
||||
|
||||
//If you went to end the stream
|
||||
// $event->track->on("ended", function () use ($pc) {
|
||||
// echo "track ended\n";
|
||||
// });
|
||||
|
||||
Reference in new issue
Block a user