0

I found a fix to the problem here but I would like to know how this works? I tried using sleep() but it did not work. CPU usage before using SDL_PollEvent(NULL) and after using are the same (viewed in htop). Then what causes this behaviour? This is sdl portion of my code.

Code:

void* video_stream()
{

    int bytes_per_pixel;
    bytes_per_pixel=(s_format.fmt.pix.sizeimage)/(s_format.fmt.pix.width*s_format.fmt.pix.height);
    SDL_Window *window=NULL;    
    SDL_Renderer *renderer=NULL;
    SDL_Texture* texture=NULL;
    SDL_Init(SDL_INIT_EVERYTHING);

    window=SDL_CreateWindow("Stream UYVY",0,0,s_format.fmt.pix.width,s_format.fmt.pix.height,SDL_WINDOW_SHOWN);
    if(window==NULL)
    {
        printf("window creatin failed\n");
    }
    renderer=SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
    if(renderer==NULL)
    {
        printf("renderer not created\n");
    }

    SDL_RenderSetLogicalSize(renderer,s_format.fmt.pix.width,s_format.fmt.pix.height);
    SDL_SetRenderDrawColor(renderer,100,100,100,255);
    SDL_RenderClear(renderer);

    texture= SDL_CreateTexture(renderer,SDL_PIXELFORMAT_UYVY,SDL_TEXTUREACCESS_STREAMING,s_format.fmt.pix.width,s_format.fmt.pix.height);
    if(texture==NULL)
    {
        printf("Create texture failed\n");
        exit(1);
    }

    buf.type=V4L2_BUF_TYPE_VIDEO_CAPTURE;
    buf.memory=V4L2_MEMORY_USERPTR;
    gettimeofday(&strt,NULL);


    while(stop==0)
    {
        if(ioctl(fd,VIDIOC_DQBUF,&buf)<0)
        {
            perror("VIDIOC_DQBUF");
            exit(1);
        }
        frame_count++;
        if(SDL_UpdateTexture(texture,NULL,buffer[buf.index].start,bytes_per_pixel*s_format.fmt.pix.width)<0)
        {
            printf("Update texture failed\n");
        }

        buf.index=buf.index;
        buf.m.userptr=(unsigned long)buffer[buf.index].start;
        buf.length=buffer[buf.index].length;

        if(ioctl(fd,VIDIOC_QBUF,&buf)<0)
        {
            perror("VIDIOC_QBUF");
        }
        SDL_RenderCopy(renderer,texture,NULL,NULL);
        SDL_RenderPresent(renderer);
        SDL_PollEvent(NULL);
    }
    if(stop==1)
    {
        SDL_DestroyTexture(texture);
        SDL_DestroyRenderer(renderer);  
        SDL_DestroyWindow(window);
        SDL_Quit();
        ++stop;
        pthread_exit(NULL);
    }
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Arv
  • 23
  • 4
  • I don't understand the question. Is it about what specific message needs to be responded so window manager will believe that your application is doing something meaningful and it probably shouldn't mark it as hanged? If so, this is very platform specific (and probably window manager specific), and have very questionable use value. – keltar Aug 09 '18 at 16:44
  • @genpfault Like you said I would like to understand, 1. how this marking of a window as unresponsive works 2. how using PollEvent(NULL) fixes the issue and why sleep() is not able to fix the issue. 3. Is using PollEvent(NULL) a good solution or some other changes in the programming can be done to fix the issue? I am using Ubuntu 14.04. – Arv Aug 10 '18 at 05:07

1 Answers1

0

SDL_PollEvent() pumps the system event loop for your window(s) (via SDL_PumpEvents()), letting the OS know your process is alive and well (...or at least responding to window events in a timely manner).

genpfault
  • 51,148
  • 11
  • 85
  • 139