Skip to content

Commit b6e8256

Browse files
authored
Merge pull request #339 from opentok/feature/broadcast-rtmp-bitrate
Feature/broadcast rtmp bitrate
2 parents eb070bb + 69fce3b commit b6e8256

File tree

9 files changed

+66
-15
lines changed

9 files changed

+66
-15
lines changed

src/OpenTok/Broadcast.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
* @property string $status
4949
* Broadcast state. Either `started` or `stopped`
5050
*
51+
* @property string $maxBitRate
52+
* Max Bitrate allowed for the broadcast composing. Must be between 400000 and 2000000
53+
*
5154
* @property boolean $isLowLatency
5255
* Whether the broadcast supports low-latency mode for the HLS stream.
5356
*
@@ -91,6 +94,8 @@ class Broadcast
9194
private $hasVideo;
9295
/** @ignore */
9396
private $status;
97+
/** @ignore */
98+
private $maxBitRate;
9499

95100
public function __construct($broadcastData, $options = array())
96101
{
@@ -123,6 +128,10 @@ public function __construct($broadcastData, $options = array())
123128
$this->multiBroadcastTag = $this->data['multiBroadcastTag'];
124129
}
125130

131+
if (isset($this->data['maxBitRate'])) {
132+
$this->maxBitRate = $this->data['maxBitRate'];
133+
}
134+
126135
if (isset($this->data['status'])) {
127136
$this->status = $this->data['status'];
128137
}
@@ -179,6 +188,8 @@ public function __get($name)
179188
return $this->hasVideo;
180189
case 'status':
181190
return $this->status;
191+
case 'maxBitRate':
192+
return $this->maxBitRate;
182193
default:
183194
return null;
184195
}

src/OpenTok/OpenTok.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,9 @@ public function disableForceMute(string $sessionId, array $options): bool
823823
* "1920x1080" (FHD landscape), "480x640" (SD portrait), "720x1280" (HD portrait), or "1080x1920"
824824
* (FHD portrait).</li>
825825
*
826+
* <li><code>maxBitRate</code> &mdash; Max Bitrate allowed for the broadcast composing. Must be between
827+
* 400000 and 2000000.</li>
828+
*
826829
* <li><code>outputs</code> (Array) &mdash;
827830
* Defines the HLS broadcast and RTMP streams. You can provide the following keys:
828831
* <ul>
@@ -864,6 +867,10 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas
864867
// not preferred to depend on that in the SDK because its then harder to garauntee backwards
865868
// compatibility
866869

870+
if (isset($options['maxBitRate'])) {
871+
Validators::validateBroadcastBitrate($options['maxBitRate']);
872+
}
873+
867874
if (isset($options['resolution'])) {
868875
Validators::validateResolution($options['resolution']);
869876
}
@@ -882,6 +889,7 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas
882889
'hasVideo' => true,
883890
'streamMode' => 'auto',
884891
'resolution' => '640x480',
892+
'maxBitRate' => 2000000,
885893
'outputs' => [
886894
'hls' => [
887895
'dvr' => false,
@@ -892,25 +900,21 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas
892900

893901
$options = array_merge($defaults, $options);
894902

895-
list($layout, $hasAudio, $hasVideo, $streamMode) = array_values($options);
896-
897-
// validate arguments
898903
Validators::validateSessionId($sessionId);
899-
Validators::validateLayout($layout);
900-
Validators::validateHasStreamMode($streamMode);
904+
Validators::validateLayout($options['layout']);
905+
Validators::validateHasStreamMode($options['streamMode']);
901906

902-
// make API call
903907
$broadcastData = $this->client->startBroadcast($sessionId, $options);
904908

905-
return new Broadcast($broadcastData, array('client' => $this->client));
909+
return new Broadcast($broadcastData, ['client' => $this->client]);
906910
}
907911

908912
/**
909913
* Stops a broadcast.
910914
*
911915
* @param String $broadcastId The ID of the broadcast.
912916
*/
913-
public function stopBroadcast($broadcastId)
917+
public function stopBroadcast($broadcastId): Broadcast
914918
{
915919
// validate arguments
916920
Validators::validateBroadcastId($broadcastId);
@@ -930,7 +934,7 @@ public function stopBroadcast($broadcastId)
930934
*
931935
* @return Broadcast An object with properties defining the broadcast.
932936
*/
933-
public function getBroadcast($broadcastId)
937+
public function getBroadcast($broadcastId): Broadcast
934938
{
935939
Validators::validateBroadcastId($broadcastId);
936940

src/OpenTok/Util/Validators.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,4 +486,15 @@ protected static function decodeSessionId($sessionId)
486486
}
487487
return $data;
488488
}
489+
490+
public static function validateBroadcastBitrate($maxBitRate): void
491+
{
492+
if (!is_int($maxBitRate)) {
493+
throw new \InvalidArgumentException('Max Bitrate must be a number');
494+
}
495+
496+
if ($maxBitRate < 400000 && $maxBitRate > 2000000) {
497+
throw new \OutOfBoundsException('Max Bitrate must be between 400000 and 2000000');
498+
}
499+
}
489500
}

tests/OpenTokTest/OpenTokTest.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,6 @@ public function testCannotStartBroadcastWithBothHlsAndDvrEnabled(): void
17251725

17261726
public function testStartsBroadcast(): void
17271727
{
1728-
// Arrange
17291728
$this->setupOTWithMocks([[
17301729
'code' => 200,
17311730
'headers' => [
@@ -1734,14 +1733,10 @@ public function testStartsBroadcast(): void
17341733
'path' => '/v2/project/APIKEY/broadcast/session_layout-bestfit'
17351734
]]);
17361735

1737-
// This sessionId was generated using a different apiKey, but this method doesn't do any
1738-
// decoding to check, so it's fine.
17391736
$sessionId = '2_MX44NTQ1MTF-fjE0NzI0MzU2MDUyMjN-eVgwNFJhZmR6MjdockFHanpxNzBXaEFXfn4';
17401737

1741-
// Act
17421738
$broadcast = $this->opentok->startBroadcast($sessionId);
17431739

1744-
// Assert
17451740
$this->assertCount(1, $this->historyContainer);
17461741

17471742
$request = $this->historyContainer[0]['request'];
@@ -1767,9 +1762,34 @@ public function testStartsBroadcast(): void
17671762
$this->assertEquals('auto', $broadcast->streamMode);
17681763
}
17691764

1765+
public function testStartsBroadcastWithMaxBitrate(): void
1766+
{
1767+
$this->setupOTWithMocks([[
1768+
'code' => 200,
1769+
'headers' => [
1770+
'Content-Type' => 'application/json'
1771+
],
1772+
'path' => '/v2/project/APIKEY/broadcast/session_layout-bestfit'
1773+
]]);
1774+
1775+
$sessionId = '2_MX44NTQ1MTF-fjE0NzI0MzU2MDUyMjN-eVgwNFJhZmR6MjdockFHanpxNzBXaEFXfn4';
1776+
1777+
$broadcast = $this->opentok->startBroadcast($sessionId, [
1778+
'maxBitRate' => 2000000
1779+
]);
1780+
1781+
$this->assertIsString($broadcast->id);
1782+
$this->assertEquals($sessionId, $broadcast->sessionId);
1783+
$this->assertIsArray($broadcast->broadcastUrls);
1784+
$this->assertArrayHasKey('hls', $broadcast->broadcastUrls);
1785+
$this->assertIsString($broadcast->broadcastUrls['hls']);
1786+
$this->assertIsString($broadcast->hlsUrl);
1787+
$this->assertFalse($broadcast->isStopped);
1788+
$this->assertEquals(2000000, $broadcast->maxBitRate);
1789+
}
1790+
17701791
public function testStartsBroadcastWithMultiBroadcastTag(): void
17711792
{
1772-
// Arrange
17731793
$this->setupOTWithMocks([[
17741794
'code' => 200,
17751795
'headers' => [

tests/mock/v2/project/APIKEY/broadcast/BROADCASTID/start_default

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"status":"started",
2525
"partnerId":854511,
2626
"maxDuration":5400,
27+
"maxBitRate": 2000000,
2728
"resolution": "1280x720",
2829
"streamMode": "auto",
2930
"hasAudio": true,

tests/mock/v2/project/APIKEY/broadcast/BROADCASTID/start_dvr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"status":"started",
2222
"partnerId":854511,
2323
"maxDuration":5400,
24+
"maxBitRate": 2000000,
2425
"resolution": "1280x720",
2526
"streamMode": "auto",
2627
"hasAudio": true,

tests/mock/v2/project/APIKEY/broadcast/BROADCASTID/start_ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"status":"started",
2525
"partnerId":854511,
2626
"maxDuration":5400,
27+
"maxBitRate": 2000000,
2728
"resolution": "1280x720",
2829
"streamMode": "auto",
2930
"hasAudio": true,

tests/mock/v2/project/APIKEY/broadcast/session_layout-bestfit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"status":"started",
2121
"partnerId":854511,
2222
"maxDuration":5400,
23+
"maxBitRate": 2000000,
2324
"resolution": "1280x720",
2425
"streamMode": "auto"
2526
}

tests/mock/v2/project/APIKEY/broadcast/session_manual_stream

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"status":"started",
2121
"partnerId":854511,
2222
"maxDuration":5400,
23+
"maxBitRate": 2000000,
2324
"resolution": "1280x720",
2425
"streamMode": "manual"
2526
}

0 commit comments

Comments
 (0)