Skip to content

Commit a35aab8

Browse files
authored
Merge pull request #292 from opentok/feature/mute-all-minimal
Implement mute stream with simple tests
2 parents d31bb67 + 2555066 commit a35aab8

File tree

6 files changed

+327
-2
lines changed

6 files changed

+327
-2
lines changed

src/OpenTok/OpenTok.php

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,112 @@ public function forceDisconnect($sessionId, $connectionId)
478478
return $this->client->forceDisconnect($sessionId, $connectionId);
479479
}
480480

481+
/**
482+
* Force the publisher of a specific stream to mute its published audio.
483+
*
484+
* <p>
485+
* Also see the <a href="#method_forceMuteAll">OpenTok->forceMuteAll()</a> method.
486+
*
487+
* @param string $sessionId The OpenTok session ID containing the stream.
488+
*
489+
* @param string $streamId The stream ID.
490+
*
491+
* @return bool Whether the call succeeded or failed.
492+
*/
493+
public function forceMuteStream(string $sessionId, string $streamId): bool
494+
{
495+
Validators::validateSessionId($sessionId);
496+
Validators::validateStreamId($streamId);
497+
498+
try {
499+
$this->client->forceMuteStream($sessionId, $streamId);
500+
} catch (\Exception $e) {
501+
return false;
502+
}
503+
504+
return true;
505+
}
506+
507+
/**
508+
* Force all streams (except for an optional list of streams) in an OpenTok session
509+
* to mute published audio.
510+
*
511+
* <p>
512+
* In addition to existing streams, any streams that are published after the call to
513+
* this method are published with audio muted. You can remove the mute state of a session
514+
* by calling the <a href="#method_disableForceMute">OpenTok->disableForceMute()</a> method.
515+
*
516+
* <p>
517+
* Also see the <a href="#method_forceMuteStream">OpenTok->forceMuteStream()</a> method.
518+
*
519+
* @param string $sessionId The OpenTok session ID.
520+
*
521+
* @param array<string> $options This array defines options and includes the following keys:
522+
*
523+
* <ul>
524+
* <li><code>'excludedStreams'</code> (array, optional) &mdash; An array of stream IDs
525+
* corresponding to streams that should not be muted. This is an optional property.
526+
* If you omit this property, all streams in the session will be muted.</li>
527+
* </ul>
528+
*
529+
* @return bool Whether the call succeeded or failed.
530+
*/
531+
public function forceMuteAll(string $sessionId, array $options): bool
532+
{
533+
// Active is always true when forcing mute all
534+
$options['active'] = true;
535+
536+
Validators::validateSessionId($sessionId);
537+
Validators::validateForceMuteAllOptions($options);
538+
539+
try {
540+
$this->client->forceMuteAll($sessionId, $options);
541+
} catch (\Exception $e) {
542+
return false;
543+
}
544+
545+
return true;
546+
}
547+
548+
/**
549+
* Disables the active mute state of the session. After you call this method, new streams
550+
* published to the session will no longer have audio muted.
551+
*
552+
* <p>
553+
* After you call the <a href="#method_forceMuteAll">OpenTok->forceMuteAll()</a> method,
554+
* any streams published after the call are published with audio muted. Call the
555+
* <c>disableForceMute()</c> method to remove the mute state of a session, so that
556+
* new published streams are not automatically muted.
557+
*
558+
* @param string $sessionId The OpenTok session ID.
559+
*
560+
* @param array<string> $options This array defines options and includes the following keys:
561+
*
562+
* <ul>
563+
* <li><code>'excludedStreams'</code> (array, optional) &mdash; An array of stream IDs
564+
* corresponding to streams that should not be muted. This is an optional property.
565+
* If you omit this property, all streams in the session will be muted.</li>
566+
* </ul>
567+
*
568+
* @return bool Whether the call succeeded or failed.
569+
*/
570+
public function disableForceMute(string $sessionId, array $options): bool
571+
{
572+
// Active is always false when disabling force mute
573+
$options['active'] = false;
574+
575+
Validators::validateSessionId($sessionId);
576+
Validators::validateForceMuteAllOptions($options);
577+
578+
try {
579+
$this->client->forceMuteAll($sessionId, $options);
580+
} catch (\Exception $e) {
581+
return false;
582+
}
583+
584+
return true;
585+
}
586+
481587
/**
482588
* Starts a live streaming broadcast of an OpenTok session.
483589
*
@@ -693,7 +799,7 @@ public function listStreams($sessionId)
693799
* <p>
694800
* This is an example of insecure call negotiation: "sip:[email protected]".
695801
*
696-
* @param array $options This array defines options for the token. It includes the
802+
* @param array $options This array defines options for the SIP call. It includes the
697803
* following keys, all of which are optional:
698804
*
699805
* <ul>

src/OpenTok/Util/Client.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,45 @@ private function postFieldsForOptions($options)
609609
return $options;
610610
}
611611

612+
public function forceMuteStream(string $sessionId, string $streamId)
613+
{
614+
$request = new Request(
615+
'POST',
616+
'/v2/project/' . $this->apiKey . '/session/' . $sessionId . '/stream/' . $streamId . '/mute'
617+
);
618+
619+
try {
620+
$response = $this->client->send($request, [
621+
'debug' => $this->isDebug(),
622+
]);
623+
$jsonResponse = json_decode($response->getBody(), true);
624+
} catch (\Exception $e) {
625+
$this->handleException($e);
626+
return false;
627+
}
628+
return $jsonResponse;
629+
}
630+
631+
public function forceMuteAll(string $sessionId, array $options)
632+
{
633+
$request = new Request(
634+
'POST',
635+
'/v2/project/' . $this->apiKey . '/session/' . $sessionId . '/mute'
636+
);
637+
638+
try {
639+
$response = $this->client->send($request, [
640+
'debug' => $this->isDebug(),
641+
'json' => $options
642+
]);
643+
$jsonResponse = json_decode($response->getBody(), true);
644+
} catch (\Exception $e) {
645+
$this->handleException($e);
646+
return false;
647+
}
648+
return $jsonResponse;
649+
}
650+
612651
//echo 'Uh oh! ' . $e->getMessage();
613652
//echo 'HTTP request URL: ' . $e->getRequest()->getUrl() . "\n";
614653
//echo 'HTTP request: ' . $e->getRequest() . "\n";

src/OpenTok/Util/Validators.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ public static function validateApiKey($apiKey)
3030
);
3131
}
3232
}
33+
34+
public static function validateForceMuteAllOptions(array $options)
35+
{
36+
$validOptions = [
37+
'excludedStreams' => 'array',
38+
'active' => 'boolean'
39+
];
40+
41+
foreach ($validOptions as $optionName => $optionType) {
42+
if (isset($options[$optionName])) {
43+
if (getType($options[$optionName]) !== $optionType) {
44+
throw new InvalidArgumentException('Invalid type given in options for: ' . $options[$optionName]);
45+
}
46+
}
47+
}
48+
49+
if (isset($options['excludedStreams'])) {
50+
foreach ($options['excludedStreams'] as $streamId) {
51+
self::validateStreamId($streamId);
52+
}
53+
}
54+
}
55+
3356
public static function validateApiSecret($apiSecret)
3457
{
3558
if (!(is_string($apiSecret))) {

tests/OpenTokTest/OpenTokTest.php

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,93 @@ public function testGetsBroadcast()
13431343
$this->assertInstanceOf('OpenTok\Broadcast', $broadcast);
13441344
}
13451345

1346+
public function testCanMuteStream(): void
1347+
{
1348+
$this->setupOTWithMocks([[
1349+
'code' => 200,
1350+
'headers' => [
1351+
'Content-Type' => 'application/json'
1352+
],
1353+
'path' => '/v2/project/APIKEY/session/SESSIONID/mute'
1354+
]]);
1355+
1356+
$sessionId = 'SESSIONID';
1357+
$streamId = 'STREAMID';
1358+
1359+
$result = $this->opentok->forceMuteStream($sessionId, $streamId);
1360+
$this->assertTrue($result);
1361+
}
1362+
1363+
public function testCanMuteStreams(): void
1364+
{
1365+
$this->setupOTWithMocks([[
1366+
'code' => 200,
1367+
'headers' => [
1368+
'Content-Type' => 'application/json'
1369+
],
1370+
'path' => '/v2/project/APIKEY/session/SESSIONID/mute'
1371+
]]);
1372+
1373+
$streamIds = ['TEST1', 'TEST2'];
1374+
$sessionId = 'SESSIONID';
1375+
1376+
$result = $this->opentok->forceMuteAll($sessionId, $streamIds);
1377+
$this->assertTrue($result);
1378+
}
1379+
1380+
public function testThrowsExceptionWhenInvalidStreamId(): void
1381+
{
1382+
$this->setupOTWithMocks([[
1383+
'code' => 404,
1384+
'headers' => [
1385+
'Content-Type' => 'application/json'
1386+
],
1387+
'path' => '/v2/project/APIKEY/session/SESSIONID/mute'
1388+
]]);
1389+
1390+
$streamId = 'TEST1';
1391+
$sessionId = 'SESSIONID';
1392+
1393+
$result = $this->opentok->forceMuteStream($sessionId, $streamId);
1394+
$this->assertFalse($result);
1395+
}
1396+
1397+
public function testThrowsExceptionWhenInvalidStreamIds(): void
1398+
{
1399+
$this->setupOTWithMocks([[
1400+
'code' => 404,
1401+
'headers' => [
1402+
'Content-Type' => 'application/json'
1403+
],
1404+
'path' => '/v2/project/APIKEY/session/SESSIONID/mute'
1405+
]]);
1406+
1407+
$streamIds = ['TEST1', 'TEST2'];
1408+
$sessionId = 'SESSIONID';
1409+
1410+
$result = $this->opentok->forceMuteAll($sessionId, $streamIds);
1411+
$this->assertFalse($result);
1412+
}
1413+
1414+
public function testCannotMuteStreamsWithWrongTypePayload(): void
1415+
{
1416+
$this->expectException(\TypeError::class);
1417+
1418+
$this->setupOTWithMocks([[
1419+
'code' => 200,
1420+
'headers' => [
1421+
'Content-Type' => 'application/json'
1422+
],
1423+
'path' => '/v2/project/APIKEY/session/SESSIONID/mute'
1424+
]]);
1425+
1426+
$streamIdsString = implode(',', ['TEST1', 'TEST2']);
1427+
$sessionId = 'SESSIONID';
1428+
1429+
$result = $this->opentok->forceMuteAll($sessionId, $streamIdsString);
1430+
$this->assertFalse($result);
1431+
}
1432+
13461433
public function testUpdatesBroadcastLayoutWithPredefined()
13471434
{
13481435
// Arrange
@@ -1980,7 +2067,6 @@ public function testListStreams()
19802067
$this->assertStringStartsWith('OpenTok-PHP-SDK/4.10.0', $userAgent);
19812068

19822069
$this->assertInstanceOf('OpenTok\StreamList', $streamList);
1983-
19842070
}
19852071

19862072
public function testsSetArchiveLayoutWithPredefined()
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace OpenTokTest\Validators;
4+
5+
use OpenTok\Exception\InvalidArgumentException;
6+
use OpenTok\Util\Validators;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class ValidatorsTest extends TestCase
10+
{
11+
public function testWillPassCorrectPayload(): void
12+
{
13+
$this->expectNotToPerformAssertions();
14+
15+
$options = [
16+
'excludedStreams' => [
17+
'streamId1',
18+
'streamId2'
19+
],
20+
'active' => true
21+
];
22+
23+
Validators::validateForceMuteAllOptions($options);
24+
}
25+
26+
public function testWillFailWhenStreamIdsAreNotCorrect(): void
27+
{
28+
$this->expectException(InvalidArgumentException::class);
29+
30+
$options = [
31+
'excludedStreams' => [
32+
3536,
33+
'streamId2'
34+
],
35+
'active' => true
36+
];
37+
38+
Validators::validateForceMuteAllOptions($options);
39+
}
40+
41+
public function testWillFailWhenActiveIsNotBool(): void
42+
{
43+
$this->expectException(InvalidArgumentException::class);
44+
45+
$options = [
46+
'excludedStreams' => [
47+
'streamId1',
48+
'streamId2'
49+
],
50+
'active' => 'true'
51+
];
52+
53+
Validators::validateForceMuteAllOptions($options);
54+
}
55+
56+
public function testWillFailWhenStreamIdsIsNotArray(): void
57+
{
58+
$this->expectException(InvalidArgumentException::class);
59+
60+
$options = [
61+
'excludedStreams' => 'streamIdOne',
62+
'active' => false
63+
];
64+
65+
Validators::validateForceMuteAllOptions($options);
66+
}
67+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"code" : 200,
3+
"message" : "OK"
4+
}

0 commit comments

Comments
 (0)