1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "modules/speech/SpeechSynthesis.h"
28
29#include "bindings/core/v8/ExceptionState.h"
30#include "core/dom/ExecutionContext.h"
31#include "modules/speech/SpeechSynthesisEvent.h"
32#include "platform/speech/PlatformSpeechSynthesisVoice.h"
33#include "wtf/CurrentTime.h"
34
35namespace blink {
36
37SpeechSynthesis* SpeechSynthesis::create(ExecutionContext* context)
38{
39    return adoptRefCountedGarbageCollectedWillBeNoop(new SpeechSynthesis(context));
40}
41
42SpeechSynthesis::SpeechSynthesis(ExecutionContext* context)
43    : ContextLifecycleObserver(context)
44    , m_platformSpeechSynthesizer(PlatformSpeechSynthesizer::create(this))
45    , m_isPaused(false)
46{
47}
48
49void SpeechSynthesis::setPlatformSynthesizer(PlatformSpeechSynthesizer* synthesizer)
50{
51    m_platformSpeechSynthesizer = synthesizer;
52}
53
54ExecutionContext* SpeechSynthesis::executionContext() const
55{
56    return ContextLifecycleObserver::executionContext();
57}
58
59void SpeechSynthesis::voicesDidChange()
60{
61    m_voiceList.clear();
62    if (executionContext() && !executionContext()->activeDOMObjectsAreStopped())
63        dispatchEvent(Event::create(EventTypeNames::voiceschanged));
64}
65
66const HeapVector<Member<SpeechSynthesisVoice> >& SpeechSynthesis::getVoices()
67{
68    if (m_voiceList.size())
69        return m_voiceList;
70
71    // If the voiceList is empty, that's the cue to get the voices from the platform again.
72    const HeapVector<Member<PlatformSpeechSynthesisVoice> >& platformVoices = m_platformSpeechSynthesizer->voiceList();
73    size_t voiceCount = platformVoices.size();
74    for (size_t k = 0; k < voiceCount; k++)
75        m_voiceList.append(SpeechSynthesisVoice::create(platformVoices[k].get()));
76
77    return m_voiceList;
78}
79
80bool SpeechSynthesis::speaking() const
81{
82    // If we have a current speech utterance, then that means we're assumed to be in a speaking state.
83    // This state is independent of whether the utterance happens to be paused.
84    return currentSpeechUtterance();
85}
86
87bool SpeechSynthesis::pending() const
88{
89    // This is true if there are any utterances that have not started.
90    // That means there will be more than one in the queue.
91    return m_utteranceQueue.size() > 1;
92}
93
94bool SpeechSynthesis::paused() const
95{
96    return m_isPaused;
97}
98
99void SpeechSynthesis::startSpeakingImmediately()
100{
101    SpeechSynthesisUtterance* utterance = currentSpeechUtterance();
102    ASSERT(utterance);
103
104    utterance->setStartTime(monotonicallyIncreasingTime());
105    m_isPaused = false;
106    m_platformSpeechSynthesizer->speak(utterance->platformUtterance());
107}
108
109void SpeechSynthesis::speak(SpeechSynthesisUtterance* utterance, ExceptionState& exceptionState)
110{
111    if (!utterance) {
112        exceptionState.throwTypeError("Invalid utterance argument");
113        return;
114    }
115
116    m_utteranceQueue.append(utterance);
117
118    // If the queue was empty, speak this immediately.
119    if (m_utteranceQueue.size() == 1)
120        startSpeakingImmediately();
121}
122
123void SpeechSynthesis::cancel()
124{
125    // Remove all the items from the utterance queue. The platform
126    // may still have references to some of these utterances and may
127    // fire events on them asynchronously.
128    m_utteranceQueue.clear();
129    m_platformSpeechSynthesizer->cancel();
130}
131
132void SpeechSynthesis::pause()
133{
134    if (!m_isPaused)
135        m_platformSpeechSynthesizer->pause();
136}
137
138void SpeechSynthesis::resume()
139{
140    if (!currentSpeechUtterance())
141        return;
142    m_platformSpeechSynthesizer->resume();
143}
144
145void SpeechSynthesis::fireEvent(const AtomicString& type, SpeechSynthesisUtterance* utterance, unsigned long charIndex, const String& name)
146{
147    if (executionContext() && !executionContext()->activeDOMObjectsAreStopped())
148        utterance->dispatchEvent(SpeechSynthesisEvent::create(type, charIndex, (currentTime() - utterance->startTime()), name));
149}
150
151void SpeechSynthesis::handleSpeakingCompleted(SpeechSynthesisUtterance* utterance, bool errorOccurred)
152{
153    ASSERT(utterance);
154
155    bool shouldStartSpeaking = false;
156    // If the utterance that completed was the one we're currently speaking,
157    // remove it from the queue and start speaking the next one.
158    if (utterance == currentSpeechUtterance()) {
159        m_utteranceQueue.removeFirst();
160        shouldStartSpeaking = !!m_utteranceQueue.size();
161    }
162
163    // Always fire the event, because the platform may have asynchronously
164    // sent an event on an utterance before it got the message that we
165    // canceled it, and we should always report to the user what actually
166    // happened.
167    fireEvent(errorOccurred ? EventTypeNames::error : EventTypeNames::end, utterance, 0, String());
168
169    // Start the next utterance if we just finished one and one was pending.
170    if (shouldStartSpeaking && !m_utteranceQueue.isEmpty())
171        startSpeakingImmediately();
172}
173
174void SpeechSynthesis::boundaryEventOccurred(PlatformSpeechSynthesisUtterance* utterance, SpeechBoundary boundary, unsigned charIndex)
175{
176    DEFINE_STATIC_LOCAL(const String, wordBoundaryString, ("word"));
177    DEFINE_STATIC_LOCAL(const String, sentenceBoundaryString, ("sentence"));
178
179    switch (boundary) {
180    case SpeechWordBoundary:
181        fireEvent(EventTypeNames::boundary, static_cast<SpeechSynthesisUtterance*>(utterance->client()), charIndex, wordBoundaryString);
182        break;
183    case SpeechSentenceBoundary:
184        fireEvent(EventTypeNames::boundary, static_cast<SpeechSynthesisUtterance*>(utterance->client()), charIndex, sentenceBoundaryString);
185        break;
186    default:
187        ASSERT_NOT_REACHED();
188    }
189}
190
191void SpeechSynthesis::didStartSpeaking(PlatformSpeechSynthesisUtterance* utterance)
192{
193    if (utterance->client())
194        fireEvent(EventTypeNames::start, static_cast<SpeechSynthesisUtterance*>(utterance->client()), 0, String());
195}
196
197void SpeechSynthesis::didPauseSpeaking(PlatformSpeechSynthesisUtterance* utterance)
198{
199    m_isPaused = true;
200    if (utterance->client())
201        fireEvent(EventTypeNames::pause, static_cast<SpeechSynthesisUtterance*>(utterance->client()), 0, String());
202}
203
204void SpeechSynthesis::didResumeSpeaking(PlatformSpeechSynthesisUtterance* utterance)
205{
206    m_isPaused = false;
207    if (utterance->client())
208        fireEvent(EventTypeNames::resume, static_cast<SpeechSynthesisUtterance*>(utterance->client()), 0, String());
209}
210
211void SpeechSynthesis::didFinishSpeaking(PlatformSpeechSynthesisUtterance* utterance)
212{
213    if (utterance->client())
214        handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance*>(utterance->client()), false);
215}
216
217void SpeechSynthesis::speakingErrorOccurred(PlatformSpeechSynthesisUtterance* utterance)
218{
219    if (utterance->client())
220        handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance*>(utterance->client()), true);
221}
222
223SpeechSynthesisUtterance* SpeechSynthesis::currentSpeechUtterance() const
224{
225    if (!m_utteranceQueue.isEmpty())
226        return m_utteranceQueue.first().get();
227    return 0;
228}
229
230const AtomicString& SpeechSynthesis::interfaceName() const
231{
232    return EventTargetNames::SpeechSynthesis;
233}
234
235void SpeechSynthesis::trace(Visitor* visitor)
236{
237    visitor->trace(m_platformSpeechSynthesizer);
238    visitor->trace(m_voiceList);
239    visitor->trace(m_utteranceQueue);
240    PlatformSpeechSynthesizerClient::trace(visitor);
241    EventTargetWithInlineData::trace(visitor);
242}
243
244} // namespace blink
245