From ab24d18c43312024f6f6b42f1c95d87a780124a3 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Tue, 2 Feb 2021 13:57:17 +0800 Subject: [PATCH] OmxEncoder: use c++ mutex&condition_variable (#19786) * use c++ mutex&condition_variable * rebase * cleanup includes&use while * remove include pthread.h old-commit-hash: aa37e95341b0df8dcd378f6ca082014828edbbbf --- selfdrive/loggerd/omx_encoder.cc | 33 +++++++++++--------------------- selfdrive/loggerd/omx_encoder.h | 6 ++---- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/selfdrive/loggerd/omx_encoder.cc b/selfdrive/loggerd/omx_encoder.cc index f8cf6f9b9b..bfbf12e004 100644 --- a/selfdrive/loggerd/omx_encoder.cc +++ b/selfdrive/loggerd/omx_encoder.cc @@ -8,8 +8,6 @@ #include #include -#include - #include #include #include @@ -34,11 +32,10 @@ extern ExitHandler do_exit; // ***** OMX callback functions ***** void OmxEncoder::wait_for_state(OMX_STATETYPE state) { - pthread_mutex_lock(&this->state_lock); + std::unique_lock lk(this->state_lock); while (this->state != state) { - pthread_cond_wait(&this->state_cv, &this->state_lock); + this->state_cv.wait(lk); } - pthread_mutex_unlock(&this->state_lock); } static OMX_CALLBACKTYPE omx_callbacks = { @@ -50,24 +47,19 @@ static OMX_CALLBACKTYPE omx_callbacks = { OMX_ERRORTYPE OmxEncoder::event_handler(OMX_HANDLETYPE component, OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2, OMX_PTR event_data) { OmxEncoder *e = (OmxEncoder*)app_data; - - switch (event) { - case OMX_EventCmdComplete: + if (event == OMX_EventCmdComplete) { assert(data1 == OMX_CommandStateSet); LOG("set state event 0x%x", data2); - pthread_mutex_lock(&e->state_lock); - e->state = (OMX_STATETYPE)data2; - pthread_cond_broadcast(&e->state_cv); - pthread_mutex_unlock(&e->state_lock); - break; - case OMX_EventError: + { + std::unique_lock lk(e->state_lock); + e->state = (OMX_STATETYPE)data2; + } + e->state_cv.notify_all(); + } else if (event == OMX_EventError) { LOGE("OMX error 0x%08x", data1); - // assert(false); - break; - default: - LOGE("unhandled event %d", event); + } else { + LOGE("OMX unhandled event %d", event); assert(false); - break; } return OMX_ErrorNone; @@ -172,9 +164,6 @@ OmxEncoder::OmxEncoder(const char* filename, int width, int height, int fps, int this->fps = fps; this->remuxing = !h265; - pthread_mutex_init(&this->state_lock, NULL); - pthread_cond_init(&this->state_cv, NULL); - this->downscale = downscale; if (this->downscale) { this->y_ptr2 = (uint8_t *)malloc(this->width*this->height); diff --git a/selfdrive/loggerd/omx_encoder.h b/selfdrive/loggerd/omx_encoder.h index 39ccbfa95f..b1c6a4b5f8 100644 --- a/selfdrive/loggerd/omx_encoder.h +++ b/selfdrive/loggerd/omx_encoder.h @@ -3,8 +3,6 @@ #include #include #include - -#include #include #include @@ -53,8 +51,8 @@ private: uint8_t *codec_config = NULL; bool wrote_codec_config; - pthread_mutex_t state_lock; - pthread_cond_t state_cv; + std::mutex state_lock; + std::condition_variable state_cv; OMX_STATETYPE state = OMX_StateLoaded; OMX_HANDLETYPE handle;