Skip to content

Commit f1e40f8

Browse files
authored
Merge pull request #335 from opentok/dev
Dev
2 parents 2701a37 + 398f722 commit f1e40f8

File tree

9 files changed

+285
-39
lines changed

9 files changed

+285
-39
lines changed

src/OpenTok/Archive.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,17 @@ class Archive
105105
private $client;
106106
/** @internal */
107107
private $multiArchiveTag;
108-
108+
/**
109+
* @var mixed|null
110+
*/
111+
private const PERMITTED_AUTO_RESOLUTIONS = [
112+
'480x640',
113+
"640x480",
114+
"720x1280",
115+
"1280x720",
116+
"1080x1920",
117+
"1920x1080"
118+
];
109119

110120
/** @internal */
111121
public function __construct($archiveData, $options = array())
@@ -141,6 +151,11 @@ public function __construct($archiveData, $options = array())
141151
}
142152
}
143153

154+
public static function getPermittedResolutions()
155+
{
156+
return self::PERMITTED_AUTO_RESOLUTIONS;
157+
}
158+
144159
/** @internal */
145160
public function __get($name)
146161
{

src/OpenTok/Broadcast.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ class Broadcast
8282
private $multiBroadcastTag;
8383
/** @ignore */
8484
private $resolution;
85-
85+
/** @ignore */
86+
private $hasAudio;
87+
/** @ignore */
88+
private $hasVideo;
89+
8690
public function __construct($broadcastData, $options = array())
8791
{
8892
// unpack optional arguments (merging with default values) into named variables
@@ -98,9 +102,12 @@ public function __construct($broadcastData, $options = array())
98102
'isHls' => true,
99103
'isLowLatency' => false,
100104
'isDvr' => false,
105+
'hasAudio' => true,
106+
'hasVideo' => true
101107
);
108+
102109
$options = array_merge($defaults, array_intersect_key($options, $defaults));
103-
list($apiKey, $apiSecret, $apiUrl, $client, $isStopped, $streamMode) = array_values($options);
110+
list($apiKey, $apiSecret, $apiUrl, $client, $isStopped, $streamMode, $hasAudio, $hasVideo) = array_values($options);
104111

105112
// validate params
106113
Validators::validateBroadcastData($broadcastData);
@@ -118,6 +125,8 @@ public function __construct($broadcastData, $options = array())
118125
$this->isHls = isset($this->data['settings']['hls']);
119126
$this->isLowLatency = $this->data['settings']['hls']['lowLatency'] ?? false;
120127
$this->isDvr = $this->data['settings']['hls']['dvr'] ?? false;
128+
$this->hasAudio = $hasAudio;
129+
$this->hasVideo = $hasVideo;
121130

122131
$this->client = isset($client) ? $client : new Client();
123132
if (!$this->client->isConfigured()) {
@@ -157,6 +166,10 @@ public function __get($name)
157166
return $this->isDvr;
158167
case 'multiBroadcastTag':
159168
return $this->multiBroadcastTag;
169+
case 'hasAudio':
170+
return $this->hasAudio;
171+
case 'hasVideo':
172+
return $this->hasVideo;
160173
default:
161174
return null;
162175
}

src/OpenTok/OpenTok.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ public function generateToken($sessionId, $options = array())
173173
* <a href="https://tokbox.com/developer/guides/end-to-end-encryption">end-to-end encryption</a>
174174
* for a routed session.</li>
175175
*
176+
* <li><code>archiveName</code> (String) &mdash; Name of the archives in auto archived sessions</li>
177+
*
178+
* <li><code>archiveResolution</code> (Enum) &mdash; Resolution of the archives in
179+
* auto archived sessions. Can be one of "480x640", "640x480", "720x1280", "1280x720", "1080x1920", "1920x1080"</li>
180+
*
176181
* <li><code>'location'</code> (String) &mdash; An IP address that the OpenTok servers
177182
* will use to situate the session in its global network. If you do not set a location hint,
178183
* the OpenTok servers will be based on the first client connecting to the session.</li>
@@ -250,14 +255,34 @@ public function createSession($options = array())
250255
'archiveMode' => ArchiveMode::MANUAL,
251256
'location' => null,
252257
'e2ee' => 'false',
258+
'archiveName' => null,
259+
'archiveResolution' => null
253260
);
254261

262+
// Have to hack this because the default system in these classes needs total refactor
263+
$resolvedArchiveMode = array_merge($defaults, array_intersect_key($options, $defaults));
264+
265+
if ($resolvedArchiveMode['archiveMode'] === ArchiveMode::ALWAYS) {
266+
$defaults['archiveResolution'] = '640x480';
267+
}
268+
255269
$options = array_merge($defaults, array_intersect_key($options, $defaults));
270+
271+
// Have to hack this because the default system in these classes needs total refactor
272+
if ($options['archiveName'] === null) {
273+
unset($options['archiveName']);
274+
}
275+
276+
if ($options['archiveResolution'] === null) {
277+
unset($options['archiveResolution']);
278+
}
279+
256280
list($mediaMode, $archiveMode, $location, $e2ee) = array_values($options);
257281

258282
// validate arguments
259283
Validators::validateMediaMode($mediaMode);
260284
Validators::validateArchiveMode($archiveMode);
285+
Validators::validateAutoArchiveMode($archiveMode, $options);
261286
Validators::validateLocation($location);
262287

263288
// make API call
@@ -843,19 +868,21 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas
843868
Validators::validateResolution($options['resolution']);
844869
}
845870

846-
if (isset($options['output']['hls'])) {
847-
Validators::validateBroadcastOutputOptions($options['output']['hls']);
871+
if (isset($options['outputs']['hls'])) {
872+
Validators::validateBroadcastOutputOptions($options['outputs']['hls']);
848873
}
849874

850-
if (isset($options['output']['rtmp'])) {
851-
Validators::validateRtmpStreams($options['output']['rtmp']);
875+
if (isset($options['outputs']['rtmp'])) {
876+
Validators::validateRtmpStreams($options['outputs']['rtmp']);
852877
}
853878

854879
$defaults = [
855880
'layout' => Layout::getBestFit(),
881+
'hasAudio' => true,
882+
'hasVideo' => true,
856883
'streamMode' => 'auto',
857884
'resolution' => '640x480',
858-
'output' => [
885+
'outputs' => [
859886
'hls' => [
860887
'dvr' => false,
861888
'lowLatency' => false
@@ -865,7 +892,7 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas
865892

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

868-
list($layout, $streamMode) = array_values($options);
895+
list($layout, $hasAudio, $hasVideo, $streamMode) = array_values($options);
869896

870897
// validate arguments
871898
Validators::validateSessionId($sessionId);
@@ -1010,7 +1037,7 @@ public function getStream($sessionId, $streamId)
10101037
}
10111038

10121039
/**
1013-
* Returns a StreamList Object for the given session ID.
1040+
* Returns a StreamList Object for the given session ID.
10141041
*
10151042
* @param String $sessionId The session ID.
10161043
*

src/OpenTok/Util/Validators.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace OpenTok\Util;
44

5+
use OpenTok\Archive;
56
use OpenTok\Util\Client;
67
use OpenTok\Layout;
78
use OpenTok\Role;
@@ -266,6 +267,7 @@ public static function validateMediaMode($mediaMode)
266267
);
267268
}
268269
}
270+
269271
public static function validateArchiveMode($archiveMode)
270272
{
271273
if (!ArchiveMode::isValidValue($archiveMode)) {
@@ -274,6 +276,22 @@ public static function validateArchiveMode($archiveMode)
274276
);
275277
}
276278
}
279+
280+
public static function validateAutoArchiveMode($archiveMode, $options)
281+
{
282+
if ($archiveMode === ArchiveMode::MANUAL) {
283+
foreach (['archiveName', 'archiveResolution'] as $key) {
284+
if (array_key_exists($key, $options)) {
285+
throw new InvalidArgumentException('Cannot set ' . $key . ' when Archive mode is Manual');
286+
}
287+
}
288+
}
289+
290+
if (array_key_exists('archiveResolution', $options)) {
291+
self::validateAutoArchiveResolution($options['archiveResolution']);
292+
}
293+
}
294+
277295
public static function validateLocation($location)
278296
{
279297
if ($location != null && !filter_var($location, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
@@ -394,6 +412,13 @@ public static function validateWebsocketOptions(array $websocketOptions)
394412
}
395413
}
396414

415+
public static function validateAutoArchiveResolution($archiveResolution)
416+
{
417+
if (! in_array($archiveResolution, Archive::getPermittedResolutions(), true)) {
418+
throw new InvalidArgumentException($archiveResolution . ' is not a valid resolution');
419+
}
420+
}
421+
397422
public static function validateLayoutClassListItem($layoutClassList)
398423
{
399424
if (!is_string($layoutClassList['id'])) {

tests/OpenTokTest/BroadcastTest.php

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use GuzzleHttp\Handler\MockHandler;
66
use GuzzleHttp\HandlerStack;
77
use GuzzleHttp\Middleware;
8-
use OpenTok\Archive;
98
use OpenTok\Broadcast;
109
use OpenTok\Exception\InvalidArgumentException;
1110
use OpenTok\StreamMode;
@@ -14,26 +13,22 @@
1413

1514
class BroadcastTest extends TestCase
1615
{
17-
18-
// Fixtures
19-
protected $broadcastData;
2016
protected $API_KEY;
2117
protected $API_SECRET;
2218

2319
protected $broadcast;
20+
protected $broadcastData;
2421
protected $client;
2522

2623
protected static $mockBasePath;
24+
/**
25+
* @var array
26+
*/
27+
private $historyContainer;
2728

28-
public static function setUpBeforeClass(): void
29-
{
30-
self::$mockBasePath = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'mock' . DIRECTORY_SEPARATOR;
31-
}
32-
33-
public function setupBroadcasts($streamMode)
29+
public function setUp(): void
3430
{
35-
// Set up fixtures
36-
$this->broadcastData = array(
31+
$this->broadcastData = [
3732
'id' => '063e72a4-64b4-43c8-9da5-eca071daab89',
3833
'createdAt' => 1394394801000,
3934
'updatedAt' => 1394394801000,
@@ -42,14 +37,27 @@ public function setupBroadcasts($streamMode)
4237
'layout' => [
4338
'type' => 'custom',
4439
'stylesheet' => 'a layout stylesheet',
45-
'streenshareType' => 'some options'
40+
'screenshareType' => 'some options'
4641
],
4742
'maxDuration' => 5400,
4843
'resolution' => '640x480',
49-
'streamMode' => $streamMode
50-
);
44+
'streamMode' => StreamMode::AUTO,
45+
'isAudio' => true,
46+
'isVideo' => true
47+
];
48+
}
5149

52-
$this->broadcast = new Broadcast($this->broadcastData, array(
50+
public static function setUpBeforeClass(): void
51+
{
52+
self::$mockBasePath = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'mock' . DIRECTORY_SEPARATOR;
53+
}
54+
55+
public function setupBroadcasts($streamMode)
56+
{
57+
$data = $this->broadcastData;
58+
$data['streamMode'] = $streamMode;
59+
60+
$this->broadcast = new Broadcast($data, array(
5361
'apiKey' => $this->API_KEY,
5462
'apiSecret' => $this->API_SECRET,
5563
'client' => $this->client
@@ -88,6 +96,39 @@ private function setupOTWithMocks($mocks)
8896
$handlerStack->push($history);
8997
}
9098

99+
public function testCannotCreateBroadcastWithAddInvalidApiKey(): void
100+
{
101+
$this->expectException(InvalidArgumentException::class);
102+
$this->expectExceptionMessage('The apiKey was not a string nor an integer: ');
103+
104+
$broadcastObject = new Broadcast($this->broadcastData, [
105+
'apiKey' => new Client()
106+
]);
107+
}
108+
109+
public function testCannotCreateBroadcastWithInvalidApiSecret(): void
110+
{
111+
$this->expectException(InvalidArgumentException::class);
112+
$this->expectExceptionMessage('The apiSecret was not a string: OpenTok\Util\Client Object');
113+
114+
$broadcastObject = new Broadcast($this->broadcastData, [
115+
'apiKey' => 'test',
116+
'apiSecret' => new Client()
117+
]);
118+
}
119+
120+
public function testCannotCreateBroadcastWithInvalidApiUrl(): void
121+
{
122+
$this->expectException(InvalidArgumentException::class);
123+
$this->expectExceptionMessage('The optional apiUrl was not a string: ');
124+
125+
$broadcastObject = new Broadcast($this->broadcastData, [
126+
'apiKey' => 'validKey',
127+
'apiSecret' => 'validSecret',
128+
'apiUrl' => 'test'
129+
]);
130+
}
131+
91132
private function setupOT()
92133
{
93134
return $this->setupOTWithMocks([]);
@@ -98,8 +139,6 @@ public function testInitializes()
98139
// Arrange
99140
$this->setupOT();
100141
$this->setupBroadcasts(StreamMode::AUTO);
101-
// Act
102-
// Assert
103142
$this->assertInstanceOf(Broadcast::class, $this->broadcast);
104143
}
105144

@@ -180,5 +219,24 @@ public function testCanRemoveStreamFromBroadcast(): void
180219
);
181220
$this->assertTrue($return);
182221
}
222+
223+
public function testCannotRemoveStreamFromBroadcastOnAuto(): void
224+
{
225+
$this->expectException(InvalidArgumentException::class);
226+
227+
$this->setupOTWithMocks([[
228+
'code' => 200,
229+
'headers' => [
230+
'Content-Type' => 'application/json'
231+
],
232+
'path' => 'v2/project/APIKEY/broadcast/BROADCASTID/get'
233+
]]);
234+
235+
$this->setupBroadcasts(StreamMode::AUTO);
236+
237+
$return = $this->broadcast->removeStreamFromBroadcast(
238+
'5dfds4-asdda4asf4'
239+
);
240+
}
183241
}
184242

0 commit comments

Comments
 (0)