Unstable Video From QCamera On stop and start
-
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?
-
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.
-
@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 tocamera->start()
, theQVideoSink::videoFrameChanged
is returning black images.
And in the desktop a second call(aftercamera->stop()
) tocamera->start()
returnsCamera is in use.
. -
@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. -
@KenAppleby-0
I use aQVideoSink
to get the frames from the camera likeQObject::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 firststart()
of the camera but for otherstart
calls, the QML Image element shows black images. Although thevideoFrameChanged
signal is emitting. But please try your method, do not trust me. -
@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-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.
-
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!
-
K KenAppleby 0 has marked this topic as solved on