Skip to content

Commit

Permalink
OmxEncoder: new function set_state, set and wait state changed (comma…
Browse files Browse the repository at this point in the history
…ai#19906)

* free buffers after state is loaded

* set_data_blocking

* fix indentation

* Empty commit,trigger Build
  • Loading branch information
deanlee authored Jan 28, 2021
1 parent a48ab61 commit 36a596e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
20 changes: 6 additions & 14 deletions selfdrive/loggerd/omx_encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ extern ExitHandler do_exit;

// ***** OMX callback functions *****

void OmxEncoder::wait_for_state(OMX_STATETYPE state) {
void OmxEncoder::set_state_blocking(OMX_STATETYPE state) {
OMX_CHECK(OMX_SendCommand(this->handle, OMX_CommandStateSet, state, NULL));
pthread_mutex_lock(&this->state_lock);
while (this->state != state) {
pthread_cond_wait(&this->state_cv, &this->state_lock);
Expand Down Expand Up @@ -304,7 +305,7 @@ OmxEncoder::OmxEncoder(const char* filename, int width, int height, int fps, int
// printf("profile %d level 0x%x\n", params.eProfile, params.eLevel);
// }

OMX_CHECK(OMX_SendCommand(this->handle, OMX_CommandStateSet, OMX_StateIdle, NULL));
set_state_blocking(OMX_StateIdle);

for (auto &buf : this->in_buf_headers) {
OMX_CHECK(OMX_AllocateBuffer(this->handle, &buf, PORT_INDEX_IN, this,
Expand All @@ -316,11 +317,7 @@ OmxEncoder::OmxEncoder(const char* filename, int width, int height, int fps, int
out_port.nBufferSize));
}

wait_for_state(OMX_StateIdle);

OMX_CHECK(OMX_SendCommand(this->handle, OMX_CommandStateSet, OMX_StateExecuting, NULL));

wait_for_state(OMX_StateExecuting);
set_state_blocking(OMX_StateExecuting);

// give omx all the output buffers
for (auto &buf : this->out_buf_headers) {
Expand Down Expand Up @@ -593,11 +590,8 @@ void OmxEncoder::encoder_close() {
OmxEncoder::~OmxEncoder() {
assert(!this->is_open);

OMX_CHECK(OMX_SendCommand(this->handle, OMX_CommandStateSet, OMX_StateIdle, NULL));

wait_for_state(OMX_StateIdle);

OMX_CHECK(OMX_SendCommand(this->handle, OMX_CommandStateSet, OMX_StateLoaded, NULL));
set_state_blocking(OMX_StateIdle);
set_state_blocking(OMX_StateLoaded);

for (auto &buf : this->in_buf_headers) {
OMX_CHECK(OMX_FreeBuffer(this->handle, PORT_INDEX_IN, buf));
Expand All @@ -607,8 +601,6 @@ OmxEncoder::~OmxEncoder() {
OMX_CHECK(OMX_FreeBuffer(this->handle, PORT_INDEX_OUT, buf));
}

wait_for_state(OMX_StateLoaded);

OMX_CHECK(OMX_FreeHandle(this->handle));

OMX_BUFFERHEADERTYPE *out_buf;
Expand Down
2 changes: 1 addition & 1 deletion selfdrive/loggerd/omx_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class OmxEncoder : public VideoEncoder {
OMX_BUFFERHEADERTYPE *buffer);

private:
void wait_for_state(OMX_STATETYPE state);
void set_state_blocking(OMX_STATETYPE state);
static void handle_out_buf(OmxEncoder *e, OMX_BUFFERHEADERTYPE *out_buf);

pthread_mutex_t lock;
Expand Down

0 comments on commit 36a596e

Please sign in to comment.