[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / Tizen.Uix.SttEngine / Tizen.Uix.SttEngine / SttEngine.cs
1 /*
2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 using System;
18 using System.Runtime.InteropServices;
19 using static Interop.SttEngine;
20
21 namespace Tizen.Uix.SttEngine
22 {
23     /// <summary>
24     /// Enumeration for the audio types.
25     /// </summary>
26     /// <since_tizen> 4 </since_tizen>
27     public enum AudioType
28     {
29         /// <summary>
30         /// Signed 16-bit audio type, little endian.
31         /// </summary>
32         PcmS16Le = 0,
33         /// <summary>
34         /// Unsigned 8-bit audio type.
35         /// </summary>
36         PcmU8
37     };
38
39     /// <summary>
40     /// Enumeration for the results.
41     /// </summary>
42     /// <since_tizen> 4 </since_tizen>
43     public enum ResultEvent
44     {
45         /// <summary>
46         /// Event when either the full matched or the final result is delivered.
47         /// </summary>
48         FinalResult = 0,
49         /// <summary>
50         /// Event when the partial matched result is delivered.
51         /// </summary>
52         PartialResult,
53         /// <summary>
54         /// Event when the recognition has failed.
55         /// </summary>
56         Error
57     };
58
59     /// <summary>
60     /// Enumeration for the result time.
61     /// </summary>
62     /// <since_tizen> 4 </since_tizen>
63     public enum TimeEvent
64     {
65         /// <summary>
66         /// Event when the token is beginning type.
67         /// </summary>
68         Beginning = 0,
69         /// <summary>
70         /// Event when the token is middle type.
71         /// </summary>
72         Middle = 1,
73         /// <summary>
74         /// Event when the token is end type.
75         /// </summary>
76         End = 2
77     };
78
79     /// <summary>
80     /// Enumeration for the speech status.
81     /// </summary>
82     /// <since_tizen> 4 </since_tizen>
83     public enum SpeechStatus
84     {
85         /// <summary>
86         /// Beginning point of the speech is detected.
87         /// </summary>
88         BeginningPointDetected = 0,
89         /// <summary>
90         /// End point of the speech is detected.
91         /// </summary>
92         EndPointDetected
93     };
94
95     /// <summary>
96     /// Enumeration for representing the result message.
97     /// </summary>
98     /// <since_tizen> 4 </since_tizen>
99     public enum ResultMessage
100     {
101         /// <summary>
102         /// No error.
103         /// </summary>
104         None,
105         /// <summary>
106         /// Recognition failed because the speech started too soon.
107         /// </summary>
108         TooSoon,
109         /// <summary>
110         /// Recognition failed because the speech is too short.
111         /// </summary>
112         TooShort,
113         /// <summary>
114         /// Recognition failed because the speech is too long.
115         /// </summary>
116         TooLong,
117         /// <summary>
118         /// Recognition failed because the speech is too quiet to listen.
119         /// </summary>
120         TooQuiet,
121         /// <summary>
122         /// Recognition failed because the speech is too loud to listen.
123         /// </summary>
124         TooLoud,
125         /// <summary>
126         /// Recognition failed because the speech is too fast to listen.
127         /// </summary>
128         TooFast
129     };
130
131
132     /// <summary>
133     /// Enumeration for the error values that can occur.
134     /// </summary>
135     /// <since_tizen> 4 </since_tizen>
136     public enum Error
137     {
138         /// <summary>
139         /// Successful, no error.
140         /// </summary>
141         None = ErrorCode.None,
142         /// <summary>
143         /// Out of memory.
144         /// </summary>
145         OutOfMemory = ErrorCode.OutOfMemory,
146         /// <summary>
147         /// I/O error.
148         /// </summary>
149         IoError = ErrorCode.IoError,
150         /// <summary>
151         /// Invalid parameter.
152         /// </summary>
153         InvalidParameter = ErrorCode.InvalidParameter,
154         /// <summary>
155         /// Network down (Out of network).
156         /// </summary>
157         NetworkDown = ErrorCode.NetworkDown,
158         /// <summary>
159         /// Invalid state.
160         /// </summary>
161         InvalidState = ErrorCode.InvalidState,
162         /// <summary>
163         /// Invalid language.
164         /// </summary>
165         InvalidLanguage = ErrorCode.InvalidLanguage,
166         /// <summary>
167         /// Operation failed.
168         /// </summary>
169         OperationFailed = ErrorCode.OperationFailed,
170         /// <summary>
171         /// Not supported feature of the current engine.
172         /// </summary>
173         NotSupportedFeature = ErrorCode.NotSupportedFeature,
174         /// <summary>
175         /// Not supported.
176         /// </summary>
177         NotSupported = ErrorCode.NotSupported,
178         /// <summary>
179         /// Permission denied.
180         /// </summary>
181         PermissionDenied = ErrorCode.PermissionDenied,
182         /// <summary>
183         /// Recording timed out.
184         /// </summary>
185         RecordingTimedOut = ErrorCode.RecordingTimedOut
186     };
187
188     /// <summary>
189     /// This class represents the STT Engine, which has to be inherited to make the engine.
190     /// </summary>
191     /// <since_tizen> 4 </since_tizen>
192     public abstract class Engine
193     {
194         private CallbackStructGCHandle _callbackStructGCHandle = new CallbackStructGCHandle();
195         private PrivateDataSetCb _privateDataSetCb;
196         private Action<string> _privateDatacallback;
197         private PrivateDataRequestedCb _privateDataRequestedCb;
198         private OutAction<string> _privateDataRequestedCallback;
199         private static Engine _engine;
200         private IntPtr _structIntPtrHandle;
201
202         /// <summary>
203         /// An action with 2 input parameters returning an error.
204         /// </summary>
205         /// <typeparam name="T">Generic type for parameter 1.</typeparam>
206         /// <param name="a">The input parameter 1.</param>
207         /// <param name="b">The input parameter 2.</param>
208         /// <returns>Error value.</returns>
209         /// <since_tizen> 4 </since_tizen>
210         public delegate Error Action<T>(T a, T b);
211
212         /// <summary>
213         /// An action with 2 out parameters returning an error.
214         /// </summary>
215         /// <typeparam name="T">Generic type for parameter 1.</typeparam>
216         /// <param name="a">The input parameter 1.</param>
217         /// <param name="b">The input parameter 2.</param>
218         /// <returns>Error value.</returns>
219         /// <since_tizen> 4 </since_tizen>
220         public delegate Error OutAction<T>(T a, out T b);
221
222         /// <summary>
223         /// Called when the STT engine provides the time stamp of result to the engine service user.
224         /// This callback function is implemented by the engine service user. Therefore, the engine developer does NOT have to implement this callback function.
225         /// </summary>
226         /// <param name="index">The result index.</param>
227         /// <param name="resultEvent">The token event.</param>
228         /// <param name="text">The result text.</param>
229         /// <param name="startTime">The time started speaking the result text.</param>
230         /// <param name="endTime">The time finished speaking the result text.</param>
231         /// <param name="userData">The user data.</param>
232         /// <returns>true to continue with the next iteration of the loop, false to break out of the loop.</returns>
233         /// <precondition>SendResult() should be called.</precondition>
234         /// <since_tizen> 4 </since_tizen>
235         public delegate bool ResultTime(int index, TimeEvent resultEvent, string text, long startTime, long endTime, IntPtr userData);
236
237         /// <summary>
238         /// Called when the STT engine informs the engine service user about the whole supported language list.
239         /// This callback function is implemented by the engine service user. Therefore, the engine developer does NOT have to implement this callback function.
240         /// </summary>
241         /// <param name="language">The language is specified as an ISO 3166 alpha-2 two letter country-code
242         /// followed by an ISO 639-1 for the two-letter language code.
243         /// For example, "ko_KR" for Korean, "en_US" for American English.</param>
244         /// <param name="userData">The user data.</param>
245         /// <returns>true to continue with the next iteration of the loop, false to break out of the loop.</returns>
246         /// <precondition>ForEachSupportedLanguages() should be called.</precondition>
247         /// <since_tizen> 4 </since_tizen>
248         public delegate bool SupportedLanguages(string language, IntPtr userData);
249
250         /// <summary>
251         /// Called when the engine service user requests the basic information of the STT engine.
252         /// </summary>
253         /// <remarks>
254         /// In order to upload the engine at Tizen Appstore, both a service application and a UI application are necessary. Therefore, engineSetting must be transferred to the engine service user.
255         /// </remarks>
256         /// <param name="engineUuid">UUID of the engine.</param>
257         /// <param name="engineName">Name of the engine.</param>
258         /// <param name="engineSetting">The engine setting application (UI app)'s ID.</param>
259         /// <param name="useNetwork">A variable for checking whether the network is used or not.</param>
260         /// <returns>
261         /// The following error codes can be returned:
262         /// 1. None
263         /// 2. OperationFailed
264         /// 3. InvalidParameter
265         /// </returns>
266         /// <since_tizen> 4 </since_tizen>
267         public abstract Error GetInformation(out string engineUuid, out string engineName, out string engineSetting, out bool useNetwork);
268
269         /// <summary>
270         /// Called when the engine service user initializes the STT engine.
271         /// This callback function is called by the engine service user to request for the STT engine to be started.
272         /// </summary>
273         /// <returns>
274         /// The following error codes can be returned:
275         /// 1. None
276         /// 2. InvalidParameter
277         /// 3. InvalidState
278         /// 4. OperationFailed
279         /// </returns>
280         /// <since_tizen> 4 </since_tizen>
281         public abstract Error Initialize();
282
283         /// <summary>
284         /// Called when the engine service user deinitializes the STT engine.
285         /// This callback function is called by the engine service user to request for the STT engine to be deinitialized.
286         /// </summary>
287         /// <returns>
288         /// The following error codes can be returned:
289         /// 1. None
290         /// 2. InvalidState
291         /// </returns>
292         /// <since_tizen> 4 </since_tizen>
293         public abstract Error Deinitialize();
294
295         /// <summary>
296         /// Called when the engine service user gets the whole supported language list.
297         /// </summary>
298         /// <remarks>
299         /// In this function, the engine service user's callback function 'SupportedLanguages' is invoked repeatedly for getting all the supported languages
300         /// and user_data must be transferred to 'SupportedLanguages'. If 'SupportedLanguages' returns false, it should be stopped to call 'SupportedLanguages'.
301         /// </remarks>
302         /// <param name="callback">The callback function,</param>
303         /// <param name="userData">The user data, which must be passed to the callback delegate 'SupportedLanguages'.</param>
304         /// <returns>
305         /// The following error codes can be returned:
306         /// 1. None
307         /// 2. InvalidState
308         /// 3. InvalidParameter
309         /// </returns>
310         /// <postcondition>
311         /// This callback function invokes SupportedLanguages repeatedly for getting the supported languages.
312         /// </postcondition>
313         /// <since_tizen> 4 </since_tizen>
314         public abstract Error ForEachSupportedLanguages(SupportedLanguages callback, IntPtr userData);
315
316         /// <summary>
317         /// Called when the engine service user checks whether the corresponding language is valid or not in the STT engine.
318         /// </summary>
319         /// <param name="language">The language is specified as an ISO 3166 alpha-2 two letter country-code followed by an ISO 639-1 for the two-letter language code.
320         /// For example, "ko_KR" for Korean, "en_US" for American English.</param>
321         /// <param name="isValid">A variable for checking whether the corresponding language is valid or not. true to be valid, false to be invalid.</param>
322         /// <returns>
323         /// The following error codes can be returned:
324         /// 1. None
325         /// 2. InvalidParameter
326         /// </returns>
327         /// <since_tizen> 4 </since_tizen>
328         public abstract Error IsValidLanguage(string language, out bool isValid);
329
330         /// <summary>
331         /// Called when the engine service user checks whether the STT engine supports silence detection.
332         /// </summary>
333         /// <returns>true to support silence detection, false not to support silence detection.</returns>
334         /// <since_tizen> 4 </since_tizen>
335         public abstract bool SupportSilenceDetection();
336
337         /// <summary>
338         /// Called when the engine service user checks whether the STT engine supports the corresponding recognition type.
339         /// </summary>
340         /// <param name="type">The type for recognition, "stt.recognition.type.FREE", or "stt.recognition.type.FREE.PARTIAL".</param>
341         /// <param name="isSupported">A variable for checking whether the STT engine supports the corresponding recognition type.
342         /// true to support the recognition type, false not to support the recognition type.</param>
343         /// <returns>
344         /// The following error codes can be returned:
345         /// 1. None
346         /// 2. InvalidParameter
347         /// </returns>
348         /// <since_tizen> 4 </since_tizen>
349         public abstract Error SupportRecognitionType(string type, out bool isSupported);
350
351         /// <summary>
352         /// Called when the engine service user gets the proper recording format of the STT engine.
353         /// The recording format is used for creating the recorder.
354         /// </summary>
355         /// <param name="types">The format used by the recorder.</param>
356         /// <param name="rate">The sample rate used by the recorder.</param>
357         /// <param name="channels">The number of channels used by the recorder.</param>
358         /// <returns>
359         /// The following error codes can be returned:
360         /// 1. None
361         /// 2. InvalidState
362         /// </returns>
363         /// <since_tizen> 4 </since_tizen>
364         public abstract Error GetRecordingFormat(out AudioType types, out int rate, out int channels);
365
366         /// <summary>
367         /// Called when the engine service user sets the silence detection.
368         /// If the engine service user sets this option as 'TRUE', the STT engine will detect the silence (EPD) and send the callback event about it.
369         /// </summary>
370         /// <param name="isSet">A variable for setting the silence detection. true to detect the silence, false not to detect the silence.</param>
371         /// <returns>
372         /// The following error codes can be returned:
373         /// 1. None
374         /// 2. InvalidState
375         /// 3. NotSupportedFeature
376         /// </returns>
377         /// <since_tizen> 4 </since_tizen>
378         public abstract Error SetSilenceDetection(bool isSet);
379
380         /// <summary>
381         /// Called when the engine service user requests for the STT engine to check whether the application agreed the usage of the STT engine.
382         /// This callback function is called when the engine service user requests for the STT engine to check the application's agreement about using the engine.
383         /// According to the need, the engine developer can provide some user interfaces to check the agreement.
384         /// </summary>
385         /// <param name="appid">The Application ID</param>
386         /// <param name="isAgreed">A variable for checking whether the application agreed to use the STT engine or not. true to agree, false to disagree.</param>
387         /// <returns>
388         /// The following error codes can be returned:
389         /// 1. None
390         /// 2. InvalidState
391         /// 3. NotSupportedFeature
392         /// </returns>
393         /// <since_tizen> 4 </since_tizen>
394         public abstract Error CheckAppAgreed(string appid, out bool isAgreed);
395
396         /// <summary>
397         /// Called when the engine service user checks whether STT engine needs the application's credential.
398         /// </summary>
399         /// <returns>true if the STT engine needs the application's credential, otherwise false.</returns>
400         /// <since_tizen> 4 </since_tizen>
401         public abstract bool NeedAppCredential();
402
403         /// <summary>
404         /// Called when the engine service user gets the result time information(stamp).
405         /// </summary>
406         /// <remarks>
407         /// In this function, the engine service user's callback delegate 'ResultTime' is invoked repeatedly for sending the time information to the engine service user
408         /// and user_data must be transferred to 'ResultTime'. If 'ResultTime' returns false, it should be stopped to call 'ResultTime'.
409         /// timeInfo is transferred from SendResult. The type of timeInfo is up to the STT engine developer.
410         /// </remarks>
411         /// <param name="timeInfo">The time information.</param>
412         /// <param name="callback">The callback function.</param>
413         /// <param name="userData">The user data, which must be passed to the callback function ResultTime.</param>
414         /// <returns>
415         /// The following error codes can be returned:
416         /// 1. None
417         /// 2. InvalidState
418         /// 3. InvalidParameter
419         /// </returns>
420         /// <precondition>
421         /// SendResult will invoke this function.
422         /// </precondition>
423         /// <postcondition>
424         /// This function invokes the ResultTime repeatedly for getting the result time information.
425         /// </postcondition>
426         /// <since_tizen> 4 </since_tizen>
427         public abstract Error ForEachResultTime(IntPtr timeInfo, ResultTime callback, IntPtr userData);
428
429         /// <summary>
430         /// Called when the engine service user starts to recognize the recording data.
431         /// In this callback function, the STT engine must transfer the recognition result and userData to the engine service user using SendResult().
432         /// Also, if the STT engine needs the application's credential, it sets the credential granted to the application.
433         /// </summary>
434         /// <param name="language">The language is specified as an ISO 3166 alpha-2 two letter country-code followed by an ISO 639-1 for the two-letter language code.
435         /// For example, "ko_KR" for Korean, "en_US" for American English.</param>
436         /// <param name="type">The recognition type, "stt.recognition.type.FREE", or "stt.recognition.type.FREE.PARTIAL".</param>
437         /// <param name="appid">The application ID.</param>
438         /// <param name="credential">The credential granted to the application.</param>
439         /// <param name="userData">The user data to be passed to the callback function.</param>
440         /// <returns>
441         /// The following error codes can be returned:
442         /// 1. None
443         /// 2. InvalidState
444         /// 3. InvalidParameter
445         /// 4. InvalidLanguage
446         /// 5. OperationFailed
447         /// 6. NetworkDown
448         /// </returns>
449         /// <precondition>
450         /// The engine is not in recognition processing.
451         /// </precondition>
452         /// <since_tizen> 4 </since_tizen>
453         public abstract Error Start(string language, string type, string appid, string credential, IntPtr userData);
454
455         /// <summary>
456         /// Called when the engine service user sets and sends the recording data for speech recognition.
457         /// This callback function is called by the engine service user to send the recording data to the STT engine. The engine receives the recording data and uses for speech recognition. 
458         /// This function should be returned immediately after recording data copy.
459         /// </summary>
460         /// <param name="data">The recording data.</param>
461         /// <param name="length">The length of the recording data.</param>
462         /// <returns>
463         /// The following error codes can be returned:
464         /// 1. None
465         /// 2. InvalidState
466         /// 3. InvalidParameter
467         /// 4. OperationFailed
468         /// </returns>
469         /// <precondition>
470         /// Start should succeed.</precondition>
471         /// <postcondition>
472         /// If the engine supports partial result, SendResult() should be invoked.</postcondition>
473         /// <since_tizen> 4 </since_tizen>
474         public abstract Error SetRecordingData(string data, uint length);
475
476         /// <summary>
477         /// Called when the engine service user stops to recognize the recording data.
478         /// This callback function is called by the engine service user to stop recording and to get the recognition result.
479         /// </summary>
480         /// <returns>The following error codes can be returned:
481         /// 1. None
482         /// 2. InvalidState
483         /// 3. OperationFailed
484         /// 4. NetworkDown
485         /// </returns>
486         /// <precondition>
487         /// Start should succeed.</precondition>
488         /// <postcondition>
489         /// After processing of the engine, SendResult() should be invoked.</postcondition>
490         /// <since_tizen> 4 </since_tizen>
491         public abstract Error Stop();
492
493         /// <summary>
494         /// Called when the engine service user cancels to recognize the recording data.
495         /// This callback function is called by the engine service user to cancel to recognize the recording data. Also, when starting the recorder is failed, this function is called.
496         /// </summary>
497         /// <returns>
498         /// The following error codes can be returned:
499         /// 1. None
500         /// 2. InvalidState
501         /// </returns>
502         /// <precondition>The STT engine is in recognition processing or recording.</precondition>
503         /// <since_tizen> 4 </since_tizen>
504         public abstract Error Cancel();
505
506         /// <summary>
507         /// Public constructor.
508         /// </summary>
509         /// <feature>
510         /// http://tizen.org/feature/speech.recognition
511         /// http://tizen.org/feature/microphone
512         /// </feature>
513         /// <since_tizen> 4 </since_tizen>
514         public Engine()
515         {
516             _engine = this;
517         }
518
519         /// <summary>
520         /// Main function for the Speech-To-Text (STT) engine.
521         /// This function is the main function for operating the STT engine.
522         /// </summary>
523         /// <privilege>
524         /// http://tizen.org/privilege/recorder
525         /// </privilege>
526         /// <feature>
527         /// http://tizen.org/feature/speech.recognition
528         /// http://tizen.org/feature/microphone
529         /// </feature>
530         /// <remarks>
531         /// ServiceAppMain should be used for working the engine after this function.
532         /// </remarks>
533         /// <param name="argc">The number of arguments.</param>
534         /// <param name="argv">The arguments array.</param>
535         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
536         /// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
537         /// <exception cref="InvalidOperationException">Thrown in case of an operation failure.</exception>
538         /// <since_tizen> 4 </since_tizen>
539         public void EngineMain(int argc, string[] argv)
540         {
541             _callbackStructGCHandle.CallbackStruct.version = 1;
542             _callbackStructGCHandle.CallbackStruct.getInfo = _getInfoCb;
543             _callbackStructGCHandle.CallbackStruct.initialize = Initialize;
544             _callbackStructGCHandle.CallbackStruct.deinitialize = _deinitializeCb;
545             _callbackStructGCHandle.CallbackStruct.supportedLanaguge = ForEachSupportedLanguages;
546             _callbackStructGCHandle.CallbackStruct.validLanaguage = _isValidLanguageCb;
547             _callbackStructGCHandle.CallbackStruct.silence = SupportSilenceDetection;
548             _callbackStructGCHandle.CallbackStruct.recognitionType = _supportRecognitionTypeCb;
549             _callbackStructGCHandle.CallbackStruct.recordingFormat = GetRecordingFormat;
550             _callbackStructGCHandle.CallbackStruct.resultTime = ForEachResultTime;
551             _callbackStructGCHandle.CallbackStruct.silenceDetection = SetSilenceDetection;
552             _callbackStructGCHandle.CallbackStruct.start = Start;
553             _callbackStructGCHandle.CallbackStruct.recordingData = SetRecordingData;
554             _callbackStructGCHandle.CallbackStruct.stop = Stop;
555             _callbackStructGCHandle.CallbackStruct.cancel = Cancel;
556             _callbackStructGCHandle.CallbackStruct.checkAppAgreed = _checkAppAgreedCb;
557             _callbackStructGCHandle.CallbackStruct.needAppCredential = NeedAppCredential;
558             _structIntPtrHandle = Marshal.AllocHGlobal(Marshal.SizeOf(_callbackStructGCHandle.CallbackStruct));
559             Marshal.StructureToPtr<RequestCallbackStruct>(_callbackStructGCHandle.CallbackStruct, _structIntPtrHandle, false);
560             Error error = STTEMain(argc, argv, _structIntPtrHandle);
561             if (error != Error.None)
562             {
563                 Log.Error(LogTag, "STTEMain Failed with error " + error);
564                 throw ExceptionFactory.CreateException((ErrorCode)error);
565             }
566
567             Log.Info(LogTag, "After STTEMain");
568         }
569
570         /// <summary>
571         /// Sends the recognition result to the engine service user.
572         /// </summary>
573         /// <feature>
574         /// http://tizen.org/feature/speech.recognition
575         /// http://tizen.org/feature/microphone
576         /// </feature>
577         /// <remarks>
578         /// This API is used in SetRecordingData() and Stop(), when the STT engine sends the recognition result to the engine service user.
579         /// This function is called in the following situations: 1) After Stop() is called, 2) The end point of speech is detected from recording, or 3) Partial result is occurred.
580         /// The recognition result must be transferred to the engine service user through this function. Also, the timeInfo must be transferred to ForEachResultTime().
581         /// The type of timeInfo is up to the STT engine developer.
582         /// </remarks>
583         /// <param name="resultEvent">The result event.</param>
584         /// <param name="type">The recognition type, "stt.recognition.type.FREE", or "stt.recognition.type.FREE.PARTIAL".</param>
585         /// <param name="result">Result texts.</param>
586         /// <param name="resultCount">Result text count.</param>
587         /// <param name="msg">Engine message.</param>
588         /// <param name="timeInfo">The time information.</param>
589         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
590         /// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
591         /// <exception cref="InvalidOperationException">Thrown in case of an operation failure.</exception>
592         /// <precondition>
593         /// The EngineMain function should be invoked before this function is called. Stop will invoke this function.
594         /// </precondition>
595         /// <postcondition>
596         /// This function invokes ForEachResultTime
597         /// </postcondition>
598         /// <since_tizen> 4 </since_tizen>
599         public void SendResult(ResultEvent resultEvent, string type, string[] result, int resultCount, ResultMessage msg, IntPtr timeInfo)
600         {
601             if ((result != null) && (result.Length != 0))
602             {
603
604                 string message = "stt.result.message.none";
605                 switch (msg)
606                 {
607                     case ResultMessage.None:
608                     message = "stt.result.message.none";
609                     break;
610
611                     case ResultMessage.TooFast:
612                     message = "stt.result.message.error.too.fast";
613                     break;
614
615                     case ResultMessage.TooLong:
616                     message = "stt.result.message.error.too.long";
617                     break;
618
619                     case ResultMessage.TooLoud:
620                     message = "stt.result.message.error.too.loud";
621                     break;
622
623                     case ResultMessage.TooQuiet:
624                     message = "stt.result.message.error.too.quiet";
625                     break;
626
627                     case ResultMessage.TooShort:
628                     message = "stt.result.message.error.too.short";
629                     break;
630
631                     case ResultMessage.TooSoon:
632                     message = "stt.result.message.error.too.soon";
633                     break;
634                 }
635
636                 Error error = STTESendResult(resultEvent, type, result, resultCount, message, timeInfo, IntPtr.Zero);
637                 if (error != Error.None)
638                 {
639                     Log.Error(LogTag, "STTESendResult Failed with error " + error);
640                     throw ExceptionFactory.CreateException((ErrorCode)error);
641                 }
642
643             }
644             else
645             {
646                 throw new ArgumentNullException("result", "is null or empty");
647             }
648         }
649
650         /// <summary>
651         /// Sends the error to the engine service user.
652         /// </summary>
653         /// <feature>
654         /// http://tizen.org/feature/speech.recognition
655         /// http://tizen.org/feature/microphone
656         /// </feature>
657         /// <param name="error">The error reason.</param>
658         /// <param name="msg">The error message.</param>
659         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
660         /// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
661         /// <exception cref="InvalidOperationException">Thrown in case of an operation failure.</exception>
662         /// <precondition>
663         /// The main function should be invoked before this function is called.
664         /// </precondition>
665         /// <since_tizen> 4 </since_tizen>
666         public void SendError(Error error, string msg)
667         {
668             Error err = STTESendError(error, msg);
669             if (err != Error.None)
670             {
671                 Log.Error(LogTag, "SendError Failed with error " + err);
672                 throw ExceptionFactory.CreateException((ErrorCode)error);
673             }
674         }
675
676         /// <summary>
677         /// Sends the speech status to the engine service user when the STT engine notifies the change of the speech status.
678         /// </summary>
679         /// <feature>
680         /// http://tizen.org/feature/speech.recognition
681         /// http://tizen.org/feature/microphone
682         /// </feature>
683         /// <remarks>
684         /// This API is invoked when the STT engine wants to notify the change of the speech status anytime. Note that this API can be invoked for recognizing the speech.
685         /// </remarks>
686         /// <param name="status">SpeechStatus</param>
687         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
688         /// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
689         /// <exception cref="InvalidOperationException">Thrown in case of an operation failure.</exception>
690         /// <precondition>
691         /// The main function should be invoked before this function is called. The Start() and SetRecordingData() will invoke this function.
692         /// </precondition>
693         /// <since_tizen> 4 </since_tizen>
694         public void SendSpeechStatus(SpeechStatus status)
695         {
696             Error error = STTESendSpeechStatus(status, IntPtr.Zero);
697             if (error != Error.None)
698             {
699                 Log.Error(LogTag, "SendSpeechStatus Failed with error " + error);
700                 throw ExceptionFactory.CreateException((ErrorCode)error);
701             }
702
703         }
704
705         /// <summary>
706         /// Sets a callback function for setting the private data.
707         /// </summary>
708         /// <privilege>
709         /// http://tizen.org/privilege/recorder
710         /// </privilege>
711         /// <feature>
712         /// http://tizen.org/feature/speech.recognition
713         /// http://tizen.org/feature/microphone
714         /// </feature>
715         /// <param name="callback">
716         /// Called when the STT engine receives the private data from the engine service user.
717         /// This callback function is called when the engine service user sends the private data to the STT engine.
718         /// In Parameters:
719         /// a = Key -- The key field of private data
720         /// b = data -- The data field of private data
721         /// The following error codes can be returned:
722         /// 1. None
723         /// 2. InvalidParameter
724         /// 3. OperationFailed
725         /// </param>
726         /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
727         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
728         /// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
729         /// <exception cref="InvalidOperationException">Thrown in case of an operation failure.</exception>
730         /// <precondition>
731         /// The main function should be invoked before this function is called.
732         /// </precondition>
733         /// <since_tizen> 4 </since_tizen>
734         public void SetPrivateDataSetDelegate(Action<string> callback)
735         {
736             if (null == callback)
737             {
738                 Log.Error(LogTag, "callback is null");
739                 throw ExceptionFactory.CreateException(ErrorCode.InvalidParameter);
740             }
741
742             _privateDatacallback = callback;
743             _privateDataSetCb = (string key, string data) =>
744             {
745                 return _privateDatacallback.Invoke(key, data);
746             };
747             Error error = STTESetPrivateDataSetCb(_privateDataSetCb);
748             if (error != Error.None)
749             {
750                 Log.Error(LogTag, "SetPrivateDataSetDelegate Failed with error " + error);
751                 throw ExceptionFactory.CreateException((ErrorCode)error);
752             }
753
754         }
755
756         /// <summary>
757         /// Sets a callback delegate for requesting the private data.
758         /// </summary>
759         /// <privilege>
760         /// http://tizen.org/privilege/recorder
761         /// </privilege>
762         /// <feature>
763         /// http://tizen.org/feature/speech.recognition
764         /// http://tizen.org/feature/microphone
765         /// </feature>
766         /// <param name="callback">The callback function.
767         /// Called when the STT engine provides the engine service user with the private data.
768         /// This callback function is called when the engine service user gets the private data from the STT engine.
769         /// Out parameters:
770         /// a = Key -- The key field of private data
771         /// b = data -- The data field of private data
772         /// The following error codes can be returned:
773         /// 1. None
774         /// 2. InvalidParameter
775         /// 3. OperationFailed
776         /// </param>
777         /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
778         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
779         /// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
780         /// <exception cref="InvalidOperationException">Thrown in case of an operation failure.</exception>
781         /// <precondition>
782         /// The main function should be invoked before this function is called.
783         /// </precondition>
784         /// <since_tizen> 4 </since_tizen>
785         public void SetPrivateDataRequestedDelegate(OutAction<string> callback)
786         {
787             if (null == callback)
788             {
789                 Log.Error(LogTag, "callback is null");
790                 throw ExceptionFactory.CreateException(ErrorCode.InvalidParameter);
791             }
792
793             _privateDataRequestedCallback = callback;
794             _privateDataRequestedCb = (string key, out string data) =>
795             {
796                 return _privateDataRequestedCallback.Invoke(key, out data);
797             };
798             Error error = STTESetPrivateDataRequestedCb(_privateDataRequestedCb);
799             if (error != Error.None)
800             {
801                 Log.Error(LogTag, "SetPrivateDataRequestedDelegate Failed with error " + error);
802                 throw ExceptionFactory.CreateException((ErrorCode)error);
803             }
804
805         }
806
807         private IsValidLanguageCb _isValidLanguageCb = (string langauge, out byte isValid) =>
808         {
809             bool valid;
810             Error err = _engine.IsValidLanguage(langauge, out valid);
811             if (true == valid)
812             {
813                 isValid = 1;
814             }
815             else
816             {
817                 isValid = 0;
818             }
819
820             return err;
821         };
822         
823         private CheckAppAgreedCb _checkAppAgreedCb = (string appid, out byte isAgreed) =>
824         {
825             bool agreed;
826             Error err = _engine.CheckAppAgreed(appid, out agreed);
827             if (true == agreed)
828             {
829                 isAgreed = 1;
830             }
831             else
832             {
833                 isAgreed = 0;
834             }
835
836             return err;
837         };
838
839         private SupportRecognitionTypeCb _supportRecognitionTypeCb = (string type, out byte isSupported) =>
840         {
841             bool supported;
842             Error err = _engine.SupportRecognitionType(type, out supported);
843             if (true == supported)
844             {
845                 isSupported = 1;
846             }
847             else
848             {
849                 isSupported = 0;
850             }
851
852             return err;
853         };
854
855         private GetInfoCb _getInfoCb = (out string engineUuid, out string engineName, out string engineSetting, out byte useNetwork) =>
856         {
857             bool network;
858
859             Error err = _engine.GetInformation(out engineUuid, out engineName, out engineSetting, out network);
860             if (true == network)
861             {
862                 useNetwork = 1;
863             }
864             else
865             {
866                 useNetwork = 0;
867             }
868
869             return err;
870         };
871
872         private DeinitializeCb _deinitializeCb = () =>
873         {
874             Marshal.FreeHGlobal(_engine._structIntPtrHandle);
875             return _engine.Deinitialize();
876         };
877     }
878 }