diff options
Diffstat (limited to 'dom/media/webspeech/recognition')
-rw-r--r-- | dom/media/webspeech/recognition/SpeechRecognition.cpp | 25 | ||||
-rw-r--r-- | dom/media/webspeech/recognition/SpeechRecognition.h | 22 |
2 files changed, 40 insertions, 7 deletions
diff --git a/dom/media/webspeech/recognition/SpeechRecognition.cpp b/dom/media/webspeech/recognition/SpeechRecognition.cpp index 7239a88237..44ad5fdd61 100644 --- a/dom/media/webspeech/recognition/SpeechRecognition.cpp +++ b/dom/media/webspeech/recognition/SpeechRecognition.cpp @@ -136,7 +136,7 @@ CreateSpeechRecognitionService(nsPIDOMWindowInner* aWindow, NS_IMPL_CYCLE_COLLECTION_WEAK_PTR_INHERITED(SpeechRecognition, DOMEventTargetHelper, mStream, mTrack, mRecognitionService, - mSpeechGrammarList) + mSpeechGrammarList, mListener) NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SpeechRecognition) NS_INTERFACE_MAP_ENTRY(nsIObserver) @@ -145,6 +145,16 @@ NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) NS_IMPL_ADDREF_INHERITED(SpeechRecognition, DOMEventTargetHelper) NS_IMPL_RELEASE_INHERITED(SpeechRecognition, DOMEventTargetHelper) +NS_IMPL_CYCLE_COLLECTION_INHERITED(SpeechRecognition::TrackListener, + DOMMediaStream::TrackListener, + mSpeechRecognition) +NS_IMPL_ADDREF_INHERITED(SpeechRecognition::TrackListener, + DOMMediaStream::TrackListener) +NS_IMPL_RELEASE_INHERITED(SpeechRecognition::TrackListener, + DOMMediaStream::TrackListener) +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SpeechRecognition::TrackListener) +NS_INTERFACE_MAP_END_INHERITING(DOMMediaStream::TrackListener) + SpeechRecognition::SpeechRecognition(nsPIDOMWindowInner* aOwnerWindow) : DOMEventTargetHelper(aOwnerWindow), mEndpointer(kSAMPLE_RATE), @@ -472,8 +482,9 @@ void SpeechRecognition::Reset() { ++mStreamGeneration; if (mStream) { - mStream->UnregisterTrackListener(this); + mStream->UnregisterTrackListener(mListener); mStream = nullptr; + mListener = nullptr; } mTrack = nullptr; mTrackIsOwned = false; @@ -642,7 +653,8 @@ RefPtr<GenericNonExclusivePromise> SpeechRecognition::StopRecording() { if (mStream) { // Ensure we don't start recording because a track became available // before we get reset. - mStream->UnregisterTrackListener(this); + mStream->UnregisterTrackListener(mListener); + mListener = nullptr; } return GenericNonExclusivePromise::CreateAndResolve(true, __func__); } @@ -801,10 +813,13 @@ void SpeechRecognition::Start(const Optional<NonNull<DOMMediaStream>>& aStream, MediaStreamConstraints constraints; constraints.mAudio.SetAsBoolean() = true; + MOZ_ASSERT(!mListener); + mListener = new TrackListener(this); + if (aStream.WasPassed()) { mStream = &aStream.Value(); mTrackIsOwned = false; - mStream->RegisterTrackListener(this); + mStream->RegisterTrackListener(mListener); nsTArray<RefPtr<AudioStreamTrack>> tracks; mStream->GetAudioTracks(tracks); for (const RefPtr<AudioStreamTrack>& track : tracks) { @@ -839,7 +854,7 @@ void SpeechRecognition::Start(const Optional<NonNull<DOMMediaStream>>& aStream, return; } mStream = std::move(aStream); - mStream->RegisterTrackListener(this); + mStream->RegisterTrackListener(mListener); for (const RefPtr<AudioStreamTrack>& track : tracks) { if (!track->Ended()) { NotifyTrackAdded(track); diff --git a/dom/media/webspeech/recognition/SpeechRecognition.h b/dom/media/webspeech/recognition/SpeechRecognition.h index 687f38041e..465cadc8cb 100644 --- a/dom/media/webspeech/recognition/SpeechRecognition.h +++ b/dom/media/webspeech/recognition/SpeechRecognition.h @@ -52,7 +52,6 @@ LogModule* GetSpeechRecognitionLog(); class SpeechRecognition final : public DOMEventTargetHelper, public nsIObserver, - public DOMMediaStream::TrackListener, public SupportsWeakPtr { public: explicit SpeechRecognition(nsPIDOMWindowInner* aOwnerWindow); @@ -133,7 +132,24 @@ class SpeechRecognition final : public DOMEventTargetHelper, EVENT_COUNT }; - void NotifyTrackAdded(const RefPtr<MediaStreamTrack>& aTrack) override; + void NotifyTrackAdded(const RefPtr<MediaStreamTrack>& aTrack); + + class TrackListener final : public DOMMediaStream::TrackListener { + public: + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(TrackListener, + DOMMediaStream::TrackListener) + explicit TrackListener(SpeechRecognition* aSpeechRecognition) + : mSpeechRecognition(aSpeechRecognition) {} + void NotifyTrackAdded(const RefPtr<MediaStreamTrack>& aTrack) override { + mSpeechRecognition->NotifyTrackAdded(aTrack); + } + + private: + virtual ~TrackListener() = default; + RefPtr<SpeechRecognition> mSpeechRecognition; + }; + // aMessage should be valid UTF-8, but invalid UTF-8 byte sequences are // replaced with the REPLACEMENT CHARACTER on conversion to UTF-16. void DispatchError(EventType aErrorType, @@ -266,6 +282,8 @@ class SpeechRecognition final : public DOMEventTargetHelper, // a conforming implementation. uint32_t mMaxAlternatives; + RefPtr<TrackListener> mListener; + void ProcessTestEventRequest(nsISupports* aSubject, const nsAString& aEventName); |