1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_dom_nsSpeechTask_h
8 #define mozilla_dom_nsSpeechTask_h
9 
10 #include "SpeechSynthesisUtterance.h"
11 #include "AudioChannelAgent.h"
12 #include "nsISpeechService.h"
13 #include "nsWeakReference.h"
14 
15 namespace mozilla {
16 
17 class SharedBuffer;
18 
19 namespace dom {
20 
21 class SpeechSynthesisUtterance;
22 class SpeechSynthesis;
23 
24 class nsSpeechTask : public nsISpeechTask,
25                      public nsIAudioChannelAgentCallback,
26                      public nsSupportsWeakReference {
27  public:
28   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
29   NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsSpeechTask, nsISpeechTask)
30 
31   NS_DECL_NSISPEECHTASK
32   NS_DECL_NSIAUDIOCHANNELAGENTCALLBACK
33 
34   explicit nsSpeechTask(SpeechSynthesisUtterance* aUtterance, bool aIsChrome);
35   nsSpeechTask(float aVolume, const nsAString& aText, bool aIsChrome);
36 
37   virtual void Pause();
38 
39   virtual void Resume();
40 
41   virtual void Cancel();
42 
43   virtual void ForceEnd();
44 
45   void SetSpeechSynthesis(SpeechSynthesis* aSpeechSynthesis);
46 
47   void Init();
48 
49   void SetChosenVoiceURI(const nsAString& aUri);
50 
51   virtual void SetAudioOutputVolume(float aVolume);
52 
53   void ForceError(float aElapsedTime, uint32_t aCharIndex);
54 
IsPreCanceled()55   bool IsPreCanceled() { return mPreCanceled; };
56 
IsPrePaused()57   bool IsPrePaused() { return mPrePaused; }
58 
IsChrome()59   bool IsChrome() { return mIsChrome; }
60 
61   enum { STATE_PENDING, STATE_SPEAKING, STATE_ENDED };
62 
GetState()63   uint32_t GetState() const { return mState; }
64 
IsSpeaking()65   bool IsSpeaking() const { return mState == STATE_SPEAKING; }
66 
IsPending()67   bool IsPending() const { return mState == STATE_PENDING; }
68 
69  protected:
70   virtual ~nsSpeechTask();
71 
72   nsresult DispatchStartImpl();
73 
74   virtual nsresult DispatchStartImpl(const nsAString& aUri);
75 
76   virtual nsresult DispatchEndImpl(float aElapsedTime, uint32_t aCharIndex);
77 
78   virtual nsresult DispatchPauseImpl(float aElapsedTime, uint32_t aCharIndex);
79 
80   virtual nsresult DispatchResumeImpl(float aElapsedTime, uint32_t aCharIndex);
81 
82   virtual nsresult DispatchErrorImpl(float aElapsedTime, uint32_t aCharIndex);
83 
84   virtual nsresult DispatchBoundaryImpl(const nsAString& aName,
85                                         float aElapsedTime, uint32_t aCharIndex,
86                                         uint32_t aCharLength, uint8_t argc);
87 
88   virtual nsresult DispatchMarkImpl(const nsAString& aName, float aElapsedTime,
89                                     uint32_t aCharIndex);
90 
91   RefPtr<SpeechSynthesisUtterance> mUtterance;
92 
93   float mVolume;
94 
95   nsString mText;
96 
97   bool mInited;
98 
99   bool mPrePaused;
100 
101   bool mPreCanceled;
102 
103  private:
104   void End();
105 
106   void CreateAudioChannelAgent();
107 
108   void DestroyAudioChannelAgent();
109 
110   nsCOMPtr<nsISpeechTaskCallback> mCallback;
111 
112   RefPtr<mozilla::dom::AudioChannelAgent> mAudioChannelAgent;
113 
114   RefPtr<SpeechSynthesis> mSpeechSynthesis;
115 
116   nsString mChosenVoiceURI;
117 
118   bool mIsChrome;
119 
120   uint32_t mState;
121 };
122 
123 }  // namespace dom
124 }  // namespace mozilla
125 
126 #endif
127