Skip to content
This repository has been archived by the owner on Apr 25, 2022. It is now read-only.

Commit

Permalink
Moves the instantiation of Cronet engine upstream.
Browse files Browse the repository at this point in the history
Per @JensenPaul, instances of CronetEngine require a lot of resources. Creating
them is slow and expensive. For this reason, it's recommended to move the
creation of the engine up in the scope of the app to a longer-lived class.

This commit moves the creation of the engine to the activity level. The activity
also reuses a single instance of the class.

This commit also bumps the Cronet dependency to version 70.3538.110.
  • Loading branch information
ricalo committed Dec 7, 2018
1 parent f739162 commit e57ff8f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Expand Up @@ -51,7 +51,7 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.7.2'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.7.2'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'org.chromium.net:cronet-embedded:69.3497.100'
implementation 'org.chromium.net:cronet-embedded:70.3538.110'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support:support-annotations:28.0.0'
Expand Down
Expand Up @@ -18,6 +18,7 @@
import android.content.Context;
import android.util.Log;

import org.chromium.net.CronetEngine;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Assert;
Expand All @@ -38,6 +39,8 @@
public class SpeechTranslationHelperAndroidTest {
private static final String TAG = "SpeechTranslationHelperAndroidTest";
private static String base64EncodedAudioMessage;
private static Context context;
private static CronetEngine cronetEngine;

@Before
public void readSpeechRecording16khzb64File() throws IOException {
Expand All @@ -53,17 +56,20 @@ public void readSpeechRecording16khzb64File() throws IOException {
stringBuilder.append(line);
}
base64EncodedAudioMessage = stringBuilder.toString();

context = InstrumentationRegistry.getInstrumentation().getTargetContext();

CronetEngine.Builder myBuilder = new CronetEngine.Builder(context);
cronetEngine = myBuilder.build();
}

@Test
public void translateAudioMessage_Success() throws InterruptedException {
final Object waiter = new Object();

Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

synchronized (waiter) {
SpeechTranslationHelper.getInstance()
.translateAudioMessage(context, base64EncodedAudioMessage,
.translateAudioMessage(context, cronetEngine, base64EncodedAudioMessage,
16000, new SpeechTranslationHelper.SpeechTranslationListener() {
@Override
public void onTranslationSucceeded(String responseBody) {
Expand Down Expand Up @@ -99,11 +105,9 @@ public void onTranslationFailed(Exception e) {
public void translateAudioMessage_Wrong_SampleRate() throws InterruptedException {
final Object waiter = new Object();

Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

synchronized (waiter) {
SpeechTranslationHelper.getInstance()
.translateAudioMessage(context, base64EncodedAudioMessage,
.translateAudioMessage(context, cronetEngine, base64EncodedAudioMessage,
24000, new SpeechTranslationHelper.SpeechTranslationListener() {
@Override
public void onTranslationSucceeded(String responseBody) {
Expand Down
Expand Up @@ -66,6 +66,7 @@
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import org.chromium.net.CronetEngine;
import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -112,6 +113,7 @@ public class PlayActivity
private String currentChannel;
private ChildEventListener channelListener;
private SimpleDateFormat fmt;
private CronetEngine cronetEngine;

private Menu channelMenu;
private TextView channelLabel;
Expand Down Expand Up @@ -262,7 +264,8 @@ private void playMessage(String gcsBucket, String gcsPath) {
mediaPlayer.start();
} else {
GcsDownloadHelper.getInstance().downloadGcsFile(
getApplicationContext(), gcsBucket, gcsPath, new GcsDownloadHelper.GcsDownloadListener() {
getApplicationContext(), getCronetEngine(), gcsBucket, gcsPath,
new GcsDownloadHelper.GcsDownloadListener() {
@Override
public void onDownloadSucceeded(File file) {
MediaPlayer mediaPlayer = MediaPlayer.create(
Expand Down Expand Up @@ -342,6 +345,7 @@ public void onRecordingSucceeded(File output) {
base64EncodedAudioMessage = Base64EncodingHelper.encode(output);
SpeechTranslationHelper.getInstance().translateAudioMessage(
getApplicationContext(),
getCronetEngine(),
base64EncodedAudioMessage,
16000,
new SpeechTranslationHelper.SpeechTranslationListener() {
Expand Down Expand Up @@ -394,6 +398,21 @@ public void onRecordingFailed(Exception e) {
}
}

/**
* Creates an instance of the CronetEngine class.
* Instances of CronetEngine require a lot of resources. Additionally, their creation is slow
* and expensive. It's recommended to delay the creation of CronetEngine instances until they
* are required and reuse them as much as possible.
* @return An instance of CronetEngine.
*/
private synchronized CronetEngine getCronetEngine() {
if(cronetEngine == null) {
CronetEngine.Builder myBuilder = new CronetEngine.Builder(this);
cronetEngine = myBuilder.build();
}
return cronetEngine;
}

private void updateUI() {
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
if (currentUser != null) {
Expand Down
Expand Up @@ -66,11 +66,13 @@ public static GcsDownloadHelper getInstance() {
* @param gcsPath The path of the object in the bucket.
* @param downloadListener The callback to deliver the results to.
*/
public void downloadGcsFile(Context context, String gcsBucket, String gcsPath, GcsDownloadListener downloadListener) {
public void downloadGcsFile(Context context, CronetEngine cronetEngine, String gcsBucket,
String gcsPath, GcsDownloadListener downloadListener) {
getGcsAccessToken(context, new GcsTokenListener() {
@Override
public void onAccessTokenRequestSucceeded(String token) {
UrlRequest request = buildGcsRequest(context, gcsBucket, gcsPath, token,downloadListener);
UrlRequest request = buildGcsRequest(context, cronetEngine, gcsBucket, gcsPath,
token, downloadListener);
request.start();
}
@Override
Expand All @@ -96,9 +98,9 @@ private void getGcsAccessToken(Context context, GcsTokenListener tokenListener)
AsyncTask.execute(runnable);
}

private UrlRequest buildGcsRequest(Context context, String gcsBucket, String gcsPath, String accessToken, GcsDownloadListener downloadListener) {
CronetEngine.Builder myBuilder = new CronetEngine.Builder(context);
CronetEngine cronetEngine = myBuilder.build();
private UrlRequest buildGcsRequest(Context context, CronetEngine cronetEngine, String gcsBucket,
String gcsPath, String accessToken,
GcsDownloadListener downloadListener) {
Executor executor = Executors.newSingleThreadExecutor();

String gcsPathEndpoint = context.getString(R.string.gcsBaseEndpoint) + gcsBucket + "/" + gcsPath;
Expand Down
Expand Up @@ -64,8 +64,8 @@ public static SpeechTranslationHelper getInstance() {
* @param sampleRateInHertz The sample rate in hertz
* @param translationListener The callback to deliver the results to.
*/
public void translateAudioMessage(Context context, String base64EncodedAudioMessage,
int sampleRateInHertz,
public void translateAudioMessage(Context context, CronetEngine cronetEngine,
String base64EncodedAudioMessage, int sampleRateInHertz,
SpeechTranslationListener translationListener) {
JSONObject requestBody = new JSONObject();
try {
Expand All @@ -79,13 +79,13 @@ public void translateAudioMessage(Context context, String base64EncodedAudioMess
}

byte[] requestBodyBytes = requestBody.toString().getBytes();
UrlRequest request = buildSpeechTranslationRequest(context, requestBodyBytes, translationListener);
UrlRequest request = buildSpeechTranslationRequest(context, cronetEngine, requestBodyBytes, translationListener);
request.start();
}

private UrlRequest buildSpeechTranslationRequest(Context context, byte[] requestBody, SpeechTranslationListener translationListener) {
CronetEngine.Builder myBuilder = new CronetEngine.Builder(context);
CronetEngine cronetEngine = myBuilder.build();
private UrlRequest buildSpeechTranslationRequest(Context context, CronetEngine cronetEngine,
byte[] requestBody,
SpeechTranslationListener translationListener) {
Executor executor = Executors.newSingleThreadExecutor();
String speechTranslateEndpoint = context.getString(R.string.speechToSpeechEndpoint);
UrlRequest.Builder requestBuilder = cronetEngine.newUrlRequestBuilder(
Expand Down

0 comments on commit e57ff8f

Please sign in to comment.