Fixed listener sptr release issue

Signed-off-by: Kishore Kumar M V <kishorekumar@huawei.com>
This commit is contained in:
Kishore Kumar M V 2021-07-28 03:07:40 +05:30
parent d09cf4dfdd
commit c1d53cc7c0
2 changed files with 32 additions and 9 deletions

View File

@ -261,7 +261,9 @@ int32_t CameraCallback::PreparePhoto(sptr<OHOS::CameraStandard::CameraManager> c
LOGE("Camera CreateSurface failed.");
return -1;
}
photoListener_ = this;
if (photoListener_ == nullptr) {
photoListener_ = new CaptureListener(this);
}
captureConsumerSurface_->RegisterConsumerListener(photoListener_);
photoOutput_ = camManagerObj->CreatePhotoOutput(captureConsumerSurface_);
@ -590,21 +592,22 @@ void CameraCallback::OnCameraOffsetChange(double x, double y)
}
}
void CameraCallback::OnBufferAvailable()
void CaptureListener::OnBufferAvailable()
{
int32_t flushFence = 0;
int64_t timestamp = 0;
OHOS::Rect damage;
OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr;
captureConsumerSurface_->AcquireBuffer(buffer, flushFence, timestamp, damage);
cameraCallback_->captureConsumerSurface_->AcquireBuffer(buffer, flushFence, timestamp, damage);
if (buffer != nullptr) {
char *addr = static_cast<char *>(buffer->GetVirAddr());
uint32_t size = buffer->GetSize();
std::string path;
SaveData(addr, size, path);
OnTakePhoto(true, path);
captureConsumerSurface_->ReleaseBuffer(buffer, -1);
hasCallPhoto_ = false;
cameraCallback_->SaveData(addr, size, path);
cameraCallback_->OnTakePhoto(true, path);
cameraCallback_->captureConsumerSurface_->ReleaseBuffer(buffer, -1);
cameraCallback_->hasCallPhoto_ = false;
} else {
LOGE("AcquireBuffer failed!");
}

View File

@ -37,7 +37,9 @@ enum State : int32_t {
STATE_BUTT
};
class CameraCallback : public IBufferConsumerListener {
class CaptureListener;
class CameraCallback {
public:
ACE_DISALLOW_COPY_AND_MOVE(CameraCallback);
using PrepareEventListener = std::function<void()>;
@ -47,6 +49,10 @@ public:
~CameraCallback()
{
photoListener_ = nullptr;
if (captureConsumerSurface_ != nullptr) {
captureConsumerSurface_->UnregisterConsumerListener();
}
Stop(true);
}
@ -65,7 +71,6 @@ public:
int32_t PreparePhoto(sptr<OHOS::CameraStandard::CameraManager> camManagerObj);
int32_t PrepareVideo(sptr<OHOS::CameraStandard::CameraManager> camManagerObj);
int32_t PrepareCamera();
void OnBufferAvailable() override;
int32_t SaveData(char *buffer, int32_t size, std::string& path);
void OnTakePhoto(bool isSucces, std::string info);
void AddTakePhotoListener(TakePhotoListener&& listener);
@ -78,6 +83,7 @@ public:
{
prepareEventListener_ = std::move(listener);
}
friend class CaptureListener;
protected:
void CloseRecorder();
@ -121,6 +127,20 @@ private:
PrepareEventListener prepareEventListener_;
};
class CaptureListener : public IBufferConsumerListener {
public:
explicit CaptureListener(CameraCallback *cameraCallback) : cameraCallback_(cameraCallback) {
}
~CaptureListener() {
cameraCallback_->photoListener_ = nullptr;
cameraCallback_ = nullptr;
}
void OnBufferAvailable() override;
private:
CameraCallback *cameraCallback_;
};
class Camera : public virtual AceType {
DECLARE_ACE_TYPE(Camera, AceType);