How to show video frame by frame in Qt?
-
Hi,
I used linux OS, I have system decoder, so I can play any type of video using Qt Player.
Now I am trying to play video frame by frame, In my code I get frame from RTSP, and I stored it in one buffer, and this buffer I pass to player.
sample code:
QMediaPlayer* player[screen]; QVideoWidget* vw[screen]; .... while(1){ if (NULL != fpFrames) { fwrite(u8NAL, 4, 1, fpFrames); fwrite(streamData->dataBuffer, streamData->dataSize, 1, fpFrames); mBuffer->open(QIODevice::ReadWrite); mBuffer->seek(0); mBuffer->setData((char *)streamData->dataBuffer,streamData->dataSize); mBuffer->write((char *)streamData->dataBuffer); mBuffer->seek(0); if(playerFlag == 0){ player[0]->setMedia(QMediaContent(), mBuffer); playerFlag = 1; player[0]->play(); } }
In above code I get frame in a streamData->dataBuffer, which is a void* type, I store this frame to QBuffer *mBuffer, and I pass this mBuffer to player, but when I run this code, player show only black screen. I store this buffer to one file also, I can play this file in VLC successfully. When I run this code, Application Output show some messages :
QBuffer::setData: Buffer is open
GStreamer; Unable to pause - ""
QBuffer::setData: Buffer is open
QBuffer::setData: Buffer is open
QBuffer::setData: Buffer is open
QBuffer::setData: Buffer is open
QBuffer::setData: Buffer is open.............
Now Please help me to come out this problem.
Thanks
Tejas Patel -
Hi,
You neve close mBuffer hence the error message.
-
Thanks @SGaist, but this is not a big issue, problem is I cannot show frame on my player using mBuffer, and ya when I store this buffer to any file and when I play this file it run successfully. How can i set media in a player? so I can show live stream.
-
You're using an infinite loop that never gives control back to the main event loop so the video widget can't be refreshed.
-
Thanks @SGaist : thats the point, now my question is where I have to put player->setmedia and player->play option?
While loop give new data to buffer every time and I want to set this buffer into player, so please give right sequence.
Thanks again..... :)
-
You're using an infinite loop that never gives control back to the main event loop so the video widget can't be refreshed.
@SGaist Please tell me how can I handle buffer and player using thread in qt? means in a main thread buffer get data in while loop and in a thread player try to play that data.
1.while(1){ buffer->write(); }
2.
player->setdata(....); player->play();
I want to handle both task simultaneously.. Please guide me for this task.
Thanks for your great interest in every question... -
It should be other way around: player is in the main thread (because all UI related stuff must be in the main thread) and filling buffer is done in another thread. The second thread emits signal each time there is a new frame available and in main thread you connect to this signal and pass the frame to the player.
Regarding multi-threading in Qt read this: http://doc.qt.io/qt-5/thread-basics.html -
@Tejas-Virpariya Please read the documentation (I posted the link before). It is a complex topic and you should understand what you are doing, giving you some lines of code will not really help.
-
OK, I will try, actually I tried before to understand thread in Qt, it was difficult for me to understand. Thanks again @jsulm.