Blame view

packages/chivox_aiengine/ios/Classes/ChivoxAienginePlugin.m 21.8 KB
0fa4e405   吴启风   feat: 将语音评测从先声迁移至驰声
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
  #import "ChivoxAienginePlugin.h"
  #include "CAIEngine/CAIEngine.h"
  
  // 经过实际测试,有以下结论:
  // 1. dart和oc代码运行在不同线程,oc代码中阻塞并不会导致dart阻塞
  // 2. 每次dart调用oc代码,都会派发到oc主线程调用,并且是串行方式。
  
  @implementation ChivoxAienginePlugin
  {
      NSMutableDictionary *engineDic;
      FlutterMethodChannel *callbackChannel;
      NSObject<FlutterPluginRegistrar> *registrar;
      
  }
  
  + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
    FlutterMethodChannel* channel = [FlutterMethodChannel
        methodChannelWithName:@"com.chivox.aiengine/call"
              binaryMessenger:[registrar messenger]];
      ChivoxAienginePlugin* instance = [[ChivoxAienginePlugin alloc] initWithRegistrar:registrar];
    [registrar addMethodCallDelegate:instance channel:channel];
  }
  
  - (instancetype) init {
      if (self = [super init]) {
          self->engineDic = [NSMutableDictionary dictionary];
      }
      return self;
  }
  
  - (instancetype) initWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
      if (self = [self init]) {
          self->callbackChannel = [FlutterMethodChannel methodChannelWithName:@"com.chivox.aiengine/callback" binaryMessenger:[registrar messenger]];
          self->registrar = registrar;
      }
      return self;
  }
  
  - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    if ([@"getPlatformVersion" isEqualToString:call.method]) {
      result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
    }
    else if ([@"aiengineNew" isEqualToString:call.method]) {
        [self handle_aiengineNew:call result:result];
    }
    else if ([@"aiengineDelete" isEqualToString:call.method]) {
        [self handle_aiengineDelete:call result:result];
    }
    else if ([@"aiengineStart" isEqualToString:call.method]) {
        [self handle_aiengineStart:call result:result];
    }
    else if ([@"aiengineFeed" isEqualToString:call.method]) {
        [self handle_aiengineFeed:call result:result];
    }
    else if ([@"aiengineStop" isEqualToString:call.method]) {
        [self handle_aiengineStop:call result:result];
    }
    else if ([@"aiengineCancel" isEqualToString:call.method]) {
        [self handle_aiengineCancel:call result:result];
    }
    else if ([@"getDeviceId" isEqualToString:call.method]) {
        [self handle_getDeviceId:call result:result];
    }
    else if ([@"getSerialNumber" isEqualToString:call.method]) {
        [self handle_getSerialNumber:call result:result];
    }
    else if ([@"clearSavedSerialNumber" isEqualToString:call.method]) {
        [self handle_clearSavedSerialNumber:call result:result];
    }
    else if ([@"getProvision" isEqualToString:call.method]) {
        [self handle_getProvision:call result:result];
    }
    else if ([@"extractRes" isEqualToString:call.method]) {
        [self handle_toolExtractRes:call result:result];
    }
    else if ([@"loadNativeCfg" isEqualToString:call.method]) {
        [self handle_toolLoadNativeCfg:call result:result];
    }
    else {
      result(FlutterMethodNotImplemented);
    }
  }
  
  static NSString * _GenerateEngineId() {
      static dispatch_once_t onceToken;
      static NSLock *_lock = nil;
      dispatch_once(&onceToken, ^{
          _lock = [[NSLock alloc] init];
      });
      
      int cnt = 0;
      @synchronized (_lock) {
          static int _cnt = 0;
          cnt = _cnt;
          _cnt++;
          if (_cnt > 65535) {
              _cnt = 0;
          }
      }
      
      int64_t timestamp = [[NSDate date] timeIntervalSince1970];
      NSString *engineId = [NSString stringWithFormat:@"%llx-%x", timestamp, cnt];
      return engineId;
  }
  
  #define _ParseJsonString(str, err) \
  [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:err]
  
  #define _CondSignal(cond) do {\
  [cond lock]; \
  [cond signal]; \
  [cond unlock]; \
  }while(0)
  
  #define _CondWait(cond) do {\
  [cond lock]; \
  [cond wait]; \
  [cond unlock]; \
  }while(0)
  
  /**
   handle_aiengineNew
   @param call
      call.arguments[0]: cfg string;
   @param result engineId / FlutterError
   */
  - (void)handle_aiengineNew:(FlutterMethodCall*)call result:(FlutterResult)result {
      NSString *strCfg = (NSString *) call.arguments[0];
      
      // 检查参数
      NSError *jerr = nil;
      id cfg = _ParseJsonString(strCfg, &jerr);
      if (jerr != nil) {
          int eid = -1;
          id emsg = @"cfg is not valid json";
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil]);
          return;
      }
      if (![cfg isKindOfClass:[NSMutableDictionary class]]) {
          int eid = -1;
          id emsg = @"cfg is not in jsonobject type";
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil]);
          return;
      }
      
      NSCondition *cond = [[NSCondition alloc] init];
      __block NSString *engineId = @"";
      __block int eid = 0;
      __block NSString *emsg = @"";
      
      // 创建引擎
      [ChivoxAIEngine create:cfg cb:[ChivoxAIEngineCreateCallback onSuccess:^(ChivoxAIEngine * _Nonnull engine) {
          engineId = _GenerateEngineId();
          NSLog(@"ChivoxAIEngine create success: engineId=%@, engine=%p", engineId, engine);
          if ([engineId length] == 0) {
              if (eid == 0) eid = -1;
              result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:@"generate engineid fail" details:nil]);
              return;
          }
  
          [self->engineDic setObject:engine forKey:engineId];
          //_CondSignal(cond);
          result(engineId);
          
      } onFail:^(ChivoxAIRetValue * _Nonnull err) {
          NSLog(@"ChivoxAIEngine create fail: %@", err);
          eid = err.errId;
          emsg = err.error;
          if (eid == 0) eid = -1;
          //_CondSignal(cond);
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil]);
      }]];
      
      //_CondWait(cond);
      
      //if ([engineId length] == 0) {
      //    if (eid == 0) eid = -1;
      //    result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil]);
      //    return;
      //}
      
      //result(engineId);
  }
  
  /**
   handle_aiengineDelete
   @param call
      call.arguments[0]: engineId;
   @param result  void
   */
  - (void)handle_aiengineDelete:(FlutterMethodCall*)call result:(FlutterResult)result {
      NSString *engineId = (NSString *) call.arguments[0];
      ChivoxAIEngine *engine = [self->engineDic objectForKey:engineId];
      if (engine != nil) {
          [self->engineDic removeObjectForKey:engineId];
          [engine destory];
      }
      result(nil);
  }
  
  static id _CheckMakeAudioSrc(NSDictionary *mapAudioSrc, FlutterError **err) {
      *err = nil;
      
      id srcType = [mapAudioSrc objectForKey:@"srcType"];
      if (srcType == nil || ![srcType isKindOfClass:[NSString class]]) {
          int eid = -1;
          id emsg = @"invalid param audioSrc: 'srcType' required, must be String";
          *err = [FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil];
          return nil;
      }
      
      if (![srcType isEqualToString:@"innerRecorder"] && ![srcType isEqualToString:@"outerFeed"]) {
          int eid = -1;
          id emsg = [NSString stringWithFormat:@"invalid param audioSrc: 'srcType' not valid: %@", srcType];
          *err = [FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil];
          return nil;
      }
      
      if ([srcType isEqualToString:@"outerFeed"]) {
          return [[ChivoxAIOuterFeed alloc] init];
      }
      // else innerRecorder
      
      ChivoxAIInnerRecorder *inner = [[ChivoxAIInnerRecorder alloc] init];
      
      id innerParam = [mapAudioSrc objectForKey:@"innerRecorderParam"];
      if (innerParam == nil) {
          return inner;
      }
      
      if (![innerParam isKindOfClass:[NSDictionary class]]) {
          int eid = -1;
          id emsg = @"invalid param audioSrc: 'innerRecorderParam' must be Map or null";
          *err = [FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil];
          return nil;
      }
      
      id duration = [innerParam objectForKey:@"duration"];
      id channel = [innerParam objectForKey:@"channel"];
      id sampleBytes = [innerParam objectForKey:@"sampleBytes"];
      id sampleRate = [innerParam objectForKey:@"sampleRate"];
      id saveFile = [innerParam objectForKey:@"saveFile"];
      
      if (duration != nil && ![duration isKindOfClass:[NSNumber class]]) {
          int eid = -1;
          id emsg = @"invalid param audioSrc: 'innerRecorderParam.duration' must be number or null";
          *err = [FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil];
          return nil;
      }
      if (channel != nil && ![channel isKindOfClass:[NSNumber class]]) {
          int eid = -1;
          id emsg = @"invalid param audioSrc: 'innerRecorderParam.channel' must be number or null";
          *err = [FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil];
          return nil;
      }
      if (sampleBytes != nil && ![sampleBytes isKindOfClass:[NSNumber class]]) {
          int eid = -1;
          id emsg = @"invalid param audioSrc: 'innerRecorderParam.sampleBytes' must be number or nul";
          *err = [FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil];
          return nil;
      }
      if (sampleRate != nil && ![sampleRate isKindOfClass:[NSNumber class]]) {
          int eid = -1;
          id emsg = @"invalid param audioSrc: 'innerRecorderParam.sampleRate' must be number or null";
          *err = [FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil];
          return nil;
      }
      if (saveFile != nil && ![saveFile isKindOfClass:[NSString class]]) {
          int eid = -1;
          id emsg = @"invalid param audioSrc: 'innerRecorderParam.saveFile' must be String or null";
          *err = [FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil];
          return nil;
      }
      
      if (duration != nil) {
          inner.recordParam.duration = [duration intValue];
      }
      if (channel != nil) {
          inner.recordParam.channel = [channel intValue];
      }
      if (sampleBytes != nil) {
          inner.recordParam.sampleBytes = [sampleBytes intValue];
      }
      if (sampleRate != nil) {
          inner.recordParam.sampleRate = [sampleRate intValue];
      }
      if (saveFile != nil) {
          inner.recordParam.saveFile = saveFile;
      }
      
      return inner;
  }
  
  static id _MakeAiengineCallbackArgs(NSString *callbackId, NSString *type, ChivoxAIEvalResult *result) {
      return @[
          callbackId,
          type,
          @(result.isLast),
          result.tokenId ? result.tokenId : @"",
          result.text ? result.text : [NSNull null],
          result.data ? [FlutterStandardTypedData typedDataWithBytes:result.data] : [NSNull null],
          result.recFilePath ? result.recFilePath : [NSNull null],
      ];
  };
  
  /**
   handle_aiengineStart
   @param call
      call.arguments[0]: engineId;
      call.arguments[1]: audioSrc Map;
      call.arguments[2]: param string;
   @param result tokenId / FlutterError
   */
  - (void)handle_aiengineStart:(FlutterMethodCall*)call result:(FlutterResult)result {
      NSString *engineId = (NSString *) call.arguments[0];
      NSDictionary *mapAudioSrc = (NSDictionary *) call.arguments[1];
      NSString *strParam = (NSString *) call.arguments[2];
      NSString *callbackId = (NSString *) call.arguments[3];
      
      FlutterError *flerr = nil;
      id audioSrc = _CheckMakeAudioSrc(mapAudioSrc, &flerr);
      if (flerr != nil) {
          result(flerr);
          return;
      }
      
      NSError *jerr = nil;
      id param = _ParseJsonString(strParam, &jerr);
      if (jerr != nil) {
          int eid = -1;
          id emsg = @"param is not valid json";
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil]);
          return;
      }
      if (![param isKindOfClass:[NSMutableDictionary class]]) {
          int eid = -1;
          id emsg = @"param is not in jsonobject type";
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil]);
          return;
      }
      
      ChivoxAIEngine *engine = [self->engineDic objectForKey:engineId];
      if (engine == nil) {
          int eid = -1;
          id emsg = @"the engineId doesn't represent any engine";
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil]);
          return;
      }
      
      NSMutableString *tokenId = [NSMutableString string];
      
      ChivoxAIEvalResultListener *listener = [[ChivoxAIEvalResultListener alloc] init];
      listener.onEvalResult = ^(NSString * _Nonnull tokenId, ChivoxAIEvalResult * _Nonnull result) {
          id args = _MakeAiengineCallbackArgs(callbackId, @"evalResult", result);
          [self->callbackChannel invokeMethod:@"aiengine_callback" arguments:args];
      };
      listener.onBinResult = ^(NSString * _Nonnull tokenId, ChivoxAIEvalResult * _Nonnull result) {
          id args = _MakeAiengineCallbackArgs(callbackId, @"binResult", result);
          [self->callbackChannel invokeMethod:@"aiengine_callback" arguments:args];
      };
      listener.onError = ^(NSString * _Nonnull tokenId, ChivoxAIEvalResult * _Nonnull result) {
          id args = _MakeAiengineCallbackArgs(callbackId, @"error", result);
          [self->callbackChannel invokeMethod:@"aiengine_callback" arguments:args];
      };
      listener.onVad = ^(NSString * _Nonnull tokenId, ChivoxAIEvalResult * _Nonnull result) {
          id args = _MakeAiengineCallbackArgs(callbackId, @"vad", result);
          [self->callbackChannel invokeMethod:@"aiengine_callback" arguments:args];
      };
      listener.onSoundIntensity = ^(NSString * _Nonnull tokenId, ChivoxAIEvalResult * _Nonnull result) {
          id args = _MakeAiengineCallbackArgs(callbackId, @"soundIntensity", result);
          [self->callbackChannel invokeMethod:@"aiengine_callback" arguments:args];
      };
      listener.onOther = ^(NSString * _Nonnull tokenId, ChivoxAIEvalResult * _Nonnull result) {
          id args = _MakeAiengineCallbackArgs(callbackId, @"other", result);
          [self->callbackChannel invokeMethod:@"aiengine_callback" arguments:args];
      };
      
      ChivoxAIRetValue * ret = [engine start:audioSrc tokenId:tokenId param:param listener:listener];
      if (ret != nil && [ret errId] != 0) {
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", [ret errId]] message:[ret error] details:nil]);
          return;
      }
      
      result([NSString stringWithString:tokenId]);
  }
  
  /**
   handle_aiengineFeed
   @param call
      call.arguments[0]: engineId;
      call.arguments[1]: bytes Uint8List;
      call.arguments[2]: length int;
   @param result void / FlutterError
   */
  - (void)handle_aiengineFeed:(FlutterMethodCall*)call result:(FlutterResult)result {
      NSString *engineId = (NSString *) call.arguments[0];
      FlutterStandardTypedData *bytes = (FlutterStandardTypedData *) call.arguments[1];
      NSNumber *length = (NSNumber *)call.arguments[2];
      
      if ([length intValue] <= 0) {
          result(nil);
          return;
      }
      
      ChivoxAIEngine *engine = [self->engineDic objectForKey:engineId];
      if (engine == nil) {
          int eid = -1;
          id emsg = @"the engineId doesn't represent any engine";
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil]);
          return;
      };
      
      NSData *data = [bytes data];
      ChivoxAIRetValue *ret = [engine feed:[data bytes] length:[length intValue]];
      if (ret != nil && [ret errId] != 0) {
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", [ret errId]] message:[ret error] details:nil]);
          return;
      }
      
      result(nil);
  }
  
  /**
   handle_aiengineStop
   @param call
      call.arguments[0]: engineId;
   @param result void / FlutterError
   */
  - (void)handle_aiengineStop:(FlutterMethodCall*)call result:(FlutterResult)result {
      NSString *engineId = (NSString *) call.arguments[0];
      ChivoxAIEngine *engine = [self->engineDic objectForKey:engineId];
      if (engine == nil) {
          int eid = -1;
          id emsg = @"the engineId doesn't represent any engine";
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil]);
          return;
      };
      
      ChivoxAIRetValue *ret = [engine stop];
      if (ret != nil && [ret errId] != 0) {
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", [ret errId]] message:[ret error] details:nil]);
          return;
      }
      
      result(nil);
  }
  
  /**
   handle_aiengineCancel
   @param call
      call.arguments[0]: engineId;
   @param result void / FlutterError
   */
  - (void)handle_aiengineCancel:(FlutterMethodCall*)call result:(FlutterResult)result {
      NSString *engineId = (NSString *) call.arguments[0];
      ChivoxAIEngine *engine = [self->engineDic objectForKey:engineId];
      if (engine == nil) {
          int eid = -1;
          id emsg = @"the engineId doesn't represent any engine";
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil]);
          return;
      };
      
      [engine cancel];
      result(nil);
      return;
  }
  
  /**
   handle_getDeviceId
   @param call no args
   @param result string / FlutterError
   */
  - (void)handle_getDeviceId:(FlutterMethodCall*)call result:(FlutterResult)result {
      NSString *devid = [ChivoxAIEngine getDeviceId];
      if (devid == nil) devid = @"";
      result(devid);
  }
  
  /**
   handle_getSerialNumber
   @param call
      call.arguments[0]: engineId;  unused;
      call.arguments[1]: input Map;
   @param result string / FlutterError
   */
  - (void)handle_getSerialNumber:(FlutterMethodCall*)call result:(FlutterResult)result {
      // unused call.arguments[0]
      id input = call.arguments[1];
      
      NSDictionary *output = [ChivoxAIEngine getSerialNumber:input];
      if (output == nil) {
          int eid = -1;
          id emsg = @"the platform interface return null";
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil]);
          return;
      }
      
      NSError *jerr = nil;
      NSData *data = [NSJSONSerialization dataWithJSONObject:output options:0 error:&jerr];
      if (jerr != nil) {
          int eid = -1;
          id emsg = @"the platform interface do json serialization failed";
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil]);
          return;
      }
      
      result([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
  }
  
  /**
   handle_clearSavedSerialNumber
   @param call
      call.arguments[0]: appKey;
   @param result bool / FlutterError
   */
  - (void)handle_clearSavedSerialNumber:(FlutterMethodCall*)call result:(FlutterResult)result {
      NSString *appKey = call.arguments[0];
      BOOL output = [ChivoxAIEngine clearSavedSerialNumber:appKey];
      result(@(output));
  }
  
  /**
   handle_getProvision
   @param call
      call.arguments[0]: engineId; unused;
      call.arguments[1]: input Map;
   @param result string / FlutterError
   */
  - (void)handle_getProvision:(FlutterMethodCall*)call result:(FlutterResult)result {
      // unused call.arguments[0]
      id input = call.arguments[1];
      
      NSDictionary *output = [ChivoxAIEngine getProvision:input];
      if (output == nil) {
          int eid = -1;
          id emsg = @"the platform interface return null";
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil]);
          return;
      }
      
      NSError *jerr = nil;
      NSData *data = [NSJSONSerialization dataWithJSONObject:output options:0 error:&jerr];
      if (jerr != nil) {
          int eid = -1;
          id emsg = @"the platform interface do json serialization failed";
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil]);
          return;
      }
      
      result([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
  }
  
  - (void)handle_toolExtractRes:(FlutterMethodCall*)call result:(FlutterResult)result {
      NSString *assetPrefix = call.arguments[0];
      NSArray *assetNames = call.arguments[1];
      NSString *targetRoot = call.arguments[2];
      NSString *callbackId = call.arguments[3];
      
      NSString *key = [self->registrar lookupKeyForAsset:assetPrefix];
      NSString *prefix = [[NSBundle mainBundle] pathForResource:key ofType:nil];
      if (prefix == nil) {
          int errId = -1;
          id errMsg = [NSString stringWithFormat:@"extrace res fail: invalid assetPrefix"];
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", errId] message:errMsg details:nil]);
          return;
      }
      
      ChivoxAIResTool *tool = [[ChivoxAIResTool alloc] init];
      tool.onProgress = ^(float i) {
          id args = @[
              callbackId,
              [NSNumber numberWithFloat:i],
          ];
          [self->callbackChannel invokeMethod:@"aiengine_callback" arguments:args];
      };
  
      id err = [tool extract:prefix assets:[assetNames mutableCopy] target:targetRoot];
      if (err != nil) {
          int errId = -1;
          id errMsg = [NSString stringWithFormat:@"extrace res fail: %@", err];
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", errId] message:errMsg details:nil]);
          return;
      }
      
      result(nil);
  }
  
  - (void)handle_toolLoadNativeCfg:(FlutterMethodCall*)call result:(FlutterResult)result {
      NSString *resRoot = call.arguments[0];
      NSArray *resNames = call.arguments[1];
      
      ChivoxAIResTool *tool = [[ChivoxAIResTool alloc] init];
      NSDictionary *dic = [tool loadNativeCfg:resRoot :[resNames mutableCopy]];
      if (dic == nil) {
          result(nil);
          return;
      }
      
      NSError *jerr = nil;
      NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:0 error:&jerr];
      if (jerr != nil) {
          int eid = -1;
          id emsg = @"the platform interface do json serialization failed";
          result([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", eid] message:emsg details:nil]);
          return;
      }
      
      result([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
  }
  
  @end