Home > Mobile >  Adding timestamp infront of jpeg data to sending it over a websocket
Adding timestamp infront of jpeg data to sending it over a websocket

Time:12-07

I am sending jpeg's to websocket clients with wsserver, now I want to add a unix timestamp infront of jpeg data so that the binary message looks like this:

<timestamp><jpeg_data>

then I can slice that on js side to compute a latency.

static void send_frame(gpointer addr, gpointer client, gpointer provider)
{
    ImgProvider_t* p = provider;
    Watcher* c = client;
    // p->buffer_data is the jpeg pointer
    // p->timestamp are the timestamp as unsigned long long
    ws_sendframe_bin(c->connection, p->buffer_data, vdo_frame_get_size(p->frame));
}

Thats my first programm with c so I have a lack of knowledge. I come from pyton where I would convert the timestamp to bytes and concate them togehter. But here in c, I dont know how to convert the unsigned long long to bytes and append that infront of the p->buffer_data pointer.

Thank u If u have any tipps for me =)

CodePudding user response:

I want to send the timestamp and the jpeg together in one ws_sendframe_bin call.

I don't think there's any free lunch here if you can't split the data into two separate websocket messages. You're going to have to allocate a new buffer to serialize both the timestamp and image bytes into.

In the adjustment to your code below, I'm assuming only one thread calls send_frame. I'm also assuming sizeof(p->timestamp) is a fixed type (like uint32_t) that isn't variable in size between architectures since the receive side can be running anything as well.

static void send_frame(gpointer addr, gpointer client, gpointer provider)
{
    static uint8_t* tmp = NULL;
    static size_t tmpsize = 0;

    ImgProvider_t* p = provider;
    Watcher* c = client;

    size_t needed = vdo_frame_get_size(p->frame)   sizeof(p->timestamp);
    if (needed > tmpsize)
    {
        free(tmp);
        tmp = malloc(needed*2); // double needed so we don't keep repeating this operation
        tmpsize=needed*2;
    }

   
    memcpy(tmp, &p->timestamp, sizeof(p->timestamp));  // include <string.h> for memcpy
    memcpy(tmp sizeof(p->timestamp), p->buffer_data, needed-sizeof(p->timestamp));


    ws_sendframe_bin(c->connection, tmp, needed));
}

The above is just a suggested and easy hack to accomplish what you want. You are not obligated to have the tmp buffer be a static variable inlined into the function. You could allocate and/or define it elsewhere.

  •  Tags:  
  • c
  • Related