Skip to content

Commit 59a8df7

Browse files
committed
feat: support libcamera dma source
1 parent c16bb68 commit 59a8df7

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

src/capturer/libcamera_capturer.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ int LibcameraCapturer::width() const { return width_; }
171171

172172
int LibcameraCapturer::height() const { return height_; }
173173

174-
bool LibcameraCapturer::is_dma_capture() const { return false; }
174+
bool LibcameraCapturer::is_dma_capture() const { return true; }
175175

176176
uint32_t LibcameraCapturer::format() const { return format_; }
177177

@@ -188,6 +188,12 @@ LibcameraCapturer &LibcameraCapturer::SetResolution(int width, int height) {
188188
camera_config_->at(0).pixelFormat = libcamera::formats::YUV420;
189189
camera_config_->at(0).bufferCount = buffer_count_;
190190

191+
if (width >= 1280 || height >= 720) {
192+
camera_config_->at(0).colorSpace = libcamera::ColorSpace::Rec709;
193+
} else {
194+
camera_config_->at(0).colorSpace = libcamera::ColorSpace::Smpte170m;
195+
}
196+
191197
auto validation = camera_config_->validate();
192198
if (validation == libcamera::CameraConfiguration::Status::Valid) {
193199
INFO_PRINT("camera validated format: %s.", camera_config_->at(0).toString().c_str());
@@ -292,15 +298,14 @@ void LibcameraCapturer::RequestComplete(libcamera::Request *request) {
292298
auto &buffers = request->buffers();
293299
auto *buffer = buffers.begin()->second;
294300

295-
auto &plane = buffer->planes()[0];
296-
int fd = plane.fd.get();
301+
int fd = buffer->planes()[0].fd.get();
297302
void *data = mapped_buffers_[fd].first;
298303
int length = mapped_buffers_[fd].second;
299304
timeval tv = {};
300305
tv.tv_sec = buffer->metadata().timestamp / 1000000000;
301306
tv.tv_usec = (buffer->metadata().timestamp % 1000000000) / 1000;
302307

303-
auto v4l2_buffer = V4L2Buffer::FromLibcamera((uint8_t *)data, length, tv, format_);
308+
auto v4l2_buffer = V4L2Buffer::FromLibcamera((uint8_t *)data, length, fd, tv, format_);
304309
frame_buffer_ = V4L2FrameBuffer::Create(width_, height_, v4l2_buffer);
305310
NextFrameBuffer(frame_buffer_);
306311

src/common/v4l2_utils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ struct V4L2Buffer {
3737
return buf;
3838
}
3939

40-
static V4L2Buffer FromLibcamera(void *start, int length, timeval timestamp, uint32_t fmt) {
40+
static V4L2Buffer FromLibcamera(void *start, int length, int dmafd, timeval timestamp,
41+
uint32_t fmt) {
4142
V4L2Buffer buf;
4243
buf.start = start;
44+
buf.dmafd = dmafd;
4345
buf.pix_fmt = fmt;
4446
buf.length = length;
4547
buf.timestamp = timestamp;

test/test_libcamera.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ int main(int argc, char *argv[]) {
3232

3333
auto capturer = LibcameraCapturer::Create(args);
3434

35-
auto observer = capturer->AsRawBufferObservable();
36-
observer->Subscribe([&](V4L2Buffer buffer) {
35+
auto observer = capturer->AsFrameBufferObservable();
36+
observer->Subscribe([&](rtc::scoped_refptr<V4L2FrameBuffer> frame_buffer) {
3737
if (i < images_nb) {
38+
auto buffer = frame_buffer->GetRawBuffer();
3839
WriteImage(buffer.start, buffer.length, ++i);
3940
} else {
4041
is_finished = true;

test/test_v4l2_encoder.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "args.h"
2-
#include "capturer/v4l2_capturer.h"
2+
#include "capturer/libcamera_capturer.h"
33
#include "codecs/v4l2/v4l2_encoder.h"
44

55
#include <chrono>
@@ -15,20 +15,20 @@ int main(int argc, char *argv[]) {
1515
bool is_finished = false;
1616
bool has_first_keyframe_ = false;
1717
int images_nb = 0;
18-
int record_sec = 10;
18+
int record_sec = 100;
1919
Args args{
2020
.cameraId = 0,
2121
.fps = 30,
22-
.width = 1280,
23-
.height = 960,
24-
.format = V4L2_PIX_FMT_YUYV,
22+
.width = 1920,
23+
.height = 1080,
24+
.format = V4L2_PIX_FMT_YUV420,
2525
.hw_accel = true,
2626
};
2727

28-
auto capturer = V4L2Capturer::Create(args);
28+
auto capturer = LibcameraCapturer::Create(args);
2929
auto observer = capturer->AsFrameBufferObservable();
3030

31-
auto encoder = V4L2Encoder::Create(args.width, args.height, V4L2_PIX_FMT_YUYV, false);
31+
auto encoder = V4L2Encoder::Create(args.width, args.height, V4L2_PIX_FMT_YUV420, true);
3232

3333
int cam_frame_count = 0;
3434
auto cam_start_time = std::chrono::steady_clock::now();

0 commit comments

Comments
 (0)