Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Unstable Video From QCamera On stop and start
Forum Updated to NodeBB v4.3 + New Features

Unstable Video From QCamera On stop and start

Scheduled Pinned Locked Moved Solved Mobile and Embedded
8 Posts 2 Posters 836 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • KenAppleby 0K Offline
    KenAppleby 0K Offline
    KenAppleby 0
    wrote on last edited by KenAppleby 0
    #1

    Qt6.5 on android.

    I am creating a QCamera thus:

    QCameraDevice cameraDevice{ QMediaDevices::defaultVideoInput() };
    camera = new QCamera{ cameraDevice };
    

    and connecting its video output to a QGraphicsVideoItem via a QCaptureSession:

    captureSession = new QMediaCaptureSession{ this };
    captureSession->setCamera(camera);
    captureSession->setVideoOutput(graphicsVideoItem);
    cameraResolution = camera->cameraFormat().resolution();
    graphicsVideoItem->setSize(cameraResolution);
    

    and this works fine on first showing of the QGraphicsView that contains the QGraphicsScene that the QGraphicsVideoItem is in.

    My problem is that the view needs to be shown and hidden depending on the state of the application. I stop the camera with

    camera->stop();
    

    when the view is hidden and start the camera again when the view is shown:

    camera->start();
    

    The problem is that on a second and subsequent calls to camera->start() the video in the QGraphicsVideoItem is very unstable, with just a few frames irregularly shown.

    I don't want to keep the camera running when the view is hidden, naturally, but I can't find a way to prevent this video instability.

    Can anyone help?

    KenAppleby 0K 1 Reply Last reply
    1
    • KenAppleby 0K KenAppleby 0

      Qt6.5 on android.

      I am creating a QCamera thus:

      QCameraDevice cameraDevice{ QMediaDevices::defaultVideoInput() };
      camera = new QCamera{ cameraDevice };
      

      and connecting its video output to a QGraphicsVideoItem via a QCaptureSession:

      captureSession = new QMediaCaptureSession{ this };
      captureSession->setCamera(camera);
      captureSession->setVideoOutput(graphicsVideoItem);
      cameraResolution = camera->cameraFormat().resolution();
      graphicsVideoItem->setSize(cameraResolution);
      

      and this works fine on first showing of the QGraphicsView that contains the QGraphicsScene that the QGraphicsVideoItem is in.

      My problem is that the view needs to be shown and hidden depending on the state of the application. I stop the camera with

      camera->stop();
      

      when the view is hidden and start the camera again when the view is shown:

      camera->start();
      

      The problem is that on a second and subsequent calls to camera->start() the video in the QGraphicsVideoItem is very unstable, with just a few frames irregularly shown.

      I don't want to keep the camera running when the view is hidden, naturally, but I can't find a way to prevent this video instability.

      Can anyone help?

      KenAppleby 0K Offline
      KenAppleby 0K Offline
      KenAppleby 0
      wrote on last edited by
      #2

      Update: it might be that the problem is the result of the use of a QGraphicsVideoItem.

      I have modified the Qt camera example to give it a couple of buttons connected to its startCamera() and stopCamera() functions and the camera restarts okay in that. The camera example uses a QVideoWidget, not a QGraphicsVideoItem.

      MesrineM 1 Reply Last reply
      0
      • KenAppleby 0K KenAppleby 0

        Update: it might be that the problem is the result of the use of a QGraphicsVideoItem.

        I have modified the Qt camera example to give it a couple of buttons connected to its startCamera() and stopCamera() functions and the camera restarts okay in that. The camera example uses a QVideoWidget, not a QGraphicsVideoItem.

        MesrineM Offline
        MesrineM Offline
        Mesrine
        wrote on last edited by
        #3

        @KenAppleby-0
        I am having the same issues. Qt for Android 6.7.0.
        I am using a QCamera, QMediaCaptureSession and a QVideoSink.
        After the second call to camera->start(), the QVideoSink::videoFrameChanged is returning black images.
        And in the desktop a second call(after camera->stop()) to camera->start() returns Camera is in use..

        KenAppleby 0K 1 Reply Last reply
        0
        • MesrineM Mesrine

          @KenAppleby-0
          I am having the same issues. Qt for Android 6.7.0.
          I am using a QCamera, QMediaCaptureSession and a QVideoSink.
          After the second call to camera->start(), the QVideoSink::videoFrameChanged is returning black images.
          And in the desktop a second call(after camera->stop()) to camera->start() returns Camera is in use..

          KenAppleby 0K Offline
          KenAppleby 0K Offline
          KenAppleby 0
          wrote on last edited by
          #4

          @Mesrine It's a show-stopper for me at the moment.

          I have modified the Qt camera example to use a QGraphicsVideoItem and it shows the same behaviour. The original example uses a QVideoWidget, and the camera stop/start behaviour is fine, but I can't use a QVideoWidget in my application because you can't put anything on top of one: I need to put a cursor in the frame.
          My next attempt was to be to use a QVideoSink and a custom widget, but it seems that you have already tried that.

          MesrineM 1 Reply Last reply
          0
          • KenAppleby 0K KenAppleby 0

            @Mesrine It's a show-stopper for me at the moment.

            I have modified the Qt camera example to use a QGraphicsVideoItem and it shows the same behaviour. The original example uses a QVideoWidget, and the camera stop/start behaviour is fine, but I can't use a QVideoWidget in my application because you can't put anything on top of one: I need to put a cursor in the frame.
            My next attempt was to be to use a QVideoSink and a custom widget, but it seems that you have already tried that.

            MesrineM Offline
            MesrineM Offline
            Mesrine
            wrote on last edited by Mesrine
            #5

            @KenAppleby-0
            I use a QVideoSink to get the frames from the camera like

             QObject::connect(videoSink,&QVideoSink::videoFrameChanged,this,[=](const QVideoFrame & Vframe)
                                 {
            
                                     if(m_camera&&m_camera->isActive()){
                                         auto picture=Vframe.toImage();
                                         WasmImageProvider::img=picture;
                                         setid();
                                     }
            
                                 });
            

            So I can see the picture in a QML Image. This works for the first start() of the camera but for other start calls, the QML Image element shows black images. Although the videoFrameChanged signal is emitting. But please try your method, do not trust me.

            1 Reply Last reply
            0
            • KenAppleby 0K Offline
              KenAppleby 0K Offline
              KenAppleby 0
              wrote on last edited by
              #6

              @Mesrine
              Same thing using a QVideoSink in a custom QWidget,

              VideoWidget::VideoWidget(QWidget * parent)
                  : QWidget{ parent }
              {
                  mVideoSink = new QVideoSink{ this };
                  connect(mVideoSink, &QVideoSink::videoFrameChanged, this, &VideoWidget::handleVideoFrameChanged);
              }
              void VideoWidget::handleVideoFrameChanged(const QVideoFrame& frame)
              {
                  if (frame.isValid())
                  {
                      mFrame = frame.toImage();
                      update();
                  }
              }
              QVideoSink * VideoWidget::videoSink()
              {
                  return mVideoSink;
              }
              void VideoWidget::paintEvent(QPaintEvent * e)
              {
                  QWidget::paintEvent(e);
                  QPainter painter{ this };
                  if (not mFrame.isNull())
                  {
                      painter.drawImage(rect(), mFrame);
                  }
              }
              

              The first camera->start() works fine, subsequent ones after a camera->stop() show variously broken video frame sequences. The camera does not report any errors.

              KenAppleby 0K 1 Reply Last reply
              0
              • KenAppleby 0K KenAppleby 0

                @Mesrine
                Same thing using a QVideoSink in a custom QWidget,

                VideoWidget::VideoWidget(QWidget * parent)
                    : QWidget{ parent }
                {
                    mVideoSink = new QVideoSink{ this };
                    connect(mVideoSink, &QVideoSink::videoFrameChanged, this, &VideoWidget::handleVideoFrameChanged);
                }
                void VideoWidget::handleVideoFrameChanged(const QVideoFrame& frame)
                {
                    if (frame.isValid())
                    {
                        mFrame = frame.toImage();
                        update();
                    }
                }
                QVideoSink * VideoWidget::videoSink()
                {
                    return mVideoSink;
                }
                void VideoWidget::paintEvent(QPaintEvent * e)
                {
                    QWidget::paintEvent(e);
                    QPainter painter{ this };
                    if (not mFrame.isNull())
                    {
                        painter.drawImage(rect(), mFrame);
                    }
                }
                

                The first camera->start() works fine, subsequent ones after a camera->stop() show variously broken video frame sequences. The camera does not report any errors.

                KenAppleby 0K Offline
                KenAppleby 0K Offline
                KenAppleby 0
                wrote on last edited by
                #7

                @KenAppleby-0
                However, replacing the custom VideoWidget with a QVideoWidget results in the camera stop() and start() working fine. The video frames delivered by the camera after a second and subsequent start() are all as expected.

                So there is something about QVideoWidget which is permitting this. Sadly, the QVideoWidget seems to insist on being on top of everything else, so it will be unusable for my purposes.

                KenAppleby 0K 1 Reply Last reply
                0
                • KenAppleby 0K KenAppleby 0

                  @KenAppleby-0
                  However, replacing the custom VideoWidget with a QVideoWidget results in the camera stop() and start() working fine. The video frames delivered by the camera after a second and subsequent start() are all as expected.

                  So there is something about QVideoWidget which is permitting this. Sadly, the QVideoWidget seems to insist on being on top of everything else, so it will be unusable for my purposes.

                  KenAppleby 0K Offline
                  KenAppleby 0K Offline
                  KenAppleby 0
                  wrote on last edited by
                  #8

                  @KenAppleby-0 @Mesrine

                  I have today upgraded from Qt 6.4.3 to Qt 6.5.3 and this problem with the android camera has been fixed.

                  Thank you Qt!

                  1 Reply Last reply
                  1
                  • KenAppleby 0K KenAppleby 0 has marked this topic as solved on

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved