Skip to content

Commit

Permalink
Merge pull request #339 from opentok/feature/broadcast-rtmp-bitrate
Browse files Browse the repository at this point in the history
Feature/broadcast rtmp bitrate
  • Loading branch information
SecondeJK committed Aug 22, 2023
2 parents eb070bb + 69fce3b commit b6e8256
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 15 deletions.
11 changes: 11 additions & 0 deletions src/OpenTok/Broadcast.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
* @property string $status
* Broadcast state. Either `started` or `stopped`
*
* @property string $maxBitRate
* Max Bitrate allowed for the broadcast composing. Must be between 400000 and 2000000
*
* @property boolean $isLowLatency
* Whether the broadcast supports low-latency mode for the HLS stream.
*
Expand Down Expand Up @@ -91,6 +94,8 @@ class Broadcast
private $hasVideo;
/** @ignore */
private $status;
/** @ignore */
private $maxBitRate;

public function __construct($broadcastData, $options = array())
{
Expand Down Expand Up @@ -123,6 +128,10 @@ public function __construct($broadcastData, $options = array())
$this->multiBroadcastTag = $this->data['multiBroadcastTag'];
}

if (isset($this->data['maxBitRate'])) {
$this->maxBitRate = $this->data['maxBitRate'];
}

if (isset($this->data['status'])) {
$this->status = $this->data['status'];
}
Expand Down Expand Up @@ -179,6 +188,8 @@ public function __get($name)
return $this->hasVideo;
case 'status':
return $this->status;
case 'maxBitRate':
return $this->maxBitRate;
default:
return null;
}
Expand Down
22 changes: 13 additions & 9 deletions src/OpenTok/OpenTok.php
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,9 @@ public function disableForceMute(string $sessionId, array $options): bool
* "1920x1080" (FHD landscape), "480x640" (SD portrait), "720x1280" (HD portrait), or "1080x1920"
* (FHD portrait).</li>
*
* <li><code>maxBitRate</code> &mdash; Max Bitrate allowed for the broadcast composing. Must be between
* 400000 and 2000000.</li>
*
* <li><code>outputs</code> (Array) &mdash;
* Defines the HLS broadcast and RTMP streams. You can provide the following keys:
* <ul>
Expand Down Expand Up @@ -864,6 +867,10 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas
// not preferred to depend on that in the SDK because its then harder to garauntee backwards
// compatibility

if (isset($options['maxBitRate'])) {
Validators::validateBroadcastBitrate($options['maxBitRate']);
}

if (isset($options['resolution'])) {
Validators::validateResolution($options['resolution']);
}
Expand All @@ -882,6 +889,7 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas
'hasVideo' => true,
'streamMode' => 'auto',
'resolution' => '640x480',
'maxBitRate' => 2000000,
'outputs' => [
'hls' => [
'dvr' => false,
Expand All @@ -892,25 +900,21 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas

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

list($layout, $hasAudio, $hasVideo, $streamMode) = array_values($options);

// validate arguments
Validators::validateSessionId($sessionId);
Validators::validateLayout($layout);
Validators::validateHasStreamMode($streamMode);
Validators::validateLayout($options['layout']);
Validators::validateHasStreamMode($options['streamMode']);

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

return new Broadcast($broadcastData, array('client' => $this->client));
return new Broadcast($broadcastData, ['client' => $this->client]);
}

/**
* Stops a broadcast.
*
* @param String $broadcastId The ID of the broadcast.
*/
public function stopBroadcast($broadcastId)
public function stopBroadcast($broadcastId): Broadcast
{
// validate arguments
Validators::validateBroadcastId($broadcastId);
Expand All @@ -930,7 +934,7 @@ public function stopBroadcast($broadcastId)
*
* @return Broadcast An object with properties defining the broadcast.
*/
public function getBroadcast($broadcastId)
public function getBroadcast($broadcastId): Broadcast
{
Validators::validateBroadcastId($broadcastId);

Expand Down
11 changes: 11 additions & 0 deletions src/OpenTok/Util/Validators.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,15 @@ protected static function decodeSessionId($sessionId)
}
return $data;
}

public static function validateBroadcastBitrate($maxBitRate): void
{
if (!is_int($maxBitRate)) {
throw new \InvalidArgumentException('Max Bitrate must be a number');
}

if ($maxBitRate < 400000 && $maxBitRate > 2000000) {
throw new \OutOfBoundsException('Max Bitrate must be between 400000 and 2000000');
}
}
}
32 changes: 26 additions & 6 deletions tests/OpenTokTest/OpenTokTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,6 @@ public function testCannotStartBroadcastWithBothHlsAndDvrEnabled(): void

public function testStartsBroadcast(): void
{
// Arrange
$this->setupOTWithMocks([[
'code' => 200,
'headers' => [
Expand All @@ -1734,14 +1733,10 @@ public function testStartsBroadcast(): void
'path' => '/v2/project/APIKEY/broadcast/session_layout-bestfit'
]]);

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

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

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

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

public function testStartsBroadcastWithMaxBitrate(): void
{
$this->setupOTWithMocks([[
'code' => 200,
'headers' => [
'Content-Type' => 'application/json'
],
'path' => '/v2/project/APIKEY/broadcast/session_layout-bestfit'
]]);

$sessionId = '2_MX44NTQ1MTF-fjE0NzI0MzU2MDUyMjN-eVgwNFJhZmR6MjdockFHanpxNzBXaEFXfn4';

$broadcast = $this->opentok->startBroadcast($sessionId, [
'maxBitRate' => 2000000
]);

$this->assertIsString($broadcast->id);
$this->assertEquals($sessionId, $broadcast->sessionId);
$this->assertIsArray($broadcast->broadcastUrls);
$this->assertArrayHasKey('hls', $broadcast->broadcastUrls);
$this->assertIsString($broadcast->broadcastUrls['hls']);
$this->assertIsString($broadcast->hlsUrl);
$this->assertFalse($broadcast->isStopped);
$this->assertEquals(2000000, $broadcast->maxBitRate);
}

public function testStartsBroadcastWithMultiBroadcastTag(): void
{
// Arrange
$this->setupOTWithMocks([[
'code' => 200,
'headers' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"status":"started",
"partnerId":854511,
"maxDuration":5400,
"maxBitRate": 2000000,
"resolution": "1280x720",
"streamMode": "auto",
"hasAudio": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"status":"started",
"partnerId":854511,
"maxDuration":5400,
"maxBitRate": 2000000,
"resolution": "1280x720",
"streamMode": "auto",
"hasAudio": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"status":"started",
"partnerId":854511,
"maxDuration":5400,
"maxBitRate": 2000000,
"resolution": "1280x720",
"streamMode": "auto",
"hasAudio": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"status":"started",
"partnerId":854511,
"maxDuration":5400,
"maxBitRate": 2000000,
"resolution": "1280x720",
"streamMode": "auto"
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"status":"started",
"partnerId":854511,
"maxDuration":5400,
"maxBitRate": 2000000,
"resolution": "1280x720",
"streamMode": "manual"
}

0 comments on commit b6e8256

Please sign in to comment.