Search code examples
androidcameraandroid-cameraandroid-camera2

Properly release target Surface used for Camera2 repeating request


Let say I have CameraCaptureSession session1 and Surface s1 as a target for repeating CaptureRequest. Now I want to change the output size. To this end I can create another Surface s2 with the appropriate size and create another CameraCaptureSession session2. Now I will use s2 as a target for future CaptureRequests so I don't need s1 anymore. Moreover, I want to create s2 only after s1 is released so I can save resources.

When exactly s1 may be released? May I release s1 right after session1.close(), or should I wait until the onClosed(Session) is triggered and release s1 only after that?


The second approach sounds correct to me but I am confused a little bit by the optimization replaceSessionClose() I found.


Solution

  • It's best to wait for onClosed, though at worst case you'd miss one or two frames being sent to the old session, and there'd be some amount of error logging in logcat, if you release the surface early.

    But it won't cause the new session to fail to be created.