summaryrefslogtreecommitdiff
path: root/test/timer-thread.cpp
blob: 2f901787f5fb64614dc091c9084bc14e8f783103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * timer-thread.cpp - Threaded timer test
 */

#include <chrono>
#include <iostream>

#include <libcamera/event_dispatcher.h>
#include <libcamera/timer.h>

#include "libcamera/internal/thread.h"

#include "test.h"

using namespace std;
using namespace libcamera;

class TimeoutHandler : public Object
{
public:
	TimeoutHandler()
		: timer_(this), timeout_(false)
	{
		timer_.timeout.connect(this, &TimeoutHandler::timeoutHandler);
		timer_.start(100);
	}

	void restart()
	{
		timeout_ = false;
		timer_.start(100);
	}

	bool timeout() const
	{
		return timeout_;
	}

private:
	void timeoutHandler(Timer *timer)
	{
		timeout_ = true;
	}

	Timer timer_;
	bool timeout_;
};

class TimerThreadTest : public Test
{
protected:
	int init()
	{
		thread_.start();
		timeout_.moveToThread(&thread_);

		return TestPass;
	}

	int run()
	{
		/*
		 * Test that the timer expires and emits the timeout signal in
		 * the thread it belongs to.
		 */
		this_thread::sleep_for(chrono::milliseconds(200));

		if (!timeout_.timeout()) {
			cout << "Timer expiration test failed" << endl;
			return TestFail;
		}

		/*
		 * Test that starting the timer from another thread fails. We
		 * need to interrupt the event dispatcher to make sure we don't
		 * succeed simply because the event dispatcher hasn't noticed
		 * the timer restart.
		 */
		timeout_.restart();
		thread_.eventDispatcher()->interrupt();

		this_thread::sleep_for(chrono::milliseconds(200));

		if (timeout_.timeout()) {
			cout << "Timer restart test failed" << endl;
			return TestFail;
		}

		return TestPass;
	}

	void cleanup()
	{
		/* Must stop thread before destroying timeout. */
		thread_.exit(0);
		thread_.wait();
	}

private:
	TimeoutHandler timeout_;
	Thread thread_;
};

TEST_REGISTER(TimerThreadTest)
<< "SDL sink only supports one camera stream at present, streaming first camera stream" << std::endl; } else if (config.empty()) { std::cerr << "Require at least one camera stream to process" << std::endl; return -EINVAL; } const libcamera::StreamConfiguration &cfg = config.at(0); rect_.w = cfg.size.width; rect_.h = cfg.size.height; switch (cfg.pixelFormat) { #ifdef HAVE_LIBJPEG case libcamera::formats::MJPEG: texture_ = std::make_unique<SDLTextureMJPG>(rect_); break; #endif #if SDL_VERSION_ATLEAST(2, 0, 16) case libcamera::formats::NV12: texture_ = std::make_unique<SDLTextureNV12>(rect_, cfg.stride); break; #endif case libcamera::formats::YUYV: texture_ = std::make_unique<SDLTextureYUYV>(rect_, cfg.stride); break; default: std::cerr << "Unsupported pixel format " << cfg.pixelFormat.toString() << std::endl; return -EINVAL; }; return 0; } int SDLSink::start() { int ret = SDL_Init(SDL_INIT_VIDEO); if (ret) { std::cerr << "Failed to initialize SDL: " << SDL_GetError() << std::endl; return ret; } init_ = true; window_ = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, rect_.w, rect_.h, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); if (!window_) { std::cerr << "Failed to create SDL window: " << SDL_GetError() << std::endl; return -EINVAL; } renderer_ = SDL_CreateRenderer(window_, -1, 0); if (!renderer_) { std::cerr << "Failed to create SDL renderer: " << SDL_GetError() << std::endl; return -EINVAL; } /* * Set for scaling purposes, not critical, don't return in case of * error. */ ret = SDL_RenderSetLogicalSize(renderer_, rect_.w, rect_.h); if (ret) std::cerr << "Failed to set SDL render logical size: " << SDL_GetError() << std::endl; ret = texture_->create(renderer_); if (ret) { return ret; } /* \todo Make the event cancellable to support stop/start cycles. */ EventLoop::instance()->addTimerEvent( 10ms, std::bind(&SDLSink::processSDLEvents, this)); return 0; } int SDLSink::stop() { texture_.reset(); if (renderer_) { SDL_DestroyRenderer(renderer_); renderer_ = nullptr; } if (window_) { SDL_DestroyWindow(window_); window_ = nullptr; } if (init_) { SDL_Quit(); init_ = false; } return FrameSink::stop(); } void SDLSink::mapBuffer(FrameBuffer *buffer) { std::unique_ptr<Image> image = Image::fromFrameBuffer(buffer, Image::MapMode::ReadOnly); assert(image != nullptr); mappedBuffers_[buffer] = std::move(image); } bool SDLSink::processRequest(Request *request) { for (auto [stream, buffer] : request->buffers()) { renderBuffer(buffer); break; /* to be expanded to launch SDL window per buffer */ } return true; } /* * Process SDL events, required for things like window resize and quit button */ void SDLSink::processSDLEvents() { for (SDL_Event e; SDL_PollEvent(&e);) { if (e.type == SDL_QUIT) { /* Click close icon then quit */ EventLoop::instance()->exit(0); } } } void SDLSink::renderBuffer(FrameBuffer *buffer) { Image *image = mappedBuffers_[buffer].get(); std::vector<Span<const uint8_t>> planes; unsigned int i = 0; planes.reserve(buffer->metadata().planes().size()); for (const FrameMetadata::Plane &meta : buffer->metadata().planes()) { Span<uint8_t> data = image->data(i);