- Add Metered and ConvexSol (CoTURN) provider drivers with response validation and caching - Add IceCandidateManager with automatic failover and structured logging - Add IceCandidateProviderInterface and DTOs conforming to RTCIceServer format
62 lines
2.0 KiB
PHP
62 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\IceCandidate\Providers;
|
|
|
|
use App\Contracts\IceCandidateProviderInterface;
|
|
use App\DTOs\IceCandidatesDto;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class ConvexSolIceCandidateProvider implements IceCandidateProviderInterface
|
|
{
|
|
public function __construct(
|
|
protected array $config = []
|
|
) {}
|
|
|
|
public function getIceCandidates(): IceCandidatesDto
|
|
{
|
|
$cacheKey = 'convexsol_coturn_credentials_cache_'.md5(serialize($this->config));
|
|
$cacheTtl = (int) ($this->config['cache_ttl'] ?? 600);
|
|
|
|
$servers = Cache::remember($cacheKey, now()->addSeconds($cacheTtl), function () {
|
|
$list = [];
|
|
|
|
// 1. STUN servers
|
|
$stunUrls = $this->config['stun_urls'] ?? ['stun:stun.convexsol.com:3478', 'stun:stun.l.google.com:19302'];
|
|
if (is_string($stunUrls)) {
|
|
$stunUrls = array_filter(array_map('trim', explode(',', $stunUrls)));
|
|
}
|
|
if (! empty($stunUrls)) {
|
|
$list[] = [
|
|
'urls' => array_values($stunUrls),
|
|
];
|
|
}
|
|
|
|
// 2. TURN servers with username and password
|
|
$turnUrls = $this->config['turn_urls'] ?? ['turn:turn.convexsol.com:3478', 'turns:turn.convexsol.com:5349'];
|
|
if (is_string($turnUrls)) {
|
|
$turnUrls = array_filter(array_map('trim', explode(',', $turnUrls)));
|
|
}
|
|
|
|
$username = $this->config['username'] ?? null;
|
|
$password = $this->config['password'] ?? null;
|
|
|
|
if (! empty($turnUrls)) {
|
|
$turnEntry = [
|
|
'urls' => array_values($turnUrls),
|
|
];
|
|
if ($username !== null) {
|
|
$turnEntry['username'] = $username;
|
|
}
|
|
if ($password !== null) {
|
|
$turnEntry['credential'] = $password;
|
|
}
|
|
$list[] = $turnEntry;
|
|
}
|
|
|
|
return $list;
|
|
});
|
|
|
|
return IceCandidatesDto::fromArray($servers);
|
|
}
|
|
}
|