将 SDL 帧发送到另一个进程并显示它

Send SDL frame to another process and display it

我正在尝试将帧发送到另一个进程以显示它。我正在使用 dranger tutorial02.

我想在调用 sws_scale 之后将 SDL_Overlay 结构序列化为字节,将其发送到其他进程,反序列化并调用 SDL_DisplayYUVOverlay 来显示它。

你觉得这是我最好的选择吗?

如果是这样,我很难序列化这个结构。这是代码:

size_t size_of_Overlay(SDL_Overlay *bmp) {
    /*
     * typedef struct  {
     *
     *  Uint32 format;
     *  int w, h;
     *  int planes;
     *  Uint16 *pitches;
     *  Uint8  **pixels;
     *  Uint32 hw_overlay:1; <- can I ignore it? cant point to a bit-field..
     *  
     *  } SDL_Overlay;
     */ 
     //      w,h,planes      format             pitches                pixels
    return sizeof(int)*3 + sizeof(Uint32) + sizeof(Uint16)*bmp->w + sizeof(Uint8)*bmp->h*3;
}

void overlay_to_buf(SDL_Overlay* bmp, char* buf) {

    if(!bmp || !buf) {
        perror("overlay_to_buf");
        exit(1);
    }

    memcpy(buf, &bmp->format, sizeof(Uint32));
    buf += sizeof(Uint32);

    memcpy(buf, &bmp->w, sizeof(int));
    buf += sizeof(int);

    memcpy(buf, &bmp->h, sizeof(int));
    buf += sizeof(int);

    memcpy(buf, &bmp->planes, sizeof(int));
    buf += sizeof(int);

    memcpy(buf, bmp->pitches, sizeof(Uint16)*bmp->w);
    buf += sizeof(Uint16)*bmp->w;

    memcpy(buf, bmp->pixels[0], sizeof(Uint8)*bmp->h);  
    buf += sizeof(Uint8)*bmp->h;

    memcpy(buf, bmp->pixels[1], sizeof(Uint8)*bmp->h);  
    buf += sizeof(Uint8)*bmp->h;

    memcpy(buf, bmp->pixels[2], sizeof(Uint8)*bmp->h);  
    buf += sizeof(Uint8)*bmp->h;
}


void buf_to_overlay(SDL_Overlay *bmp, char* buf) {

    if(!bmp || !buf) {
        perror("buf_to_overlay");
        exit(1);
    }

    memcpy(&bmp->format, buf, sizeof(Uint32));
    buf += sizeof(Uint32);

    memcpy(&bmp->w, buf, sizeof(int));
    buf += sizeof(int);

    memcpy(&bmp->h, buf, sizeof(int));
    buf += sizeof(int);

    memcpy(&bmp->planes, buf, sizeof(int));
    buf += sizeof(int);

    bmp->pitches = (Uint16*)malloc(sizeof(Uint16)*bmp->w);
    memcpy(bmp->pitches, buf, sizeof(Uint16)*bmp->w);
    buf += sizeof(Uint16)*bmp->w;

    bmp->pixels[0] = (Uint8*)malloc(sizeof(Uint8)*bmp->h);
    memcpy(bmp->pixels[0], buf, sizeof(Uint8)*bmp->h);
    buf += sizeof(Uint8)*bmp->h;

    bmp->pixels[1] = (Uint8*)malloc(sizeof(Uint8)*bmp->h);
    memcpy(bmp->pixels[1], buf, sizeof(Uint8)*bmp->h);
    buf += sizeof(Uint8)*bmp->h;

    bmp->pixels[2] = (Uint8*)malloc(sizeof(Uint8)*bmp->h);
    memcpy(bmp->pixels[2], buf, sizeof(Uint8)*bmp->h);
    buf += sizeof(Uint8)*bmp->h;
}

我已经成功序列化它并显示在另一个进程上..

序列化方式错误,正确代码如下:

size_t size_of_Overlay(SDL_Overlay *bmp) {
    /*
     * typedef struct  {
     *
     *  Uint32 format;
     *  int w, h;
     *  int planes;
     *  Uint16 *pitches;
     *  Uint8  **pixels;
     *  Uint32 hw_overlay:1; <- doesn't being accounted
     *  
     *  } SDL_Overlay;
     */                                                     
    return sizeof(int)*3 + sizeof(Uint32) + sizeof(Uint16)*3 + sizeof(Uint8)*bmp->h*bmp->w*3;
}

void overlay_to_buf(SDL_Overlay* bmp, char* buf) {

    if(!bmp || !buf) {
        perror("overlay_to_buf");
        exit(-1);
    }

    memcpy(buf, &bmp->format, sizeof(Uint32));
    buf += sizeof(Uint32);

    memcpy(buf, &bmp->w, sizeof(int));
    buf += sizeof(int);

    memcpy(buf, &bmp->h, sizeof(int));
    buf += sizeof(int);

    memcpy(buf, &bmp->planes, sizeof(int));
    buf += sizeof(int);

    memcpy(buf, &bmp->pitches[0], sizeof(Uint16));
    buf += sizeof(Uint16);

    memcpy(buf, &bmp->pitches[1], sizeof(Uint16));
    buf += sizeof(Uint16);

    memcpy(buf, &bmp->pitches[2], sizeof(Uint16));
    buf += sizeof(Uint16);

    memcpy(buf, bmp->pixels[0], sizeof(Uint8)*bmp->h*bmp->w);   
    buf += sizeof(Uint8)*bmp->h*bmp->w;

    memcpy(buf, bmp->pixels[1], sizeof(Uint8)*bmp->h*bmp->w);   
    buf += sizeof(Uint8)*bmp->h*bmp->w;

    memcpy(buf, bmp->pixels[2], sizeof(Uint8)*bmp->h*bmp->w);   
}


void buf_to_overlay(SDL_Overlay *bmp, char* buf) {

    if(!bmp || !buf) {
        perror("to_message");
        exit(-1);
    }

    memcpy(&bmp->format, buf, sizeof(Uint32));
    buf += sizeof(Uint32);

    memcpy(&bmp->w, buf, sizeof(int));
    buf += sizeof(int);

    memcpy(&bmp->h, buf, sizeof(int));
    buf += sizeof(int);

    memcpy(&bmp->planes, buf, sizeof(int));
    buf += sizeof(int);

    memcpy(&bmp->pitches[0], buf, sizeof(Uint16));
    buf += sizeof(Uint16);

    memcpy(&bmp->pitches[1], buf, sizeof(Uint16));
    buf += sizeof(Uint16);

    memcpy(&bmp->pitches[2], buf, sizeof(Uint16));
    buf += sizeof(Uint16);

    /* pixels are allocated outside the function,
     * just one allocation during the whole video since
     * the screen size doesn't change.
     */
    memcpy(bmp->pixels[0], buf, sizeof(Uint8)*bmp->h*bmp->w);
    buf += sizeof(Uint8)*bmp->h*bmp->w;

    memcpy(bmp->pixels[1], buf, sizeof(Uint8)*bmp->h*bmp->w);
    buf += sizeof(Uint8)*bmp->h*bmp->w;

    memcpy(bmp->pixels[2], buf, sizeof(Uint8)*bmp->h*bmp->w);
}