From c09e8136094509cc3841c710b014696a42f5664a Mon Sep 17 00:00:00 2001 From: James Seconde Date: Thu, 17 Aug 2023 15:20:03 +0100 Subject: [PATCH 1/2] Add new max bitrate --- src/OpenTok/Broadcast.php | 8 +++++ src/OpenTok/OpenTok.php | 19 +++++------ src/OpenTok/Util/Validators.php | 11 +++++++ tests/OpenTokTest/OpenTokTest.php | 32 +++++++++++++++---- .../broadcast/BROADCASTID/start_default | 1 + .../APIKEY/broadcast/BROADCASTID/start_dvr | 1 + .../APIKEY/broadcast/BROADCASTID/start_ll | 1 + .../APIKEY/broadcast/session_layout-bestfit | 1 + .../APIKEY/broadcast/session_manual_stream | 1 + 9 files changed, 60 insertions(+), 15 deletions(-) diff --git a/src/OpenTok/Broadcast.php b/src/OpenTok/Broadcast.php index 2fc0d4d..3e4dbac 100644 --- a/src/OpenTok/Broadcast.php +++ b/src/OpenTok/Broadcast.php @@ -91,6 +91,8 @@ class Broadcast private $hasVideo; /** @ignore */ private $status; + /** @ignore */ + private $maxBitRate; public function __construct($broadcastData, $options = array()) { @@ -123,6 +125,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']; } @@ -179,6 +185,8 @@ public function __get($name) return $this->hasVideo; case 'status': return $this->status; + case 'maxBitRate': + return $this->maxBitRate; default: return null; } diff --git a/src/OpenTok/OpenTok.php b/src/OpenTok/OpenTok.php index 4fe4d43..81b334a 100644 --- a/src/OpenTok/OpenTok.php +++ b/src/OpenTok/OpenTok.php @@ -864,6 +864,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']); } @@ -882,6 +886,7 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas 'hasVideo' => true, 'streamMode' => 'auto', 'resolution' => '640x480', + 'maxBitRate' => 2000000, 'outputs' => [ 'hls' => [ 'dvr' => false, @@ -892,17 +897,13 @@ 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]); } /** @@ -910,7 +911,7 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas * * @param String $broadcastId The ID of the broadcast. */ - public function stopBroadcast($broadcastId) + public function stopBroadcast($broadcastId): Broadcast { // validate arguments Validators::validateBroadcastId($broadcastId); @@ -930,7 +931,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); diff --git a/src/OpenTok/Util/Validators.php b/src/OpenTok/Util/Validators.php index ad67ed5..6607b45 100755 --- a/src/OpenTok/Util/Validators.php +++ b/src/OpenTok/Util/Validators.php @@ -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'); + } + } } diff --git a/tests/OpenTokTest/OpenTokTest.php b/tests/OpenTokTest/OpenTokTest.php index 781f824..c24d77a 100644 --- a/tests/OpenTokTest/OpenTokTest.php +++ b/tests/OpenTokTest/OpenTokTest.php @@ -1725,7 +1725,6 @@ public function testCannotStartBroadcastWithBothHlsAndDvrEnabled(): void public function testStartsBroadcast(): void { - // Arrange $this->setupOTWithMocks([[ 'code' => 200, 'headers' => [ @@ -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']; @@ -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' => [ diff --git a/tests/mock/v2/project/APIKEY/broadcast/BROADCASTID/start_default b/tests/mock/v2/project/APIKEY/broadcast/BROADCASTID/start_default index ee0eb86..cde5b3f 100644 --- a/tests/mock/v2/project/APIKEY/broadcast/BROADCASTID/start_default +++ b/tests/mock/v2/project/APIKEY/broadcast/BROADCASTID/start_default @@ -24,6 +24,7 @@ "status":"started", "partnerId":854511, "maxDuration":5400, + "maxBitRate": 2000000, "resolution": "1280x720", "streamMode": "auto", "hasAudio": true, diff --git a/tests/mock/v2/project/APIKEY/broadcast/BROADCASTID/start_dvr b/tests/mock/v2/project/APIKEY/broadcast/BROADCASTID/start_dvr index ae92c24..d5ad0b0 100644 --- a/tests/mock/v2/project/APIKEY/broadcast/BROADCASTID/start_dvr +++ b/tests/mock/v2/project/APIKEY/broadcast/BROADCASTID/start_dvr @@ -21,6 +21,7 @@ "status":"started", "partnerId":854511, "maxDuration":5400, + "maxBitRate": 2000000, "resolution": "1280x720", "streamMode": "auto", "hasAudio": true, diff --git a/tests/mock/v2/project/APIKEY/broadcast/BROADCASTID/start_ll b/tests/mock/v2/project/APIKEY/broadcast/BROADCASTID/start_ll index 7c31627..9c341dd 100644 --- a/tests/mock/v2/project/APIKEY/broadcast/BROADCASTID/start_ll +++ b/tests/mock/v2/project/APIKEY/broadcast/BROADCASTID/start_ll @@ -24,6 +24,7 @@ "status":"started", "partnerId":854511, "maxDuration":5400, + "maxBitRate": 2000000, "resolution": "1280x720", "streamMode": "auto", "hasAudio": true, diff --git a/tests/mock/v2/project/APIKEY/broadcast/session_layout-bestfit b/tests/mock/v2/project/APIKEY/broadcast/session_layout-bestfit index d78248b..130d7b4 100644 --- a/tests/mock/v2/project/APIKEY/broadcast/session_layout-bestfit +++ b/tests/mock/v2/project/APIKEY/broadcast/session_layout-bestfit @@ -20,6 +20,7 @@ "status":"started", "partnerId":854511, "maxDuration":5400, + "maxBitRate": 2000000, "resolution": "1280x720", "streamMode": "auto" } diff --git a/tests/mock/v2/project/APIKEY/broadcast/session_manual_stream b/tests/mock/v2/project/APIKEY/broadcast/session_manual_stream index 12183e2..59ffbd6 100644 --- a/tests/mock/v2/project/APIKEY/broadcast/session_manual_stream +++ b/tests/mock/v2/project/APIKEY/broadcast/session_manual_stream @@ -20,6 +20,7 @@ "status":"started", "partnerId":854511, "maxDuration":5400, + "maxBitRate": 2000000, "resolution": "1280x720", "streamMode": "manual" } From 8bb51859cae6761b066d96d1328f92ef01282c63 Mon Sep 17 00:00:00 2001 From: James Seconde Date: Thu, 17 Aug 2023 15:24:20 +0100 Subject: [PATCH 2/2] docs --- src/OpenTok/Broadcast.php | 3 +++ src/OpenTok/OpenTok.php | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/OpenTok/Broadcast.php b/src/OpenTok/Broadcast.php index 3e4dbac..9745a24 100644 --- a/src/OpenTok/Broadcast.php +++ b/src/OpenTok/Broadcast.php @@ -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. * diff --git a/src/OpenTok/OpenTok.php b/src/OpenTok/OpenTok.php index 81b334a..6a69aed 100644 --- a/src/OpenTok/OpenTok.php +++ b/src/OpenTok/OpenTok.php @@ -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). * + *
  • maxBitRate — Max Bitrate allowed for the broadcast composing. Must be between + * 400000 and 2000000.
  • + * *
  • outputs (Array) — * Defines the HLS broadcast and RTMP streams. You can provide the following keys: *