Skip to content

Commit 48fb694

Browse files
authored
Merge pull request #165 from aoberoi/broadcast-custom-layout-api
Broadcast custom layout api
2 parents 5390d5f + cee48c7 commit 48fb694

File tree

19 files changed

+925
-7
lines changed

19 files changed

+925
-7
lines changed

src/OpenTok/Archive.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
* 10 minutes. To generate a new URL, call the Archive.listArchives() or OpenTok.getArchive() method.
7777
*/
7878
class Archive {
79+
// NOTE: after PHP 5.3.0 support is dropped, the class can implement JsonSerializable
7980

8081
/** @internal */
8182
private $data;
@@ -196,16 +197,23 @@ public function delete()
196197
*/
197198
public function toJson()
198199
{
199-
return json_encode($this->data);
200+
return json_encode($this->jsonSerialize());
200201
}
201202

202203
/**
203204
* Returns an associative array representation of this Archive object.
205+
* @deprecated 3.0.0 A more standard name for this method is supplied by JsonSerializable
206+
* @see Archive::jsonSerialize() for a method with the same behavior
204207
*/
205208
public function toArray()
206209
{
207210
return $this->data;
208211
}
212+
213+
public function jsonSerialize()
214+
{
215+
return $this->data;
216+
}
209217
}
210218

211219
/* vim: set ts=4 sw=4 tw=100 sts=4 et :*/

src/OpenTok/Broadcast.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace OpenTok;
4+
5+
use OpenTok\Exception\BroadcastDomainException;
6+
use OpenTok\Exception\BroadcastUnexpectedValueException;
7+
use OpenTok\Util\Validators;
8+
9+
class Broadcast {
10+
// NOTE: after PHP 5.3.0 support is dropped, the class can implement JsonSerializable
11+
12+
private $data;
13+
private $isStopped = false;
14+
private $client;
15+
16+
public function __construct($broadcastData, $options = array()) {
17+
// unpack optional arguments (merging with default values) into named variables
18+
$defaults = array(
19+
'apiKey' => null,
20+
'apiSecret' => null,
21+
'apiUrl' => 'https://api.opentok.com',
22+
'client' => null,
23+
'isStopped' => false
24+
);
25+
$options = array_merge($defaults, array_intersect_key($options, $defaults));
26+
list($apiKey, $apiSecret, $apiUrl, $client, $isStopped) = array_values($options);
27+
28+
// validate params
29+
Validators::validateBroadcastData($broadcastData);
30+
Validators::validateClient($client);
31+
32+
$this->data = $broadcastData;
33+
34+
$this->isStopped = $isStopped;
35+
36+
$this->client = isset($client) ? $client : new Client();
37+
if (!$this->client->isConfigured()) {
38+
Validators::validateApiKey($apiKey);
39+
Validators::validateApiSecret($apiSecret);
40+
Validators::validateApiUrl($apiUrl);
41+
42+
$this->client->configure($apiKey, $apiSecret, $apiUrl);
43+
}
44+
}
45+
46+
public function __get($name)
47+
{
48+
switch ($name) {
49+
case 'createdAt':
50+
case 'updatedAt':
51+
case 'id':
52+
case 'partnerId':
53+
case 'sessionId':
54+
case 'broadcastUrls':
55+
return $this->data[$name];
56+
break;
57+
case 'hlsUrl':
58+
return $this->data['broadcastUrls']['hls'];
59+
break;
60+
case 'isStopped':
61+
return $this->isStopped;
62+
break;
63+
default:
64+
return null;
65+
}
66+
}
67+
68+
public function stop()
69+
{
70+
if ($this->isStopped) {
71+
throw new BroadcastDomainException(
72+
'You cannot stop a broadcast which is already stopped.'
73+
);
74+
}
75+
76+
$broadcastData = $this->client->stopBroadcast($this->data['id']);
77+
78+
try {
79+
Validators::validateBroadcastData($broadcastData);
80+
} catch (InvalidArgumentException $e) {
81+
throw new BroadcastUnexpectedValueException('The broadcast JSON returned after stopping was not valid', null, $e);
82+
}
83+
84+
$this->data = $broadcastData;
85+
return $this;
86+
}
87+
88+
// TODO: not yet implemented by the platform
89+
// public function getLayout()
90+
// {
91+
// $layoutData = $this->client->getLayout($this->id, 'broadcast');
92+
// return Layout::fromData($layoutData);
93+
// }
94+
95+
public function updateLayout($layout)
96+
{
97+
Validators::validateLayout($layout);
98+
99+
// TODO: platform implementation did not meet API review spec
100+
// $layoutData = $this->client->updateLayout($this->id, $layout, 'broadcast');
101+
// return Layout::fromData($layoutData);
102+
103+
$this->client->updateLayout($this->id, $layout, 'broadcast');
104+
}
105+
106+
public function jsonSerialize()
107+
{
108+
return $this->data;
109+
}
110+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace OpenTok\Exception;
4+
5+
/**
6+
* Defines the exception thrown when you use an invalid API or secret and call a broadcast method.
7+
*/
8+
class BroadcastAuthenticationException extends \OpenTok\Exception\AuthenticationException implements \OpenTok\Exception\BroadcastException
9+
{
10+
}
11+
/* vim: set ts=4 sw=4 tw=100 sts=4 et :*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace OpenTok\Exception;
4+
5+
// TODO: this kind of exception has a detailed message from the HTTP response, use it.
6+
/**
7+
* Defines an exception thrown when a call to a broadcast method results in an error response from
8+
* the server.
9+
*/
10+
class BroadcastDomainException extends \OpenTok\Exception\DomainException implements \OpenTok\Exception\BroadcastException
11+
{
12+
}
13+
/* vim: set ts=4 sw=4 tw=100 sts=4 et :*/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace OpenTok\Exception;
4+
5+
/**
6+
* The interface used by all exceptions resulting from calls to the broadcast API.
7+
*/
8+
interface BroadcastException
9+
{
10+
}
11+
/* vim: set ts=4 sw=4 tw=100 sts=4 et :*/
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace OpenTok\Exception;
4+
5+
/**
6+
* Defines an exception thrown when a call to a broadcast method results in an error due to an
7+
* unexpected value.
8+
*/
9+
class BroadcastUnexpectedValueException extends \OpenTok\Exception\UnexpectedValueException implements \OpenTok\Exception\BroadcastException
10+
{
11+
}
12+
/* vim: set ts=4 sw=4 tw=100 sts=4 et :*/

src/OpenTok/Layout.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace OpenTok;
4+
5+
use OpenTok\Util\Validators;
6+
7+
class Layout {
8+
// NOTE: after PHP 5.3.0 support is dropped, the class can implement JsonSerializable
9+
10+
private static $bestFit = null;
11+
private static $pip = null;
12+
private static $verticalPresentation = null;
13+
private static $horizontalPresentaton = null;
14+
15+
public static function getBestFit()
16+
{
17+
if (is_null(self::$bestFit)) {
18+
self::$bestFit = new Layout('bestFit');
19+
}
20+
return self::$bestFit;
21+
}
22+
23+
public static function getPIP()
24+
{
25+
if (is_null(self::$pip)) {
26+
self::$pip = new Layout('pip');
27+
}
28+
return self::$pip;
29+
}
30+
31+
public static function getVerticalPresentation()
32+
{
33+
if (is_null(self::$verticalPresentation)) {
34+
self::$verticalPresentation = new Layout('verticalPresentation');
35+
}
36+
return self::$verticalPresentation;
37+
}
38+
39+
public static function getHorizontalPresentation()
40+
{
41+
if (is_null(self::$horizontalPresentaton)) {
42+
self::$horizontalPresentaton = new Layout('horizontalPresentaton');
43+
}
44+
return self::$horizontalPresentaton;
45+
}
46+
47+
public static function createCustom($options)
48+
{
49+
// unpack optional arguments (merging with default values) into named variables
50+
// NOTE: the default value of stylesheet=null will not pass validation, this essentially
51+
// means that stylesheet is not optional. its still purposely left as part of the
52+
// $options argument so that it can become truly optional in the future.
53+
$defaults = array('stylesheet' => null);
54+
$options = array_merge($defaults, array_intersect_key($options, $defaults));
55+
list($stylesheet) = array_values($options);
56+
57+
// validate arguments
58+
Validators::validateLayoutStylesheet($stylesheet);
59+
60+
return new Layout('custom', $stylesheet);
61+
}
62+
63+
64+
public static function fromData($layoutData)
65+
{
66+
if (array_key_exists('stylesheet', $layoutData)) {
67+
return new Layout($layoutData['type'], $layoutData['stylesheet']);
68+
} else {
69+
return new Layout($layoutData['type']);
70+
}
71+
}
72+
73+
private $type;
74+
private $stylesheet;
75+
76+
private function __construct($type, $stylesheet = null)
77+
{
78+
$this->type = $type;
79+
$this->stylesheet = $stylesheet;
80+
}
81+
82+
public function jsonSerialize() {
83+
$data = array(
84+
'type' => $this->type
85+
);
86+
87+
// omit 'stylesheet' property unless it is explicitly defined
88+
if (isset($this->stylesheet)) {
89+
$data['stylesheet'] = $this->stylesheet;
90+
}
91+
return $data;
92+
}
93+
94+
public function toJson()
95+
{
96+
return json_encode($this->jsonSerialize());
97+
}
98+
}

src/OpenTok/OpenTok.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use OpenTok\Session;
66
use OpenTok\Archive;
7+
use OpenTok\Broadcast;
8+
use OpenTok\Layout;
79
use OpenTok\Role;
810
use OpenTok\MediaMode;
911
use OpenTok\ArchiveMode;
@@ -384,6 +386,88 @@ public function listArchives($offset=0, $count=null)
384386
return new ArchiveList($archiveListData, array( 'client' => $this->client ));
385387
}
386388

389+
public function startBroadcast($sessionId, $options=array())
390+
{
391+
// unpack optional arguments (merging with default values) into named variables
392+
// NOTE: although the server can be authoritative about the default value of layout, its
393+
// not preferred to depend on that in the SDK because its then harder to garauntee backwards
394+
// compatibility
395+
$defaults = array(
396+
'layout' => Layout::getBestFit()
397+
);
398+
$options = array_merge($defaults, array_intersect_key($options, $defaults));
399+
list($layout) = array_values($options);
400+
401+
// validate arguments
402+
Validators::validateSessionId($sessionId);
403+
Validators::validateLayout($layout);
404+
405+
// make API call
406+
$broadcastData = $this->client->startBroadcast($sessionId, $options);
407+
408+
return new Broadcast($broadcastData, array( 'client' => $this->client ));
409+
}
410+
411+
public function stopBroadcast($broadcastId)
412+
{
413+
// validate arguments
414+
Validators::validateBroadcastId($broadcastId);
415+
416+
// make API call
417+
$broadcastData = $this->client->stopBroadcast($broadcastId);
418+
return new Broadcast($broadcastData, array(
419+
'client' => $this->client,
420+
'isStopped' => true
421+
));
422+
}
423+
424+
public function getBroadcast($broadcastId)
425+
{
426+
Validators::validateBroadcastId($broadcastId);
427+
428+
$broadcastData = $this->client->getBroadcast($broadcastId);
429+
return new Broadcast($broadcastData, array( 'client' => $this->client ));
430+
}
431+
432+
// TODO: not yet implemented by the platform
433+
// public function getBroadcastLayout($broadcastId)
434+
// {
435+
// Validators::validateBroadcastId($broadcastId);
436+
//
437+
// $layoutData = $this->client->getLayout($broadcastId, 'broadcast');
438+
// return Layout::fromData($layoutData);
439+
// }
440+
441+
public function updateBroadcastLayout($broadcastId, $layout)
442+
{
443+
Validators::validateBroadcastId($broadcastId);
444+
Validators::validateLayout($layout);
445+
446+
// TODO: platform implementation does not meet API Review spec
447+
// $layoutData = $this->client->updateLayout($broadcastId, $layout, 'broadcast');
448+
// return Layout::fromData($layoutData);
449+
450+
$this->client->updateLayout($broadcastId, $layout, 'broadcast');
451+
}
452+
453+
public function updateStream($sessionId, $streamId, $properties = array())
454+
{
455+
// unpack optional arguments (merging with default values) into named variables
456+
$defaults = array(
457+
'layoutClassList' => array()
458+
);
459+
$properties = array_merge($defaults, array_intersect_key($properties, $defaults));
460+
list($layoutClassList) = array_values($properties);
461+
462+
// validate arguments
463+
Validators::validateSessionId($sessionId);
464+
Validators::validateStreamId($streamId);
465+
Validators::validateLayoutClassList($layoutClassList, 'JSON');
466+
467+
// make API call
468+
$this->client->updateStream($sessionId, $streamId, $properties);
469+
}
470+
387471
/**
388472
* Initiate an outgoing SIP call
389473
*

0 commit comments

Comments
 (0)