    CL_CHECK(clReleaseCommandQueue(q));
  }
  
  bool CameraBuf::acquire() {
    {
      std::unique_lock<std::mutex> lk(frame_queue_mutex);
      bool got_frame = frame_queue_cv.wait_for(lk, std::chrono::milliseconds(1), [this]{ return !frame_queue.empty(); });
      if (!got_frame) return false;
  
      cur_buf_idx = frame_queue.front();
      frame_queue.pop();
    }
  
    const FrameMetadata &frame_data = camera_bufs_metadata[cur_buf_idx];
    if (frame_data.frame_id == -1) {
      LOGE("no frame data? wtf");
      release();
 
</s> remove void CameraBuf::queue(size_t buf_idx){
  {
    std::lock_guard<std::mutex> lk(frame_queue_mutex);
    frame_queue.push(buf_idx);
  }
  frame_queue_cv.notify_one();
 </s> add void CameraBuf::queue(size_t buf_idx) {
  safe_queue.push(buf_idx); </s> remove         if (encoders.size() > 1) {
          int out_segment_alt = -1;
          encoders[1]->encode_frame(buf->y, buf->u, buf->v,
                                    buf->width, buf->height,
                                    &out_segment_alt, extra.timestamp_eof);
        }

        // publish encode index
        MessageBuilder msg;
        // this is really ugly
        auto eidx = cam_idx == LOG_CAMERA_ID_DCAMERA ? msg.initEvent().initFrontEncodeIdx() :
                    (cam_idx == LOG_CAMERA_ID_ECAMERA ? msg.initEvent().initWideEncodeIdx() : msg.initEvent().initEncodeIdx());
        eidx.setFrameId(extra.frame_id);
        eidx.setTimestampSof(extra.timestamp_sof);
        eidx.setTimestampEof(extra.timestamp_eof);
  #ifdef QCOM2
        eidx.setType(cereal::EncodeIndex::Type::FULL_H_E_V_C);
  #else
        eidx.setType(cam_idx == LOG_CAMERA_ID_DCAMERA ? cereal::EncodeIndex::Type::FRONT : cereal::EncodeIndex::Type::FULL_H_E_V_C);
  #endif
        eidx.setEncodeId(cnt);
        eidx.setSegmentNum(out_segment);
        eidx.setSegmentId(out_id);

        if (lh) {
          auto bytes = msg.toBytes();
          lh_log(lh, bytes.begin(), bytes.size(), false);
 </s> add         if (i == 0 && out_id != -1) {
          // publish encode index
          MessageBuilder msg;
          // this is really ugly
          auto eidx = cam_idx == LOG_CAMERA_ID_DCAMERA ? msg.initEvent().initFrontEncodeIdx() :
                      (cam_idx == LOG_CAMERA_ID_ECAMERA ? msg.initEvent().initWideEncodeIdx() : msg.initEvent().initEncodeIdx());
          eidx.setFrameId(extra.frame_id);
          eidx.setTimestampSof(extra.timestamp_sof);
          eidx.setTimestampEof(extra.timestamp_eof);
    #ifdef QCOM2
          eidx.setType(cereal::EncodeIndex::Type::FULL_H_E_V_C);
    #else
          eidx.setType(cam_idx == LOG_CAMERA_ID_DCAMERA ? cereal::EncodeIndex::Type::FRONT : cereal::EncodeIndex::Type::FULL_H_E_V_C);
    #endif
          eidx.setEncodeId(cnt);
          eidx.setSegmentNum(out_segment);
          eidx.setSegmentId(out_id);
          if (lh) {
            auto bytes = msg.toBytes();
            lh_log(lh, bytes.begin(), bytes.size(), false);
          } </s> remove   queue_push(&e->free_in, (void*)buffer);
 </s> add   e->free_in.push(buffer); </s> remove       {
 </s> add       for (int i = 0; i < encoders.size(); ++i) { </s> remove         int out_id = encoders[0]->encode_frame(buf->y, buf->u, buf->v,
 </s> add         int out_id = encoders[i]->encode_frame(buf->y, buf->u, buf->v,
  if (!safe_queue.try_pop(cur_buf_idx, 1)) return false;
    CL_CHECK(clReleaseCommandQueue(q));
  }
  
  bool CameraBuf::acquire() {
  if (!safe_queue.try_pop(cur_buf_idx, 1)) return false;
  if (!safe_queue.try_pop(cur_buf_idx, 1)) return false;
  if (!safe_queue.try_pop(cur_buf_idx, 1)) return false;
  if (!safe_queue.try_pop(cur_buf_idx, 1)) return false;
  if (!safe_queue.try_pop(cur_buf_idx, 1)) return false;
  if (!safe_queue.try_pop(cur_buf_idx, 1)) return false;
  if (!safe_queue.try_pop(cur_buf_idx, 1)) return false;
  if (!safe_queue.try_pop(cur_buf_idx, 1)) return false;
  
    const FrameMetadata &frame_data = camera_bufs_metadata[cur_buf_idx];
    if (frame_data.frame_id == -1) {
      LOGE("no frame data? wtf");
      release();
keep keep keep keep replace replace replace replace replace replace replace replace keep keep keep keep keep
 remove cqueue, use class SafeQueue (#19774)

* class SafeQueue

* cleanup includes

* space

* add timeout

use try_poll

* add function empty() & size()

* class SafeQueue

* add timeout

use try_poll

* add function empty() & size()

* rebase master

* rebase master

* for loop

* fix bug 
selfdrive/camerad/cameras/camera_common.cc