HarmonyOS 继承自C++类的TS类如何使用Sendable协议?

由于SDK核心代码是C++实现,用TS封装了API。

// TS 声明的C++类
import image from '@ohos.multimedia.image';
import { XavThumbnailInfo } from '../../../../ets/videoedit/define/XavThumbnailInfo';
import { XavEditTimeline } from '../../../../ets/videoedit/project/XavEditTimeline';

import { lang, collections } from '@kit.ArkTS'

@Sendable
export declare class NapiThumbnailGetter {
  constructor();

  destroy: () => void;
  getThumbnailFromFilePath: (filePath: string, timestamp: number, timeStep: number, expectWidth: number,
    expectHeight: number) => image.PixelMap;
  getThumbnailFromTimeline: (timeline: XavEditTimeline, timestamp: number, timeStep: number, expectWidth: number,
    expectHeight: number) => image.PixelMap;
  getThumbnailInfoFromTimeline: (timeline: XavEditTimeline, trackIndex: number, timestamp: number, timeStep: number,
    expectWidth: number, expectHeight: number, thumbnailInfo: XavThumbnailInfo) => boolean;
}

// 继承自C++类的TS API
import NativeSdk from 'libares.so';

@Sendable
export class XavThumbnailGetter extends NativeSdk.NapiThumbnailGetter {
}

// 由于ThumbnailGetter这个类的几个方法耗时高,因此业务层需要另起一个worker或者taskPool执行ThumbnailGetter的方法。类似下面的代码
@Concurrent
function grabberThumbnailFromTimeline(timeline: XavEditTimeline, thumbnailGetter: XavThumbnailGetter,
  timeStamp: number): image.PixelMap {
  console.info("printArgs: " + timeStamp);
  return thumbnailGetter.getThumbnailFromTimeline(timeline, timeStamp, 1000, 100, 100);
}

this.addButton('Timeline抽缩略图异步', () => {
  for (let i = 0; i < 100; i++) {
    taskpool.execute(grabberThumbnailFromTimeline, this.timeline, this.thumbnailGetter, i * 1000)
      .then((image: Object) => {
        this.image = this.image;
      });
  }
});

编译工程时会发生如下错误:

ERROR: ArkTS:ERROR File: /Users/user/Development/red/videoeditlib-xhs/harmony/ReDemo/videoedit/src/main/cpp/types/libvideoedit/utils/NapiThumbnailGetter.ets:11:5
Properties in "Sendable" classes and interfaces must have a Sendable data type (arkts-sendable-prop-types)
ERROR: ArkTS:ERROR File: /Users/user/Development/red/videoeditlib-xhs/harmony/ReDemo/videoedit/src/main/cpp/types/libvideoedit/utils/NapiThumbnailGetter.ets:12:5
Properties in "Sendable" classes and interfaces must have a Sendable data type (arkts-sendable-prop-types)
ERROR: ArkTS:ERROR File: /Users/user/Development/red/videoeditlib-xhs/harmony/ReDemo/videoedit/src/main/cpp/types/libvideoedit/utils/NapiThumbnailGetter.ets:14:5
Properties in "Sendable" classes and interfaces must have a Sendable data type (arkts-sendable-prop-types)
ERROR: ArkTS:ERROR File: /Users/user/Development/red/videoeditlib-xhs/harmony/ReDemo/videoedit/src/main/cpp/types/libvideoedit/utils/NapiThumbnailGetter.ets:16:5
Properties in "Sendable" classes and interfaces must have a Sendable data type (arkts-sendable-prop-types)

上述这种应用场景,有没有简便的方法支持多线程共享对象?

阅读 425
1 个回答

可通过使用Native对象的this指针实现了JS多线程共享Native对象。

logo
HarmonyOS
子站问答
访问
宣传栏