Skip to content

Commit 57d9bd6

Browse files
authored
Merge pull request #340 from opentok/dev
Dev Release
2 parents 859d6d0 + b6e8256 commit 57d9bd6

File tree

10 files changed

+120
-36
lines changed

10 files changed

+120
-36
lines changed

src/OpenTok/Broadcast.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
*
4545
* @property boolean $isDvr
4646
* Whether the broadcast supports DVR functionality for the HLS stream.
47+
48+
* @property string $status
49+
* Broadcast state. Either `started` or `stopped`
50+
*
51+
* @property string $maxBitRate
52+
* Max Bitrate allowed for the broadcast composing. Must be between 400000 and 2000000
4753
*
4854
* @property boolean $isLowLatency
4955
* Whether the broadcast supports low-latency mode for the HLS stream.
@@ -86,6 +92,10 @@ class Broadcast
8692
private $hasAudio;
8793
/** @ignore */
8894
private $hasVideo;
95+
/** @ignore */
96+
private $status;
97+
/** @ignore */
98+
private $maxBitRate;
8999

90100
public function __construct($broadcastData, $options = array())
91101
{
@@ -107,34 +117,41 @@ public function __construct($broadcastData, $options = array())
107117
);
108118

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

112-
// validate params
113121
Validators::validateBroadcastData($broadcastData);
114-
Validators::validateClient($client);
115-
Validators::validateHasStreamMode($streamMode);
122+
Validators::validateClient($options['client']);
123+
Validators::validateHasStreamMode($options['streamMode']);
116124

117125
$this->data = $broadcastData;
118126

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

123-
$this->isStopped = $isStopped;
131+
if (isset($this->data['maxBitRate'])) {
132+
$this->maxBitRate = $this->data['maxBitRate'];
133+
}
134+
135+
if (isset($this->data['status'])) {
136+
$this->status = $this->data['status'];
137+
}
138+
139+
$this->isStopped = $options['isStopped'];
124140
$this->resolution = $this->data['resolution'];
125141
$this->isHls = isset($this->data['settings']['hls']);
126142
$this->isLowLatency = $this->data['settings']['hls']['lowLatency'] ?? false;
127143
$this->isDvr = $this->data['settings']['hls']['dvr'] ?? false;
128-
$this->hasAudio = $hasAudio;
129-
$this->hasVideo = $hasVideo;
144+
$this->hasAudio = $options['hasAudio'];
145+
$this->hasVideo = $options['hasVideo'];
146+
147+
$this->client = $options['client'] ?? new Client();
130148

131-
$this->client = isset($client) ? $client : new Client();
132149
if (!$this->client->isConfigured()) {
133-
Validators::validateApiKey($apiKey);
134-
Validators::validateApiSecret($apiSecret);
135-
Validators::validateApiUrl($apiUrl);
150+
Validators::validateApiKey($options['apiKey']);
151+
Validators::validateApiSecret($options['apiSecret']);
152+
Validators::validateApiUrl($options['apiUrl']);
136153

137-
$this->client->configure($apiKey, $apiSecret, $apiUrl);
154+
$this->client->configure($options['apiKey'], $options['apiSecret'], $options['apiUrl']);
138155
}
139156
}
140157

@@ -148,7 +165,6 @@ public function __get($name)
148165
case 'partnerId':
149166
case 'sessionId':
150167
case 'broadcastUrls':
151-
case 'status':
152168
case 'maxDuration':
153169
case 'streamMode':
154170
return $this->data[$name];
@@ -170,6 +186,10 @@ public function __get($name)
170186
return $this->hasAudio;
171187
case 'hasVideo':
172188
return $this->hasVideo;
189+
case 'status':
190+
return $this->status;
191+
case 'maxBitRate':
192+
return $this->maxBitRate;
173193
default:
174194
return null;
175195
}

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/BroadcastTest.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function setUp(): void
3434
'updatedAt' => 1394394801000,
3535
'partnerId' => 685,
3636
'sessionId' => '2_MX44NTQ1MTF-flR1ZSBOb3YgMTIgMDk6NDA6NTkgUFNUIDIwMTN-MC43NjU0Nzh-',
37+
'multiBroadcastTag' => 'broadcast-1234b',
3738
'layout' => [
3839
'type' => 'custom',
3940
'stylesheet' => 'a layout stylesheet',
@@ -42,8 +43,9 @@ public function setUp(): void
4243
'maxDuration' => 5400,
4344
'resolution' => '640x480',
4445
'streamMode' => StreamMode::AUTO,
45-
'isAudio' => true,
46-
'isVideo' => true
46+
'status' => 'started',
47+
'hasAudio' => true,
48+
'hasVideo' => true
4749
];
4850
}
4951

@@ -134,12 +136,12 @@ private function setupOT()
134136
return $this->setupOTWithMocks([]);
135137
}
136138

137-
public function testInitializes()
139+
public function testInitializes(): void
138140
{
139-
// Arrange
140141
$this->setupOT();
141142
$this->setupBroadcasts(StreamMode::AUTO);
142143
$this->assertInstanceOf(Broadcast::class, $this->broadcast);
144+
143145
}
144146

145147
public function testCannotAddStreamToBroadcastInAutoMode(): void
@@ -238,5 +240,20 @@ public function testCannotRemoveStreamFromBroadcastOnAuto(): void
238240
'5dfds4-asdda4asf4'
239241
);
240242
}
243+
244+
public function testGetters(): void
245+
{
246+
$broadcastObject = new Broadcast($this->broadcastData, [
247+
'apiKey' => 'abc',
248+
'apiSecret' => 'efg',
249+
'client' => $this->client
250+
]);
251+
252+
$this->assertTrue($broadcastObject->hasAudio);
253+
$this->assertTrue($broadcastObject->hasVideo);
254+
$this->assertEquals('broadcast-1234b', $broadcastObject->multiBroadcastTag);
255+
$this->assertEquals('started', $broadcastObject->status);
256+
$this->assertNull($broadcastObject->wrongKey);
257+
}
241258
}
242259

tests/OpenTokTest/OpenTokTest.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,7 @@ public function testCanStartBroadcastWithDefaultHlsOptions(): void
16401640
$this->assertTrue($broadcast->isHls);
16411641
$this->assertFalse($broadcast->isDvr);
16421642
$this->assertFalse($broadcast->isLowLatency);
1643+
$this->assertEquals('live', $broadcast->broadcastUrls['rtmp']['foo']['status']);
16431644
}
16441645

16451646
public function testCanStartBroadcastWithDvrEnabled(): void
@@ -1724,7 +1725,6 @@ public function testCannotStartBroadcastWithBothHlsAndDvrEnabled(): void
17241725

17251726
public function testStartsBroadcast(): void
17261727
{
1727-
// Arrange
17281728
$this->setupOTWithMocks([[
17291729
'code' => 200,
17301730
'headers' => [
@@ -1733,14 +1733,10 @@ public function testStartsBroadcast(): void
17331733
'path' => '/v2/project/APIKEY/broadcast/session_layout-bestfit'
17341734
]]);
17351735

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

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

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

17461742
$request = $this->historyContainer[0]['request'];
@@ -1766,9 +1762,34 @@ public function testStartsBroadcast(): void
17661762
$this->assertEquals('auto', $broadcast->streamMode);
17671763
}
17681764

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+
17691791
public function testStartsBroadcastWithMultiBroadcastTag(): void
17701792
{
1771-
// Arrange
17721793
$this->setupOTWithMocks([[
17731794
'code' => 200,
17741795
'headers' => [

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
"createdAt":1472435659497,
66
"broadcastUrls":{
77
"hls":"https://cdn-broadcast001-dub.tokbox.com/29908/29908_6706b658-2eba-42cc-b4d2-7d01a104d182.smil/playlist.m3u8",
8+
"hlsStatus": "ready",
89
"rtmp": {
910
"foo": {
1011
"serverUrl": "rtmp://myfooserver/myfooapp",
11-
"streamName": "myfoostreamname"
12+
"streamName": "myfoostreamname",
13+
"status": "live"
1214
},
1315
"bar": {
1416
"serverUrl": "rtmp://mybarserver/mybarapp",
15-
"streamName": "mybarstreamname"
17+
"streamName": "mybarstreamname",
18+
"status": "offline"
1619
}
1720
}
1821
},
@@ -21,6 +24,7 @@
2124
"status":"started",
2225
"partnerId":854511,
2326
"maxDuration":5400,
27+
"maxBitRate": 2000000,
2428
"resolution": "1280x720",
2529
"streamMode": "auto",
2630
"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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
"createdAt":1472435659497,
66
"broadcastUrls":{
77
"hls":"https://cdn-broadcast001-dub.tokbox.com/29908/29908_6706b658-2eba-42cc-b4d2-7d01a104d182.smil/playlist.m3u8",
8+
"hlsStatus": "ready",
89
"rtmp": {
910
"foo": {
1011
"serverUrl": "rtmp://myfooserver/myfooapp",
11-
"streamName": "myfoostreamname"
12+
"streamName": "myfoostreamname",
13+
"status": "live"
1214
},
1315
"bar": {
1416
"serverUrl": "rtmp://mybarserver/mybarapp",
15-
"streamName": "mybarstreamname"
17+
"streamName": "mybarstreamname",
18+
"status": "offline"
1619
}
1720
}
1821
},
@@ -21,6 +24,7 @@
2124
"status":"started",
2225
"partnerId":854511,
2326
"maxDuration":5400,
27+
"maxBitRate": 2000000,
2428
"resolution": "1280x720",
2529
"streamMode": "auto",
2630
"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)