Skip to content

Commit c053061

Browse files
author
QuasarStream Team
committed
add ice port range in configuration for host candidates
1 parent 8b023cb commit c053061

File tree

3 files changed

+54
-45
lines changed

3 files changed

+54
-45
lines changed

README.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ guide for detailed instructions on how to get involved. Together, we can make a
6464
### Our Plans for the Future
6565
We are not stopping here. We're actively continuing development on the PHP WebRTC packages.
6666

67-
Right now, we're working privately on a **[Selective Forwarding Unit (SFU)](https://getstream.io/resources/projects/webrtc/architectures/sfu/)** implementation and a **Laravel package that bundles everything together**, including this WebRTC package. Once that's ready, our goal is to build a **minimal video conferencing web** app using Laravel, and to continue maintaining this repository along with 24 other related packages.
67+
Right now, we're working privately on a **[Selective Forwarding Unit (SFU)](https://quasarstream.com/blog/sfu/)** implementation and a **Laravel package that bundles everything together**, including this WebRTC package. Once that's ready, our goal is to build a **minimal video conferencing web** app using Laravel, and to continue maintaining this repository along with 24 other related packages.
6868

6969
If you're interested in building real-time communication tools in PHP, like a video conferencing app, you are more than welcome to join the project. Fork the repos, contribute to them, and help grow this community.
7070

@@ -79,16 +79,6 @@ If that sounds like you, this could be a great place to get involved.
7979

8080
Feel free to reach out by email(via github@aminyazdanpanah.com) with your GitHub username (for example: github.com/your-username) and we’ll get back to you soon.
8181

82-
### About Git History
83-
We've been working on this repository and many other PHP WebRTC-related ones
84-
(**such as ICE, RTP, RTCP, and more than 22 others**)
85-
for a long time privately(in our git server) before making them open source(**We released our packages only after they have been fully tested and thoroughly debugged**).
86-
Originally, there was a long commit history that reflected all our work.
87-
88-
However, we decided to **remove that history** in the initial public commit to **protect our privacy**. The original commits included details like our **working hours based on commit times and counts**, as well as our **personal email addresses**, which we did not feel comfortable sharing publicly.
89-
90-
**Removing the history helps us keep that information private and stay a bit safer from potential security risks.**
91-
9282

9383
## License
9484

src/RTCConfiguration.php

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Webrtc\Webrtc;
1313

1414
use Webrtc\Exception\InvalidArgumentException;
15+
use Webrtc\ICE\Enum\TransportPolicyType;
1516
use Webrtc\ICE\RTCIceServer;
1617
use Webrtc\ICE\RTCIceServerInterface;
1718

@@ -52,21 +53,23 @@ class RTCConfiguration implements RTCConfigurationInterface
5253
*/
5354
private ?string $privateKeyPath = null;
5455

56+
private ?array $icePortRange = null;
57+
private ?TransportPolicyType $transportPolicy = null;
58+
5559
/**
5660
* Constructs a new RTCConfiguration instance
5761
*
58-
* @param array<RTCIceServerInterface> $iceServers
59-
* @param string|null $certificatePath
60-
* @param string|null $privateKeyPath
62+
* @param array|null $configuration Optional configuration array. If null,
63+
* default configuration with a Google STUN server will be used.
64+
* Expected keys:
65+
* - 'iceServers': Array of ICE server configurations
66+
* - 'certificatePath': Path to a certificate file (optional)
67+
* - 'privateKeyPath': Path to a private key file (optional)
68+
* @throws InvalidArgumentException If iceServer configuration is invalid
6169
*/
62-
public function __construct(
63-
array $iceServers = [],
64-
?string $certificatePath = null,
65-
?string $privateKeyPath = null
66-
) {
67-
$this->iceServers = empty($iceServers) ? $this->getDefaultIceServer() : $iceServers;
68-
$this->certificatePath = $certificatePath;
69-
$this->privateKeyPath = $privateKeyPath;
70+
public function __construct(?array $configuration = null)
71+
{
72+
$configuration !== null ? $this->parseConfiguration($configuration) : $this->getDefaultConfiguration();
7073
}
7174

7275
/**
@@ -150,11 +153,11 @@ public function setPrivateKeyPath(?string $privateKeyPath): void
150153
* - 'iceServers': Array of ICE server configurations (required if present)
151154
* - 'certificatePath': Path to a certificate file (optional)
152155
* - 'privateKeyPath': Path to a private key file (optional)
153-
* @return RTCConfiguration
156+
* @return void
157+
* @throws InvalidArgumentException If iceServer configuration is missing required 'urls' key
154158
*/
155-
public static function parseConfiguration(array $configuration): self
159+
private function parseConfiguration(array $configuration): void
156160
{
157-
$iceServers = [];
158161
if (isset($configuration['iceServers'])) {
159162
foreach ($configuration["iceServers"] as $iceServer) {
160163
if (!isset($iceServer["urls"])) {
@@ -167,28 +170,46 @@ public static function parseConfiguration(array $configuration): self
167170
$iceServerObj->setCredential($iceServer["credential"] ?? null);
168171
$iceServerObj->setCredentialType($iceServer["credentialType"] ?? null);
169172

170-
$iceServers[] = $iceServerObj;
173+
$this->addIceServer($iceServerObj);
171174
}
172-
}else{
173-
$iceServers = (new RTCConfiguration)->getDefaultIceServer();
174175
}
175176

176-
$certificatePath = $configuration["certificatePath"] ?? null;
177-
$privateKeyPath = $configuration["privateKeyPath"] ?? null;
178-
179-
return new static($iceServers, $certificatePath, $privateKeyPath);
177+
$this->certificatePath = $configuration["certificatePath"] ?? null;
178+
$this->privateKeyPath = $configuration["privateKeyPath"] ?? null;
180179
}
181180

182181
/**
183-
* return the default ice configuration with Google's public STUN server
182+
* Sets up default configuration with Google's public STUN server
184183
*
185-
* @return array<RTCIceServer>
184+
* @return void
186185
*/
187-
private function getDefaultIceServer(): array
186+
private function getDefaultConfiguration(): void
188187
{
189188
$iceServer = new RTCIceServer();
190189
$iceServer->setUrls([self::DEFAULT_STUN_SERVER]);
190+
$this->addIceServer($iceServer);
191+
}
191192

192-
return [$iceServer];
193+
public function getIcePortRange(): ?array
194+
{
195+
return $this->icePortRange;
196+
}
197+
198+
public function setIcePortRange(int $minPort, int $maxPort): void
199+
{
200+
if ($maxPort- $minPort < 100) {
201+
throw new InvalidArgumentException("maxPort - minPort must be greater than 100");
202+
}
203+
$this->icePortRange = [$minPort, $maxPort];
204+
}
205+
206+
public function getTransportPolicy(): ?TransportPolicyType
207+
{
208+
return $this->transportPolicy;
209+
}
210+
211+
public function setTransportPolicy(?TransportPolicyType $transportPolicy): void
212+
{
213+
$this->transportPolicy = $transportPolicy;
193214
}
194215
}

src/RTCPeerConnection.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,13 @@ class RTCPeerConnection extends EventEmitter implements RTCPeerConnectionInterfa
245245
/**
246246
* Creates a new RTCPeerConnection instance.
247247
*
248-
* @param array|RTCConfigurationInterface $configuration Configuration options for the connection
249-
* @throws DateInvalidOperationException If there's an SSL-related error
250-
* @throws OpenSSLException If there's an SSL-related error
248+
* @param array|RTCConfiguration|null $configuration Configuration options for the connection
251249
* @throws RTCCertificateException If certificate generation fails
250+
* @throws OpenSSLException|DateInvalidOperationException If there's an SSL-related error
252251
*/
253-
public function __construct(array|RTCConfigurationInterface $configuration = [])
252+
public function __construct(null|array|RTCConfigurationInterface $configuration = null)
254253
{
255-
$this->configuration = $configuration instanceof RTCConfigurationInterface ? $configuration : RTCConfiguration::parseConfiguration($configuration);
254+
$this->configuration = $configuration instanceof RTCConfigurationInterface ? $configuration : new RTCConfiguration($configuration);
256255
$this->certificates[] = new RTCCertificate($this->configuration->getPrivateKeyPath(), $this->configuration->getCertificatePath());
257256
$this->cname = Uuid::uuid4()->toString();
258257
$this->streamId = Uuid::uuid4()->toString();
@@ -735,7 +734,7 @@ private function createSctpTransport(): void
735734
private function createDtlsTransport(): RTCDtlsTransport
736735
{
737736
// create ICE transport
738-
$iceGatherer = new RTCIceGatherer($this->configuration->getIceServers());
737+
$iceGatherer = new RTCIceGatherer($this->configuration->getIceServers(), $this->configuration->getIcePortRange(), $this->configuration->getTransportPolicy(), logger: $this->logger);
739738
$iceGatherer->on("statechange", fn() => $this->updateIceGatheringState());
740739
$iceTransport = new RTCIceTransport($iceGatherer, $this->logger);
741740
$iceTransport->on("statechange", fn() => $this->updateIceConnectionState());
@@ -977,14 +976,14 @@ public function setLocalDescription(RTCSessionDescription $rtcSessionDescription
977976
}
978977
}
979978

980-
async(fn() => $this->connect())();
981-
982979
if ($sessionDescription->isType("answer")) {
983980
$this->currentLocalDescription = $sessionDescription;
984981
$this->pendingLocalDescription = null;
985982
} else {
986983
$this->pendingLocalDescription = $sessionDescription;
987984
}
985+
986+
async(fn() => $this->connect())();
988987
})();
989988
}
990989

@@ -1246,7 +1245,6 @@ private function removeBundleTransport(GroupDescription $bundleGroupDescription,
12461245
}
12471246
}
12481247

1249-
12501248
if ($this->sctp and $this->sctp->getMid() == $masterMid) {
12511249
$masterTransport = $this->sctp->getDtlsTransport();
12521250
}

0 commit comments

Comments
 (0)