相机服务异常排查指南
==========

#### 问题现象

当相机进入预览界面后，将APP状态在前后台之间反复多次切换，会发现有7400201的报错。代码中对前后台切换场景做了处理，会主动释放重启相机流。

#### 背景知识

[7400201 相机服务异常](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/errorcode-camera#section7400201-相机服务异常)：相机服务异常，比如相机服务重启、跨进程调用异常等。

#### 问题定位

1.  通过关键词“C02B0|7400201”用正则匹配找出日志中相关内容：
    
    01-29 10:03:04.337   53320-53320   C02B01/com.exa...cation/CAMERA  com.examp...lication  I     {Stop():1655} Stop is called
    01-29 10:03:04.337   53320-53320   C02B01/com.exa...cation/CAMERA  com.examp...lication  I     {Close():415} Close is called
    01-29 10:03:04.338   53320-53320   C02B01/com.exa...cation/CAMERA  com.examp...lication  I     {Release():469} PreviewOutputNapi::Release is called
    01-29 10:03:04.338   53320-53320   C02B01/com.exa...cation/CAMERA  com.examp...lication  I     {Release():1055} Release is called
    01-29 10:03:04.338   53320-53320   C02B01/com.exa...cation/CAMERA  com.examp...lication  I     {Release():1697} Release is called
    01-29 10:03:04.343   53320-53609   C02B01/com.exa...cation/CAMERA  com.examp...lication  I     {operator()():429} CameraInputNapi::Close running on worker
    01-29 10:03:04.345   53320-53607   C02B01/com.exa...cation/CAMERA  com.examp...lication  I     {operator()():483} PreviewOutputNapi::Release running on worker
    01-29 10:03:04.346   53320-53602   C02B01/com.exa...cation/CAMERA  com.examp...lication  I     {operator()():1069} PhotoOutputNapi::Release running on worker
    01-29 10:03:04.346   53320-53599   C02B01/com.exa...cation/CAMERA  com.examp...lication  I     {operator()():1711} CameraSessionNapi::Release running on worker
    01-29 10:03:04.351   53320-53590   C02B01/com.exa...cation/CAMERA  com.examp...lication  I     {operator()():1669} CameraSessionNapi::Stop running on worker
    01-29 10:03:04.351   53320-53590   C02B01/com.exa...cation/CAMERA  com.examp...lication  E     {Stop():1297} CaptureSession::Stop() captureSession is nullptr
    01-29 10:03:04.351   53320-53590   C02B01/com.exa...cation/CAMERA  com.examp...lication  I     {operator()():1676} CameraSessionNapi::Stop errorCode:7400201
    01-29 10:03:04.351   53320-53590   C02B01/com.exa...cation/CAMERA  com.examp...lication  I     {operator()():1676} CameraSessionNapi::Stop errorCode:7400201
    01-29 10:03:04.351   53320-53609   C02B01/com.exa...cation/CAMERA  com.examp...lication  E     {Close():382} CameraInput::Close() deviceObj is nullptr
    
    从日志中可以看到应用调用Stop()接口时报错7400201，报错原因是Session已经释放，无法再次释放。
    
2.  再往前排查日志，检查Session提前释放的原因：
    
    01-29 10:03:03.925   1606-53996    C02B01/camera\_service/CAMERA    camera\_service        I     {Start():581} HStreamRepeat::Start streamId:599 hdiStreamId:1 With capture ID: 294, repeatStreamType:0
    01-29 10:03:03.939   1606-1930     C02B01/camera\_service/CAMERA    camera\_service        I     {Stop():1240} HStreamOperator::Stop prepare execute
    01-29 10:03:03.939   1606-1930     C02B01/camera\_service/CAMERA    camera\_service        I     {Stop():637} HStreamRepeat::Stop streamId:599 hdiStreamId:1 With capture ID: 294, repeatStreamType:0
    01-29 10:03:03.940   1606-1930     C02B01/camera\_service/CAMERA    camera\_service        I     {OnCaptureEndedExt\_V1\_4():2316} HStreamOperator::OnCaptureEndedExt\_V1\_4, captureId is 294
    01-29 10:03:03.940   1606-1930     C02B01/camera\_service/CAMERA    camera\_service        I     {ProcessRepeatStream():2349} ProcessRepeatStream is called
    01-29 10:03:03.940   1606-1930     C02B01/camera\_service/CAMERA    camera\_service        I     {ProcessRepeatStream():2366} HStreamOperator::OnCaptureEndedExt\_V1\_4 captureId:294 videoId: isDeferredVideo:0, deferredVideoEnhanceFlag:0
    01-29 10:03:03.940   1606-1930     C02B01/camera\_service/CAMERA    camera\_service        I     {OnDeferredVideoEnhancementInfo():757} HStreamRepeat::OnDeferredVideoEnhancementInfo
    01-29 10:03:03.940   1606-1930     C02B01/camera\_service/CAMERA    camera\_service        I     {Stop():637} HStreamRepeat::Stop streamId:601 hdiStreamId:3 With capture ID: 0, repeatStreamType:3
    01-29 10:03:03.940   1606-1930     C02B01/camera\_service/CAMERA    camera\_service        E     {Stop():639} HStreamRepeat::Stop, Stream not started yet
    
    从日志可知，系统起了两个capture ID，结合频繁快速切换前后台的操作场景，猜测可能是申请相机资源冲突导致异常，排查代码得知代码onPageShow()和onPageHide()方法中申请/释放相机资源的函数未加await关键词，导致相机资源冲突。
    

#### 分析结论

start()、stop()等方法均是异步执行，在前后台频繁快速切换的情况下，导致了相机资源冲突，导致了重复释放相机资源，引发7400201报错。

#### 修改建议

相机资源申请和释放的异步函数增加await关键词约束，确保资源的申请释放均能按序执行，代码可参考[预览开发完整示例](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/camera-preview#完整示例)。

#### 常见FAQ

Q：相机Kit多个接口均可能抛出7400201错误码，这个错误码的根因是否有大致分类？

A：7400201错误码涉及面比较宽泛，但大致可分为两类：重复释放资源和底层报错。