not able to switch width and height in the v4l2 VIDIOC_S_FMT

24 Views Asked by At

I try to create a application to switch the V4L2, I can able to switch in the first try after running the application. but afterwards it does not get switch. it can again able to switch only when i restart my PC.

enter image description here

this is my function, how to solve this issue?

bool set_format( short int fmt_nu, short int width, short int height )
{
        struct v4l2_fmtdesc fmtdes;
        struct v4l2_format format;
        bool rtn_status = OK;

                        printf(" in setting Format\n");
        do
        {
                fmtdes.index = fmt_nu;
                fmtdes.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
                if( 0 != ioctl( fd, VIDIOC_ENUM_FMT, &fmtdes ) )
                {
                        printf("Error in setting Format\n");
                        rtn_status = FAIL;
                        break;
                }

                format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
                if (ioctl(fd, VIDIOC_G_FMT, &format) < 0) {
                        perror("VIDIOC_G_FMT");
                        exit(1);
                }

                format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
                //format.fmt.pix.field = V4L2_FIELD_NONE;
                format.fmt.pix.pixelformat = fmtdes.pixelformat;
                format.fmt.pix.width = width;
                format.fmt.pix.width = height;
                format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
                if( -1 == ioctl( fd, VIDIOC_TRY_FMT, &format ) )
                {
                        perror( "E" );
                        rtn_status = FAIL;
                        break;
                }


                if( -1 == ioctl( fd, VIDIOC_G_FMT, &format ) )
                {
                        printf( "TEST_INIOCTL\n" );
                        perror( "E" );
                        rtn_status = FAIL;
                        break;
                }
                        printf( "TEST_OUTICOTL\n" );

                if( fmtdes.pixelformat == format.fmt.pix.pixelformat &&
                                format.fmt.pix.width == width &&
                                format.fmt.pix.height == height )
                {
                        printf( "Format Change Successfully\n" );
                        rtn_status = FAIL;
                }
                else
                {
                        printf("Format Change Fail" );
                        rtn_status = FAIL;
                        break;
                }
        }while( 0 );
        return rtn_status;
}

I tried v4l2-ctl -d0 --set-fmt-video=width=1280,height=720 --verbose, this switch successfull.

0

There are 0 best solutions below