Skip to content

Commit

Permalink
Merge pull request #340 from opentok/dev
Browse files Browse the repository at this point in the history
Dev Release
  • Loading branch information
SecondeJK committed Aug 22, 2023
2 parents 859d6d0 + b6e8256 commit 57d9bd6
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 36 deletions.
46 changes: 33 additions & 13 deletions src/OpenTok/Broadcast.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
*
* @property boolean $isDvr
* Whether the broadcast supports DVR functionality for the HLS stream.
* @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 @@ -86,6 +92,10 @@ class Broadcast
private $hasAudio;
/** @ignore */
private $hasVideo;
/** @ignore */
private $status;
/** @ignore */
private $maxBitRate;

public function __construct($broadcastData, $options = array())
{
Expand All @@ -107,34 +117,41 @@ public function __construct($broadcastData, $options = array())
);

$options = array_merge($defaults, array_intersect_key($options, $defaults));
list($apiKey, $apiSecret, $apiUrl, $client, $isStopped, $streamMode, $hasAudio, $hasVideo) = array_values($options);

// validate params
Validators::validateBroadcastData($broadcastData);
Validators::validateClient($client);
Validators::validateHasStreamMode($streamMode);
Validators::validateClient($options['client']);
Validators::validateHasStreamMode($options['streamMode']);

$this->data = $broadcastData;

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

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

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

$this->isStopped = $options['isStopped'];
$this->resolution = $this->data['resolution'];
$this->isHls = isset($this->data['settings']['hls']);
$this->isLowLatency = $this->data['settings']['hls']['lowLatency'] ?? false;
$this->isDvr = $this->data['settings']['hls']['dvr'] ?? false;
$this->hasAudio = $hasAudio;
$this->hasVideo = $hasVideo;
$this->hasAudio = $options['hasAudio'];
$this->hasVideo = $options['hasVideo'];

$this->client = $options['client'] ?? new Client();

$this->client = isset($client) ? $client : new Client();
if (!$this->client->isConfigured()) {
Validators::validateApiKey($apiKey);
Validators::validateApiSecret($apiSecret);
Validators::validateApiUrl($apiUrl);
Validators::validateApiKey($options['apiKey']);
Validators::validateApiSecret($options['apiSecret']);
Validators::validateApiUrl($options['apiUrl']);

$this->client->configure($apiKey, $apiSecret, $apiUrl);
$this->client->configure($options['apiKey'], $options['apiSecret'], $options['apiUrl']);
}
}

Expand All @@ -148,7 +165,6 @@ public function __get($name)
case 'partnerId':
case 'sessionId':
case 'broadcastUrls':
case 'status':
case 'maxDuration':
case 'streamMode':
return $this->data[$name];
Expand All @@ -170,6 +186,10 @@ public function __get($name)
return $this->hasAudio;
case 'hasVideo':
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');
}
}
}
25 changes: 21 additions & 4 deletions tests/OpenTokTest/BroadcastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function setUp(): void
'updatedAt' => 1394394801000,
'partnerId' => 685,
'sessionId' => '2_MX44NTQ1MTF-flR1ZSBOb3YgMTIgMDk6NDA6NTkgUFNUIDIwMTN-MC43NjU0Nzh-',
'multiBroadcastTag' => 'broadcast-1234b',
'layout' => [
'type' => 'custom',
'stylesheet' => 'a layout stylesheet',
Expand All @@ -42,8 +43,9 @@ public function setUp(): void
'maxDuration' => 5400,
'resolution' => '640x480',
'streamMode' => StreamMode::AUTO,
'isAudio' => true,
'isVideo' => true
'status' => 'started',
'hasAudio' => true,
'hasVideo' => true
];
}

Expand Down Expand Up @@ -134,12 +136,12 @@ private function setupOT()
return $this->setupOTWithMocks([]);
}

public function testInitializes()
public function testInitializes(): void
{
// Arrange
$this->setupOT();
$this->setupBroadcasts(StreamMode::AUTO);
$this->assertInstanceOf(Broadcast::class, $this->broadcast);

}

public function testCannotAddStreamToBroadcastInAutoMode(): void
Expand Down Expand Up @@ -238,5 +240,20 @@ public function testCannotRemoveStreamFromBroadcastOnAuto(): void
'5dfds4-asdda4asf4'
);
}

public function testGetters(): void
{
$broadcastObject = new Broadcast($this->broadcastData, [
'apiKey' => 'abc',
'apiSecret' => 'efg',
'client' => $this->client
]);

$this->assertTrue($broadcastObject->hasAudio);
$this->assertTrue($broadcastObject->hasVideo);
$this->assertEquals('broadcast-1234b', $broadcastObject->multiBroadcastTag);
$this->assertEquals('started', $broadcastObject->status);
$this->assertNull($broadcastObject->wrongKey);
}
}

33 changes: 27 additions & 6 deletions tests/OpenTokTest/OpenTokTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,7 @@ public function testCanStartBroadcastWithDefaultHlsOptions(): void
$this->assertTrue($broadcast->isHls);
$this->assertFalse($broadcast->isDvr);
$this->assertFalse($broadcast->isLowLatency);
$this->assertEquals('live', $broadcast->broadcastUrls['rtmp']['foo']['status']);
}

public function testCanStartBroadcastWithDvrEnabled(): void
Expand Down Expand Up @@ -1724,7 +1725,6 @@ public function testCannotStartBroadcastWithBothHlsAndDvrEnabled(): void

public function testStartsBroadcast(): void
{
// Arrange
$this->setupOTWithMocks([[
'code' => 200,
'headers' => [
Expand All @@ -1733,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 @@ -1766,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 @@ -5,14 +5,17 @@
"createdAt":1472435659497,
"broadcastUrls":{
"hls":"https://cdn-broadcast001-dub.tokbox.com/29908/29908_6706b658-2eba-42cc-b4d2-7d01a104d182.smil/playlist.m3u8",
"hlsStatus": "ready",
"rtmp": {
"foo": {
"serverUrl": "rtmp://myfooserver/myfooapp",
"streamName": "myfoostreamname"
"streamName": "myfoostreamname",
"status": "live"
},
"bar": {
"serverUrl": "rtmp://mybarserver/mybarapp",
"streamName": "mybarstreamname"
"streamName": "mybarstreamname",
"status": "offline"
}
}
},
Expand All @@ -21,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
8 changes: 6 additions & 2 deletions tests/mock/v2/project/APIKEY/broadcast/BROADCASTID/start_ll
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
"createdAt":1472435659497,
"broadcastUrls":{
"hls":"https://cdn-broadcast001-dub.tokbox.com/29908/29908_6706b658-2eba-42cc-b4d2-7d01a104d182.smil/playlist.m3u8",
"hlsStatus": "ready",
"rtmp": {
"foo": {
"serverUrl": "rtmp://myfooserver/myfooapp",
"streamName": "myfoostreamname"
"streamName": "myfoostreamname",
"status": "live"
},
"bar": {
"serverUrl": "rtmp://mybarserver/mybarapp",
"streamName": "mybarstreamname"
"streamName": "mybarstreamname",
"status": "offline"
}
}
},
Expand All @@ -21,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 57d9bd6

Please sign in to comment.