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)

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

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

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

分享
微博
QQ
微信
回复
3天前
相关问题
ArkTS调用C++成员函数
1342浏览 • 1回复 待解决
HarmonyOS TS方法重载不能正常使用
12浏览 • 1回复 待解决
tsc++传递数组,c++如何解析
1934浏览 • 1回复 待解决
Har如何导出ts和方法?
429浏览 • 1回复 待解决
HarmonyOS 可以被继承并重写吗
25浏览 • 1回复 待解决
ArkTS中继承机制是怎样?
174浏览 • 1回复 待解决
Uint8Array是@Sendable吗?
289浏览 • 1回复 待解决
HarmonyOS ArkTS支持TS抽象吗?
292浏览 • 1回复 待解决
c++中实例化自定义并调用方法
346浏览 • 1回复 待解决