Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video: Fix QtMultimedia Receiver Crashes #12209

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/VideoManager/VideoReceiver/QtMultimedia/QtMultimediaReceiver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <QtQuick/QQuickItem>
#include <QtQuick/QQuickItemGrabResult>

QGC_LOGGING_CATEGORY(QtMultimediaReceiverLog, "READY.video.qtmultimedia.qtmultimediareceiver")
QGC_LOGGING_CATEGORY(QtMultimediaReceiverLog, "qgc.video.qtmultimedia.qtmultimediareceiver")

QtMultimediaReceiver::QtMultimediaReceiver(QObject *parent)
: VideoReceiver(parent)
Expand All @@ -35,14 +35,14 @@ QtMultimediaReceiver::QtMultimediaReceiver(QObject *parent)

(void) connect(_mediaPlayer, &QMediaPlayer::playingChanged, this, &QtMultimediaReceiver::streamingChanged);
(void) connect(_mediaPlayer, &QMediaPlayer::hasVideoChanged, this, &QtMultimediaReceiver::decodingChanged);
(void) connect(_mediaPlayer, &QMediaPlayer::playbackStateChanged, this, [this](QMediaPlayer::PlaybackState newState){
(void) connect(_mediaPlayer, &QMediaPlayer::playbackStateChanged, this, [this](QMediaPlayer::PlaybackState newState) {
if (newState == QMediaPlayer::PlaybackState::PlayingState) {
_frameTimer->start();
} else if (newState == QMediaPlayer::PlaybackState::StoppedState) {
_frameTimer->stop();
}
});
(void) connect(_mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, [this](QMediaPlayer::MediaStatus status){
(void) connect(_mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, [this](QMediaPlayer::MediaStatus status) {
switch (status) {
case QMediaPlayer::MediaStatus::LoadingMedia:
_streamDevice = _mediaPlayer->sourceDevice();
Expand Down Expand Up @@ -110,7 +110,7 @@ void *QtMultimediaReceiver::createVideoSink(QObject *parent, QQuickItem *widget)

QVideoSink *videoSink = nullptr;
if (widget) {
QQuickVideoOutput* const videoOutput = reinterpret_cast<QQuickVideoOutput*>(widget);
QQuickVideoOutput *const videoOutput = reinterpret_cast<QQuickVideoOutput*>(widget);
videoSink = videoOutput->videoSink();
}

Expand All @@ -119,12 +119,12 @@ void *QtMultimediaReceiver::createVideoSink(QObject *parent, QQuickItem *widget)

void QtMultimediaReceiver::releaseVideoSink(void *sink)
{
if (!sink) {
/*if (!sink) {
return;
}

QVideoSink* const videoSink = reinterpret_cast<QVideoSink*>(sink);
videoSink->deleteLater();
videoSink->deleteLater();*/
}

VideoReceiver *QtMultimediaReceiver::createVideoReceiver(QObject *parent)
Expand Down Expand Up @@ -173,7 +173,7 @@ void QtMultimediaReceiver::stop()

if (!_mediaPlayer->isPlaying()) {
qCDebug(QtMultimediaReceiverLog) << "Already stopped!";
emit onStartComplete(STATUS_INVALID_STATE);
emit onStopComplete(STATUS_INVALID_STATE);
return;
}

Expand All @@ -194,7 +194,7 @@ void QtMultimediaReceiver::startDecoding(void *sink)
{
qCDebug(QtMultimediaReceiverLog) << Q_FUNC_INFO;

if (sink == nullptr) {
if (!sink) {
qCCritical(QtMultimediaReceiverLog) << "VideoSink is NULL";
emit onStartDecodingComplete(STATUS_FAIL);
return;
Expand Down Expand Up @@ -231,7 +231,7 @@ void QtMultimediaReceiver::stopDecoding()
{
qCDebug(QtMultimediaReceiverLog) << Q_FUNC_INFO;

if (_videoSink == nullptr) {
if (!_videoSink) {
qCWarning(QtMultimediaReceiverLog) << "VideoSink is NULL";
emit onStartDecodingComplete(STATUS_INVALID_STATE);
return;
Expand Down Expand Up @@ -299,18 +299,19 @@ void QtMultimediaReceiver::takeScreenshot(const QString &imageFile)
if (!_videoSink) {
qCWarning(QtMultimediaReceiverLog) << "Video Sink is NULL";
emit onTakeScreenshotComplete(STATUS_FAIL);
return;
}

const QVideoFrame frame = _videoSink->videoFrame();
if (frame.isValid() && frame.isReadable()) {
const QVideoFrameFormat frameFormat = frame.surfaceFormat();
const QImage frameImage = frame.toImage();
Q_UNUSED(frameImage);
} else {
if (!frame.isValid() || !frame.isReadable()) {
qCWarning(QtMultimediaReceiverLog) << "Screenshot Frame is Invalid";
emit onTakeScreenshotComplete(STATUS_FAIL);
return;
}

// const QVideoFrameFormat frameFormat = frame.surfaceFormat();
// const QImage frameImage = frame.toImage();

_videoOutput = reinterpret_cast<QQuickVideoOutput*>(_mediaPlayer->videoOutput());
const QSize targetSize = _mediaRecorder->videoResolution();
QSharedPointer<QQuickItemGrabResult> screenshot = _videoOutput->grabToImage(targetSize);
Expand Down
Loading