Skip to content

Commit 3fe0e17

Browse files
authored
Merge pull request #343 from opentok/dev
Dev to Main release
2 parents 57d9bd6 + 5d255be commit 3fe0e17

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

src/OpenTok/OpenTok.php

+60
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,66 @@ public function connectAudio(string $sessionId, string $token, array $websocketO
12621262
return $this->client->connectAudio($sessionId, $token, $websocketOptions);
12631263
}
12641264

1265+
/**
1266+
*
1267+
* Use this method to start real-time Live Captions for an OpenTok Session.
1268+
*
1269+
* The maximum allowed duration is 4 hours, after which the audio captioning will stop without any effect on the
1270+
* ongoing OpenTok Session. An event will be posted to your callback URL if provided when starting the captions.
1271+
*
1272+
* Each OpenTok Session supports only one audio captioning session.
1273+
*
1274+
* For more information about the Live Captions feature, see the Live Captions developer guide.
1275+
*
1276+
* @param string $sessionId The session ID of the OpenTok session. The audio from Publishers publishing into
1277+
* this session will be used to generate the captions.
1278+
*
1279+
* @param string $token A valid OpenTok token with role set to Moderator.
1280+
*
1281+
* @param string $languageCode (Optional) The BCP-47 code for a spoken language used on this call. The default
1282+
* value is "en-US". The following language codes are supported: "en-AU" (English, Australia), "en-GB"
1283+
* (English, UK), "es-US" (English, US), "zh-CN” (Chinese, Simplified), "fr-FR" (French), "fr-CA" (French, Canadian),
1284+
* "de-DE" (German), "hi-IN" (Hindi, Indian), "it-IT" (Italian), "ja-JP" (Japanese), "ko-KR" (Korean),
1285+
* "pt-BR" (Portuguese, Brazilian), "th-TH" (Thai).
1286+
*
1287+
* @param int $maxDuration (Optional) The maximum duration for the audio captioning, in seconds. The default value
1288+
* is 14,400 seconds (4 hours), the maximum duration allowed.
1289+
*
1290+
* @param bool $partialCaptions (Optional) Whether to enable this to faster captioning at the cost of some
1291+
* degree of inaccuracies. The default value is true.
1292+
*
1293+
* @param string $statusCallbackUrl (Optional) A publicly reachable URL controlled by the customer and capable
1294+
* of generating the content to be rendered without user intervention. The minimum length of the URL
1295+
* is 15 characters and the maximum length is 2048 characters. For more information,
1296+
* see <a href="https://tokbox.com/developer/guides/live-captions/#live-caption-status-updates">Live Caption status updates.</a>
1297+
*/
1298+
public function startCaptions(
1299+
string $sessionId,
1300+
string $token,
1301+
?string $languageCode = null,
1302+
?int $maxDuration = null,
1303+
?bool $partialCaptions = null,
1304+
?string $statusCallbackUrl = null
1305+
): array
1306+
{
1307+
return $this->client->startCaptions(
1308+
$sessionId,
1309+
$token,
1310+
$languageCode,
1311+
$maxDuration,
1312+
$partialCaptions,
1313+
$statusCallbackUrl
1314+
);
1315+
}
1316+
1317+
/**
1318+
* Use this method to stop live captions for a session.
1319+
*/
1320+
public function stopCaptions(string $captionsId)
1321+
{
1322+
return $this->client->stopCaptions($captionsId);
1323+
}
1324+
12651325
/** @internal */
12661326
private function signString($string, $secret)
12671327
{

src/OpenTok/Util/Client.php

+66
Original file line numberDiff line numberDiff line change
@@ -881,9 +881,75 @@ public function connectAudio(string $sessionId, string $token, array $websocketO
881881
$this->handleException($e);
882882
return false;
883883
}
884+
885+
return $jsonResponse;
886+
}
887+
888+
public function startCaptions(
889+
string $sessionId,
890+
string $token,
891+
?string $languageCode,
892+
?int $maxDuration,
893+
?bool $partialCaptions,
894+
?string $statusCallbackUrl
895+
)
896+
{
897+
$request = new Request(
898+
'POST',
899+
'/v2/project/' . $this->apiKey . '/captions'
900+
);
901+
902+
$body = [
903+
'sessionId' => $sessionId,
904+
'token' => $token,
905+
];
906+
907+
if ($languageCode !== null) {
908+
$body['languageCode'] = $languageCode;
909+
}
910+
911+
if ($maxDuration !== null) {
912+
$body['maxDuration'] = $maxDuration;
913+
}
914+
915+
if ($partialCaptions !== null) {
916+
$body['partialCaptions'] = $partialCaptions;
917+
}
918+
919+
if ($statusCallbackUrl !== null) {
920+
$body['statusCallbackUrl'] = $statusCallbackUrl;
921+
}
922+
923+
try {
924+
$response = $this->client->send($request, [
925+
'debug' => $this->isDebug(),
926+
'json' => $body
927+
]);
928+
$jsonResponse = json_decode($response->getBody(), true);
929+
} catch (\Exception $e) {
930+
$this->handleException($e);
931+
}
932+
884933
return $jsonResponse;
885934
}
886935

936+
public function stopCaptions(string $captionsId)
937+
{
938+
$request = new Request(
939+
'POST',
940+
'/v2/project/' . $this->apiKey . '/captions/' . $captionsId . '/stop'
941+
);
942+
943+
try {
944+
$this->client->send($request, [
945+
'debug' => $this->isDebug(),
946+
]);
947+
return true;
948+
} catch (\Exception $e) {
949+
$this->handleException($e);
950+
}
951+
}
952+
887953
private function handleException($e)
888954
{
889955
// TODO: test coverage

tests/OpenTokTest/OpenTokTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -2836,5 +2836,33 @@ public function testDefaultTimeoutErrorsIfLessThanZero(): void
28362836
$this->expectExceptionMessage('Default Timeout must be a number greater than zero');
28372837
new OpenTok('1234', 'abd', ['timeout' => -1]);
28382838
}
2839+
2840+
public function testCanStartCaptions(): void
2841+
{
2842+
$this->setupOTWithMocks([[
2843+
'code' => 202,
2844+
'headers' => [
2845+
'Content-Type' => 'application/json'
2846+
],
2847+
'path' => '/v2/project/APIKEY/session/SESSIONID/caption-start'
2848+
]]);
2849+
2850+
$result = $this->opentok->startCaptions('SESSION_ID', 'abc');
2851+
$this->assertEquals('7c0680fc-6274-4de5-a66f-d0648e8d3ac2', $result['captionsId']);
2852+
}
2853+
2854+
public function testCanStopCaptions(): void
2855+
{
2856+
$this->setupOTWithMocks([[
2857+
'code' => 202,
2858+
'headers' => [
2859+
'Content-Type' => 'application/json'
2860+
],
2861+
'path' => '/v2/project/APIKEY/session/SESSIONID/caption-stop'
2862+
]]);
2863+
2864+
$result = $this->opentok->stopCaptions('7c0680fc-6274-4de5-a66f-d0648e8d3ac2');
2865+
$this->assertTrue($result);
2866+
}
28392867
}
28402868

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"captionsId": "7c0680fc-6274-4de5-a66f-d0648e8d3ac2"
3+
}

tests/mock/v2/project/APIKEY/session/SESSIONID/caption-stop

Whitespace-only changes.

0 commit comments

Comments
 (0)