Skip to content

Commit 379a71e

Browse files
authored
Added Captions API (#245)
* Added Captions API * Bump version: v4.12.0 → v4.13.0 * Fixed bad character
1 parent e0bc90a commit 379a71e

File tree

9 files changed

+431
-10
lines changed

9 files changed

+431
-10
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[bumpversion]
22
commit = True
33
tag = False
4-
current_version = v4.12.0
4+
current_version = v4.13.0
55
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+)(?P<build>\d+))?
66
serialize =
77
{major}.{minor}.{patch}-{release}{build}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ When you use Maven as your build tool, you can manage dependencies in the `pom.x
3434
<dependency>
3535
<groupId>com.tokbox</groupId>
3636
<artifactId>opentok-server-sdk</artifactId>
37-
<version>4.12.0</version>
37+
<version>4.13.0</version>
3838
</dependency>
3939
```
4040

@@ -44,7 +44,7 @@ When you use Gradle as your build tool, you can manage dependencies in the `buil
4444

4545
```groovy
4646
dependencies {
47-
compile group: 'com.tokbox', name: 'opentok-server-sdk', version: '4.12.0'
47+
compile group: 'com.tokbox', name: 'opentok-server-sdk', version: '4.13.0'
4848
}
4949
```
5050

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ plugins {
1111

1212
group = 'com.tokbox'
1313
archivesBaseName = 'opentok-server-sdk'
14-
version = '4.12.0'
14+
version = '4.13.0'
1515
sourceCompatibility = "1.8"
1616
targetCompatibility = "1.8"
1717

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* OpenTok Java SDK
3+
* Copyright (C) 2023 Vonage.
4+
* http://www.tokbox.com
5+
*
6+
* Licensed under The MIT License (MIT). See LICENSE file for more information.
7+
*/
8+
package com.opentok;
9+
10+
import com.fasterxml.jackson.annotation.JsonCreator;
11+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
12+
import com.fasterxml.jackson.annotation.JsonProperty;
13+
import com.fasterxml.jackson.annotation.JsonValue;
14+
import com.fasterxml.jackson.core.JsonProcessingException;
15+
import com.fasterxml.jackson.databind.ObjectMapper;
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
import java.util.Map;
19+
20+
/**
21+
* Represents the response from {@link OpenTok#startCaptions(String, String, CaptionProperties)}.
22+
*/
23+
@JsonIgnoreProperties(ignoreUnknown=true)
24+
public class Caption {
25+
private String captionsId;
26+
27+
/**
28+
* The unique ID for the audio captioning session.
29+
*
30+
* @return The captions UUID as a string.
31+
*/
32+
@JsonProperty("captionsId")
33+
public String getCaptionsId() {
34+
return captionsId;
35+
}
36+
37+
@Override
38+
public String toString() {
39+
try {
40+
return new ObjectMapper().writeValueAsString(this);
41+
} catch (JsonProcessingException e) {
42+
return "";
43+
}
44+
}
45+
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/**
2+
* OpenTok Java SDK
3+
* Copyright (C) 2023 Vonage.
4+
* http://www.tokbox.com
5+
*
6+
* Licensed under The MIT License (MIT). See LICENSE file for more information.
7+
*/
8+
package com.opentok;
9+
10+
/**
11+
* Defines values for the <code>properties</code> parameter of the
12+
* {@link OpenTok#startCaptions(String, String, CaptionProperties)} method.
13+
*
14+
* @see OpenTok#startCaptions(String, String, CaptionProperties)
15+
*/
16+
public class CaptionProperties {
17+
private final String statusCallbackUrl, languageCode;
18+
private final int maxDuration;
19+
private final boolean partialCaptions;
20+
21+
private CaptionProperties(Builder builder) {
22+
statusCallbackUrl = builder.statusCallbackUrl;
23+
languageCode = builder.languageCode;
24+
maxDuration = builder.maxDuration;
25+
partialCaptions = builder.partialCaptions;
26+
}
27+
28+
/**
29+
* A publicly reachable URL controlled by the customer and capable of generating the content to
30+
* be rendered without user intervention. The minimum length of the URL is 15 characters and the
31+
* maximum length is 2048 characters. For more information, see
32+
* <a href=https://tokbox.com/developer/guides/live-captions/#live-caption-status-updates>
33+
* Live Caption status updates</a>.
34+
*
35+
* @return The status callback URL as a string, or {@code null} if not set.
36+
*/
37+
public String getStatusCallbackUrl() {
38+
return statusCallbackUrl;
39+
}
40+
41+
/**
42+
* The BCP-47 code for a spoken language used on this call.
43+
*
44+
* @return The language code as a string.
45+
*/
46+
public String getLanguageCode() {
47+
return languageCode;
48+
}
49+
50+
/**
51+
* The maximum duration for the audio captioning, in seconds.
52+
*
53+
* @return The maximum captioning duration as an integer.
54+
*/
55+
public int getMaxDuration() {
56+
return maxDuration;
57+
}
58+
59+
/**
60+
* Whether faster captioning is enabled at the cost of some degree of inaccuracies.
61+
*
62+
* @return {@code true} if the partial captions setting is enabled.
63+
*/
64+
public boolean partialCaptions() {
65+
return partialCaptions;
66+
}
67+
68+
/**
69+
* Entry point for constructing an instance of this class.
70+
*
71+
* @return A new Builder.
72+
*/
73+
public static Builder Builder() {
74+
return new Builder();
75+
}
76+
77+
/**
78+
* Used to create a CaptionProperties object.
79+
*
80+
* @see CaptionProperties
81+
*/
82+
public static class Builder {
83+
private String languageCode = "en-US", statusCallbackUrl;
84+
private int maxDuration = 14400;
85+
private boolean partialCaptions = true;
86+
87+
private Builder() {
88+
}
89+
90+
/**
91+
* The BCP-47 code for a spoken language used on this call. The default value is "en-US". The following
92+
* language codes are supported: "en-AU" (English, Australia), "en-GB" (Englsh, UK), "es-US" (English, US),
93+
* "zh-CN" (Chinese, Simplified), "fr-FR" (French), "fr-CA" (French, Canadian), "de-DE" (German),
94+
* "hi-IN" (Hindi, Indian), "it-IT" (Italian), "ja-JP" (Japanese), "ko-KR" (Korean),
95+
* "pt-BR" (Portuguese, Brazilian), "th-TH" (Thai).
96+
*
97+
* @param languageCode The BCP-47 language code as a string.
98+
*
99+
* @return This Builder with the languageCode property setting.
100+
*/
101+
public Builder languageCode(String languageCode) {
102+
if (languageCode == null || languageCode.length() != 5 || languageCode.charAt(2) != '-') {
103+
throw new IllegalArgumentException("Invalid language code.");
104+
}
105+
this.languageCode = languageCode;
106+
return this;
107+
}
108+
109+
/**
110+
* A publicly reachable URL controlled by the customer and capable of generating the content to
111+
* be rendered without user intervention. The minimum length of the URL is 15 characters and the
112+
* maximum length is 2048 characters. For more information, see
113+
* <a href=https://tokbox.com/developer/guides/live-captions/#live-caption-status-updates>
114+
* Live Caption status updates</a>.
115+
*
116+
* @param statusCallbackUrl The status callback URL as a string.
117+
*
118+
* @return This Builder with the statusCallbackUrl property setting.
119+
*/
120+
public Builder statusCallbackUrl(String statusCallbackUrl) {
121+
if (statusCallbackUrl == null || statusCallbackUrl.length() < 15 || statusCallbackUrl.length() > 2048) {
122+
throw new IllegalArgumentException("Status callback URL must be between 15 and 2048 characters.");
123+
}
124+
this.statusCallbackUrl = statusCallbackUrl;
125+
return this;
126+
}
127+
128+
/**
129+
* The maximum duration for the audio captioning, in seconds.
130+
* The default value is 14,400 seconds (4 hours), the maximum duration allowed.
131+
*
132+
* @param maxDuration The maximum captions duration in seconds.
133+
*
134+
* @return This Builder with the maxDuration property setting.
135+
*/
136+
public Builder maxDuration(int maxDuration) {
137+
if ((this.maxDuration = maxDuration) < 0 || maxDuration > 14400) {
138+
throw new IllegalArgumentException("Max duration must be positive and less than 14400 seconds.");
139+
}
140+
return this;
141+
}
142+
143+
/**
144+
* Whether to enable this to faster captioning at the cost of some degree of inaccuracies.
145+
* The default value is {@code true}.
146+
*
147+
* @param partialCaptions Whether to enable faster captions.
148+
*
149+
* @return This Builder with the partialCaptions property setting.
150+
*/
151+
public Builder partialCaptions(boolean partialCaptions) {
152+
this.partialCaptions = partialCaptions;
153+
return this;
154+
}
155+
156+
/**
157+
* Builds the CaptionProperties object.
158+
*
159+
* @return The CaptionProperties object with this builder's settings.
160+
*/
161+
public CaptionProperties build() {
162+
return new CaptionProperties(this);
163+
}
164+
}
165+
}

src/main/java/com/opentok/OpenTok.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public class OpenTok {
5252
broadcastReader = new ObjectMapper().readerFor(Broadcast.class),
5353
renderReader = new ObjectMapper().readerFor(Render.class),
5454
renderListReader = new ObjectMapper().readerForListOf(Render.class),
55-
connectReader = new ObjectMapper().readerFor(AudioConnector.class);
55+
connectReader = new ObjectMapper().readerFor(AudioConnector.class),
56+
captionReader = new ObjectMapper().readerFor(Caption.class);
5657

5758
static final String defaultApiUrl = "https://api.opentok.com";
5859

@@ -978,6 +979,55 @@ public List<Render> listRenders(Integer offset, Integer count) throws OpenTokExc
978979
}
979980
}
980981

982+
/**
983+
* Use the Live Captions API to transcribe audio streams and generate real-time captions for your application.
984+
* Live Captions is enabled by default for all projects, and it is a usage-based product. The Live Captions
985+
* feature is only supported in routed sessions (sessions that use the OpenTok Media Router). You can send up to
986+
* 50 audio streams from a single Vonage session at a time to the transcription service for captions.
987+
*
988+
* @param sessionId The session ID of the OpenTok session. The audio from Publishers publishing into
989+
* this session will be used to generate the captions.
990+
*
991+
* @param token A valid OpenTok token with role set to Moderator.
992+
*
993+
* @param properties The {@link CaptionProperties} object defining optional properties of the live captioning.
994+
*
995+
* @return A {@link Caption} response containing the captions ID for this call.
996+
*
997+
* @throws OpenTokException
998+
*/
999+
public Caption startCaptions(String sessionId, String token, CaptionProperties properties) throws OpenTokException {
1000+
if (StringUtils.isEmpty(sessionId)) {
1001+
throw new InvalidArgumentException("Session ID is required.");
1002+
}
1003+
if (StringUtils.isEmpty(token)) {
1004+
throw new InvalidArgumentException("Token is required.");
1005+
}
1006+
String captions = client.startCaption(sessionId, token,
1007+
properties != null ? properties : CaptionProperties.Builder().build()
1008+
);
1009+
try {
1010+
return captionReader.readValue(captions);
1011+
} catch (JsonProcessingException e) {
1012+
throw new RequestException("Exception mapping json: " + e.getMessage());
1013+
}
1014+
}
1015+
1016+
/**
1017+
* Use this method to stop live captions for a session.
1018+
*
1019+
* @param captionsId The unique ID for the audio captioning session,
1020+
* as obtained from {@linkplain Caption#getCaptionsId()}.
1021+
*
1022+
* @throws OpenTokException
1023+
*/
1024+
public void stopCaptions(String captionsId) throws OpenTokException {
1025+
if (StringUtils.isEmpty(captionsId)) {
1026+
throw new InvalidArgumentException("Captions id is required.");
1027+
}
1028+
client.stopCaption(captionsId);
1029+
}
1030+
9811031
/**
9821032
* Used to create an OpenTok object with advanced settings. You can set
9831033
* the request timeout for API calls and a proxy to use for API calls.

src/main/java/com/opentok/constants/Version.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
package com.opentok.constants;
99

1010
public class Version {
11-
public static final String VERSION = "4.12.0";
11+
public static final String VERSION = "4.13.0";
1212
}

0 commit comments

Comments
 (0)