I am facing some problem in below code when a client is called getCameraSource() then my main thread is get blocked until EventHandler::eventHandled by EventHandler and also notify() to exit from wait condition.
class MediaServer : public BnMediaServer {
public:
MediaServer(){
mEvtHandler = std::make_shared<EventHandler>();
};
virtual ~MediaServer();
::ndk::ScopedAStatus getCameraSource(CameraID in_cameraId, Media_Handle* out_handle, Media_Status* _aidl_return){
*_aidl_return = (STATUS)ResourceManager::getResourceManager()->getCameraSource(
(com::elektrobit::mediaserver::CameraID)in_cameraId);
LOG(INFO) << "MediaServer getCameraSource waiting...";
// Wait for the event to be handled
std::unique_lock<std::mutex> lock(EventHandler::mtx);
EventHandler::cv.wait(lock, [this] { return EventHandler::eventHandled; });
EVENT mEvent = EventHandler::eventStatus;
if (mEvent == EVENT::EVENT_GET_CAMERA_SOURCE_SUCCESS) {
*_aidl_return = STATUS::CAM_SOURCE_ACQUIRED;
LOG(INFO) << "Event handled successfully.";
} else {
*_aidl_return = STATUS::CAM_SOURCE_FAILED;
LOG(ERROR) << "Event handling failed.";
}
LOG(ERROR) << "MediaServer getCameraSource Done";
//reset it before return
EventHandler::eventHandled = false;
return ndk::ScopedAStatus::ok();
}
private:
std::shared_ptr<EventHandler> mEvtHandler = nullptr;
};
class EventHandler : public IExternalCallback
{
public:
EventHandler();
~EventHandler();
EVENT getActualEVENT(Media_Status status);
void onEvent(com::elektrobit::mediaserver::CameraID camID, Media_Status status) override
{
std::unique_lock<std::mutex> lock(EventHandler::mtx);
if (auto mCallbackResult = mCallbackList.find((
CameraID)camID);
mCallbackResult != mCallbackList.end())
{
LOG(INFO) << "MediaServer inside if";
Buffer buffer;
buffer.camId = (CameraID)camID;
EventHandler::eventStatus = getActualEvent(status);
EventHandler::eventHandled = true;
EventHandler::cv.notify_one();
mCallbackResult->second->onDataEvent
(buffer,getActualEvent(status));
}
}
static std::condition_variable cv;
static std::mutex mtx;
static bool eventHandled;
static EVENT eventStatus;
private:
void dispatchThread();
bool mExit;
std::mutex mutex_;
std::condition_variable mCond_var;
std::thread mDispatchThread;
CameraDataBufferQueue *mDispatchFramesQueue = nullptr;
std::map<CameraID, std::shared_ptr<Media_Callback>> mCallbackList;
};
std::condition_variable EventHandler::cv;
std::mutex EventHandler::mtx;
bool EventHandler::eventHandled;
EVENT EventHandler::eventStatus = EVENT::EVENT_UNKOWN;
Could you please help me in this code to wait in getCamerasource until the eventhandler received the status in onevent and notify main thread to exit from wait in side getcamerasouirce