      checked_ioctl(fd, VIDIOC_ENCODER_CMD, &encoder_cmd);
      // join waits for V4L2_QCOM_BUF_FLAG_EOS
      dequeue_handler_thread.join();
      assert(extras.empty());
      writer_close();
    }
    this->is_open = false;
  }
  
  V4LEncoder::~V4LEncoder() {
 
</s> remove   writer_close();
 </s> add  </s> remove   writer_open(path);
 </s> add  </s> remove   if (e->write) {
    e->to_write.push(words);
  } else {
    delete words;
  }
}

// TODO: writing should be moved to loggerd
void VideoEncoder::write_handler(VideoEncoder *e, const char *path) {
  VideoWriter writer(path, e->filename, e->codec != cereal::EncodeIndex::Type::FULL_H_E_V_C, e->out_width, e->out_height, e->fps, e->codec);

  bool first = true;
  kj::Array<capnp::word>* out_buf;
  while ((out_buf = e->to_write.pop())) {
    capnp::FlatArrayMessageReader cmsg(*out_buf);
    cereal::Event::Reader event = cmsg.getRoot<cereal::Event>();

    auto edata = (e->type == DriverCam) ? event.getDriverEncodeData() :
      ((e->type == WideRoadCam) ? event.getWideRoadEncodeData() :
      (e->in_width == e->out_width ? event.getRoadEncodeData() : event.getQRoadEncodeData()));
    auto idx = edata.getIdx();
    auto flags = idx.getFlags();

    if (first) {
      assert(flags & V4L2_BUF_FLAG_KEYFRAME);
      auto header = edata.getHeader();
      writer.write((uint8_t *)header.begin(), header.size(), idx.getTimestampEof()/1000, true, false);
      first = false;
    }

    // dangerous cast from const, but should be fine
    auto data = edata.getData();
    if (data.size() > 0) {
      writer.write((uint8_t *)data.begin(), data.size(), idx.getTimestampEof()/1000, false, flags & V4L2_BUF_FLAG_KEYFRAME);
    }

    // free the data
    delete out_buf;
  }

  // VideoWriter is freed on out of scope
 </s> add   delete words; </s> remove   void writer_open(const char* path) {
    if (this->write) write_handler_thread = std::thread(VideoEncoder::write_handler, this, path);
  }

  void writer_close() {
    if (this->write) {
      to_write.push(NULL);
      write_handler_thread.join();
    }
    assert(to_write.empty());
  }
 </s> add  </s> remove     bitrate(bitrate), codec(codec), out_width(out_width), out_height(out_height), write(write) { }
 </s> add     bitrate(bitrate), codec(codec), out_width(out_width), out_height(out_height) { }
      checked_ioctl(fd, VIDIOC_ENCODER_CMD, &encoder_cmd);
      // join waits for V4L2_QCOM_BUF_FLAG_EOS
      dequeue_handler_thread.join();
      assert(extras.empty());

    }
    this->is_open = false;
  }
  
  V4LEncoder::~V4LEncoder() {
keep keep keep keep replace keep keep keep keep keep
 Encoderd: encoders don't write, they only encode (#28499)

* Encoders dont write, they encode

* Delete words

* unused

---------

Co-authored-by: Harald Schaefer <torq_boi@Haralds-MacBook-Air.local>
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> 
system/loggerd/encoder/v4l_encoder.cc