[c-host] app: add initial frame capture support

This commit is contained in:
Geoffrey McRae 2019-03-02 20:33:21 +11:00
parent 7285f9e9ad
commit 61108ba760
2 changed files with 142 additions and 29 deletions

View File

@ -22,6 +22,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include <stdio.h> #include <stdio.h>
#include <inttypes.h> #include <inttypes.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
#include "debug.h" #include "debug.h"
#include "capture/interfaces.h" #include "capture/interfaces.h"
#include "KVMFR.h" #include "KVMFR.h"
@ -33,9 +34,11 @@ Place, Suite 330, Boston, MA 02111-1307 USA
struct app struct app
{ {
KVMFRHeader * shmHeader; KVMFRHeader * shmHeader;
uint8_t * cursorData; uint8_t * pointerData;
unsigned int cursorDataSize; unsigned int pointerDataSize;
unsigned int cursorOffset; unsigned int pointerOffset;
CaptureInterface * iface;
uint8_t * frames; uint8_t * frames;
unsigned int frameSize; unsigned int frameSize;
@ -43,18 +46,32 @@ struct app
unsigned int frameOffset[MAX_FRAMES]; unsigned int frameOffset[MAX_FRAMES];
bool running; bool running;
osThreadHandle * cursorThread; osEventHandle * updateEvent;
osThreadHandle * pointerThread;
osEventHandle * pointerEvent;
osThreadHandle * frameThread; osThreadHandle * frameThread;
osEventHandle * frameEvent;
}; };
static struct app app; static struct app app;
static int cursorThread(void * opaque) static int pointerThread(void * opaque)
{ {
DEBUG_INFO("Cursor thread started"); DEBUG_INFO("Cursor thread started");
while(app.running) while(app.running)
usleep(10000); {
if (!os_waitEvent(app.pointerEvent) || !app.running)
break;
#if 0
CapturePointer pointer;
pointer->data = app.pointerData;
if (!app.iface->getPointer(&pointer))
DEBUG_ERROR("Failed to get the pointer");
os_signalEvent(app.updateEvent);
#endif
}
DEBUG_INFO("Cursor thread stopped"); DEBUG_INFO("Cursor thread stopped");
return 0; return 0;
@ -64,9 +81,21 @@ static int frameThread(void * opaque)
{ {
DEBUG_INFO("Frame thread started"); DEBUG_INFO("Frame thread started");
int frameIndex = 0;
while(app.running) while(app.running)
usleep(10000); {
if (!os_waitEvent(app.frameEvent) || !app.running)
break;
CaptureFrame frame;
frame.data = app.frame[frameIndex];
if (!app.iface->getFrame(&frame))
DEBUG_ERROR("Failed to get the frame");
os_signalEvent(app.updateEvent);
if (++frameIndex == MAX_FRAMES)
frameIndex = 0;
}
DEBUG_INFO("Frame thread stopped"); DEBUG_INFO("Frame thread stopped");
return 0; return 0;
} }
@ -74,9 +103,9 @@ static int frameThread(void * opaque)
bool startThreads() bool startThreads()
{ {
app.running = true; app.running = true;
if (!os_createThread("CursorThread", cursorThread, NULL, &app.cursorThread)) if (!os_createThread("CursorThread", pointerThread, NULL, &app.pointerThread))
{ {
DEBUG_ERROR("Failed to create the cursor thread"); DEBUG_ERROR("Failed to create the pointer thread");
return false; return false;
} }
@ -94,6 +123,9 @@ bool stopThreads()
bool ok = true; bool ok = true;
app.running = false; app.running = false;
os_signalEvent(app.frameEvent );
os_signalEvent(app.pointerEvent);
if (app.frameThread && !os_joinThread(app.frameThread, NULL)) if (app.frameThread && !os_joinThread(app.frameThread, NULL))
{ {
DEBUG_WARN("Failed to join the frame thread"); DEBUG_WARN("Failed to join the frame thread");
@ -101,21 +133,21 @@ bool stopThreads()
} }
app.frameThread = NULL; app.frameThread = NULL;
if (app.cursorThread && !os_joinThread(app.cursorThread, NULL)) if (app.pointerThread && !os_joinThread(app.pointerThread, NULL))
{ {
DEBUG_WARN("Failed to join the cursor thread"); DEBUG_WARN("Failed to join the pointer thread");
ok = false; ok = false;
} }
app.cursorThread = NULL; app.pointerThread = NULL;
return ok; return ok;
} }
static bool captureStart(struct CaptureInterface * iface) static bool captureStart()
{ {
DEBUG_INFO("Using : %s", iface->getName()); DEBUG_INFO("Using : %s", app.iface->getName());
const unsigned int maxFrameSize = iface->getMaxFrameSize(); const unsigned int maxFrameSize = app.iface->getMaxFrameSize();
if (maxFrameSize > app.frameSize) if (maxFrameSize > app.frameSize)
{ {
DEBUG_ERROR("Maximum frame size of %d bytes excceds maximum space available", maxFrameSize); DEBUG_ERROR("Maximum frame size of %d bytes excceds maximum space available", maxFrameSize);
@ -142,15 +174,15 @@ int app_main()
DEBUG_INFO("IVSHMEM Address : 0x%" PRIXPTR, (uintptr_t)shmemMap); DEBUG_INFO("IVSHMEM Address : 0x%" PRIXPTR, (uintptr_t)shmemMap);
app.shmHeader = (KVMFRHeader *)shmemMap; app.shmHeader = (KVMFRHeader *)shmemMap;
app.cursorData = (uint8_t *)ALIGN_UP(shmemMap + sizeof(KVMFRHeader)); app.pointerData = (uint8_t *)ALIGN_UP(shmemMap + sizeof(KVMFRHeader));
app.cursorDataSize = 1048576; // 1MB fixed for cursor size, should be more then enough app.pointerDataSize = 1048576; // 1MB fixed for pointer size, should be more then enough
app.cursorOffset = app.cursorData - shmemMap; app.pointerOffset = app.pointerData - shmemMap;
app.frames = (uint8_t *)ALIGN_UP(app.cursorData + app.cursorDataSize); app.frames = (uint8_t *)ALIGN_UP(app.pointerData + app.pointerDataSize);
app.frameSize = ALIGN_DN((shmemSize - (app.frames - shmemMap)) / MAX_FRAMES); app.frameSize = ALIGN_DN((shmemSize - (app.frames - shmemMap)) / MAX_FRAMES);
DEBUG_INFO("Max Cursor Size : %u MiB" , app.cursorDataSize / 1048576); DEBUG_INFO("Max Cursor Size : %u MiB" , app.pointerDataSize / 1048576);
DEBUG_INFO("Max Frame Size : %u MiB" , app.frameSize / 1048576); DEBUG_INFO("Max Frame Size : %u MiB" , app.frameSize / 1048576);
DEBUG_INFO("Cursor : 0x%" PRIXPTR " (0x%08x)", (uintptr_t)app.cursorData, app.cursorOffset); DEBUG_INFO("Cursor : 0x%" PRIXPTR " (0x%08x)", (uintptr_t)app.pointerData, app.pointerOffset);
for (int i = 0; i < MAX_FRAMES; ++i) for (int i = 0; i < MAX_FRAMES; ++i)
{ {
@ -159,7 +191,7 @@ int app_main()
DEBUG_INFO("Frame %d : 0x%" PRIXPTR " (0x%08x)", i, (uintptr_t)app.frame[i], app.frameOffset[i]); DEBUG_INFO("Frame %d : 0x%" PRIXPTR " (0x%08x)", i, (uintptr_t)app.frame[i], app.frameOffset[i]);
} }
struct CaptureInterface * iface = NULL; CaptureInterface * iface = NULL;
for(int i = 0; CaptureInterfaces[i]; ++i) for(int i = 0; CaptureInterfaces[i]; ++i)
{ {
iface = CaptureInterfaces[i]; iface = CaptureInterfaces[i];
@ -185,17 +217,50 @@ int app_main()
goto fail; goto fail;
} }
if (!captureStart(iface)) app.iface = iface;
app.frameEvent = os_createEvent();
if (!app.frameEvent)
{
DEBUG_ERROR("Failed to create the frame event");
exitcode = -1;
goto exit;
}
app.updateEvent = os_createEvent();
if (!app.updateEvent)
{
DEBUG_ERROR("Failed to create the update event");
exitcode = -1;
goto exit;
}
app.pointerEvent = os_createEvent();
if (!app.pointerEvent)
{
DEBUG_ERROR("Failed to create the pointer event");
exitcode = -1;
goto exit;
}
if (!captureStart())
{ {
exitcode = -1; exitcode = -1;
goto exit; goto exit;
} }
// start signalled
os_signalEvent(app.updateEvent);
while(app.running) while(app.running)
{ {
bool hasFrameUpdate = false; // wait for one of the threads to flag an update
bool hasPointerUpdate = false; if (!os_waitEvent(app.updateEvent) || !app.running)
switch(iface->capture(&hasFrameUpdate, &hasPointerUpdate)) break;
bool frameUpdate = false;
bool pointerUpdate = false;
switch(iface->capture(&frameUpdate, &pointerUpdate))
{ {
case CAPTURE_RESULT_OK: case CAPTURE_RESULT_OK:
break; break;
@ -218,7 +283,7 @@ int app_main()
goto finish; goto finish;
} }
if (!captureStart(iface)) if (!captureStart())
{ {
exitcode = -1; exitcode = -1;
goto finish; goto finish;
@ -230,11 +295,35 @@ int app_main()
exitcode = -1; exitcode = -1;
goto finish; goto finish;
} }
if (frameUpdate && !os_signalEvent(app.frameEvent))
{
DEBUG_ERROR("Failed to signal the frame thread");
exitcode = -1;
goto finish;
}
if (pointerUpdate && !os_signalEvent(app.pointerEvent))
{
DEBUG_ERROR("Failed to signal the pointer thread");
exitcode = -1;
goto finish;
}
} }
finish: finish:
stopThreads(); stopThreads();
exit: exit:
if (app.pointerEvent)
os_freeEvent(app.pointerEvent);
if (app.frameEvent)
os_freeEvent(app.frameEvent);
if (app.updateEvent)
os_freeEvent(app.updateEvent);
iface->deinit(); iface->deinit();
iface->free(); iface->free();
fail: fail:
@ -245,4 +334,5 @@ fail:
void app_quit() void app_quit()
{ {
app.running = false; app.running = false;
os_signalEvent(app.updateEvent);
} }

View File

@ -31,7 +31,27 @@ typedef enum CaptureResult
} }
CaptureResult; CaptureResult;
struct CaptureInterface typedef enum CaptureFormat
{
CAPTURE_FMT_BGRA,
CAPTURE_FMT_RGBA,
CAPTURE_FMT_RGBA10,
CAPTURE_FMT_YUV420,
CAPTURE_FMT_MAX
}
CaptureFormat;
typedef struct CaptureFrame
{
unsigned int width;
unsigned int height;
unsigned int pitch;
CaptureFormat format;
void * data;
}
CaptureFrame;
typedef struct CaptureInterface
{ {
const char * (*getName )(); const char * (*getName )();
bool (*create )(); bool (*create )();
@ -43,4 +63,7 @@ struct CaptureInterface
CaptureResult (*capture)( CaptureResult (*capture)(
bool * hasFrameUpdate, bool * hasFrameUpdate,
bool * hasPointerUpdate); bool * hasPointerUpdate);
};
bool (*getFrame)(CaptureFrame * frame);
}
CaptureInterface;