Bug 1185235 - Implement SpeechRecognition::maxAlternatives. r=smaug
authorKelly Davis <kdavis@mozilla.com>
Sun, 26 Jul 2015 23:02:00 -0400
changeset 287541 487ee69fefe9c41dad46b9dd1335d6cacac79005
parent 287540 00726df4997cf83e734d559316e6283e3cc1425f
child 287542 0fdb9415359330d25b7db5f79e484b588d74eaa7
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1185235
milestone42.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1185235 - Implement SpeechRecognition::maxAlternatives. r=smaug
dom/media/webspeech/recognition/PocketSphinxSpeechRecognitionService.cpp
dom/media/webspeech/recognition/SpeechRecognition.cpp
dom/media/webspeech/recognition/SpeechRecognition.h
dom/media/webspeech/recognition/test/FakeSpeechRecognitionService.cpp
--- a/dom/media/webspeech/recognition/PocketSphinxSpeechRecognitionService.cpp
+++ b/dom/media/webspeech/recognition/PocketSphinxSpeechRecognitionService.cpp
@@ -49,23 +49,26 @@ public:
                                    // thread!
 
     // Declare javascript result events
     nsRefPtr<SpeechEvent> event = new SpeechEvent(
       mRecognition, SpeechRecognition::EVENT_RECOGNITIONSERVICE_FINAL_RESULT);
     SpeechRecognitionResultList* resultList =
       new SpeechRecognitionResultList(mRecognition);
     SpeechRecognitionResult* result = new SpeechRecognitionResult(mRecognition);
-    SpeechRecognitionAlternative* alternative =
-      new SpeechRecognitionAlternative(mRecognition);
+    ErrorResult rv;
+    if (0 < mRecognition->GetMaxAlternatives(rv)) { // GetMaxAlternatives can't fail
+      SpeechRecognitionAlternative* alternative =
+        new SpeechRecognitionAlternative(mRecognition);
 
-    alternative->mTranscript = mResult;
-    alternative->mConfidence = 100;
+      alternative->mTranscript = mResult;
+      alternative->mConfidence = 100;
 
-    result->mItems.AppendElement(alternative);
+      result->mItems.AppendElement(alternative);
+    }
     resultList->mItems.AppendElement(result);
 
     event->mRecognitionResultList = resultList;
     NS_DispatchToMainThread(event);
 
     // If we don't destroy the thread when we're done with it, it will hang
     // around forever... bad!
     // But thread->Shutdown must be called from the main thread, not from the
@@ -325,21 +328,24 @@ PocketSphinxSpeechRecognitionService::Ob
 }
 
 SpeechRecognitionResultList*
 PocketSphinxSpeechRecognitionService::BuildMockResultList()
 {
   SpeechRecognitionResultList* resultList =
     new SpeechRecognitionResultList(mRecognition);
   SpeechRecognitionResult* result = new SpeechRecognitionResult(mRecognition);
-  SpeechRecognitionAlternative* alternative =
-    new SpeechRecognitionAlternative(mRecognition);
+  ErrorResult rv;
+  if (0 < mRecognition->GetMaxAlternatives(rv)) { // GetMaxAlternatives can't fail
+    SpeechRecognitionAlternative* alternative =
+      new SpeechRecognitionAlternative(mRecognition);
 
-  alternative->mTranscript = NS_LITERAL_STRING("Mock final result");
-  alternative->mConfidence = 0.0f;
+    alternative->mTranscript = NS_LITERAL_STRING("Mock final result");
+    alternative->mConfidence = 0.0f;
 
-  result->mItems.AppendElement(alternative);
+    result->mItems.AppendElement(alternative);
+  }
   resultList->mItems.AppendElement(result);
 
   return resultList;
 }
 
 } // namespace mozilla
--- a/dom/media/webspeech/recognition/SpeechRecognition.cpp
+++ b/dom/media/webspeech/recognition/SpeechRecognition.cpp
@@ -112,16 +112,17 @@ struct SpeechRecognition::TestConfig Spe
 
 SpeechRecognition::SpeechRecognition(nsPIDOMWindow* aOwnerWindow)
   : DOMEventTargetHelper(aOwnerWindow)
   , mEndpointer(kSAMPLE_RATE)
   , mAudioSamplesPerChunk(mEndpointer.FrameSize())
   , mSpeechDetectionTimer(do_CreateInstance(NS_TIMER_CONTRACTID))
   , mSpeechGrammarList(new SpeechGrammarList(GetParentObject()))
   , mInterimResults(false)
+  , mMaxAlternatives(1)
 {
   SR_LOG("created SpeechRecognition");
 
   mTestConfig.Init();
   if (mTestConfig.mEnableTests) {
     nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
     obs->AddObserver(this, SPEECH_RECOGNITION_TEST_EVENT_REQUEST_TOPIC, false);
     obs->AddObserver(this, SPEECH_RECOGNITION_TEST_END_TOPIC, false);
@@ -680,24 +681,23 @@ SpeechRecognition::SetInterimResults(boo
 {
   mInterimResults = aArg;
   return;
 }
 
 uint32_t
 SpeechRecognition::GetMaxAlternatives(ErrorResult& aRv) const
 {
-  aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
-  return 0;
+  return mMaxAlternatives;
 }
 
 void
 SpeechRecognition::SetMaxAlternatives(uint32_t aArg, ErrorResult& aRv)
 {
-  aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
+  mMaxAlternatives = aArg;
   return;
 }
 
 void
 SpeechRecognition::GetServiceURI(nsString& aRetVal, ErrorResult& aRv) const
 {
   aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
   return;
--- a/dom/media/webspeech/recognition/SpeechRecognition.h
+++ b/dom/media/webspeech/recognition/SpeechRecognition.h
@@ -261,16 +261,27 @@ private:
   // 2. If true, interim results SHOULD be returned
   // 3. If false, interim results MUST NOT be returned
   //
   // Pocketsphinx does not return interm results; so, defaulting
   // mInterimResults to false, then ignoring its subsequent value
   // is a conforming implementation.
   bool mInterimResults;
 
+  // WebSpeechAPI (http://bit.ly/1JAiqeo) states:
+  //
+  // 1. Default value is 1
+  // 2. Subsequent value is the "maximum number of SpeechRecognitionAlternatives per result"
+  //
+  // Pocketsphinx can only return at maximum a single SpeechRecognitionAlternative
+  // per SpeechRecognitionResult. So defaulting mMaxAlternatives to 1, for all non
+  // zero values ignoring mMaxAlternatives while for a 0 value returning no
+  // SpeechRecognitionAlternative per result is a conforming implementation.
+  uint32_t mMaxAlternatives;
+
   void ProcessTestEventRequest(nsISupports* aSubject, const nsAString& aEventName);
 
   const char* GetName(FSMState aId);
   const char* GetName(SpeechEvent* aId);
 };
 
 class SpeechEvent : public nsRunnable
 {
--- a/dom/media/webspeech/recognition/test/FakeSpeechRecognitionService.cpp
+++ b/dom/media/webspeech/recognition/test/FakeSpeechRecognitionService.cpp
@@ -97,20 +97,23 @@ FakeSpeechRecognitionService::Observe(ns
   return NS_OK;
 }
 
 SpeechRecognitionResultList*
 FakeSpeechRecognitionService::BuildMockResultList()
 {
   SpeechRecognitionResultList* resultList = new SpeechRecognitionResultList(mRecognition);
   SpeechRecognitionResult* result = new SpeechRecognitionResult(mRecognition);
-  SpeechRecognitionAlternative* alternative = new SpeechRecognitionAlternative(mRecognition);
+  ErrorResult rv;
+  if (0 < mRecognition->GetMaxAlternatives(rv)) { // GetMaxAlternatives can't fail
+    SpeechRecognitionAlternative* alternative = new SpeechRecognitionAlternative(mRecognition);
 
-  alternative->mTranscript = NS_LITERAL_STRING("Mock final result");
-  alternative->mConfidence = 0.0f;
+    alternative->mTranscript = NS_LITERAL_STRING("Mock final result");
+    alternative->mConfidence = 0.0f;
 
-  result->mItems.AppendElement(alternative);
+    result->mItems.AppendElement(alternative);
+  }
   resultList->mItems.AppendElement(result);
 
   return resultList;
 }
 
 } // namespace mozilla