Skip to content

Commit 9fefacd

Browse files
authored
Merge pull request #334 from opentok/feature/auto-archive-name-resolution
Feature/auto archive name resolution
2 parents d6e93ba + 8e952c3 commit 9fefacd

File tree

4 files changed

+171
-2
lines changed

4 files changed

+171
-2
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/OpenTok.php

Lines changed: 26 additions & 1 deletion
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
@@ -1010,7 +1035,7 @@ public function getStream($sessionId, $streamId)
10101035
}
10111036

10121037
/**
1013-
* Returns a StreamList Object for the given session ID.
1038+
* Returns a StreamList Object for the given session ID.
10141039
*
10151040
* @param String $sessionId The session ID.
10161041
*

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,110 @@ public function testCreatesDefaultSession(): void
234234
);
235235
}
236236

237+
public function testCanStartAutoSessionWithArchiveNameAndResolution(): void
238+
{
239+
// Arrange
240+
$this->setupOTWithMocks([[
241+
'code' => 200,
242+
'headers' => [
243+
'Content-Type' => 'text/xml'
244+
],
245+
'path' => 'session/create/alwaysarchived'
246+
]]);
247+
248+
$options = [
249+
'archiveMode' => ArchiveMode::ALWAYS,
250+
'archiveName' => 'testAutoArchives',
251+
'archiveResolution' => '640x480'
252+
];
253+
254+
// Act
255+
$session = $this->opentok->createSession($options);
256+
257+
// Assert
258+
$this->assertCount(1, $this->historyContainer);
259+
260+
$request = $this->historyContainer[0]['request'];
261+
$this->assertEquals('POST', strtoupper($request->getMethod()));
262+
$this->assertEquals('/session/create', $request->getUri()->getPath());
263+
$this->assertEquals('api.opentok.com', $request->getUri()->getHost());
264+
$this->assertEquals('https', $request->getUri()->getScheme());
265+
266+
$e2ee = $this->getPostField($request, 'e2ee');
267+
$this->assertEquals('false', $e2ee);
268+
269+
$this->assertEquals('always', $this->getPostField($request, 'archiveMode'));
270+
$this->assertEquals('testAutoArchives', $this->getPostField($request, 'archiveName'));
271+
$this->assertEquals('640x480', $this->getPostField($request, 'archiveResolution'));
272+
273+
274+
$this->assertInstanceOf('OpenTok\Session', $session);
275+
}
276+
277+
public function testCannotStartSessionWithArchiveNameInManualMode(): void
278+
{
279+
$this->expectException(InvalidArgumentException::class);
280+
281+
// Arrange
282+
$this->setupOTWithMocks([[
283+
'code' => 200,
284+
'headers' => [
285+
'Content-Type' => 'text/xml'
286+
],
287+
'path' => 'session/create/relayed'
288+
]]);
289+
290+
$options = [
291+
'archiveName' => 'testAutoArchives',
292+
];
293+
294+
// Act
295+
$session = $this->opentok->createSession($options);
296+
}
297+
298+
public function testCannotStartSessionWithArchiveResolutionInManualMode(): void
299+
{
300+
$this->expectException(InvalidArgumentException::class);
301+
302+
// Arrange
303+
$this->setupOTWithMocks([[
304+
'code' => 200,
305+
'headers' => [
306+
'Content-Type' => 'text/xml'
307+
],
308+
'path' => 'session/create/relayed'
309+
]]);
310+
311+
$options = [
312+
'archiveResolution' => '640x480'
313+
];
314+
315+
// Act
316+
$session = $this->opentok->createSession($options);
317+
}
318+
319+
public function testCannotStartSessionWithInvalidResolutionInAutoArchive(): void
320+
{
321+
$this->expectException(InvalidArgumentException::class);
322+
323+
// Arrange
324+
$this->setupOTWithMocks([[
325+
'code' => 200,
326+
'headers' => [
327+
'Content-Type' => 'text/xml'
328+
],
329+
'path' => 'session/create/relayed'
330+
]]);
331+
332+
$options = [
333+
'archiveMode' => ArchiveMode::ALWAYS,
334+
'archiveResolution' => '680x440',
335+
];
336+
337+
// Act
338+
$session = $this->opentok->createSession($options);
339+
}
340+
237341
public function testCreatesE2EESession(): void
238342
{
239343
// Arrange

0 commit comments

Comments
 (0)