1 /* 2 * Copyright 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; 17 18 import com.github.davidmoten.rx2.Bytes; 19 import io.reactivex.Flowable; 20 import java.io.File; 21 import java.util.concurrent.CompletableFuture; 22 import java.util.function.Consumer; 23 import org.junit.Test; 24 import software.amazon.awssdk.core.SdkBytes; 25 import software.amazon.awssdk.services.transcribestreaming.model.AudioEvent; 26 import software.amazon.awssdk.services.transcribestreaming.model.AudioStream; 27 import software.amazon.awssdk.services.transcribestreaming.model.LanguageCode; 28 import software.amazon.awssdk.services.transcribestreaming.model.MediaEncoding; 29 import software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionRequest; 30 import software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionResponseHandler; 31 import software.amazon.awssdk.services.transcribestreaming.model.TranscriptEvent; 32 import software.amazon.awssdk.services.transcribestreaming.model.TranscriptResultStream; 33 34 /** 35 * Option 2: Update current method to automatically reconnect and hide: (1) the need for non-replayable publishers, 36 * (2) the reconnect boilerplate. 37 * 38 * This behavior can be configured (e.g. disabled) via the "reconnect policy" on the client constructor. 39 */ 40 public class Option2Test { 41 private File audioFile = new File(getClass().getClassLoader().getResource("silence_16kHz_s16le.wav").getFile()); 42 43 @Test option2()44 public void option2() { 45 try (TranscribeStreamingAsyncClient client = TranscribeStreamingAsyncClient.create()) { 46 // Create the request for transcribe that includes the audio metadata 47 StartStreamTranscriptionRequest audioMetadata = 48 StartStreamTranscriptionRequest.builder() 49 .languageCode(LanguageCode.EN_US) 50 .mediaEncoding(MediaEncoding.PCM) 51 .mediaSampleRateHertz(16_000) 52 .build(); 53 54 // Create the audio stream for transcription 55 Flowable<AudioStream> audioStream = 56 Bytes.from(audioFile) 57 .map(SdkBytes::fromByteArray) 58 .map(bytes -> AudioEvent.builder().audioChunk(bytes).build()) 59 .cast(AudioStream.class); 60 61 // Create the visitor that handles the transcriptions from transcribe 62 Consumer<TranscriptResultStream> reader = event -> { 63 if (event instanceof TranscriptEvent) { 64 TranscriptEvent transcriptEvent = (TranscriptEvent) event; 65 System.out.println(transcriptEvent.transcript().results()); 66 } 67 }; 68 69 StartStreamTranscriptionResponseHandler responseHandler = StartStreamTranscriptionResponseHandler.builder() 70 .subscriber(reader) 71 .build(); 72 73 // Start talking with transcribe using the existing method 74 CompletableFuture<Void> result = client.startStreamTranscription(audioMetadata, audioStream, responseHandler); 75 result.join(); 76 } 77 } 78 79 @Test disableReconnectsDemo()80 public void disableReconnectsDemo() { 81 // Turn off reconnects 82 try (TranscribeStreamingAsyncClient client = 83 TranscribeStreamingAsyncClient.builder() 84 .overrideConfiguration(c -> c.reconnectPolicy(ReconnectPolicy.none())) 85 .build()) { 86 // .... 87 } 88 } 89 } 90