Commit 5c51f9dd authored by Shubham Agrawal's avatar Shubham Agrawal Committed by Commit Bot

Initialize TtsPlatformImpl on background thread

TtsPlatformImpl#initialize() calls TextToSpeech#isLanguageAvailable for
each of the installed locales. This call can result in certain calls to
the platform which end up doing I/O tasks. Since this method is run on
the UI thread, it causes those I/O tasks to be performed on the UI
thread as well.

This change fixes that issue by ensuring that that method is called on a
background thread, and only the calls necessary to run on the UI thread
run on it.

Bug: 813669
Test: Manually verified that skipped frames are no longer reported
Change-Id: If092b23eeed35b5384daa5f045c09fb8eda63281
Reviewed-on: https://chromium-review.googlesource.com/925822
Commit-Queue: Bernhard Bauer <bauerb@chromium.org>
Reviewed-by: default avatarBernhard Bauer <bauerb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#539844}
parent af231fc6
......@@ -759,6 +759,7 @@ Shouqun Liu <shouqun.liu@intel.com>
Shreeram Kushwaha <shreeram.k@samsung.com>
Shreyas Gopal <shreyas.g@samsung.com>
Shreyas VA <v.a.shreyas@gmail.com>
Shubham Agrawal <shubag@amazon.com>
Siba Samal <siba.samal@samsung.com>
Siddharth Bagai <b.siddharth@samsung.com>
Siddharth Shankar <funkysidd@gmail.com>
......
......@@ -4,6 +4,7 @@
package org.chromium.chrome.browser;
import android.os.AsyncTask;
import android.os.Build;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
......@@ -15,6 +16,7 @@ import org.chromium.base.annotations.CalledByNative;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
/**
......@@ -64,7 +66,7 @@ class TtsPlatformImpl {
private long mNativeTtsPlatformImplAndroid;
protected final TextToSpeech mTextToSpeech;
private boolean mInitialized;
private ArrayList<TtsVoice> mVoices;
private List<TtsVoice> mVoices;
private String mCurrentLanguage;
private PendingUtterance mPendingUtterance;
......@@ -262,49 +264,52 @@ class TtsPlatformImpl {
* we can call nativeVoicesChanged directly.
*/
private void initialize() {
assert mNativeTtsPlatformImplAndroid != 0;
TraceEvent.begin("TtsPlatformImpl:initialize");
// Note: Android supports multiple speech engines, but querying the
// metadata about all of them is expensive. So we deliberately only
// support the default speech engine, and expose the different
// supported languages for the default engine as different voices.
String defaultEngineName = mTextToSpeech.getDefaultEngine();
String engineLabel = defaultEngineName;
for (TextToSpeech.EngineInfo info : mTextToSpeech.getEngines()) {
if (info.name.equals(defaultEngineName)) engineLabel = info.label;
}
Locale[] locales = Locale.getAvailableLocales();
mVoices = new ArrayList<TtsVoice>();
for (int i = 0; i < locales.length; ++i) {
if (!locales[i].getVariant().isEmpty()) continue;
try {
if (mTextToSpeech.isLanguageAvailable(locales[i]) > 0) {
String name = locales[i].getDisplayLanguage();
if (!locales[i].getCountry().isEmpty()) {
name += " " + locales[i].getDisplayCountry();
new AsyncTask<Void, Void, List<TtsVoice>>() {
@Override
protected List<TtsVoice> doInBackground(Void... unused) {
assert mNativeTtsPlatformImplAndroid != 0;
try (TraceEvent te = TraceEvent.scoped("TtsPlatformImpl:initialize.async_task")) {
Locale[] locales = Locale.getAvailableLocales();
final List<TtsVoice> voices = new ArrayList<>();
for (Locale locale : locales) {
if (!locale.getVariant().isEmpty()) continue;
try {
if (mTextToSpeech.isLanguageAvailable(locale) > 0) {
String name = locale.getDisplayLanguage();
if (!locale.getCountry().isEmpty()) {
name += " " + locale.getDisplayCountry();
}
TtsVoice voice = new TtsVoice(name, locale.toString());
voices.add(voice);
}
} catch (Exception e) {
// Just skip the locale if it's invalid.
//
// We used to catch only java.util.MissingResourceException,
// but we need to catch more exceptions to work around a bug
// in Google TTS when we query "bn".
// http://crbug.com/792856
}
}
TtsVoice voice = new TtsVoice(name, locales[i].toString());
mVoices.add(voice);
return voices;
}
} catch (Exception e) {
// Just skip the locale if it's invalid.
//
// We used to catch only java.util.MissingResourceException,
// but we need to catch more exceptions to work around a bug
// in Google TTS when we query "bn".
// http://crbug.com/792856
}
}
mInitialized = true;
nativeVoicesChanged(mNativeTtsPlatformImplAndroid);
@Override
protected void onPostExecute(List<TtsVoice> voices) {
mVoices = voices;
mInitialized = true;
nativeVoicesChanged(mNativeTtsPlatformImplAndroid);
if (mPendingUtterance != null) mPendingUtterance.speak();
if (mPendingUtterance != null) mPendingUtterance.speak();
TraceEvent.end("TtsPlatformImpl:initialize");
TraceEvent.end("TtsPlatformImpl:initialize");
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
private native void nativeVoicesChanged(long nativeTtsPlatformImplAndroid);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment