2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * 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.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER 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.
27 #include "web/SpeechRecognitionClientProxy.h"
29 #include "core/dom/ExecutionContext.h"
30 #include "modules/mediastream/MediaStreamTrack.h"
31 #include "modules/speech/SpeechGrammarList.h"
32 #include "modules/speech/SpeechRecognition.h"
33 #include "modules/speech/SpeechRecognitionError.h"
34 #include "modules/speech/SpeechRecognitionResult.h"
35 #include "modules/speech/SpeechRecognitionResultList.h"
36 #include "platform/RuntimeEnabledFeatures.h"
37 #include "platform/weborigin/SecurityOrigin.h"
38 #include "public/platform/WebMediaStreamTrack.h"
39 #include "public/platform/WebSecurityOrigin.h"
40 #include "public/web/WebSpeechGrammar.h"
41 #include "public/web/WebSpeechRecognitionHandle.h"
42 #include "public/web/WebSpeechRecognitionParams.h"
43 #include "public/web/WebSpeechRecognitionResult.h"
44 #include "public/web/WebSpeechRecognizer.h"
45 #include "wtf/PassRefPtr.h"
49 SpeechRecognitionClientProxy::~SpeechRecognitionClientProxy()
53 PassOwnPtr
<SpeechRecognitionClientProxy
> SpeechRecognitionClientProxy::create(WebSpeechRecognizer
* recognizer
)
55 return adoptPtr(new SpeechRecognitionClientProxy(recognizer
));
58 void SpeechRecognitionClientProxy::start(SpeechRecognition
* recognition
, const SpeechGrammarList
* grammarList
, const String
& lang
, const String
& serviceURI
, bool continuous
, bool interimResults
, unsigned long maxAlternatives
, MediaStreamTrack
* audioTrack
)
60 WebVector
<WebSpeechGrammar
> webSpeechGrammars(static_cast<size_t>(grammarList
->length()));
61 for (unsigned long i
= 0; i
< grammarList
->length(); ++i
)
62 webSpeechGrammars
[i
] = grammarList
->item(i
);
64 WebMediaStreamTrack track
;
65 if (RuntimeEnabledFeatures::mediaStreamSpeechEnabled() && audioTrack
)
66 track
.assign(audioTrack
->component());
67 WebSpeechRecognitionParams
params(webSpeechGrammars
, lang
, serviceURI
, continuous
, interimResults
, maxAlternatives
, track
, WebSecurityOrigin(recognition
->executionContext()->securityOrigin()));
68 m_recognizer
->start(recognition
, params
, this);
71 void SpeechRecognitionClientProxy::stop(SpeechRecognition
* recognition
)
73 m_recognizer
->stop(recognition
, this);
76 void SpeechRecognitionClientProxy::abort(SpeechRecognition
* recognition
)
78 m_recognizer
->abort(recognition
, this);
81 void SpeechRecognitionClientProxy::didStartAudio(const WebSpeechRecognitionHandle
& handle
)
83 SpeechRecognition
* recognition(handle
);
84 recognition
->didStartAudio();
87 void SpeechRecognitionClientProxy::didStartSound(const WebSpeechRecognitionHandle
& handle
)
89 SpeechRecognition
* recognition(handle
);
90 recognition
->didStartSound();
91 recognition
->didStartSpeech();
94 void SpeechRecognitionClientProxy::didEndSound(const WebSpeechRecognitionHandle
& handle
)
96 SpeechRecognition
* recognition(handle
);
97 recognition
->didEndSpeech();
98 recognition
->didEndSound();
101 void SpeechRecognitionClientProxy::didEndAudio(const WebSpeechRecognitionHandle
& handle
)
103 SpeechRecognition
* recognition(handle
);
104 recognition
->didEndAudio();
107 void SpeechRecognitionClientProxy::didReceiveResults(const WebSpeechRecognitionHandle
& handle
, const WebVector
<WebSpeechRecognitionResult
>& newFinalResults
, const WebVector
<WebSpeechRecognitionResult
>& currentInterimResults
)
109 SpeechRecognition
* recognition(handle
);
111 HeapVector
<Member
<SpeechRecognitionResult
>> finalResultsVector(newFinalResults
.size());
112 for (size_t i
= 0; i
< newFinalResults
.size(); ++i
)
113 finalResultsVector
[i
] = Member
<SpeechRecognitionResult
>(newFinalResults
[i
]);
115 HeapVector
<Member
<SpeechRecognitionResult
>> interimResultsVector(currentInterimResults
.size());
116 for (size_t i
= 0; i
< currentInterimResults
.size(); ++i
)
117 interimResultsVector
[i
] = Member
<SpeechRecognitionResult
>(currentInterimResults
[i
]);
119 recognition
->didReceiveResults(finalResultsVector
, interimResultsVector
);
122 void SpeechRecognitionClientProxy::didReceiveNoMatch(const WebSpeechRecognitionHandle
& handle
, const WebSpeechRecognitionResult
& result
)
124 SpeechRecognition
* recognition(handle
);
125 recognition
->didReceiveNoMatch(result
);
128 void SpeechRecognitionClientProxy::didReceiveError(const WebSpeechRecognitionHandle
& handle
, const WebString
& message
, WebSpeechRecognizerClient::ErrorCode code
)
130 SpeechRecognition
* recognition(handle
);
131 SpeechRecognitionError::ErrorCode errorCode
= static_cast<SpeechRecognitionError::ErrorCode
>(code
);
132 recognition
->didReceiveError(SpeechRecognitionError::create(errorCode
, message
));
135 void SpeechRecognitionClientProxy::didStart(const WebSpeechRecognitionHandle
& handle
)
137 SpeechRecognition
* recognition(handle
);
138 recognition
->didStart();
141 void SpeechRecognitionClientProxy::didEnd(const WebSpeechRecognitionHandle
& handle
)
143 SpeechRecognition
* recognition(handle
);
144 recognition
->didEnd();
147 SpeechRecognitionClientProxy::SpeechRecognitionClientProxy(WebSpeechRecognizer
* recognizer
)
148 : m_recognizer(recognizer
)