Results 1 to 17 of 17

Thread: Help in creating video streaming app

  1. #1
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Help in creating video streaming app

    Hi,

    I am trying to create an application in qt to view a video stream from a webcamera. I'm using a Logitech webcam on a Mandrake 10 machine. I am able to get the video from the webcam into a buffer...
    However, once i get the buffer and nextFrame() functions ready, i want to know how to display the video stream using qt.
    I think openGL is the way to go, but i am not familiar with its usage, or how to use QGLWidget.
    Also, the other forum posts say that using QPixmap is very slow, adn not useful for my project.

    Please send me any links/code that will help me in this regard.

    Thanks in advance
    superutsav
    Last edited by superutsav; 13th January 2006 at 18:34.
    He who laughs last thinks slowest.

  2. #2
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Help in creating video streaming app

    You can draw either a QPixmap or a QImage using a painter on a QGLWidget. Qt4 takes care of uploading the image as texture to the graphicscard and draw it using two triangles I think.
    It's nice to be important but it's more important to be nice.

  3. #3
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help in creating video streaming app

    Hi,

    I should also mention what i'm using right now, which i'm sure is really horrible, inefficient code, and which keeps the processor busy in a loop, hence making it impossible for me to close the app without killing the process:

    Qt Code:
    1. QPixmap myPixmap;
    2. QImage image;
    3. QLabel myLabel;
    4. QWidget window;
    5. QHBoxLayout layout;
    6. layout.addWidget(myLabel);
    7. window.setLayout(&layout);
    8.  
    9. while(1)
    10. {
    11. image = QImage::QImage((uchar*)deviceAccessTool.nextFrame(), width, height, QImage::Format_Rgb32);
    12. image = myprocessingcode(image);
    13. myPixmap = myPixmap.fromImage(image);
    14. myLabel.setPixmap(myPixmap);
    15. window.repaint();
    16. }
    To copy to clipboard, switch view to plain text mode 


    this code displays the streaming video without any flicker, yet. However, if i try to process the image before displaying, then it flikers a lot.

    any code u guys can suggest would be of great help

    Thanks in advance
    superutsav
    He who laughs last thinks slowest.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Help in creating video streaming app

    I would change your code along the lines of:
    Qt Code:
    1. #
    2. image = QImage( width, height, QImage::Format_Rgb32);
    3. unsigned char *buff = image.bits();
    4.  
    5. while(1)
    6. {
    7. memcpy(buff,(uchar*)deviceAccessTool.nextFrame(),/*size in bytes of the frame*/); //this should be faster then allocating each time a new image
    8. image = myprocessingcode(image); //if you want this to fit in 25hz, then you should take in consideration that grabbing and processing should not be longer then 40ms!
    9. myPixmap = myPixmap.fromImage(image);
    10. myLabel.setPixmap(myPixmap);
    11. window.repaint();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Also since I dont have much experiance with qt4, I would suggest to try (and can't say this IS better) the following:
    instead of :
    Qt Code:
    1. myPixmap = myPixmap.fromImage(image);
    2. myLabel.setPixmap(myPixmap);
    To copy to clipboard, switch view to plain text mode 
    I would try to use the QPainter with a QImage as paint device (one of the new Qt4 fetures, it could be faster then setPixmap()):
    Qt Code:
    1. //somewhere declared: QPainter painter(&myLabel);
    2. painter.drawImage(maLabel.rect(),image,image.rect());
    To copy to clipboard, switch view to plain text mode 

    So put togeather it would look somthing like:
    Qt Code:
    1. image = QImage( width, height, QImage::Format_Rgb32);
    2. unsigned char *buff = image.bits();
    3. QPainter painter(&myLabel);
    4.  
    5. while(1)
    6. {
    7. memcpy(buff,(uchar*)deviceAccessTool.nextFrame(),/*size in bytes of the frame*/);
    8. image = myprocessingcode(image); //if you want this to fit in 25hz, then you should take in consideration that grabbing and processing should not be longer then 40ms!
    9. painter.drawImage(maLabel.rect(),image,image.rect());
    10. }
    To copy to clipboard, switch view to plain text mode 

    Please note, under Qt3 I can show you how to do a video stream in a crossplatform way, but under Qt4, this code is only a suggestion I think you shoud try, and take in consideration I didn't test it!
    Since drawing in Qt4 is done only in a paint event, this all needs to be in a paint event...

    Any comments from you Qt4 experts?

  5. #5
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help in creating video streaming app

    So, out of curiosity, if I wanted to make an application that streams video content in QT3, how do I start? (ie what classes would be appropriate etc)

  6. #6
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help in creating video streaming app

    Qt Code:
    1. memcpy(buff,(uchar*)deviceAccessTool.nextFrame(),/*size in bytes of the frame*/); //this should be faster then allocating each time a new image
    To copy to clipboard, switch view to plain text mode 
    I tried this... it doesnt speed up the frame rate at all... atleast not to the naked eye...

    Also since I dont have much experiance with qt4, I would suggest to try (and can't say this IS better) the following:
    instead of :

    Qt Code:
    1. 1.
    2. myPixmap = myPixmap.fromImage(image);
    3. 2.
    4. myLabel.setPixmap(myPixmap);
    To copy to clipboard, switch view to plain text mode 
    I would try to use the QPainter with a QImage as paint device (one of the new Qt4 fetures, it could be faster then setPixmap()):
    Qt Code:
    1. 1.
    2. //somewhere declared: QPainter painter(&myLabel);
    3. 2.
    4. painter.drawImage(maLabel.rect(),image,image.rect());
    To copy to clipboard, switch view to plain text mode 
    I tried this, too... didn't speed up the thing much, either, but there is a small improvement.... i had some trouble getting it to work, though.

    Qt Code:
    1. image = myprocessingcode(image); //if you want this to fit in 25hz, then you should take in consideration that grabbing and processing should not be longer then 40ms!
    To copy to clipboard, switch view to plain text mode 
    How do i measure the amount of time its taking to process?... do i have a way to do that?


    Thanks
    superutsav
    Last edited by superutsav; 13th January 2006 at 05:22.
    He who laughs last thinks slowest.

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    52
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help in creating video streaming app

    well if you want to measure time, you could do it certainly like this:

    Qt Code:
    1. t.start();
    2. some_lengthy_task();
    3. qDebug("Time elapsed: %d ms", t.elapsed());
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Help in creating video streaming app

    Quote Originally Posted by superutsav
    I should also mention what i'm using right now, which i'm sure is really horrible, inefficient code, and which keeps the processor busy in a loop, hence making it impossible for me to close the app without killing the process:
    You have to kill the process because of the "while(1) {}" infinite loop. You have to give Qt a chance to handle events. So for testing I would add a qapp->processEvents() call to the loop.


    Quote Originally Posted by superutsav
    Qt Code:
    1. QPixmap myPixmap;
    2. QImage image;
    3. QLabel myLabel;
    4. QWidget window;
    5. QHBoxLayout layout;
    6. layout.addWidget(myLabel);
    7. window.setLayout(&layout);
    8.  
    9. while(1)
    10. {
    11. image = QImage::QImage((uchar*)deviceAccessTool.nextFrame(), width, height, QImage::Format_Rgb32);
    12. image = myprocessingcode(image);
    13. myPixmap = myPixmap.fromImage(image);
    14. myLabel.setPixmap(myPixmap);
    15. window.repaint();
    16. }
    To copy to clipboard, switch view to plain text mode 
    My idea would be to subclass QWidget and override the paintEvent() method.

    following code untested!!
    Qt Code:
    1. void VideoWidget::paintEvent()
    2. {
    3. QPainter p( this );
    4.  
    5. QImage image((uchar*)deviceAccessTool.nextFrame(), width, height, QImage::Format_Rgb32);
    6. myprocessingcode(&image);
    7.  
    8. p.drawImage (image.rect(), image);
    9. }
    To copy to clipboard, switch view to plain text mode 


    and somewhere else maybe....

    Qt Code:
    1. {
    2. deviceAccessTool.loadNextFrame();
    3. connect(deviceAccessTool, SIGNAL(nextFrameAvailable()),
    4. videoWidget, SLOT(update()));
    5. }
    To copy to clipboard, switch view to plain text mode 
    Cervisia maintainer - http://cervisia.kde.org

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Help in creating video streaming app

    Quote Originally Posted by kroenecker
    So, out of curiosity, if I wanted to make an application that streams video content in QT3, how do I start? (ie what classes would be appropriate etc)
    Nothing easier, here is an example of my QVideo plugin for 32 bit rgb frames:
    Qt Code:
    1. /** No descriptions */
    2. void QVideo::showRGB32(unsigned int *buff){
    3. int buffSize = width()*height()*sizeof(unsigned int);
    4.  
    5. if(!m_image32)
    6. m_image32 = new QImage(width(),height(),32);
    7. if(m_image32->width() != width() || m_image32->height() != height())
    8. {
    9. delete m_image32;
    10. m_image32 = new QImage(width(),height(),32);
    11. }
    12.  
    13. memcpy(m_image32->bits(),buff,buffSize);
    14. if(!m_image32->isNull())
    15. bitBlt(this,0,0,m_image32);
    16. // m_lastImage = m_image32; //<--this is not thread safe
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help in creating video streaming app

    Quote Originally Posted by Mike
    well if you want to measure time, you could do it certainly like this:

    Qt Code:
    1. t.start();
    2. some_lengthy_task();
    3. qDebug("Time elapsed: %d ms", t.elapsed());
    To copy to clipboard, switch view to plain text mode 

    Hey... I tried this... and thank god i did....
    Apparently the QPixmap::fromImage() function is taking an average of 33ms to run!!! Theres no way i can fit the whole thing into 40ms for a 25fps stream if just the fromImage is taking so long..... what do i do about this???

    superutsav
    He who laughs last thinks slowest.

  11. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Help in creating video streaming app

    Try to time the code I suggested before with teh painter.drawImage(), and see how long does that take...
    You can also try to bitBlt() as I showed here (with Qt3), though bitBlt must do a conversion to pixmap internally, it works quite fast.
    I have no problem using that for a sterao video stream each at 640x480 at 25hz.
    Last edited by high_flyer; 13th January 2006 at 16:30.

  12. #12
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help in creating video streaming app

    You have to kill the process because of the "while(1) {}" infinite loop. You have to give Qt a chance to handle events. So for testing I would add a qapp->processEvents() call to the loop.
    Hey... Thanks... this processEvents() thing works great... i dont have to kill the process amymore.

    You can also try to bitBlt() as I showed here (with Qt3), though bitBlt must do a conversion to pixmap internally, it works quite fast.
    Does Qt4 have bitBlt??? It doesn't show in the documentation... only some vague stuff... but i'll try it nevertheless and tell u the result

    Thanks
    superutsav
    He who laughs last thinks slowest.

  13. #13
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Help in creating video streaming app

    Does Qt4 have bitBlt??? It doesn't show in the documentation... only some vague stuff... but i'll try it nevertheless and tell u the result
    I would be surprozed if it did, since under Qt4 you can only draw in a paintEvent, and bitBlt() draws directly on a paint device when ever its called, which would break the Qt4 drawing rules.
    But on the other hand, Qt4 added the ability for a painter to draw an image on the paint device which is basically what bitBlt does (well sorts of), and that is what i tried to do with the code i offered you at the beginning.

  14. #14
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help in creating video streaming app

    Hi

    I dont think Qt4 has bitBlt, after all... if i call bitBlt anywhere, it gives the following compilation error:

    Qt Code:
    1. error: `bitBlt' undeclared (first use this function)
    To copy to clipboard, switch view to plain text mode 

    is there some other way of calling it other than directly saying bitBlt(...) ?? Which class does it belong to???

    superutsav
    He who laughs last thinks slowest.

  15. #15
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Help in creating video streaming app

    Did you read my last post at all??
    I answered all your last questions there.

  16. #16
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help in creating video streaming app

    hi,
    Can you explain this line
    (uchar*)deviceAccessTool.nextFrame(), width, height, QImage::Format_Rgb32)
    from you code. in this , " deviceAccessTool.nextFrame() " what you mean with this?

  17. #17
    Join Date
    Dec 2009
    Posts
    26
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Help in creating video streaming app

    hi superutsav,
    I am a new at QT video stream.
    Could please explain the mention of myprocessingcode(image) function. Does it the buffer which come from QIODevice(function read)?

    Thanks,
    Simbad.

Similar Threads

  1. [SOLVED] DirectShow Video Player Inside Qt
    By ToddAtWSU in forum Qt Programming
    Replies: 16
    Last Post: 3rd November 2011, 07:47
  2. Video Streaming
    By stefandetter in forum Qt Programming
    Replies: 1
    Last Post: 4th June 2008, 09:59
  3. Video streaming with QT4
    By QiT in forum Qt Programming
    Replies: 1
    Last Post: 10th April 2008, 18:09
  4. Video streaming on QMainWindow
    By yagabey in forum Qt Programming
    Replies: 3
    Last Post: 13th January 2008, 17:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.