OmxEncoder: use c++ mutex&condition_variable (#19786)

* use c++ mutex&condition_variable

* rebase

* cleanup includes&use while

* remove include pthread.h
old-commit-hash: aa37e95341
commatwo_master
Dean Lee 4 years ago committed by GitHub
parent 02788f3a0e
commit ab24d18c43
  1. 33
      selfdrive/loggerd/omx_encoder.cc
  2. 6
      selfdrive/loggerd/omx_encoder.h

@ -8,8 +8,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <pthread.h>
#include <OMX_Component.h> #include <OMX_Component.h>
#include <OMX_IndexExt.h> #include <OMX_IndexExt.h>
#include <OMX_VideoExt.h> #include <OMX_VideoExt.h>
@ -34,11 +32,10 @@ extern ExitHandler do_exit;
// ***** OMX callback functions ***** // ***** OMX callback functions *****
void OmxEncoder::wait_for_state(OMX_STATETYPE state) { 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) { 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 = { 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_ERRORTYPE OmxEncoder::event_handler(OMX_HANDLETYPE component, OMX_PTR app_data, OMX_EVENTTYPE event,
OMX_U32 data1, OMX_U32 data2, OMX_PTR event_data) { OMX_U32 data1, OMX_U32 data2, OMX_PTR event_data) {
OmxEncoder *e = (OmxEncoder*)app_data; OmxEncoder *e = (OmxEncoder*)app_data;
if (event == OMX_EventCmdComplete) {
switch (event) {
case OMX_EventCmdComplete:
assert(data1 == OMX_CommandStateSet); assert(data1 == OMX_CommandStateSet);
LOG("set state event 0x%x", data2); LOG("set state event 0x%x", data2);
pthread_mutex_lock(&e->state_lock); {
e->state = (OMX_STATETYPE)data2; std::unique_lock lk(e->state_lock);
pthread_cond_broadcast(&e->state_cv); e->state = (OMX_STATETYPE)data2;
pthread_mutex_unlock(&e->state_lock); }
break; e->state_cv.notify_all();
case OMX_EventError: } else if (event == OMX_EventError) {
LOGE("OMX error 0x%08x", data1); LOGE("OMX error 0x%08x", data1);
// assert(false); } else {
break; LOGE("OMX unhandled event %d", event);
default:
LOGE("unhandled event %d", event);
assert(false); assert(false);
break;
} }
return OMX_ErrorNone; return OMX_ErrorNone;
@ -172,9 +164,6 @@ OmxEncoder::OmxEncoder(const char* filename, int width, int height, int fps, int
this->fps = fps; this->fps = fps;
this->remuxing = !h265; this->remuxing = !h265;
pthread_mutex_init(&this->state_lock, NULL);
pthread_cond_init(&this->state_cv, NULL);
this->downscale = downscale; this->downscale = downscale;
if (this->downscale) { if (this->downscale) {
this->y_ptr2 = (uint8_t *)malloc(this->width*this->height); this->y_ptr2 = (uint8_t *)malloc(this->width*this->height);

@ -3,8 +3,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <pthread.h>
#include <vector> #include <vector>
#include <OMX_Component.h> #include <OMX_Component.h>
@ -53,8 +51,8 @@ private:
uint8_t *codec_config = NULL; uint8_t *codec_config = NULL;
bool wrote_codec_config; bool wrote_codec_config;
pthread_mutex_t state_lock; std::mutex state_lock;
pthread_cond_t state_cv; std::condition_variable state_cv;
OMX_STATETYPE state = OMX_StateLoaded; OMX_STATETYPE state = OMX_StateLoaded;
OMX_HANDLETYPE handle; OMX_HANDLETYPE handle;

Loading…
Cancel
Save