PDA

View Full Version : How to play video using libavcodec frames using QLable Widget



vijayQt
15th May 2011, 20:30
Hi All,
I am new to Qt and am implementing video phone in my new company. For displaying video I choosed QLable Widget and I am able to play video frame by frame, But when I trying to
display all frames in one shot It's not displaying (I put nextFrame function in a loop to diplay continue frames). Only I am seeing last frame :(. Is this correct approach or am I doing anything wrong here.
Please help me to come out of this problem.


Here is my code:


/************************************/
Working code: Frame by Frame display
/************************************/

void MainWindow::image2Pixmap(QImage &img,QPixmap &pixmap)
{
// Convert the QImage to a QPixmap for display
pixmap = QPixmap(img.size());
QPainter painter;
painter.begin(&pixmap);
painter.drawImage(0,0,img);
painter.end();
}


void MainWindow::displayFrame()
{.
.
.
.....

QPixmap p;
image2Pixmap(img,p);
// Display the QPixmap
ui->labelVideoFrame->setPixmap(p);
....
....
}


void MainWindow::on_pushButtonNextFrame_clicked()
{
if(!decoder.seekNextFrame())
{
QMessageBox::critical(this,"Error","seekNextFrame failed");
}

decoder.decodeSeekFrame();

displayFrame();
}

/*********************************/
Not Working: play all frames in one shot (playing video)
/*********************************/
void MainWindow::on_pushButtonNextFrame_clicked()
{
while(decoder.seekNextFrame()) {

decoder.decodeSeekFrame();

displayFrame();
}
}



Thanks in advance :)

Lykurg
15th May 2011, 21:25
the while loop is blocking the event loop, so the gui only gets updated at the end of the loop. Thus, move the decoder to a separate thread and connect it to the gui via signal and slot mechanism or call QApplication::processEvents() or read wysota's qq article on how to keep the ui responsive. You will find it in our wiki.

Further, please use the code tags next time.

vijayQt
17th May 2011, 07:17
Thanks for your valuable solution... Now I am able to play the frames continuely by using processEvents() function, but only problem is video playing very very fast (like fast forward) and one more problem is not able to maximus or close my main video while play video :(.

Do I need to follow some other approach for avoiding thses kindof problems. Please cn you provide sample code for this :).

Thanks in advance.

Lykurg
17th May 2011, 07:47
Thanks for your valuable solution... Now I am able to play the frames continuely by using processEvents() function, but only problem is video playing very very fast (like fast forward) and one more problem is not able to maximus or close my main video while play video :(.Yes, that's because your while loop still kind of blocks the entire application. Thus, the only solution is to move all in a thread like I said. But furthermore, can's you use phonon or the QtMultimedia module?


Please cn you provide sample code for this :).Hmm, let me consider: You work in a company. You get paid. I should do your work. hmm. Yes that is how life works today all over the world, but no you have to do it yourself. We are here to help people but not in providing working code. We try to push them in the right direction or simply say what we would do, but it is up to everybody to solve their problems (=coding) alone.

high_flyer
17th May 2011, 08:59
but only problem is video playing very very fast (like fast forward)
That is a good problem!
Usually the problem is to have the video run fast enough.
You need to know what is the speed your video has to have - how many frames per second.
Since it looks you have either a very small frame, or some other reason that makes you frame render very quickly, you can use a QTimer, and fire it in the right frequency for your video.
This will also solve you non responsive GUI problem as you wont be needing your loop.

vijayQt
17th May 2011, 13:39
As I am new to Qt I choosed QLable Wedget by default. I am not sure which widget will be suitable for my requirement.

My requirements are:
-> Display frames of local video file.
-> Display streamed frames from server (like youtube).
-> One Widget should display more than one source (From multiple video cams). Basically widget should display videos from multiple video cams.

Which widget will be suitable for my requirements? Please guide me in right direction so that later on I should not change the widget.

I am using libavcodec for encoding and decoding video and audio. I am not sure Phonon OR QMultiMedia will support this :(. Please let me know your comments.

Thanks in advance

high_flyer
17th May 2011, 13:44
Who said that QLabel was not a good choice?
It seems to work for you, so stay with it.

vijayQt
17th May 2011, 13:59
Hi high_flyer,
Thanks for your quick reply. You mean to say QLable will fulfill all my feature requirements??..

Thanks

high_flyer
17th May 2011, 14:03
I don't know.
Your original post was about non responsive gui and a too fast video.
So based on that I take it you have no problem showing your video.
On that topic Lykurg and I answered you.