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