Skip to content

Commit 5f7f068

Browse files
committed
Add transcribestreaming module
1 parent ec047fa commit 5f7f068

File tree

14 files changed

+714
-0
lines changed

14 files changed

+714
-0
lines changed

aws-sdk-java/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
<groupId>software.amazon.awssdk</groupId>
6161
<version>${awsjavasdk.version}</version>
6262
</dependency>
63+
<dependency>
64+
<artifactId>transcribestreaming</artifactId>
65+
<groupId>software.amazon.awssdk</groupId>
66+
<version>${awsjavasdk.version}</version>
67+
</dependency>
6368
<dependency>
6469
<artifactId>sms</artifactId>
6570
<groupId>software.amazon.awssdk</groupId>

bom/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
<groupId>software.amazon.awssdk</groupId>
5959
<version>${awsjavasdk.version}</version>
6060
</dependency>
61+
<dependency>
62+
<artifactId>transcribestreaming</artifactId>
63+
<groupId>software.amazon.awssdk</groupId>
64+
<version>${awsjavasdk.version}</version>
65+
</dependency>
6166
<dependency>
6267
<artifactId>servicecatalog</artifactId>
6368
<groupId>software.amazon.awssdk</groupId>

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,10 @@
881881
<title>Support</title>
882882
<packages>software.amazon.awssdk.services.support*</packages>
883883
</group>
884+
<group>
885+
<title>Transcribe Streaming</title>
886+
<packages>software.amazon.awssdk.services.transcribestreaming*</packages>
887+
</group>
884888
<group>
885889
<title>CloudTrail</title>
886890
<packages>software.amazon.awssdk.services.cloudtrail*</packages>

services/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
<module>serverlessapplicationrepository</module>
153153
<module>servicediscovery</module>
154154
<module>translate</module>
155+
<module>transcribestreaming</module>
155156
<module>workmail</module>
156157
<module>secretsmanager</module>
157158
<module>chime</module>

services/transcribestreaming/pom.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
~ Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License").
6+
~ You may not use this file except in compliance with the License.
7+
~ A copy of the License is located at
8+
~
9+
~ http://aws.amazon.com/apache2.0
10+
~
11+
~ or in the "license" file accompanying this file. This file is distributed
12+
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
~ express or implied. See the License for the specific language governing
14+
~ permissions and limitations under the License.
15+
-->
16+
17+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
18+
xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
20+
<modelVersion>4.0.0</modelVersion>
21+
<parent>
22+
<groupId>software.amazon.awssdk</groupId>
23+
<artifactId>services</artifactId>
24+
<version>2.0.0-preview-14-SNAPSHOT</version>
25+
</parent>
26+
<artifactId>transcribestreaming</artifactId>
27+
<name>AWS Java SDK :: Services :: AWS Transcribe Streaming</name>
28+
<description>The AWS Java SDK for AWS Transcribe Streaming module holds the client classes that are used for
29+
communicating with AWS Transcribe Streaming Service
30+
</description>
31+
<url>https://aws.amazon.com/sdkforjava</url>
32+
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-jar-plugin</artifactId>
38+
<configuration>
39+
<archive>
40+
<manifestEntries>
41+
<Automatic-Module-Name>software.amazon.awssdk.services.transcribestreaming</Automatic-Module-Name>
42+
</manifestEntries>
43+
</archive>
44+
</configuration>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
49+
<dependencies>
50+
<dependency>
51+
<groupId>software.amazon.awssdk</groupId>
52+
<artifactId>aws-json-protocol</artifactId>
53+
<version>${awsjavasdk.version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>software.amazon.awssdk</groupId>
57+
<artifactId>protocol-core</artifactId>
58+
<version>${awsjavasdk.version}</version>
59+
</dependency>
60+
</dependencies>
61+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/*
2+
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package software.amazon.awssdk.services.transcribestreaming;
16+
17+
import static org.junit.Assert.assertTrue;
18+
import static software.amazon.awssdk.core.http.HttpResponseHandler.X_AMZN_REQUEST_ID_HEADER;
19+
20+
import java.io.File;
21+
import java.io.FileInputStream;
22+
import java.io.FileNotFoundException;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.io.UncheckedIOException;
26+
import java.net.URISyntaxException;
27+
import java.nio.ByteBuffer;
28+
import java.util.concurrent.CompletableFuture;
29+
import java.util.concurrent.ExecutionException;
30+
import java.util.concurrent.ExecutorService;
31+
import java.util.concurrent.Executors;
32+
import java.util.concurrent.atomic.AtomicLong;
33+
import org.junit.BeforeClass;
34+
import org.junit.Ignore;
35+
import org.junit.Test;
36+
import org.reactivestreams.Publisher;
37+
import org.reactivestreams.Subscriber;
38+
import org.reactivestreams.Subscription;
39+
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
40+
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
41+
import software.amazon.awssdk.core.SdkBytes;
42+
import software.amazon.awssdk.regions.Region;
43+
import software.amazon.awssdk.services.transcribestreaming.model.AudioEvent;
44+
import software.amazon.awssdk.services.transcribestreaming.model.AudioStream;
45+
import software.amazon.awssdk.services.transcribestreaming.model.LanguageCode;
46+
import software.amazon.awssdk.services.transcribestreaming.model.MediaEncoding;
47+
import software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionRequest;
48+
import software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionResponseHandler;
49+
import software.amazon.awssdk.services.transcribestreaming.model.TranscriptEvent;
50+
51+
/**
52+
* An example test class to show the usage of
53+
* {@link TranscribeStreamingAsyncClient#startStreamTranscription(StartStreamTranscriptionRequest, Publisher,
54+
* StartStreamTranscriptionResponseHandler)} API.
55+
*
56+
* The audio files used in this class don't have voice, so there won't be any transcripted text would be empty
57+
*/
58+
@Ignore
59+
public class TranscribeStreamingIntegrationTest {
60+
61+
private static TranscribeStreamingAsyncClient client;
62+
63+
@BeforeClass
64+
public static void setup() throws URISyntaxException {
65+
client = TranscribeStreamingAsyncClient.builder()
66+
.region(Region.US_EAST_1)
67+
.credentialsProvider(getCredentials())
68+
.build();
69+
}
70+
71+
@Test
72+
public void testFileWith16kRate() throws ExecutionException, InterruptedException, URISyntaxException {
73+
CompletableFuture<Void> result = client.startStreamTranscription(getRequest(16_000),
74+
new AudioStreamPublisher(
75+
getInputStream("silence_16kHz_s16le.wav")),
76+
getResponseHandler());
77+
78+
// Blocking call to keep the main thread for shutting down
79+
result.get();
80+
}
81+
82+
@Test
83+
public void testFileWith8kRate() throws ExecutionException, InterruptedException, URISyntaxException {
84+
CompletableFuture<Void> result = client.startStreamTranscription(getRequest(8_000),
85+
new AudioStreamPublisher(
86+
getInputStream("silence_8kHz_s16le.wav")),
87+
getResponseHandler());
88+
89+
result.get();
90+
}
91+
92+
private static AwsCredentialsProvider getCredentials() {
93+
return DefaultCredentialsProvider.create();
94+
}
95+
96+
private StartStreamTranscriptionRequest getRequest(Integer mediaSampleRateHertz) {
97+
return StartStreamTranscriptionRequest.builder()
98+
.languageCode(LanguageCode.EN_US.toString())
99+
.mediaEncoding(MediaEncoding.PCM)
100+
.mediaSampleRateHertz(mediaSampleRateHertz)
101+
.build();
102+
}
103+
104+
private InputStream getInputStream(String audioFileName) {
105+
try {
106+
File inputFile = new File(getClass().getClassLoader().getResource(audioFileName).getFile());
107+
assertTrue(inputFile.exists());
108+
InputStream audioStream = new FileInputStream(inputFile);
109+
return audioStream;
110+
} catch (FileNotFoundException e) {
111+
throw new RuntimeException(e);
112+
}
113+
}
114+
115+
private StartStreamTranscriptionResponseHandler getResponseHandler() {
116+
return StartStreamTranscriptionResponseHandler.builder()
117+
.onResponse(r -> {
118+
String idFromHeader = r.sdkHttpResponse()
119+
.firstMatchingHeader(X_AMZN_REQUEST_ID_HEADER)
120+
.orElse(null);
121+
System.out.println("Received Initial response: " + idFromHeader);
122+
})
123+
.onError(e -> {
124+
System.out.println("Error message: " + e.getMessage());
125+
})
126+
.onComplete(() -> {
127+
System.out.println("All records stream successfully");
128+
})
129+
.subscriber(event -> {
130+
System.out.println(((TranscriptEvent) event).transcript().results());
131+
})
132+
.build();
133+
}
134+
135+
private class AudioStreamPublisher implements Publisher<AudioStream> {
136+
private final InputStream inputStream;
137+
138+
private AudioStreamPublisher(InputStream inputStream) {
139+
this.inputStream = inputStream;
140+
}
141+
142+
@Override
143+
public void subscribe(Subscriber<? super AudioStream> s) {
144+
s.onSubscribe(new SubscriptionImpl(s, inputStream));
145+
}
146+
}
147+
148+
private class SubscriptionImpl implements Subscription {
149+
private static final int CHUNK_SIZE_IN_BYTES = 1024 * 1;
150+
private ExecutorService executor = Executors.newFixedThreadPool(1);
151+
private AtomicLong demand = new AtomicLong(0);
152+
153+
private final Subscriber<? super AudioStream> subscriber;
154+
private final InputStream inputStream;
155+
156+
private SubscriptionImpl(Subscriber<? super AudioStream> s, InputStream inputStream) {
157+
this.subscriber = s;
158+
this.inputStream = inputStream;
159+
}
160+
161+
@Override
162+
public void request(long n) {
163+
if (n <= 0) {
164+
subscriber.onError(new IllegalArgumentException("Demand must be positive"));
165+
}
166+
167+
demand.getAndAdd(n);
168+
169+
executor.submit(() -> {
170+
try {
171+
do {
172+
ByteBuffer audioBuffer = getNextEvent();
173+
if (audioBuffer.remaining() > 0) {
174+
AudioEvent audioEvent = audioEventFromBuffer(audioBuffer);
175+
subscriber.onNext(audioEvent);
176+
} else {
177+
subscriber.onComplete();
178+
break;
179+
}
180+
} while (demand.decrementAndGet() > 0);
181+
} catch (Exception e) {
182+
subscriber.onError(e);
183+
}
184+
});
185+
}
186+
187+
@Override
188+
public void cancel() {
189+
190+
}
191+
192+
private ByteBuffer getNextEvent() {
193+
ByteBuffer audioBuffer = null;
194+
byte[] audioBytes = new byte[CHUNK_SIZE_IN_BYTES];
195+
196+
int len = 0;
197+
try {
198+
len = inputStream.read(audioBytes);
199+
200+
if (len <= 0) {
201+
audioBuffer = ByteBuffer.allocate(0);
202+
} else {
203+
audioBuffer = ByteBuffer.wrap(audioBytes, 0, len);
204+
}
205+
} catch (IOException e) {
206+
throw new UncheckedIOException(e);
207+
}
208+
209+
return audioBuffer;
210+
}
211+
212+
private AudioEvent audioEventFromBuffer(ByteBuffer bb) {
213+
return AudioEvent.builder()
214+
.audioChunk(SdkBytes.fromByteBuffer(bb))
215+
.build();
216+
}
217+
}
218+
}
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.services.transcribestreaming.internal;
17+
18+
import java.time.Duration;
19+
import software.amazon.awssdk.annotations.SdkInternalApi;
20+
import software.amazon.awssdk.http.SdkHttpConfigurationOption;
21+
import software.amazon.awssdk.utils.AttributeMap;
22+
23+
@SdkInternalApi
24+
public final class DefaultHttpConfigurationOptions {
25+
26+
private static final AttributeMap OPTIONS = AttributeMap
27+
.builder()
28+
.put(SdkHttpConfigurationOption.READ_TIMEOUT, Duration.ofSeconds(100))
29+
.put(SdkHttpConfigurationOption.WRITE_TIMEOUT, Duration.ofSeconds(30))
30+
.build();
31+
32+
private DefaultHttpConfigurationOptions() {
33+
}
34+
35+
public static AttributeMap defaultHttpConfig() {
36+
return OPTIONS;
37+
}
38+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"serviceSpecificHttpConfig": "software.amazon.awssdk.services.transcribestreaming.internal.DefaultHttpConfigurationOptions",
3+
"skipSyncClientGeneration": true
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"version": "1.0",
3+
"examples": {
4+
}
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"pagination": {
3+
}
4+
}

0 commit comments

Comments
 (0)