Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try out a plugin scheme #1

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ struct scrcpy {
struct sc_decoder audio_decoder;
struct sc_recorder recorder;
struct sc_delay_buffer display_buffer;
#ifdef HAVE_VNC
struct sc_vnc_sink vnc_sink;
#endif
struct plugin* plugins[2];
#ifdef HAVE_V4L2
struct sc_v4l2_sink v4l2_sink;
struct sc_delay_buffer v4l2_buffer;
Expand Down Expand Up @@ -316,9 +314,6 @@ scrcpy(struct scrcpy_options *options) {
bool recorder_started = false;
#ifdef HAVE_V4L2
bool v4l2_sink_initialized = false;
#endif
#ifdef HAVE_VNC
bool vnc_sink_initialized = false;
#endif
bool video_demuxer_started = false;
bool audio_demuxer_started = false;
Expand Down Expand Up @@ -703,17 +698,36 @@ scrcpy(struct scrcpy_options *options) {
&s->audio_player.frame_sink);
}
}

#ifdef HAVE_VNC
if (options->vnc_server) {
if (!sc_vnc_sink_init(&s->vnc_sink, "my vnc server", controller)) {
struct sc_vnc_sink plugindata = {
.vncservername = "my vnc server",
.controller = controller,
};
struct plugin p = {
.name = "vnc",
.needs_video_decoder = true,
.sink_initialized = false,
.plugindata = &plugindata,
.init = sc_vnc_sink_init,
.destroy = sc_vnc_sink_destroy,
.frame_source = &s->video_decoder.frame_source,
.should_be_init = options->vnc_server,
};
plugindata.plugin = &p;
s->plugins[0] = &p;
#endif

for(uint8_t i = 0; i<sizeof(s->plugins)/sizeof(void*); i++) {
struct plugin *p = s->plugins[i];
if(p == NULL) continue;
if(!p->init(p->plugindata)) {
printf("bad vnc init \n");
goto end;
}
vnc_sink_initialized = true;
struct sc_frame_source *src = &s->video_decoder.frame_source;
sc_frame_source_add_sink(src, &s->vnc_sink.frame_sink);
p->sink_initialized = true;
sc_frame_source_add_sink(p->frame_source, &(p->frame_sink));
}
#endif

#ifdef HAVE_V4L2
if (options->v4l2_device) {
Expand Down Expand Up @@ -806,11 +820,13 @@ scrcpy(struct scrcpy_options *options) {
sc_v4l2_sink_destroy(&s->v4l2_sink);
}
#endif
#ifdef HAVE_VNC
if (vnc_sink_initialized) {
sc_vnc_sink_destroy(&s->vnc_sink);
for(uint8_t i = 0; i<sizeof(s->plugins)/sizeof(void*); i++) {
struct plugin *p = s->plugins[i];
if(p == NULL) continue;
if (p->sink_initialized) {
p->destroy(p->plugindata);
}
}
#endif

#ifdef HAVE_USB
if (aoa_hid_initialized) {
Expand Down
13 changes: 7 additions & 6 deletions app/src/vnc_sink.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "util/str.h"

/** Downcast frame_sink to sc_vnc_sink */
#define DOWNCAST(SINK) container_of(SINK, struct sc_vnc_sink, frame_sink)
#define DOWNCAST(SINK) container_of(SINK, struct plugin, frame_sink)


static bool
Expand All @@ -24,7 +24,8 @@ sc_vnc_frame_sink_close(struct sc_frame_sink *sink) {

static bool
sc_vnc_frame_sink_push(struct sc_frame_sink *sink, const AVFrame *frame) {
struct sc_vnc_sink *vnc = DOWNCAST(sink);
struct plugin *plugin = DOWNCAST(sink);
struct sc_vnc_sink *vnc = plugin->plugindata;
// XXX: ideally this would get "damage" regions from the decoder
// to prevent marking the entire screen as modified if only a small
// part changed
Expand Down Expand Up @@ -60,7 +61,7 @@ sc_vnc_frame_sink_push(struct sc_frame_sink *sink, const AVFrame *frame) {
}

bool
sc_vnc_sink_init(struct sc_vnc_sink *vs, const char *device_name, struct sc_controller *controller) {
sc_vnc_sink_init(struct sc_vnc_sink *vs) {
uint8_t placeholder_width = 32;
uint8_t placeholder_height = 32;
static const struct sc_frame_sink_ops ops = {
Expand All @@ -69,16 +70,16 @@ sc_vnc_sink_init(struct sc_vnc_sink *vs, const char *device_name, struct sc_cont
.push = sc_vnc_frame_sink_push,
};

vs->frame_sink.ops = &ops;
printf("from init, name is %s\n", vs->vncservername);
vs->plugin->frame_sink.ops = &ops;
vs->bpp = 4;
vs->screen = rfbGetScreen(0, NULL, placeholder_width, placeholder_height, 8, 3, vs->bpp);
vs->screen->desktopName = device_name;
vs->screen->desktopName = vs->vncservername;
vs->screen->alwaysShared = true;
vs->screen->frameBuffer = (char *)malloc(placeholder_width * placeholder_height * vs->bpp);
vs->screen->ptrAddEvent = ptr_add_event;
vs->screen->screenData = vs;
vs->was_down = false;
vs->controller = controller;
rfbInitServer(vs->screen);
rfbRunEventLoop(vs->screen, -1, true); // TODO: integrate into proper lifecycle
return true;
Expand Down
22 changes: 19 additions & 3 deletions app/src/vnc_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,24 @@
#include "frame_buffer.h"
#include "util/tick.h"

struct sc_vnc_sink {
struct plugin {
char name[4];
bool needs_video_decoder;
bool sink_initialized;
bool should_be_init;
//struct sc_delay_buffer delay_buffer;
void* plugindata;
void* frame_source;

struct sc_frame_sink frame_sink; // frame sink trait

bool (*init)(const void* plugindata);
bool (*destroy)(const void* plugindata);
};

struct sc_vnc_sink {
char* vncservername;
struct plugin *plugin;
struct sc_controller *controller;

struct SwsContext * ctx;
Expand All @@ -26,11 +42,11 @@ struct sc_vnc_sink {
uint8_t bpp;

bool was_down;
char *device_name;
};


bool
sc_vnc_sink_init(struct sc_vnc_sink *vs, const char *device_name, struct sc_controller *controller);
sc_vnc_sink_init(struct sc_vnc_sink *vs);

void
sc_vnc_sink_destroy(struct sc_vnc_sink *vs);
Expand Down