Skip to content

Commit 398f722

Browse files
authored
Merge pull request #330 from opentok/feature/audio-only-broadcast
isAudio and isVideo added to Broadcast
2 parents 9fefacd + 2b9177c commit 398f722

File tree

7 files changed

+114
-37
lines changed

7 files changed

+114
-37
lines changed

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: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -868,19 +868,21 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas
868868
Validators::validateResolution($options['resolution']);
869869
}
870870

871-
if (isset($options['output']['hls'])) {
872-
Validators::validateBroadcastOutputOptions($options['output']['hls']);
871+
if (isset($options['outputs']['hls'])) {
872+
Validators::validateBroadcastOutputOptions($options['outputs']['hls']);
873873
}
874874

875-
if (isset($options['output']['rtmp'])) {
876-
Validators::validateRtmpStreams($options['output']['rtmp']);
875+
if (isset($options['outputs']['rtmp'])) {
876+
Validators::validateRtmpStreams($options['outputs']['rtmp']);
877877
}
878878

879879
$defaults = [
880880
'layout' => Layout::getBestFit(),
881+
'hasAudio' => true,
882+
'hasVideo' => true,
881883
'streamMode' => 'auto',
882884
'resolution' => '640x480',
883-
'output' => [
885+
'outputs' => [
884886
'hls' => [
885887
'dvr' => false,
886888
'lowLatency' => false
@@ -890,7 +892,7 @@ public function startBroadcast(string $sessionId, array $options = []): Broadcas
890892

891893
$options = array_merge($defaults, $options);
892894

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

895897
// validate arguments
896898
Validators::validateSessionId($sessionId);

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

tests/OpenTokTest/OpenTokTest.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ class OpenTokTest extends TestCase
3434
protected $client;
3535

3636
protected static $mockBasePath;
37-
/**
38-
* @var array
39-
*/
37+
4038
public $historyContainer;
4139

4240
public static function setUpBeforeClass(): void
@@ -1541,7 +1539,7 @@ public function testCanStartBroadcastWithRmtp()
15411539
$sessionId = '2_MX44NTQ1MTF-fjE0NzI0MzU2MDUyMjN-eVgwNFJhZmR6MjdockFHanpxNzBXaEFXfn4';
15421540

15431541
$options = [
1544-
'output' => [
1542+
'outputs' => [
15451543
'hls' => [
15461544
'dvr' => true,
15471545
'lowLatency' => false
@@ -1583,7 +1581,7 @@ public function testCannotStartBroadcastWithOver5RtmpChannels(): void
15831581
$sessionId = '2_MX44NTQ1MTF-fjE0NzI0MzU2MDUyMjN-eVgwNFJhZmR6MjdockFHanpxNzBXaEFXfn4';
15841582

15851583
$options = [
1586-
'output' => [
1584+
'outputs' => [
15871585
'hls' => [
15881586
'dvr' => true,
15891587
'lowLatency' => false
@@ -1657,7 +1655,7 @@ public function testCanStartBroadcastWithDvrEnabled(): void
16571655
$sessionId = '2_MX44NTQ1MTF-fjE0NzI0MzU2MDUyMjN-eVgwNFJhZmR6MjdockFHanpxNzBXaEFXfn4';
16581656

16591657
$options = [
1660-
'output' => [
1658+
'outputs' => [
16611659
'hls' => [
16621660
'dvr' => true,
16631661
'lowLatency' => false
@@ -1684,7 +1682,7 @@ public function testCanStartBroadcastWithLowLatencyEnabled(): void
16841682
$sessionId = '2_MX44NTQ1MTF-fjE0NzI0MzU2MDUyMjN-eVgwNFJhZmR6MjdockFHanpxNzBXaEFXfn4';
16851683

16861684
$options = [
1687-
'output' => [
1685+
'outputs' => [
16881686
'hls' => [
16891687
'dvr' => true,
16901688
'lowLatency' => false
@@ -1713,7 +1711,7 @@ public function testCannotStartBroadcastWithBothHlsAndDvrEnabled(): void
17131711
$sessionId = '2_MX44NTQ1MTF-fjE0NzI0MzU2MDUyMjN-eVgwNFJhZmR6MjdockFHanpxNzBXaEFXfn4';
17141712

17151713
$options = [
1716-
'output' => [
1714+
'outputs' => [
17171715
'hls' => [
17181716
'dvr' => true,
17191717
'lowLatency' => true

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@
2222
"partnerId":854511,
2323
"maxDuration":5400,
2424
"resolution": "1280x720",
25-
"streamMode": "auto"
25+
"streamMode": "auto",
26+
"hasAudio": true,
27+
"hasVideo": true
2628
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@
2222
"partnerId":854511,
2323
"maxDuration":5400,
2424
"resolution": "1280x720",
25-
"streamMode": "auto"
25+
"streamMode": "auto",
26+
"hasAudio": true,
27+
"hasVideo": true
2628
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@
2222
"partnerId":854511,
2323
"maxDuration":5400,
2424
"resolution": "1280x720",
25-
"streamMode": "auto"
25+
"streamMode": "auto",
26+
"hasAudio": true,
27+
"hasVideo": true
2628
}

0 commit comments

Comments
 (0)