TextToSpeechService.java revision 01dedf59db924726ee323aac89708031ce8f320f
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16package android.speech.tts;
17
18import android.annotation.NonNull;
19import android.app.Service;
20import android.content.Intent;
21import android.media.AudioAttributes;
22import android.media.AudioManager;
23import android.net.Uri;
24import android.os.Binder;
25import android.os.Bundle;
26import android.os.Handler;
27import android.os.HandlerThread;
28import android.os.IBinder;
29import android.os.Looper;
30import android.os.Message;
31import android.os.MessageQueue;
32import android.os.ParcelFileDescriptor;
33import android.os.RemoteCallbackList;
34import android.os.RemoteException;
35import android.provider.Settings;
36import android.speech.tts.TextToSpeech.Engine;
37import android.text.TextUtils;
38import android.util.Log;
39
40import java.io.FileOutputStream;
41import java.io.IOException;
42import java.util.ArrayList;
43import java.util.HashMap;
44import java.util.HashSet;
45import java.util.List;
46import java.util.Locale;
47import java.util.MissingResourceException;
48import java.util.Set;
49
50
51/**
52 * Abstract base class for TTS engine implementations. The following methods
53 * need to be implemented:
54 * <ul>
55 * <li>{@link #onIsLanguageAvailable}</li>
56 * <li>{@link #onLoadLanguage}</li>
57 * <li>{@link #onGetLanguage}</li>
58 * <li>{@link #onSynthesizeText}</li>
59 * <li>{@link #onStop}</li>
60 * </ul>
61 * The first three deal primarily with language management, and are used to
62 * query the engine for it's support for a given language and indicate to it
63 * that requests in a given language are imminent.
64 *
65 * {@link #onSynthesizeText} is central to the engine implementation. The
66 * implementation should synthesize text as per the request parameters and
67 * return synthesized data via the supplied callback. This class and its helpers
68 * will then consume that data, which might mean queuing it for playback or writing
69 * it to a file or similar. All calls to this method will be on a single thread,
70 * which will be different from the main thread of the service. Synthesis must be
71 * synchronous which means the engine must NOT hold on to the callback or call any
72 * methods on it after the method returns.
73 *
74 * {@link #onStop} tells the engine that it should stop
75 * all ongoing synthesis, if any. Any pending data from the current synthesis
76 * will be discarded.
77 *
78 * {@link #onGetLanguage} is not required as of JELLYBEAN_MR2 (API 18) and later, it is only
79 * called on earlier versions of Android.
80 *
81 * API Level 20 adds support for Voice objects. Voices are an abstraction that allow the TTS
82 * service to expose multiple backends for a single locale. Each one of them can have a different
83 * features set. In order to fully take advantage of voices, an engine should implement
84 * the following methods:
85 * <ul>
86 * <li>{@link #onGetVoices()}</li>
87 * <li>{@link #onIsValidVoiceName(String)}</li>
88 * <li>{@link #onLoadVoice(String)}</li>
89 * <li>{@link #onGetDefaultVoiceNameFor(String, String, String)}</li>
90 * </ul>
91 * The first three methods are siblings of the {@link #onGetLanguage},
92 * {@link #onIsLanguageAvailable} and {@link #onLoadLanguage} methods. The last one,
93 * {@link #onGetDefaultVoiceNameFor(String, String, String)} is a link between locale and voice
94 * based methods. Since API level 21 {@link TextToSpeech#setLanguage} is implemented by
95 * calling {@link TextToSpeech#setVoice} with the voice returned by
96 * {@link #onGetDefaultVoiceNameFor(String, String, String)}.
97 *
98 * If the client uses a voice instead of a locale, {@link SynthesisRequest} will contain the
99 * requested voice name.
100 *
101 * The default implementations of Voice-related methods implement them using the
102 * pre-existing locale-based implementation.
103 */
104public abstract class TextToSpeechService extends Service {
105
106    private static final boolean DBG = false;
107    private static final String TAG = "TextToSpeechService";
108
109    private static final String SYNTH_THREAD_NAME = "SynthThread";
110
111    private SynthHandler mSynthHandler;
112    // A thread and it's associated handler for playing back any audio
113    // associated with this TTS engine. Will handle all requests except synthesis
114    // to file requests, which occur on the synthesis thread.
115    @NonNull private AudioPlaybackHandler mAudioPlaybackHandler;
116    private TtsEngines mEngineHelper;
117
118    private CallbackMap mCallbacks;
119    private String mPackageName;
120
121    private final Object mVoicesInfoLock = new Object();
122
123    @Override
124    public void onCreate() {
125        if (DBG) Log.d(TAG, "onCreate()");
126        super.onCreate();
127
128        SynthThread synthThread = new SynthThread();
129        synthThread.start();
130        mSynthHandler = new SynthHandler(synthThread.getLooper());
131
132        mAudioPlaybackHandler = new AudioPlaybackHandler();
133        mAudioPlaybackHandler.start();
134
135        mEngineHelper = new TtsEngines(this);
136
137        mCallbacks = new CallbackMap();
138
139        mPackageName = getApplicationInfo().packageName;
140
141        String[] defaultLocale = getSettingsLocale();
142
143        // Load default language
144        onLoadLanguage(defaultLocale[0], defaultLocale[1], defaultLocale[2]);
145    }
146
147    @Override
148    public void onDestroy() {
149        if (DBG) Log.d(TAG, "onDestroy()");
150
151        // Tell the synthesizer to stop
152        mSynthHandler.quit();
153        // Tell the audio playback thread to stop.
154        mAudioPlaybackHandler.quit();
155        // Unregister all callbacks.
156        mCallbacks.kill();
157
158        super.onDestroy();
159    }
160
161    /**
162     * Checks whether the engine supports a given language.
163     *
164     * Can be called on multiple threads.
165     *
166     * Its return values HAVE to be consistent with onLoadLanguage.
167     *
168     * @param lang ISO-3 language code.
169     * @param country ISO-3 country code. May be empty or null.
170     * @param variant Language variant. May be empty or null.
171     * @return Code indicating the support status for the locale.
172     *         One of {@link TextToSpeech#LANG_AVAILABLE},
173     *         {@link TextToSpeech#LANG_COUNTRY_AVAILABLE},
174     *         {@link TextToSpeech#LANG_COUNTRY_VAR_AVAILABLE},
175     *         {@link TextToSpeech#LANG_MISSING_DATA}
176     *         {@link TextToSpeech#LANG_NOT_SUPPORTED}.
177     */
178    protected abstract int onIsLanguageAvailable(String lang, String country, String variant);
179
180    /**
181     * Returns the language, country and variant currently being used by the TTS engine.
182     *
183     * This method will be called only on Android 4.2 and before (API <= 17). In later versions
184     * this method is not called by the Android TTS framework.
185     *
186     * Can be called on multiple threads.
187     *
188     * @return A 3-element array, containing language (ISO 3-letter code),
189     *         country (ISO 3-letter code) and variant used by the engine.
190     *         The country and variant may be {@code ""}. If country is empty, then variant must
191     *         be empty too.
192     * @see Locale#getISO3Language()
193     * @see Locale#getISO3Country()
194     * @see Locale#getVariant()
195     */
196    protected abstract String[] onGetLanguage();
197
198    /**
199     * Notifies the engine that it should load a speech synthesis language. There is no guarantee
200     * that this method is always called before the language is used for synthesis. It is merely
201     * a hint to the engine that it will probably get some synthesis requests for this language
202     * at some point in the future.
203     *
204     * Can be called on multiple threads.
205     * In <= Android 4.2 (<= API 17) can be called on main and service binder threads.
206     * In > Android 4.2 (> API 17) can be called on main and synthesis threads.
207     *
208     * @param lang ISO-3 language code.
209     * @param country ISO-3 country code. May be empty or null.
210     * @param variant Language variant. May be empty or null.
211     * @return Code indicating the support status for the locale.
212     *         One of {@link TextToSpeech#LANG_AVAILABLE},
213     *         {@link TextToSpeech#LANG_COUNTRY_AVAILABLE},
214     *         {@link TextToSpeech#LANG_COUNTRY_VAR_AVAILABLE},
215     *         {@link TextToSpeech#LANG_MISSING_DATA}
216     *         {@link TextToSpeech#LANG_NOT_SUPPORTED}.
217     */
218    protected abstract int onLoadLanguage(String lang, String country, String variant);
219
220    /**
221     * Notifies the service that it should stop any in-progress speech synthesis.
222     * This method can be called even if no speech synthesis is currently in progress.
223     *
224     * Can be called on multiple threads, but not on the synthesis thread.
225     */
226    protected abstract void onStop();
227
228    /**
229     * Tells the service to synthesize speech from the given text. This method should block until
230     * the synthesis is finished. Called on the synthesis thread.
231     *
232     * @param request The synthesis request.
233     * @param callback The callback that the engine must use to make data available for playback or
234     *     for writing to a file.
235     */
236    protected abstract void onSynthesizeText(SynthesisRequest request, SynthesisCallback callback);
237
238    /**
239     * Queries the service for a set of features supported for a given language.
240     *
241     * Can be called on multiple threads.
242     *
243     * @param lang ISO-3 language code.
244     * @param country ISO-3 country code. May be empty or null.
245     * @param variant Language variant. May be empty or null.
246     * @return A list of features supported for the given language.
247     */
248    protected Set<String> onGetFeaturesForLanguage(String lang, String country, String variant) {
249        return new HashSet<String>();
250    }
251
252    private int getExpectedLanguageAvailableStatus(Locale locale) {
253        int expectedStatus = TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE;
254        if (locale.getVariant().isEmpty()) {
255            if (locale.getCountry().isEmpty()) {
256                expectedStatus = TextToSpeech.LANG_AVAILABLE;
257            } else {
258                expectedStatus = TextToSpeech.LANG_COUNTRY_AVAILABLE;
259            }
260        }
261        return expectedStatus;
262    }
263
264    /**
265     * Queries the service for a set of supported voices.
266     *
267     * Can be called on multiple threads.
268     *
269     * The default implementation tries to enumerate all available locales, pass them to
270     * {@link #onIsLanguageAvailable(String, String, String)} and create Voice instances (using
271     * the locale's BCP-47 language tag as the voice name) for the ones that are supported.
272     * Note, that this implementation is suitable only for engines that don't have multiple voices
273     * for a single locale. Also, this implementation won't work with Locales not listed in the
274     * set returned by the {@link Locale#getAvailableLocales()} method.
275     *
276     * @return A list of voices supported.
277     */
278    public List<Voice> onGetVoices() {
279        // Enumerate all locales and check if they are available
280        ArrayList<Voice> voices = new ArrayList<Voice>();
281        for (Locale locale : Locale.getAvailableLocales()) {
282            int expectedStatus = getExpectedLanguageAvailableStatus(locale);
283            try {
284                int localeStatus = onIsLanguageAvailable(locale.getISO3Language(),
285                        locale.getISO3Country(), locale.getVariant());
286                if (localeStatus != expectedStatus) {
287                    continue;
288                }
289            } catch (MissingResourceException e) {
290                // Ignore locale without iso 3 codes
291                continue;
292            }
293            Set<String> features = onGetFeaturesForLanguage(locale.getISO3Language(),
294                    locale.getISO3Country(), locale.getVariant());
295            String voiceName = onGetDefaultVoiceNameFor(locale.getISO3Language(),
296                    locale.getISO3Country(), locale.getVariant());
297            voices.add(new Voice(voiceName, locale, Voice.QUALITY_NORMAL,
298                    Voice.LATENCY_NORMAL, false, features));
299        }
300        return voices;
301    }
302
303    /**
304     * Return a name of the default voice for a given locale.
305     *
306     * This method provides a mapping between locales and available voices. This method is
307     * used in {@link TextToSpeech#setLanguage}, which calls this method and then calls
308     * {@link TextToSpeech#setVoice} with the voice returned by this method.
309     *
310     * Also, it's used by {@link TextToSpeech#getDefaultVoice()} to find a default voice for
311     * the default locale.
312     *
313     * @param lang ISO-3 language code.
314     * @param country ISO-3 country code. May be empty or null.
315     * @param variant Language variant. May be empty or null.
316
317     * @return A name of the default voice for a given locale.
318     */
319    public String onGetDefaultVoiceNameFor(String lang, String country, String variant) {
320        int localeStatus = onIsLanguageAvailable(lang, country, variant);
321        Locale iso3Locale = null;
322        switch (localeStatus) {
323            case TextToSpeech.LANG_AVAILABLE:
324                iso3Locale = new Locale(lang);
325                break;
326            case TextToSpeech.LANG_COUNTRY_AVAILABLE:
327                iso3Locale = new Locale(lang, country);
328                break;
329            case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
330                iso3Locale = new Locale(lang, country, variant);
331                break;
332            default:
333                return null;
334        }
335        Locale properLocale = TtsEngines.normalizeTTSLocale(iso3Locale);
336        String voiceName = properLocale.toLanguageTag();
337        if (onIsValidVoiceName(voiceName) == TextToSpeech.SUCCESS) {
338            return voiceName;
339        } else {
340            return null;
341        }
342    }
343
344    /**
345     * Notifies the engine that it should load a speech synthesis voice. There is no guarantee
346     * that this method is always called before the voice is used for synthesis. It is merely
347     * a hint to the engine that it will probably get some synthesis requests for this voice
348     * at some point in the future.
349     *
350     * Will be called only on synthesis thread.
351     *
352     * The default implementation creates a Locale from the voice name (by interpreting the name as
353     * a BCP-47 tag for the locale), and passes it to
354     * {@link #onLoadLanguage(String, String, String)}.
355     *
356     * @param voiceName Name of the voice.
357     * @return {@link TextToSpeech#ERROR} or {@link TextToSpeech#SUCCESS}.
358     */
359    public int onLoadVoice(String voiceName) {
360        Locale locale = Locale.forLanguageTag(voiceName);
361        if (locale == null) {
362            return TextToSpeech.ERROR;
363        }
364        int expectedStatus = getExpectedLanguageAvailableStatus(locale);
365        try {
366            int localeStatus = onIsLanguageAvailable(locale.getISO3Language(),
367                    locale.getISO3Country(), locale.getVariant());
368            if (localeStatus != expectedStatus) {
369                return TextToSpeech.ERROR;
370            }
371            onLoadLanguage(locale.getISO3Language(),
372                    locale.getISO3Country(), locale.getVariant());
373            return TextToSpeech.SUCCESS;
374        } catch (MissingResourceException e) {
375            return TextToSpeech.ERROR;
376        }
377    }
378
379    /**
380     * Checks whether the engine supports a voice with a given name.
381     *
382     * Can be called on multiple threads.
383     *
384     * The default implementation treats the voice name as a language tag, creating a Locale from
385     * the voice name, and passes it to {@link #onIsLanguageAvailable(String, String, String)}.
386     *
387     * @param voiceName Name of the voice.
388     * @return {@link TextToSpeech#ERROR} or {@link TextToSpeech#SUCCESS}.
389     */
390    public int onIsValidVoiceName(String voiceName) {
391        Locale locale = Locale.forLanguageTag(voiceName);
392        if (locale == null) {
393            return TextToSpeech.ERROR;
394        }
395        int expectedStatus = getExpectedLanguageAvailableStatus(locale);
396        try {
397            int localeStatus = onIsLanguageAvailable(locale.getISO3Language(),
398                    locale.getISO3Country(), locale.getVariant());
399            if (localeStatus != expectedStatus) {
400                return TextToSpeech.ERROR;
401            }
402            return TextToSpeech.SUCCESS;
403        } catch (MissingResourceException e) {
404            return TextToSpeech.ERROR;
405        }
406    }
407
408    private int getDefaultSpeechRate() {
409        return getSecureSettingInt(Settings.Secure.TTS_DEFAULT_RATE, Engine.DEFAULT_RATE);
410    }
411
412    private String[] getSettingsLocale() {
413        final Locale locale = mEngineHelper.getLocalePrefForEngine(mPackageName);
414        return TtsEngines.toOldLocaleStringFormat(locale);
415    }
416
417    private int getSecureSettingInt(String name, int defaultValue) {
418        return Settings.Secure.getInt(getContentResolver(), name, defaultValue);
419    }
420
421    /**
422     * Synthesizer thread. This thread is used to run {@link SynthHandler}.
423     */
424    private class SynthThread extends HandlerThread implements MessageQueue.IdleHandler {
425
426        private boolean mFirstIdle = true;
427
428        public SynthThread() {
429            super(SYNTH_THREAD_NAME, android.os.Process.THREAD_PRIORITY_DEFAULT);
430        }
431
432        @Override
433        protected void onLooperPrepared() {
434            getLooper().getQueue().addIdleHandler(this);
435        }
436
437        @Override
438        public boolean queueIdle() {
439            if (mFirstIdle) {
440                mFirstIdle = false;
441            } else {
442                broadcastTtsQueueProcessingCompleted();
443            }
444            return true;
445        }
446
447        private void broadcastTtsQueueProcessingCompleted() {
448            Intent i = new Intent(TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED);
449            if (DBG) Log.d(TAG, "Broadcasting: " + i);
450            sendBroadcast(i);
451        }
452    }
453
454    private class SynthHandler extends Handler {
455        private SpeechItem mCurrentSpeechItem = null;
456
457        // When a message with QUEUE_FLUSH arrives we add the caller identity to the List and when a
458        // message with QUEUE_DESTROY arrives we increment mFlushAll. Then a message is added to the
459        // handler queue that removes the caller identify from the list and decrements the mFlushAll
460        // counter. This is so that when a message is processed and the caller identity is in the
461        // list or mFlushAll is not zero, we know that the message should be flushed.
462        // It's important that mFlushedObjects is a List and not a Set, and that mFlushAll is an
463        // int and not a bool. This is because when multiple messages arrive with QUEUE_FLUSH or
464        // QUEUE_DESTROY, we want to keep flushing messages until we arrive at the last QUEUE_FLUSH
465        // or QUEUE_DESTROY message.
466        private List<Object> mFlushedObjects = new ArrayList<>();
467        private int mFlushAll = 0;
468
469        public SynthHandler(Looper looper) {
470            super(looper);
471        }
472
473        private void startFlushingSpeechItems(Object callerIdentity) {
474            synchronized (mFlushedObjects) {
475                if (callerIdentity == null) {
476                    mFlushAll += 1;
477                } else {
478                    mFlushedObjects.add(callerIdentity);
479                }
480            }
481        }
482        private void endFlushingSpeechItems(Object callerIdentity) {
483            synchronized (mFlushedObjects) {
484                if (callerIdentity == null) {
485                    mFlushAll -= 1;
486                } else {
487                    mFlushedObjects.remove(callerIdentity);
488                }
489            }
490        }
491        private boolean isFlushed(SpeechItem speechItem) {
492            synchronized (mFlushedObjects) {
493                return mFlushAll > 0 || mFlushedObjects.contains(speechItem.getCallerIdentity());
494            }
495        }
496
497        private synchronized SpeechItem getCurrentSpeechItem() {
498            return mCurrentSpeechItem;
499        }
500
501        private synchronized SpeechItem setCurrentSpeechItem(SpeechItem speechItem) {
502            SpeechItem old = mCurrentSpeechItem;
503            mCurrentSpeechItem = speechItem;
504            return old;
505        }
506
507        private synchronized SpeechItem maybeRemoveCurrentSpeechItem(Object callerIdentity) {
508            if (mCurrentSpeechItem != null &&
509                    (mCurrentSpeechItem.getCallerIdentity() == callerIdentity)) {
510                SpeechItem current = mCurrentSpeechItem;
511                mCurrentSpeechItem = null;
512                return current;
513            }
514
515            return null;
516        }
517
518        public boolean isSpeaking() {
519            return getCurrentSpeechItem() != null;
520        }
521
522        public void quit() {
523            // Don't process any more speech items
524            getLooper().quit();
525            // Stop the current speech item
526            SpeechItem current = setCurrentSpeechItem(null);
527            if (current != null) {
528                current.stop();
529            }
530            // The AudioPlaybackHandler will be destroyed by the caller.
531        }
532
533        /**
534         * Adds a speech item to the queue.
535         *
536         * Called on a service binder thread.
537         */
538        public int enqueueSpeechItem(int queueMode, final SpeechItem speechItem) {
539            UtteranceProgressDispatcher utterenceProgress = null;
540            if (speechItem instanceof UtteranceProgressDispatcher) {
541                utterenceProgress = (UtteranceProgressDispatcher) speechItem;
542            }
543
544            if (!speechItem.isValid()) {
545                if (utterenceProgress != null) {
546                    utterenceProgress.dispatchOnError(
547                            TextToSpeech.ERROR_INVALID_REQUEST);
548                }
549                return TextToSpeech.ERROR;
550            }
551
552            if (queueMode == TextToSpeech.QUEUE_FLUSH) {
553                stopForApp(speechItem.getCallerIdentity());
554            } else if (queueMode == TextToSpeech.QUEUE_DESTROY) {
555                stopAll();
556            }
557            Runnable runnable = new Runnable() {
558                @Override
559                public void run() {
560                    if (isFlushed(speechItem)) {
561                        speechItem.stop();
562                    } else {
563                        setCurrentSpeechItem(speechItem);
564                        speechItem.play();
565                        setCurrentSpeechItem(null);
566                    }
567                }
568            };
569            Message msg = Message.obtain(this, runnable);
570
571            // The obj is used to remove all callbacks from the given app in
572            // stopForApp(String).
573            //
574            // Note that this string is interned, so the == comparison works.
575            msg.obj = speechItem.getCallerIdentity();
576
577            if (sendMessage(msg)) {
578                return TextToSpeech.SUCCESS;
579            } else {
580                Log.w(TAG, "SynthThread has quit");
581                if (utterenceProgress != null) {
582                    utterenceProgress.dispatchOnError(TextToSpeech.ERROR_SERVICE);
583                }
584                return TextToSpeech.ERROR;
585            }
586        }
587
588        /**
589         * Stops all speech output and removes any utterances still in the queue for
590         * the calling app.
591         *
592         * Called on a service binder thread.
593         */
594        public int stopForApp(final Object callerIdentity) {
595            if (callerIdentity == null) {
596                return TextToSpeech.ERROR;
597            }
598
599            // Flush pending messages from callerIdentity
600            startFlushingSpeechItems(callerIdentity);
601
602            // This stops writing data to the file / or publishing
603            // items to the audio playback handler.
604            //
605            // Note that the current speech item must be removed only if it
606            // belongs to the callingApp, else the item will be "orphaned" and
607            // not stopped correctly if a stop request comes along for the item
608            // from the app it belongs to.
609            SpeechItem current = maybeRemoveCurrentSpeechItem(callerIdentity);
610            if (current != null) {
611                current.stop();
612            }
613
614            // Remove any enqueued audio too.
615            mAudioPlaybackHandler.stopForApp(callerIdentity);
616
617            // Stop flushing pending messages
618            Runnable runnable = new Runnable() {
619                @Override
620                public void run() {
621                    endFlushingSpeechItems(callerIdentity);
622                }
623            };
624            sendMessage(Message.obtain(this, runnable));
625            return TextToSpeech.SUCCESS;
626        }
627
628        public int stopAll() {
629            // Order to flush pending messages
630            startFlushingSpeechItems(null);
631
632            // Stop the current speech item unconditionally .
633            SpeechItem current = setCurrentSpeechItem(null);
634            if (current != null) {
635                current.stop();
636            }
637            // Remove all pending playback as well.
638            mAudioPlaybackHandler.stop();
639
640            // Message to stop flushing pending messages
641            Runnable runnable = new Runnable() {
642                @Override
643                public void run() {
644                    endFlushingSpeechItems(null);
645                }
646            };
647            sendMessage(Message.obtain(this, runnable));
648
649
650            return TextToSpeech.SUCCESS;
651        }
652    }
653
654    interface UtteranceProgressDispatcher {
655        public void dispatchOnStop();
656        public void dispatchOnSuccess();
657        public void dispatchOnStart();
658        public void dispatchOnError(int errorCode);
659        public void dispatchOnBeginSynthesis(int sampleRateInHz, int audioFormat, int channelCount);
660        public void dispatchOnAudioAvailable(byte[] audio);
661    }
662
663    /** Set of parameters affecting audio output. */
664    static class AudioOutputParams {
665        /**
666         * Audio session identifier. May be used to associate audio playback with one of the
667         * {@link android.media.audiofx.AudioEffect} objects. If not specified by client,
668         * it should be equal to {@link AudioManager#AUDIO_SESSION_ID_GENERATE}.
669         */
670        public final int mSessionId;
671
672        /**
673         * Volume, in the range [0.0f, 1.0f]. The default value is
674         * {@link TextToSpeech.Engine#DEFAULT_VOLUME} (1.0f).
675         */
676        public final float mVolume;
677
678        /**
679         * Left/right position of the audio, in the range [-1.0f, 1.0f].
680         * The default value is {@link TextToSpeech.Engine#DEFAULT_PAN} (0.0f).
681         */
682        public final float mPan;
683
684
685        /**
686         * Audio attributes, set by {@link TextToSpeech#setAudioAttributes}
687         * or created from the value of {@link TextToSpeech.Engine#KEY_PARAM_STREAM}.
688         */
689        public final AudioAttributes mAudioAttributes;
690
691        /** Create AudioOutputParams with default values */
692        AudioOutputParams() {
693            mSessionId = AudioManager.AUDIO_SESSION_ID_GENERATE;
694            mVolume = Engine.DEFAULT_VOLUME;
695            mPan = Engine.DEFAULT_PAN;
696            mAudioAttributes = null;
697        }
698
699        AudioOutputParams(int sessionId, float volume, float pan,
700                AudioAttributes audioAttributes) {
701            mSessionId = sessionId;
702            mVolume = volume;
703            mPan = pan;
704            mAudioAttributes = audioAttributes;
705        }
706
707        /** Create AudioOutputParams from A {@link SynthesisRequest#getParams()} bundle */
708        static AudioOutputParams createFromParamsBundle(Bundle paramsBundle, boolean isSpeech) {
709            if (paramsBundle == null) {
710                return new AudioOutputParams();
711            }
712
713            AudioAttributes audioAttributes =
714                    (AudioAttributes) paramsBundle.getParcelable(
715                            Engine.KEY_PARAM_AUDIO_ATTRIBUTES);
716            if (audioAttributes == null) {
717                int streamType = paramsBundle.getInt(
718                        Engine.KEY_PARAM_STREAM, Engine.DEFAULT_STREAM);
719                audioAttributes = (new AudioAttributes.Builder())
720                        .setLegacyStreamType(streamType)
721                        .setContentType((isSpeech ?
722                                AudioAttributes.CONTENT_TYPE_SPEECH :
723                                AudioAttributes.CONTENT_TYPE_SONIFICATION))
724                        .build();
725            }
726
727            return new AudioOutputParams(
728                    paramsBundle.getInt(
729                            Engine.KEY_PARAM_SESSION_ID,
730                            AudioManager.AUDIO_SESSION_ID_GENERATE),
731                    paramsBundle.getFloat(
732                            Engine.KEY_PARAM_VOLUME,
733                            Engine.DEFAULT_VOLUME),
734                    paramsBundle.getFloat(
735                            Engine.KEY_PARAM_PAN,
736                            Engine.DEFAULT_PAN),
737                    audioAttributes);
738        }
739    }
740
741
742    /**
743     * An item in the synth thread queue.
744     */
745    private abstract class SpeechItem {
746        private final Object mCallerIdentity;
747        private final int mCallerUid;
748        private final int mCallerPid;
749        private boolean mStarted = false;
750        private boolean mStopped = false;
751
752        public SpeechItem(Object caller, int callerUid, int callerPid) {
753            mCallerIdentity = caller;
754            mCallerUid = callerUid;
755            mCallerPid = callerPid;
756        }
757
758        public Object getCallerIdentity() {
759            return mCallerIdentity;
760        }
761
762        public int getCallerUid() {
763            return mCallerUid;
764        }
765
766        public int getCallerPid() {
767            return mCallerPid;
768        }
769
770        /**
771         * Checker whether the item is valid. If this method returns false, the item should not
772         * be played.
773         */
774        public abstract boolean isValid();
775
776        /**
777         * Plays the speech item. Blocks until playback is finished.
778         * Must not be called more than once.
779         *
780         * Only called on the synthesis thread.
781         */
782        public void play() {
783            synchronized (this) {
784                if (mStarted) {
785                    throw new IllegalStateException("play() called twice");
786                }
787                mStarted = true;
788            }
789            playImpl();
790        }
791
792        protected abstract void playImpl();
793
794        /**
795         * Stops the speech item.
796         * Must not be called more than once.
797         *
798         * Can be called on multiple threads,  but not on the synthesis thread.
799         */
800        public void stop() {
801            synchronized (this) {
802                if (mStopped) {
803                    throw new IllegalStateException("stop() called twice");
804                }
805                mStopped = true;
806            }
807            stopImpl();
808        }
809
810        protected abstract void stopImpl();
811
812        protected synchronized boolean isStopped() {
813             return mStopped;
814        }
815
816        protected synchronized boolean isStarted() {
817            return mStarted;
818       }
819    }
820
821    /**
822     * An item in the synth thread queue that process utterance (and call back to client about
823     * progress).
824     */
825    private abstract class UtteranceSpeechItem extends SpeechItem
826        implements UtteranceProgressDispatcher  {
827
828        public UtteranceSpeechItem(Object caller, int callerUid, int callerPid) {
829            super(caller, callerUid, callerPid);
830        }
831
832        @Override
833        public void dispatchOnSuccess() {
834            final String utteranceId = getUtteranceId();
835            if (utteranceId != null) {
836                mCallbacks.dispatchOnSuccess(getCallerIdentity(), utteranceId);
837            }
838        }
839
840        @Override
841        public void dispatchOnStop() {
842            final String utteranceId = getUtteranceId();
843            if (utteranceId != null) {
844                mCallbacks.dispatchOnStop(getCallerIdentity(), utteranceId, isStarted());
845            }
846        }
847
848        @Override
849        public void dispatchOnStart() {
850            final String utteranceId = getUtteranceId();
851            if (utteranceId != null) {
852                mCallbacks.dispatchOnStart(getCallerIdentity(), utteranceId);
853            }
854        }
855
856        @Override
857        public void dispatchOnError(int errorCode) {
858            final String utteranceId = getUtteranceId();
859            if (utteranceId != null) {
860                mCallbacks.dispatchOnError(getCallerIdentity(), utteranceId, errorCode);
861            }
862        }
863
864        @Override
865        public void dispatchOnBeginSynthesis(int sampleRateInHz, int audioFormat, int channelCount) {
866            final String utteranceId = getUtteranceId();
867            if (utteranceId != null) {
868                mCallbacks.dispatchOnBeginSynthesis(getCallerIdentity(), utteranceId, sampleRateInHz, audioFormat, channelCount);
869            }
870        }
871
872        @Override
873        public void dispatchOnAudioAvailable(byte[] audio) {
874            final String utteranceId = getUtteranceId();
875            if (utteranceId != null) {
876                mCallbacks.dispatchOnAudioAvailable(getCallerIdentity(), utteranceId, audio);
877            }
878        }
879
880        abstract public String getUtteranceId();
881
882        String getStringParam(Bundle params, String key, String defaultValue) {
883            return params == null ? defaultValue : params.getString(key, defaultValue);
884        }
885
886        int getIntParam(Bundle params, String key, int defaultValue) {
887            return params == null ? defaultValue : params.getInt(key, defaultValue);
888        }
889
890        float getFloatParam(Bundle params, String key, float defaultValue) {
891            return params == null ? defaultValue : params.getFloat(key, defaultValue);
892        }
893    }
894
895    /**
896     * Synthesis parameters are kept in a single Bundle passed as parameter. This class allow
897     * subclasses to access them conveniently.
898     */
899    private abstract class UtteranceSpeechItemWithParams extends UtteranceSpeechItem {
900        protected final Bundle mParams;
901        protected final String mUtteranceId;
902
903        UtteranceSpeechItemWithParams(
904                Object callerIdentity,
905                int callerUid,
906                int callerPid,
907                Bundle params,
908                String utteranceId) {
909            super(callerIdentity, callerUid, callerPid);
910            mParams = params;
911            mUtteranceId = utteranceId;
912        }
913
914        boolean hasLanguage() {
915            return !TextUtils.isEmpty(getStringParam(mParams, Engine.KEY_PARAM_LANGUAGE, null));
916        }
917
918        int getSpeechRate() {
919            return getIntParam(mParams, Engine.KEY_PARAM_RATE, getDefaultSpeechRate());
920        }
921
922        int getPitch() {
923            return getIntParam(mParams, Engine.KEY_PARAM_PITCH, Engine.DEFAULT_PITCH);
924        }
925
926        @Override
927        public String getUtteranceId() {
928            return mUtteranceId;
929        }
930
931        AudioOutputParams getAudioParams() {
932            return AudioOutputParams.createFromParamsBundle(mParams, true);
933        }
934    }
935
936    class SynthesisSpeechItem extends UtteranceSpeechItemWithParams {
937        // Never null.
938        private final CharSequence mText;
939        private final SynthesisRequest mSynthesisRequest;
940        private final String[] mDefaultLocale;
941        // Non null after synthesis has started, and all accesses
942        // guarded by 'this'.
943        private AbstractSynthesisCallback mSynthesisCallback;
944        private final EventLogger mEventLogger;
945        private final int mCallerUid;
946
947        public SynthesisSpeechItem(
948                Object callerIdentity,
949                int callerUid,
950                int callerPid,
951                Bundle params,
952                String utteranceId,
953                CharSequence text) {
954            super(callerIdentity, callerUid, callerPid, params, utteranceId);
955            mText = text;
956            mCallerUid = callerUid;
957            mSynthesisRequest = new SynthesisRequest(mText, mParams);
958            mDefaultLocale = getSettingsLocale();
959            setRequestParams(mSynthesisRequest);
960            mEventLogger = new EventLogger(mSynthesisRequest, callerUid, callerPid, mPackageName);
961        }
962
963        public CharSequence getText() {
964            return mText;
965        }
966
967        @Override
968        public boolean isValid() {
969            if (mText == null) {
970                Log.e(TAG, "null synthesis text");
971                return false;
972            }
973            if (mText.length() >= TextToSpeech.getMaxSpeechInputLength()) {
974                Log.w(TAG, "Text too long: " + mText.length() + " chars");
975                return false;
976            }
977            return true;
978        }
979
980        @Override
981        protected void playImpl() {
982            AbstractSynthesisCallback synthesisCallback;
983            mEventLogger.onRequestProcessingStart();
984            synchronized (this) {
985                // stop() might have been called before we enter this
986                // synchronized block.
987                if (isStopped()) {
988                    return;
989                }
990                mSynthesisCallback = createSynthesisCallback();
991                synthesisCallback = mSynthesisCallback;
992            }
993
994            TextToSpeechService.this.onSynthesizeText(mSynthesisRequest, synthesisCallback);
995
996            // Fix for case where client called .start() & .error(), but did not called .done()
997            if (synthesisCallback.hasStarted() && !synthesisCallback.hasFinished()) {
998                synthesisCallback.done();
999            }
1000        }
1001
1002        protected AbstractSynthesisCallback createSynthesisCallback() {
1003            return new PlaybackSynthesisCallback(getAudioParams(),
1004                    mAudioPlaybackHandler, this, getCallerIdentity(), mEventLogger, false);
1005        }
1006
1007        private void setRequestParams(SynthesisRequest request) {
1008            String voiceName = getVoiceName();
1009            request.setLanguage(getLanguage(), getCountry(), getVariant());
1010            if (!TextUtils.isEmpty(voiceName)) {
1011                request.setVoiceName(getVoiceName());
1012            }
1013            request.setSpeechRate(getSpeechRate());
1014            request.setCallerUid(mCallerUid);
1015            request.setPitch(getPitch());
1016        }
1017
1018        @Override
1019        protected void stopImpl() {
1020            AbstractSynthesisCallback synthesisCallback;
1021            synchronized (this) {
1022                synthesisCallback = mSynthesisCallback;
1023            }
1024            if (synthesisCallback != null) {
1025                // If the synthesis callback is null, it implies that we haven't
1026                // entered the synchronized(this) block in playImpl which in
1027                // turn implies that synthesis would not have started.
1028                synthesisCallback.stop();
1029                TextToSpeechService.this.onStop();
1030            } else {
1031                dispatchOnStop();
1032            }
1033        }
1034
1035        private String getCountry() {
1036            if (!hasLanguage()) return mDefaultLocale[1];
1037            return getStringParam(mParams, Engine.KEY_PARAM_COUNTRY, "");
1038        }
1039
1040        private String getVariant() {
1041            if (!hasLanguage()) return mDefaultLocale[2];
1042            return getStringParam(mParams, Engine.KEY_PARAM_VARIANT, "");
1043        }
1044
1045        public String getLanguage() {
1046            return getStringParam(mParams, Engine.KEY_PARAM_LANGUAGE, mDefaultLocale[0]);
1047        }
1048
1049        public String getVoiceName() {
1050            return getStringParam(mParams, Engine.KEY_PARAM_VOICE_NAME, "");
1051        }
1052    }
1053
1054    private class SynthesisToFileOutputStreamSpeechItem extends SynthesisSpeechItem {
1055        private final FileOutputStream mFileOutputStream;
1056
1057        public SynthesisToFileOutputStreamSpeechItem(
1058                Object callerIdentity,
1059                int callerUid,
1060                int callerPid,
1061                Bundle params,
1062                String utteranceId,
1063                CharSequence text,
1064                FileOutputStream fileOutputStream) {
1065            super(callerIdentity, callerUid, callerPid, params, utteranceId, text);
1066            mFileOutputStream = fileOutputStream;
1067        }
1068
1069        @Override
1070        protected AbstractSynthesisCallback createSynthesisCallback() {
1071            return new FileSynthesisCallback(mFileOutputStream.getChannel(), this, false);
1072        }
1073
1074        @Override
1075        protected void playImpl() {
1076            dispatchOnStart();
1077            super.playImpl();
1078            try {
1079              mFileOutputStream.close();
1080            } catch(IOException e) {
1081              Log.w(TAG, "Failed to close output file", e);
1082            }
1083        }
1084    }
1085
1086    private class AudioSpeechItem extends UtteranceSpeechItemWithParams {
1087        private final AudioPlaybackQueueItem mItem;
1088
1089        public AudioSpeechItem(
1090                Object callerIdentity,
1091                int callerUid,
1092                int callerPid,
1093                Bundle params,
1094                String utteranceId,
1095                Uri uri) {
1096            super(callerIdentity, callerUid, callerPid, params, utteranceId);
1097            mItem = new AudioPlaybackQueueItem(this, getCallerIdentity(),
1098                    TextToSpeechService.this, uri, getAudioParams());
1099        }
1100
1101        @Override
1102        public boolean isValid() {
1103            return true;
1104        }
1105
1106        @Override
1107        protected void playImpl() {
1108            mAudioPlaybackHandler.enqueue(mItem);
1109        }
1110
1111        @Override
1112        protected void stopImpl() {
1113            // Do nothing.
1114        }
1115
1116        @Override
1117        public String getUtteranceId() {
1118            return getStringParam(mParams, Engine.KEY_PARAM_UTTERANCE_ID, null);
1119        }
1120
1121        @Override
1122        AudioOutputParams getAudioParams() {
1123            return AudioOutputParams.createFromParamsBundle(mParams, false);
1124        }
1125    }
1126
1127    private class SilenceSpeechItem extends UtteranceSpeechItem {
1128        private final long mDuration;
1129        private final String mUtteranceId;
1130
1131        public SilenceSpeechItem(Object callerIdentity, int callerUid, int callerPid,
1132                String utteranceId, long duration) {
1133            super(callerIdentity, callerUid, callerPid);
1134            mUtteranceId = utteranceId;
1135            mDuration = duration;
1136        }
1137
1138        @Override
1139        public boolean isValid() {
1140            return true;
1141        }
1142
1143        @Override
1144        protected void playImpl() {
1145            mAudioPlaybackHandler.enqueue(new SilencePlaybackQueueItem(
1146                    this, getCallerIdentity(), mDuration));
1147        }
1148
1149        @Override
1150        protected void stopImpl() {
1151
1152        }
1153
1154        @Override
1155        public String getUtteranceId() {
1156            return mUtteranceId;
1157        }
1158    }
1159
1160    /**
1161     * Call {@link TextToSpeechService#onLoadLanguage} on synth thread.
1162     */
1163    private class LoadLanguageItem extends SpeechItem {
1164        private final String mLanguage;
1165        private final String mCountry;
1166        private final String mVariant;
1167
1168        public LoadLanguageItem(Object callerIdentity, int callerUid, int callerPid,
1169                String language, String country, String variant) {
1170            super(callerIdentity, callerUid, callerPid);
1171            mLanguage = language;
1172            mCountry = country;
1173            mVariant = variant;
1174        }
1175
1176        @Override
1177        public boolean isValid() {
1178            return true;
1179        }
1180
1181        @Override
1182        protected void playImpl() {
1183            TextToSpeechService.this.onLoadLanguage(mLanguage, mCountry, mVariant);
1184        }
1185
1186        @Override
1187        protected void stopImpl() {
1188            // No-op
1189        }
1190    }
1191
1192    /**
1193     * Call {@link TextToSpeechService#onLoadLanguage} on synth thread.
1194     */
1195    private class LoadVoiceItem extends SpeechItem {
1196        private final String mVoiceName;
1197
1198        public LoadVoiceItem(Object callerIdentity, int callerUid, int callerPid,
1199                String voiceName) {
1200            super(callerIdentity, callerUid, callerPid);
1201            mVoiceName = voiceName;
1202        }
1203
1204        @Override
1205        public boolean isValid() {
1206            return true;
1207        }
1208
1209        @Override
1210        protected void playImpl() {
1211            TextToSpeechService.this.onLoadVoice(mVoiceName);
1212        }
1213
1214        @Override
1215        protected void stopImpl() {
1216            // No-op
1217        }
1218    }
1219
1220
1221    @Override
1222    public IBinder onBind(Intent intent) {
1223        if (TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE.equals(intent.getAction())) {
1224            return mBinder;
1225        }
1226        return null;
1227    }
1228
1229    /**
1230     * Binder returned from {@code #onBind(Intent)}. The methods in this class can be called called
1231     * from several different threads.
1232     */
1233    // NOTE: All calls that are passed in a calling app are interned so that
1234    // they can be used as message objects (which are tested for equality using ==).
1235    private final ITextToSpeechService.Stub mBinder =
1236            new ITextToSpeechService.Stub() {
1237                @Override
1238                public int speak(
1239                        IBinder caller,
1240                        CharSequence text,
1241                        int queueMode,
1242                        Bundle params,
1243                        String utteranceId) {
1244                    if (!checkNonNull(caller, text, params)) {
1245                        return TextToSpeech.ERROR;
1246                    }
1247
1248                    SpeechItem item =
1249                            new SynthesisSpeechItem(
1250                                    caller,
1251                                    Binder.getCallingUid(),
1252                                    Binder.getCallingPid(),
1253                                    params,
1254                                    utteranceId,
1255                                    text);
1256                    return mSynthHandler.enqueueSpeechItem(queueMode, item);
1257                }
1258
1259                @Override
1260                public int synthesizeToFileDescriptor(
1261                        IBinder caller,
1262                        CharSequence text,
1263                        ParcelFileDescriptor fileDescriptor,
1264                        Bundle params,
1265                        String utteranceId) {
1266                    if (!checkNonNull(caller, text, fileDescriptor, params)) {
1267                        return TextToSpeech.ERROR;
1268                    }
1269
1270                    // In test env, ParcelFileDescriptor instance may be EXACTLY the same
1271                    // one that is used by client. And it will be closed by a client, thus
1272                    // preventing us from writing anything to it.
1273                    final ParcelFileDescriptor sameFileDescriptor =
1274                            ParcelFileDescriptor.adoptFd(fileDescriptor.detachFd());
1275
1276                    SpeechItem item =
1277                            new SynthesisToFileOutputStreamSpeechItem(
1278                                    caller,
1279                                    Binder.getCallingUid(),
1280                                    Binder.getCallingPid(),
1281                                    params,
1282                                    utteranceId,
1283                                    text,
1284                                    new ParcelFileDescriptor.AutoCloseOutputStream(
1285                                            sameFileDescriptor));
1286                    return mSynthHandler.enqueueSpeechItem(TextToSpeech.QUEUE_ADD, item);
1287                }
1288
1289                @Override
1290                public int playAudio(
1291                        IBinder caller,
1292                        Uri audioUri,
1293                        int queueMode,
1294                        Bundle params,
1295                        String utteranceId) {
1296                    if (!checkNonNull(caller, audioUri, params)) {
1297                        return TextToSpeech.ERROR;
1298                    }
1299
1300                    SpeechItem item =
1301                            new AudioSpeechItem(
1302                                    caller,
1303                                    Binder.getCallingUid(),
1304                                    Binder.getCallingPid(),
1305                                    params,
1306                                    utteranceId,
1307                                    audioUri);
1308                    return mSynthHandler.enqueueSpeechItem(queueMode, item);
1309                }
1310
1311                @Override
1312                public int playSilence(
1313                        IBinder caller, long duration, int queueMode, String utteranceId) {
1314                    if (!checkNonNull(caller)) {
1315                        return TextToSpeech.ERROR;
1316                    }
1317
1318                    SpeechItem item =
1319                            new SilenceSpeechItem(
1320                                    caller,
1321                                    Binder.getCallingUid(),
1322                                    Binder.getCallingPid(),
1323                                    utteranceId,
1324                                    duration);
1325                    return mSynthHandler.enqueueSpeechItem(queueMode, item);
1326                }
1327
1328                @Override
1329                public boolean isSpeaking() {
1330                    return mSynthHandler.isSpeaking() || mAudioPlaybackHandler.isSpeaking();
1331                }
1332
1333                @Override
1334                public int stop(IBinder caller) {
1335                    if (!checkNonNull(caller)) {
1336                        return TextToSpeech.ERROR;
1337                    }
1338
1339                    return mSynthHandler.stopForApp(caller);
1340                }
1341
1342                @Override
1343                public String[] getLanguage() {
1344                    return onGetLanguage();
1345                }
1346
1347                @Override
1348                public String[] getClientDefaultLanguage() {
1349                    return getSettingsLocale();
1350                }
1351
1352                /*
1353                 * If defaults are enforced, then no language is "available" except
1354                 * perhaps the default language selected by the user.
1355                 */
1356                @Override
1357                public int isLanguageAvailable(String lang, String country, String variant) {
1358                    if (!checkNonNull(lang)) {
1359                        return TextToSpeech.ERROR;
1360                    }
1361
1362                    return onIsLanguageAvailable(lang, country, variant);
1363                }
1364
1365                @Override
1366                public String[] getFeaturesForLanguage(
1367                        String lang, String country, String variant) {
1368                    Set<String> features = onGetFeaturesForLanguage(lang, country, variant);
1369                    String[] featuresArray = null;
1370                    if (features != null) {
1371                        featuresArray = new String[features.size()];
1372                        features.toArray(featuresArray);
1373                    } else {
1374                        featuresArray = new String[0];
1375                    }
1376                    return featuresArray;
1377                }
1378
1379                /*
1380                 * There is no point loading a non default language if defaults
1381                 * are enforced.
1382                 */
1383                @Override
1384                public int loadLanguage(
1385                        IBinder caller, String lang, String country, String variant) {
1386                    if (!checkNonNull(lang)) {
1387                        return TextToSpeech.ERROR;
1388                    }
1389                    int retVal = onIsLanguageAvailable(lang, country, variant);
1390
1391                    if (retVal == TextToSpeech.LANG_AVAILABLE
1392                            || retVal == TextToSpeech.LANG_COUNTRY_AVAILABLE
1393                            || retVal == TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE) {
1394
1395                        SpeechItem item =
1396                                new LoadLanguageItem(
1397                                        caller,
1398                                        Binder.getCallingUid(),
1399                                        Binder.getCallingPid(),
1400                                        lang,
1401                                        country,
1402                                        variant);
1403
1404                        if (mSynthHandler.enqueueSpeechItem(TextToSpeech.QUEUE_ADD, item)
1405                                != TextToSpeech.SUCCESS) {
1406                            return TextToSpeech.ERROR;
1407                        }
1408                    }
1409                    return retVal;
1410                }
1411
1412                @Override
1413                public List<Voice> getVoices() {
1414                    return onGetVoices();
1415                }
1416
1417                @Override
1418                public int loadVoice(IBinder caller, String voiceName) {
1419                    if (!checkNonNull(voiceName)) {
1420                        return TextToSpeech.ERROR;
1421                    }
1422                    int retVal = onIsValidVoiceName(voiceName);
1423
1424                    if (retVal == TextToSpeech.SUCCESS) {
1425                        SpeechItem item =
1426                                new LoadVoiceItem(
1427                                        caller,
1428                                        Binder.getCallingUid(),
1429                                        Binder.getCallingPid(),
1430                                        voiceName);
1431                        if (mSynthHandler.enqueueSpeechItem(TextToSpeech.QUEUE_ADD, item)
1432                                != TextToSpeech.SUCCESS) {
1433                            return TextToSpeech.ERROR;
1434                        }
1435                    }
1436                    return retVal;
1437                }
1438
1439                public String getDefaultVoiceNameFor(String lang, String country, String variant) {
1440                    if (!checkNonNull(lang)) {
1441                        return null;
1442                    }
1443                    int retVal = onIsLanguageAvailable(lang, country, variant);
1444
1445                    if (retVal == TextToSpeech.LANG_AVAILABLE
1446                            || retVal == TextToSpeech.LANG_COUNTRY_AVAILABLE
1447                            || retVal == TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE) {
1448                        return onGetDefaultVoiceNameFor(lang, country, variant);
1449                    } else {
1450                        return null;
1451                    }
1452                }
1453
1454                @Override
1455                public void setCallback(IBinder caller, ITextToSpeechCallback cb) {
1456                    // Note that passing in a null callback is a valid use case.
1457                    if (!checkNonNull(caller)) {
1458                        return;
1459                    }
1460
1461                    mCallbacks.setCallback(caller, cb);
1462                }
1463
1464                private String intern(String in) {
1465                    // The input parameter will be non null.
1466                    return in.intern();
1467                }
1468
1469                private boolean checkNonNull(Object... args) {
1470                    for (Object o : args) {
1471                        if (o == null) return false;
1472                    }
1473                    return true;
1474                }
1475            };
1476
1477    private class CallbackMap extends RemoteCallbackList<ITextToSpeechCallback> {
1478        private final HashMap<IBinder, ITextToSpeechCallback> mCallerToCallback
1479                = new HashMap<IBinder, ITextToSpeechCallback>();
1480
1481        public void setCallback(IBinder caller, ITextToSpeechCallback cb) {
1482            synchronized (mCallerToCallback) {
1483                ITextToSpeechCallback old;
1484                if (cb != null) {
1485                    register(cb, caller);
1486                    old = mCallerToCallback.put(caller, cb);
1487                } else {
1488                    old = mCallerToCallback.remove(caller);
1489                }
1490                if (old != null && old != cb) {
1491                    unregister(old);
1492                }
1493            }
1494        }
1495
1496        public void dispatchOnStop(Object callerIdentity, String utteranceId, boolean started) {
1497            ITextToSpeechCallback cb = getCallbackFor(callerIdentity);
1498            if (cb == null) return;
1499            try {
1500                cb.onStop(utteranceId, started);
1501            } catch (RemoteException e) {
1502                Log.e(TAG, "Callback onStop failed: " + e);
1503            }
1504        }
1505
1506        public void dispatchOnSuccess(Object callerIdentity, String utteranceId) {
1507            ITextToSpeechCallback cb = getCallbackFor(callerIdentity);
1508            if (cb == null) return;
1509            try {
1510                cb.onSuccess(utteranceId);
1511            } catch (RemoteException e) {
1512                Log.e(TAG, "Callback onDone failed: " + e);
1513            }
1514        }
1515
1516        public void dispatchOnStart(Object callerIdentity, String utteranceId) {
1517            ITextToSpeechCallback cb = getCallbackFor(callerIdentity);
1518            if (cb == null) return;
1519            try {
1520                cb.onStart(utteranceId);
1521            } catch (RemoteException e) {
1522                Log.e(TAG, "Callback onStart failed: " + e);
1523            }
1524        }
1525
1526        public void dispatchOnError(Object callerIdentity, String utteranceId,
1527                int errorCode) {
1528            ITextToSpeechCallback cb = getCallbackFor(callerIdentity);
1529            if (cb == null) return;
1530            try {
1531                cb.onError(utteranceId, errorCode);
1532            } catch (RemoteException e) {
1533                Log.e(TAG, "Callback onError failed: " + e);
1534            }
1535        }
1536
1537        public void dispatchOnBeginSynthesis(Object callerIdentity, String utteranceId, int sampleRateInHz, int audioFormat, int channelCount) {
1538            ITextToSpeechCallback cb = getCallbackFor(callerIdentity);
1539            if (cb == null) return;
1540            try {
1541                cb.onBeginSynthesis(utteranceId, sampleRateInHz, audioFormat, channelCount);
1542            } catch (RemoteException e) {
1543                Log.e(TAG, "Callback dispatchOnBeginSynthesis(String, int, int, int) failed: " + e);
1544            }
1545        }
1546
1547        public void dispatchOnAudioAvailable(Object callerIdentity, String utteranceId, byte[] buffer) {
1548            ITextToSpeechCallback cb = getCallbackFor(callerIdentity);
1549            if (cb == null) return;
1550            try {
1551                cb.onAudioAvailable(utteranceId, buffer);
1552            } catch (RemoteException e) {
1553                Log.e(TAG, "Callback dispatchOnAudioAvailable(String, byte[]) failed: " + e);
1554            }
1555        }
1556
1557        @Override
1558        public void onCallbackDied(ITextToSpeechCallback callback, Object cookie) {
1559            IBinder caller = (IBinder) cookie;
1560            synchronized (mCallerToCallback) {
1561                mCallerToCallback.remove(caller);
1562            }
1563            //mSynthHandler.stopForApp(caller);
1564        }
1565
1566        @Override
1567        public void kill() {
1568            synchronized (mCallerToCallback) {
1569                mCallerToCallback.clear();
1570                super.kill();
1571            }
1572        }
1573
1574        private ITextToSpeechCallback getCallbackFor(Object caller) {
1575            ITextToSpeechCallback cb;
1576            IBinder asBinder = (IBinder) caller;
1577            synchronized (mCallerToCallback) {
1578                cb = mCallerToCallback.get(asBinder);
1579            }
1580
1581            return cb;
1582        }
1583    }
1584}
1585