问题背景
鸿蒙相机按照流程调用数据之后,想将相机数据输出预览画面到鸿蒙的XComponent上面,这里踩了一个坑了,一直报错CAMERA_SERVICE_FATAL_ERROR。这个问题通过官方文档查询错误原因是:相机服务异常。没有相机权限、相机服务重启、跨进程调用异常等。
问题排查
通过系统日志发现错误日志:
04-27 15:09:12.173 16262-16262 C01401/com.exa...s/Bufferqueue com.examp...eancplus E <surface_utils.cpp:48-GetSurface>: Cannot find surface, uniqueId: 0.
04-27 15:09:12.173 16262-18181 C02B01/com.exa...ncplus/CAMERA com.examp...eancplus I {OnCameraStatusChanged():197} OnCameraStatusChanged cameraId: device/0, status: 3
04-27 15:09:12.173 16262-16262 C02B01/com.exa...ncplus/CAMERA com.examp...eancplus E {CreatePreviewOutput()-camera_manager_impl.cpp:542} Failed to get previewOutput surface
发现是没有找到surface。最后原因是我弄混了Xcomponent和Surface两个不同的概念,我将Xcomponent的Id作为Surface的Id作为入参surfaceId的值,函数无法找到相应的surface导致的。
关键代码
static napi_value InitSurface(napi_env env, napi_callback_info info) {
napi_value result;
size_t typeLen = 0;
size_t argc = 1;
napi_value args[1] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
webrtc::ohos::CameraRender *render = webrtc::ohos::CameraRender::getCameraInstance();
napi_get_value_string_utf8(env, args[0], nullptr, 0, &typeLen);
render->surfaceId = new char[typeLen + 1]; // 从外部取到surfaceId
napi_get_value_string_utf8(env, args[0], render->surfaceId, typeLen + 1, &typeLen);
napi_create_int32(env, argc, &result);
return result;
}
int32_t OhosCamera::createOutputPreview(webrtc::ohos::CameraRender *render) {
char* surfaceId = render->surfaceId; // 这里不能取XComponentID
Camera_ErrorCode ret = OH_CameraManager_CreatePreviewOutput(camera_manager_, preview_profile_, surfaceId, &preview_output_);
if(ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "createOutputPreview failed.");
return -1;
}
return 0;
}