mirror of
https://github.com/lxsang/antd-wvnc-plugin.git
synced 2025-03-12 19:02:46 +01:00
add jpeg support
This commit is contained in:
parent
3739769c71
commit
c81bd71534
4
Makefile
4
Makefile
@ -13,9 +13,9 @@ ifeq ($(UNAME_S),Darwin)
|
|||||||
DEP= -I/usr/local/opt/jpeg-turbo/include -L/usr/local/opt/jpeg-turbo/lib
|
DEP= -I/usr/local/opt/jpeg-turbo/include -L/usr/local/opt/jpeg-turbo/lib
|
||||||
endif
|
endif
|
||||||
|
|
||||||
PLUGINLIBS = libantd.$(EXT) -lvncclient -lpthread -lz# -lsqlite3
|
PLUGINLIBS = libantd.$(EXT) -lvncclient -lpthread -lz -ljpeg# -lsqlite3
|
||||||
|
|
||||||
PCFLAGS=-W -Wall -g -D DEBUG $(PPF_FLAG) $(DEP) -D USE_ZLIB
|
PCFLAGS=-W -Wall -g -D DEBUG $(PPF_FLAG) $(DEP) -D USE_JPEG #-D USE_ZLIB
|
||||||
|
|
||||||
main: $(PLUGINSDEP) $(PLUGINS) #lib
|
main: $(PLUGINSDEP) $(PLUGINS) #lib
|
||||||
|
|
||||||
|
@ -1 +1,5 @@
|
|||||||
# antd-wvnc-plugin
|
# antd-wvnc-plugin
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
* Zlib
|
||||||
|
* libjpeg-turbo
|
130
wvnc.c
130
wvnc.c
@ -5,6 +5,9 @@
|
|||||||
#ifdef USE_ZLIB
|
#ifdef USE_ZLIB
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_JPEG
|
||||||
|
#include <jpeglib.h>
|
||||||
|
#endif
|
||||||
#include "plugin.h"
|
#include "plugin.h"
|
||||||
#define get_user_data(x) ((wvnc_user_data_t *)x)
|
#define get_user_data(x) ((wvnc_user_data_t *)x)
|
||||||
#define R_SHIFT(x) (0)
|
#define R_SHIFT(x) (0)
|
||||||
@ -47,25 +50,82 @@ static rfbCredential *get_credential(rfbClient *cl, int credentialType);
|
|||||||
static void update(rfbClient *cl, int x, int y, int w, int h);
|
static void update(rfbClient *cl, int x, int y, int w, int h);
|
||||||
|
|
||||||
#ifdef USE_ZLIB
|
#ifdef USE_ZLIB
|
||||||
int buff_compress(uint8_t* src, uint8_t* dest, int len)
|
int zlib_compress(uint8_t *src, int len)
|
||||||
{
|
{
|
||||||
|
uint8_t *dest = (uint8_t *)malloc(len);
|
||||||
z_stream defstream;
|
z_stream defstream;
|
||||||
defstream.zalloc = Z_NULL;
|
defstream.zalloc = Z_NULL;
|
||||||
defstream.zfree = Z_NULL;
|
defstream.zfree = Z_NULL;
|
||||||
defstream.opaque = Z_NULL;
|
defstream.opaque = Z_NULL;
|
||||||
// setup "a" as the input and "b" as the compressed output
|
// setup "a" as the input and "b" as the compressed output
|
||||||
defstream.avail_in = (uInt)len; // size of input
|
defstream.avail_in = (uInt)len; // size of input
|
||||||
defstream.next_in = (Bytef *)src; // input char array
|
defstream.next_in = (Bytef *)src; // input char array
|
||||||
defstream.avail_out = (uInt)len; // size of output
|
defstream.avail_out = (uInt)len; // size of output
|
||||||
defstream.next_out = (Bytef *)dest; // output char array
|
defstream.next_out = (Bytef *)dest; // output char array
|
||||||
|
|
||||||
// the actual compression work.
|
// the actual compression work.
|
||||||
deflateInit(&defstream, Z_BEST_COMPRESSION);
|
deflateInit(&defstream, Z_BEST_COMPRESSION);
|
||||||
deflate(&defstream, Z_FINISH);
|
deflate(&defstream, Z_FINISH);
|
||||||
deflateEnd(&defstream);
|
deflateEnd(&defstream);
|
||||||
|
memcpy(src, dest, defstream.total_out);
|
||||||
|
free(dest);
|
||||||
return defstream.total_out;
|
return defstream.total_out;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef USE_JPEG
|
||||||
|
int jpeg_compress(uint8_t *buff, int w, int h, int bbp)
|
||||||
|
{
|
||||||
|
uint8_t *tmp = buff;
|
||||||
|
/*if(bbp == 4)
|
||||||
|
{
|
||||||
|
tmp = (uint8_t*)malloc(w*h*(bbp-1));
|
||||||
|
for(int i = 0; i < w*h; i++)
|
||||||
|
{
|
||||||
|
memcpy(tmp + (i*(bbp-1)), buff+ i*bbp, bbp-1 );
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
struct jpeg_compress_struct cinfo = {0};
|
||||||
|
struct jpeg_error_mgr jerror = {0};
|
||||||
|
jerror.trace_level = 10;
|
||||||
|
cinfo.err = jpeg_std_error(&jerror);
|
||||||
|
jerror.trace_level = 10;
|
||||||
|
cinfo.err->trace_level = 10;
|
||||||
|
jpeg_create_compress(&cinfo);
|
||||||
|
|
||||||
|
uint8_t *out = NULL;
|
||||||
|
unsigned long outbuffer_size = 0;
|
||||||
|
jpeg_mem_dest(&cinfo, &out, &outbuffer_size);
|
||||||
|
cinfo.image_width = w;
|
||||||
|
cinfo.image_height = h;
|
||||||
|
cinfo.input_components = bbp;
|
||||||
|
cinfo.in_color_space = bbp==4?JCS_EXT_RGBA:JCS_RGB;
|
||||||
|
jpeg_set_defaults(&cinfo);
|
||||||
|
jpeg_set_quality(&cinfo, 50, true);
|
||||||
|
jpeg_start_compress(&cinfo, true);
|
||||||
|
//unsigned counter = 0;
|
||||||
|
JSAMPROW row_pointer[1];
|
||||||
|
while (cinfo.next_scanline < cinfo.image_height)
|
||||||
|
{
|
||||||
|
row_pointer[0] = (JSAMPROW)(&tmp[cinfo.next_scanline * w * bbp]);
|
||||||
|
unsigned return_code = jpeg_write_scanlines(&cinfo, row_pointer, 1);
|
||||||
|
}
|
||||||
|
jpeg_finish_compress(&cinfo);
|
||||||
|
jpeg_destroy_compress(&cinfo);
|
||||||
|
//LOG("before %d after %d\n", w*h*bbp, );
|
||||||
|
if(outbuffer_size < w*h*bbp)
|
||||||
|
{
|
||||||
|
memcpy(buff, out, outbuffer_size);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
outbuffer_size = 0;
|
||||||
|
}
|
||||||
|
//if(bbp == 4) free(tmp);
|
||||||
|
free(out);
|
||||||
|
return outbuffer_size;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
int get_pixel_format(uint8_t deep, wvnc_pixel_format_t *d)
|
int get_pixel_format(uint8_t deep, wvnc_pixel_format_t *d)
|
||||||
{
|
{
|
||||||
switch (deep)
|
switch (deep)
|
||||||
@ -187,7 +247,7 @@ static rfbBool resize(rfbClient *client)
|
|||||||
client->updateRect.x = client->updateRect.y = 0;
|
client->updateRect.x = client->updateRect.y = 0;
|
||||||
client->updateRect.w = width;
|
client->updateRect.w = width;
|
||||||
client->updateRect.h = height;
|
client->updateRect.h = height;
|
||||||
//depth = 32;
|
//depth = 24;
|
||||||
void *data = rfbClientGetClientData(client, identify());
|
void *data = rfbClientGetClientData(client, identify());
|
||||||
wvnc_user_data_t *user_data = get_user_data(data);
|
wvnc_user_data_t *user_data = get_user_data(data);
|
||||||
//client->width = sdl->pitch / (depth / 8);
|
//client->width = sdl->pitch / (depth / 8);
|
||||||
@ -237,14 +297,8 @@ static void update(rfbClient *client, int x, int y, int w, int h)
|
|||||||
uint8_t bbp = (uint8_t)client->format.bitsPerPixel / 8;
|
uint8_t bbp = (uint8_t)client->format.bitsPerPixel / 8;
|
||||||
int size = w * h * bbp;
|
int size = w * h * bbp;
|
||||||
uint8_t *cmd = (uint8_t *)malloc(size + 10); // + 9
|
uint8_t *cmd = (uint8_t *)malloc(size + 10); // + 9
|
||||||
#ifdef USE_ZLIB
|
uint8_t *tmp = cmd + 10;
|
||||||
uint8_t* buff = (uint8_t *)malloc(size);
|
uint8_t flag = 0;
|
||||||
if (!buff)
|
|
||||||
{
|
|
||||||
vnc_fatal(user_data, "Cannot allocate data for update");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (!cmd)
|
if (!cmd)
|
||||||
{
|
{
|
||||||
vnc_fatal(user_data, "Cannot allocate data for update");
|
vnc_fatal(user_data, "Cannot allocate data for update");
|
||||||
@ -255,23 +309,19 @@ static void update(rfbClient *client, int x, int y, int w, int h)
|
|||||||
LOG("Client frame buffe data not found\n");
|
LOG("Client frame buffe data not found\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#ifdef USE_ZLIB
|
uint8_t *dest_ptr = tmp;
|
||||||
uint8_t *dest_ptr = buff;
|
|
||||||
#else
|
|
||||||
uint8_t *dest_ptr = cmd + 10;
|
|
||||||
#endif
|
|
||||||
uint8_t *src_ptr;
|
uint8_t *src_ptr;
|
||||||
|
|
||||||
//cpy line by line
|
//cpy line by line
|
||||||
int cw = client->width;
|
int cw = client->width;
|
||||||
for(int j = y; j < y+ h; j++)
|
for (int j = y; j < y + h; j++)
|
||||||
{
|
{
|
||||||
src_ptr = client->frameBuffer + ( j*cw*bbp + x*bbp);
|
src_ptr = client->frameBuffer + (j * cw * bbp + x * bbp);
|
||||||
memcpy(dest_ptr, src_ptr, w*bbp);
|
memcpy(dest_ptr, src_ptr, w * bbp);
|
||||||
if(bbp == 4)
|
if (bbp == 4)
|
||||||
for(int i= bbp -1; i < w*bbp; i+=bbp)
|
for (int i = bbp - 1; i < w * bbp; i += bbp)
|
||||||
dest_ptr[i] = 255;
|
dest_ptr[i] = 255;
|
||||||
dest_ptr += w*bbp;
|
dest_ptr += w * bbp;
|
||||||
}
|
}
|
||||||
cmd[0] = 0x84; //update command
|
cmd[0] = 0x84; //update command
|
||||||
cmd[1] = (uint8_t)(x & 0xFF);
|
cmd[1] = (uint8_t)(x & 0xFF);
|
||||||
@ -282,16 +332,26 @@ static void update(rfbClient *client, int x, int y, int w, int h)
|
|||||||
cmd[6] = (uint8_t)(w >> 8);
|
cmd[6] = (uint8_t)(w >> 8);
|
||||||
cmd[7] = (uint8_t)(h & 0xFF);
|
cmd[7] = (uint8_t)(h & 0xFF);
|
||||||
cmd[8] = (uint8_t)(h >> 8);
|
cmd[8] = (uint8_t)(h >> 8);
|
||||||
cmd[9] = 0x0;
|
|
||||||
#ifdef USE_ZLIB
|
#ifdef USE_JPEG
|
||||||
cmd[9] = 0x1;
|
if(bbp == 3 || bbp == 4)
|
||||||
size = buff_compress(buff, cmd+10, size);
|
{
|
||||||
|
int ret = jpeg_compress(tmp, w, h, bbp);
|
||||||
|
if(ret > 0)
|
||||||
|
{
|
||||||
|
flag |= 0x01;
|
||||||
|
size = ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_ZLIB
|
||||||
|
flag |= 0x02;
|
||||||
|
size = zlib_compress(tmp, size);
|
||||||
|
#endif
|
||||||
|
cmd[9] = flag;
|
||||||
ws_b(user_data->client, cmd, size + 10);
|
ws_b(user_data->client, cmd, size + 10);
|
||||||
free(cmd);
|
free(cmd);
|
||||||
#ifdef USE_ZLIB
|
|
||||||
free(buff);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static rfbCredential *get_credential(rfbClient *cl, int credentialType)
|
static rfbCredential *get_credential(rfbClient *cl, int credentialType)
|
||||||
@ -380,7 +440,7 @@ void open_session(void *data, const char *addr)
|
|||||||
}
|
}
|
||||||
process(user_data, 0);
|
process(user_data, 0);
|
||||||
//LOG("ENd process \n");
|
//LOG("ENd process \n");
|
||||||
int status = WaitForMessage(user_data->vncl, 500);//500
|
int status = WaitForMessage(user_data->vncl, 500); //500
|
||||||
if (status < 0)
|
if (status < 0)
|
||||||
{
|
{
|
||||||
if (user_data->vncl)
|
if (user_data->vncl)
|
||||||
@ -442,7 +502,7 @@ void *consume_client(void *ptr, wvnc_cmd_t header)
|
|||||||
return NULL;
|
return NULL;
|
||||||
return strdup((char *)header.data);
|
return strdup((char *)header.data);
|
||||||
case 0x03: // client enter a credential
|
case 0x03: // client enter a credential
|
||||||
data = (uint8_t*)malloc(size);
|
data = (uint8_t *)malloc(size);
|
||||||
memcpy(data, header.data, size);
|
memcpy(data, header.data, size);
|
||||||
return data;
|
return data;
|
||||||
break;
|
break;
|
||||||
@ -452,7 +512,7 @@ void *consume_client(void *ptr, wvnc_cmd_t header)
|
|||||||
return data;
|
return data;
|
||||||
break;
|
break;
|
||||||
case 0x05: //mouse event
|
case 0x05: //mouse event
|
||||||
SendPointerEvent(user_data->vncl, header.data[0]|(header.data[1]<<8) , header.data[2]|(header.data[3]<<8), header.data[4]);
|
SendPointerEvent(user_data->vncl, header.data[0] | (header.data[1] << 8), header.data[2] | (header.data[3] << 8), header.data[4]);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return vnc_fatal(user_data, "Unknown client command");
|
return vnc_fatal(user_data, "Unknown client command");
|
||||||
@ -465,7 +525,7 @@ void handle(void *cl, const char *m, const char *rqp, dictionary rq)
|
|||||||
if (ws_enable(rq))
|
if (ws_enable(rq))
|
||||||
{
|
{
|
||||||
#ifdef USE_ZLIB
|
#ifdef USE_ZLIB
|
||||||
LOG("Zlib is enabled\n");
|
LOG("Zlib is enabled\n");
|
||||||
#endif
|
#endif
|
||||||
//set time out for the tcp socket
|
//set time out for the tcp socket
|
||||||
// set timeout to socket
|
// set timeout to socket
|
||||||
|
Loading…
x
Reference in New Issue
Block a user